代码是用汇编写的,见下;clock
LEDBUF EQU 7AH
ORG 0000H
AJMP MAIN
ORG 0100H
MAIN: MOV 21H,#00H;时钟清零
MOV 22H,#00H
MOV 23H,#00H
MOV TMOD,#01H;设定定时器工作模式 方式1 16位定时器 GATE=0_软件控制计数 C/T=0_定时器模式
MOV TL0,#0B0H
MOV TH0,#0F0H
MOV R7,#00H;50ms计数寄存器
SETB TR0
COUNT: ;SETB TR0;开始计时
LCALL DISPLAY
JBC TF0,SECOND
AJMP COUNT
SECOND: ;LCALL DISPLAY
CLR TF0
MOV TL0,#0B0H;重新装载计数初值
MOV TH0,#0F0H
SETB TR0
INC R7;定时器每溢出1次,50ms计数寄存器+1
CJNE R7,#20,COUNT;50ms计数器计满20次则为1s,秒计数器+1
MOV R7,#00H
INC 21H
MOV R6,21H
CJNE R6,#12,COUNT
;LCALL DISPLAY
MOV 21H,#00H
INC 22H
MOV R6,22H
CJNE R6,#12,COUNT
;LCALL DISPLAY
MOV 22H,#00H
INC 23H
;LCALL DISPLAY
MOV R6,23H
CJNE R6,#10,COUNT
AJMP MAIN
DISPLAY:
MOV R1,#00000001B
MOV R4,#06H
MOV DPTR,#TABLE;
MOV A,21H
MOV B,#10
DIV AB
MOV 7EH,A
MOV 7FH,B
MOV A,22H
MOV B,#10
DIV AB
MOV 7CH,A
MOV 7DH,B
MOV A,23H
MOV B,#10
DIV AB
MOV 7AH,A
MOV 7BH,B
MOV R0,#LEDBUF
DSPLAYLOOP:
MOV A,@R0
MOVC A,@A+DPTR
MOV P2,R1
MOV P0,A
INC R0
MOV A,R1
RL A
MOV R1,A
DJNZ R4,DSPLAYLOOP;
RET;
TABLE:
DB 3FH,06H,5BH,4FH,66H,6DH,7DH
DB 07H,7FH,6FH,77H,7CH,39H
DB 5EH,79H,71H,73H,40H,00H
END |