实用单片机电子钟的制作
扫描二维码
随时随地手机看文章
P0接数码管的8段,P2.6-P2.1分别选通6个数码管,顺序依次是:P2.6 是秒的低位,P2.1选通时的高位。再有就是P3.2-P3.5接四个按键。DCF51-1型开发系统已经通过!!
;;主程序(MAIN),如下:
ORG 0000H
AJMP MAIN
ORG 000BH
AJMP TIME
ORG 0300H
MAIN:
mov 20h,#00h
MOV 21H,#00H
MOV 22H,#00H
MOV 23H,#00H
MOV IP,#02H ;IP,IE初始化
MOV IE,#82H
MOV TMOD,#01H ;设定定时器工作方式?
MOV TL0,#0B0H
MOV TH0,#3CH
SETB TR0 ;启动定时?
MOV SP,#40H ;重设堆栈指针
NEXT: LCALL DISP ;调用显示子程序?
LCALL KEY ;调用按键检测子程序
JZ NEXT ;
LCALL ANKEY ;调用按键处理子程序
SJMP NEXT ;重新循环
NOP
NOP
NOP
;定时中断处理程序:
TIME: PUSH ACC;保护现场
PUSH PSW
MOV TL0,#0B4H ;赋定时初值
MOV TH0,#3CH
INC 20H ;
MOV A,20H
CJNE A,#20,RETI1
MOV 20H,#00H ;一秒钟时间到
MOV A,21H
ADD A,#01H
DA A
MOV 21H,A
CJNE A,#60H,RETI1
MOV 21H,#00H ;一分钟时间到
MOV A,22H
ADD A,#01H
DA A
MOV 22H,A
CJNE A,#60H,RETI1
MOV 22H,#00H ;一小时时间到
MOV A,23H
ADD A,#01H
DA A
MOV 23H,A
CJNE A,#24H,RETI1
MOV 23H,#00H ;到时间达到24小时,清零.
RETI1: POP PSW ;恢复现场
POP ACC
RETI ;中断返回?
NOP
NOP
;显示子程序
DISP: ANL 2FH,#10H ;处理小数点
MOV A,21H ;处理秒21H-->2DH,2EH
ANL A,#0FH
ORL A,2FH
MOV 2FH,A
MOV A,21H
ANL A,#0F0H
SWAP A
MOV 2EH,A
ANL 2DH,#10H
MOV A,22H ;处理分钟22H-->2CH,2DH
ANL A,#0FH
ORL A,2DH
MOV 2DH,A
MOV A,22H
ANL A,#0F0H
SWAP A
MOV 2CH,A
ANL 2BH,#10H
MOV A,23H ;处理小时23H-->2AH,2BH
ANL A,#0FH
ORL A,2BH
MOV 2BH,A
MOV A,23H
ANL A,#0F0H
SWAP A
MOV 2AH,A
MOV R0,#2FH ;显示偏移量
MOV R3,#06H
MOV DPTR,#TABLE
MOV A,#0BFH
LOOP1: MOV B,A ;
MOV P2,a
MOV A,@R0
MOVC A,@A+DPTR
MOV P0,a ;送显示
MOV R2,#80H ;延时
DJNZ R2,$
DEC R0
MOV A,B
RR A
DJNZ R3,LOOP1 ;循环显示
RET
TABLE: db 28h,7eh,0a2h,62h,74h,61h,21h ;不带小数点
DB 7ah,20h,60h,00,00,00,00,00,00
DB 8H,5eH,82H,42H,54H,41H,1H,5aH ;带小数点
DB 00H,40H,00,00,00,00,00,00
NOP
NOP
;按键判断程序
KEY: MOV P3,#0FFH ;
MOV A,P3
CPL A
ANL A,#3CH
JZ RETX ;无键按下则返回
LCALL DISP ;
LCALL DISP
MOV A,P3
CPL A
ANL A,#3CH
JZ RETX ;键盘去抖动。
MOV R6,A ;将键值存入R6。
LOOP2: LCALL DISP ;
MOV A,P3
CPL A
ANL A,#3CH
JNZ LOOP2 ;等待键释放
MOV A,R6
RETX: RET
NOP
NOP
;按键处理子程序
ANKEY: CLR EA ;关中断
LX: MOV A,R6
JB ACC.2,L1 ;是功能键转L1
JB ACC.3,L2 ;是确认键转L2
JB ACC.4,L3 ;是减1键转L3
JNB ACC.5,L12 ;不是增1键,转L12
JB 2BH.4,L6 ;判断使哪一位(时、分、秒)的值加1
JB 2DH.4,L8
JB 2FH.4,L9
L12: LCALL DISP
LCALL DISP
LCALL KEY ;判断有无键按下。
JZ L12
LJMP LX
L2: MOV 25H,#00H ;确认键处理程序
CLR 2BH.4
CLR 2DH.4
CLR 2FH.4
SETB EA
RET
L3: JB 2BH.4,L61 ;增一键处理程序
JB 2DH.4,L81
JB 2FH.4,L91
AJMP L12
L1: MOV A,25H ;功能键处理程序
JZ LB1
JB ACC.0,LB2
JB ACC.1,LB3
JNB ACC.2,L12
LB1: MOV 25H,#01H ;25H单元是标志位,(25H)=01H调节时单元的值
SETB 2BH.4
CLR 2DH.4
CLR 2FH.4
AJMP L12
LB3: MOV 25H,#04H ;25H单元是标志位,(25H)=01H调节秒单元的值
SETB 2FH.4
CLR 2DH.4
CLR 2BH.4
AJMP L12
LB2: MOV 25H,#02H ;25H单元是标志位,(25H)=01H调节分单元的值
SETB 2DH.4
CLR 2BH.4
CLR 2FH.4
AJMP L12
L61: AJMP L611 ;
L81: AJMP L811 ;
L91: AJMP L911
L6: MOV A,23H ;时加一
ADD A,#01H
DA A
MOV 23H,A
CJNE A,#24H,L15
L15: JC L112
MOV 23H,#00H
L112: AJMP L12
L8: MOV A,22H ;分加一
ADD A,#01H
DA A
MOV 22H,A
CJNE A,#60H,L16
L16: JC L112
MOV 22H,#00H
AJMP L12
L9: MOV A,21H ;秒加一
ADD A,#01H
DA A
MOV 21H,A
CJNE A,#60H,L17
L17: JC L112
MOV 21H,#00H
AJMP L12
L611: MOV A,23H ;时减一
ADD A,#99H
DA A
MOV 23H,A
CJNE A,#99H,L112
MOV 23H,#23H
AJMP L12
L811: MOV A,22H ;分减一
ADD A,#99H
DA A
MOV 22H,A
CJNE A,#99H,L112
MOV 22H,#59H
AJMP L12
L911: MOV A,21H ;秒减一
ADD A,#99H
DA A
MOV 21H,A
CJNE A,#99H,L112
MOV 21H,#59H
AJMP L12
NOP
NOP
END