上机1 发光二极管的闪烁
;开关断开,发光二极管灭;开关闭合,奇偶数发光二极管间隔0.1s交替点亮。
ORG 0
AJMP MAIN
ORG 0100H
MAIN: JB P3.0,DK
BH: MOV P1,#55H
ACALL YS01S
MOV P1,#0AAH
ACALL YS01S
SJMP MAIN
DK: MOV P1,#0FFH
SJMP MAIN
;延时0.1s
YS01S: MOV R4,#100
LOOP: MOV R3,#249
LOOP1: NOP
NOP
DJNZ R3,LOOP1
NOP
DJNZ R4,LOOP
RET
END
上机2 秒表、倒计时
;(1)可设置初值的秒表
JS EQU 60
CZ EQU 0
ORG 0000H
AJMP MAIN
ORG 0100H
MAIN: MOV DPTR,#TAB
MOV R0,#CZ
LOOP: ACALL HBCD
ACALL DISP
INC R0
CJNE R0,#JS,LOOP
MOV R0,#0
SJMP LOOP
HBCD: MOV A,R0
MOV B,#10
DIV AB
MOV R1,A
MOV R2,B
RET
DISP: MOV R3,#10
LOOP3: MOV A,R1
MOVC A,@A+DPTR
MOV P2,A
MOV P3,#01H
ACALL YS50MS
MOV P3,#0
MOV A,R2
MOVC A,@A+DPTR
MOV P2,A
MOV P3,#02H
ACALL YS50MS
MOV P3,#0
DJNZ R3,LOOP3
RET
YS50MS: MOV R4,#50
LOOP1: MOV R5,#249
LOOP2: NOP
NOP
DJNZ R5,LOOP2
NOP
DJNZ R4,LOOP1
RET
;共阳数码管显示1~9的查表值
TAB: DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
END
;(2)可设置初值的倒计时,最大初值99
DJS EQU 0
CZ EQU 10
ORG 0000H
AJMP MAIN
ORG 0100H
MAIN: MOV DPTR,#TAB
MOV R0,#CZ
LOOP: ACALL HBCD
ACALL DISP
DEC R0
CJNE R0,#DJS,LOOP
ACALL HBCD
NEXT: ACALL DISP
SJMP NEXT
HBCD: MOV A,R0
MOV B,#10
DIV AB
MOV R1,A
MOV R2,B
RET DISP:
MOV R3,#10
LOOP3: MOV A,R1
MOVC A,@A+DPTR
MOV P2,A
MOV P3,#01H
ACALL YS50MS
MOV P3,#0
MOV A,R2
MOVC A,@A+DPTR
MOV P2,A
MOV P3,#02H
ACALL YS50MS
MOV P3,#0
DJNZ R3,LOOP3
RET
YS50MS: MOV R4,#50
LOOP1: MOV R5,#249
LOOP2: NOP
NOP
DJNZ R5,LOOP2
NOP
DJNZ R4,LOOP1
RET
;共阳数码管显示1~9的查表值
TAB: DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
END
上机3 交通灯的模拟
;模拟十字路口红绿灯
GREENL EQU 50
GREENS EQU 6
YELLOWS EQU 4
ORG 0
AJMP MAIN
ORG 0100H
MAIN: CLR P1.1
CLR P1.6
MOV R0,#GREENL
DXLDL: ACALL YS05S
DJNZ R0,DXLDL
MOV R0,#GREENS
DXLDS: CPL P1.1
ACALL YS05S
DJNZ R0,DXLDS
SETB P1.1
MOV R0,#YELLOWS
DXHDS: CPL P1.2
ACALL YS05S
DJNZ R0,DXHDS
SETB P1.2
SETB P1.6
CLR P1.4
CLR P1.3
MOV R0,#GREENL
NBLDL: ACALL YS05S
DJNZ R0,NBLDL
MOV R0,#GREENS
NBLDS: CPL P1.4
ACALL YS05S
DJNZ R0,NBLDS
SETB P1.4
MOV R0,#YELLOWS
NBHDS: CPL P1.5
ACALL YS05S
DJNZ R0,NBHDS
SETB P1.5
SETB P1.3
SJMP MAIN
YS50MS: MOV TMOD,#01H
MOV TH0,#3CH
MOV TL0,#0B0H
SETB TR0
LOOP: JBC TF0,NEXT
SJMP LOOP
NEXT: RET
YS05S: MOV R1,#10
LOOP1: ACALL YS50MS
DJNZ R1,LOOP1
RET
END
附:带数字显示的交通灯模拟
;模拟带数字显示的十字路口红绿灯
GREENL EQU 25
GREENS EQU 3
YELLOWS EQU 2
REDL EQU 30
ORG 0
AJMP MAIN
ORG 0100H
MAIN: MOV DPTR,#TAB
MOV P3,#0
MOV R0,#GREENL
MOV R1,#REDL
CLR P1.1
CLR P1.6
DXLDL: ACALL DISP05S
ACALL DISP05S
DEC R1
DJNZ R0,DXLDL
MOV R0,#GREENS
DXLDS: CPL P1.1
ACALL DISP05S
CPL P1.1
ACALL DISP05S
DEC R1
DJNZ R0,DXLDS
SETB P1.1
MOV R0,#YELLOWS
DXHDS: CPL P1.2
ACALL DISP05S
CPL P1.2
ACALL DISP05S
DEC R1
DJNZ R0,DXHDS
SETB P1.2
SETB P1.6
MOV R0,#REDL
MOV R1,#GREENL
CLR P1.4
CLR P1.3
NBLDL: ACALL DISP05S
ACALL DISP05S
DEC R0
DJNZ R1,NBLDL
MOV R1,#GREENS
NBLDS: CPL P1.4
ACALL DISP05S
CPL P1.4
ACALL DISP05S
DEC R0
DJNZ R1,NBLDS
SETB P1.4
MOV R1,#YELLOWS
NBHDS: CPL P1.5
ACALL DISP05S
CPL P1.5
ACALL DISP05S
DEC R0
DJNZ R1,NBHDS
SETB P1.5 SETB P1.3
AJMP MAIN
DISP05S: MOV R2,#10
LOOP: MOV 20H,R0
ACALL HBCD
ACALL DXDISP
MOV 20H,R1
ACALL HBCD
ACALL NBDISP
DJNZ R2,LOOP
RET
HBCD: MOV A,20H
MOV B,#10
DIV AB
MOV 21H,A
MOV 22H,B
RET
;东西路口显示
DXDISP: MOV A,21H
MOVC A,@A+DPTR
MOV P2,A
SETB P0.5
CLR P0.4
ACALL YS12MS5
CLR P0.5
MOV A,22H
MOVC A,@A+DPTR
MOV P2,A
SETB P0.4
CLR P0.5
ACALL YS12MS5
CLR P0.4
RET
;南北路口显示
NBDISP: MOV A,21H
MOVC A,@A+DPTR
MOV P3,A
SETB P0.7
CLR P0.6
ACALL YS12MS5
CLR P0.7
MOV A,22H
MOVC A,@A+DPTR
MOV P3,A
SETB P0.6
CLR P0.7
ACALL YS12MS5
CLR P0.6
RET
;延时12.5MS
YS12MS5: MOV TMOD,#01H
MOV TH0,#0CFH
MOV TL0,#2CH
SETB TR0
LOOP1: JBC TF0,NEXT
SJMP LOOP1
NEXT: RET
;共阳极0~F的数值
TAB: DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H
DB 80H,90H,88H,83H,0C6H,0A1H,86H,8EH
END
来源:互联网