最近闲瑕之余,写了个比较简单的电子钟程序,当然没有用到DS1302,
个人感觉非常实用,调节也非常方便.当然,功能也比较简单,
给爱好趣味学习的朋友.
本人的源程序:
大家多多指教!!!
程序中对50MS中断定时进行了修正,解决了走时误差问题.将50MS校正为49.975MS,每天走时误差约1S左右.
/*调试程序12M晶振*/
ORG 0000H
AJMP MAIN
ORG 000BH
AJMP TIME
ORG 0030H
MAIN: MOV 20H, #00H /*S定义*/
MOV 21H, #00H /*M 定义*/
MOV 22H, #00H /*H 定义*/
MOV R0, #00H /*MS定义*/
MOV SP, #5FH
MOV P0,#0FFH
MOV P3,#0FFH
MOV P1,#0FFH
MOV TMOD, #01H
MOV TH0, #4CH /*校正49.975MS*/
MOV TL0, #17H
SETB EA
SETB ET0
SETB TR0
NEXT: LCALL DISP
LCALL KEYCAN
LJMP NEXT
TIME: PUSH PSW
PUSH ACC
INC R0
MOV A,R0
CJNE A,#20,RET1 /*1S计时*/
MOV R0,#00H
MOV A,20H
ADD A,#01H
DA A
MOV 20H,A
CJNE A,#60H,RET1 /*1分计时*/
MOV 20H,#00H
MOV A,21H
ADD A,#01H
DA A
MOV 21H,A
CJNE A,#60H,RET1 /*1小时计时*/
MOV 21H,#00H
MOV A,22H
ADD A,#01H
DA A
MOV 22H,A
CJNE A,#24H,RET1 /*24小时计时*/
MOV 22H, #00H
RET1: POP ACC
POP PSW
MOV TH0, #4CH /*校正49.975MS*/
MOV TL0, #17H
RETI
DISP: MOV A,20H /*输出数码管*/
ANL A,#0FH
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
CLR P3.7
ACALL DELAY1MS
SETB P3.7
MOV A,20H
ANL A,#0F0H
SWAP A
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
CLR P3.6
ACALL DELAY1MS
SETB P3.6
MOV A,21H
ANL A,#0FH
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
CLR P3.4
ACALL DELAY1MS
SETB P3.4
MOV A,21H
ANL A,#0F0H
SWAP A
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
CLR P3.3
ACALL DELAY1MS
SETB P3.3
MOV A,22H
ANL A,#0FH
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
CLR P3.1
ACALL DELAY1MS
SETB P3.1
MOV A,22H
ANL A,#0F0H
SWAP A
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
CLR P3.0
ACALL DELAY1MS
SETB P3.0
MOV P0,#0BFH
CLR P3.5
ACALL DELAY1MS
SETB P3.5
MOV P0,#0BFH
CLR P3.2
ACALL DELAY1MS
SETB P3.2
RET
DELAY1MS: MOV R2, #50 /*约1MS*/
D1: MOV R3, #10
DJNZ R3, $
DJNZ R2, D1
RET
TABLE: DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H /*0-9*/
KEYCAN:NOP
SW19: MOV C,P1.0
JC SW20
LCALL DISP
LCALL DISP
MOV C,P1.0
JC SW20
LCALL DISP
LCALL DISP
LOOP1: MOV C,P1.0
LCALL DISP
LCALL DISP
JNC LOOP1
MOV A,22H
ADD A,#01H
DA A
MOV 22H,A
CJNE A,#24H, SW22
MOV 22H,#00H
SW20: MOV C,P1.1
JC SW21
LCALL DISP
LCALL DISP
MOV C,P1.1
JC SW21
LCALL DISP
LCALL DISP
LOOP2: MOV C,P1.1
LCALL DISP
LCALL DISP
JNC LOOP2
MOV A,21H
ADD A,#01H
DA A
MOV 21H,A
CJNE A,#60H, SW22
MOV 21H,#00H
SW21: MOV C,P1.2
JC SW22
LCALL DISP
LCALL DISP
JC SW22
LCALL DISP
LCALL DISP
LOOP3: MOV C,P1.2
LCALL DISP
LCALL DISP
JNC LOOP3
MOV 20H,#00H
SW22: RET
END
文章评论(0条评论)
登录后参与讨论