實(shí)用單片機(jī)電子鐘的制作
P0接數(shù)碼管的8段,P2.6-P2.1分別選通6個(gè)數(shù)碼管,順序依次是:P2.6 是秒的低位,P2.1選通時(shí)的高位。再有就是P3.2-P3.5接四個(gè)按鍵。DCF51-1型開(kāi)發(fā)系統(tǒng)已經(jīng)通過(guò)??!
;;主程序(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 ;設(shè)定定時(shí)器工作方式?
MOV TL0,#0B0H
MOV TH0,#3CH
SETB TR0 ;啟動(dòng)定時(shí)?
MOV SP,#40H ;重設(shè)堆棧指針
NEXT: LCALL DISP ;調(diào)用顯示子程序?
LCALL KEY ;調(diào)用按鍵檢測(cè)子程序
JZ NEXT ;
LCALL ANKEY ;調(diào)用按鍵處理子程序
SJMP NEXT ;重新循環(huán)
NOP
NOP
NOP
;定時(shí)中斷處理程序:
TIME: PUSH ACC;保護(hù)現(xiàn)場(chǎng)
PUSH PSW
MOV TL0,#0B4H ;賦定時(shí)初值
MOV TH0,#3CH
INC 20H ;
MOV A,20H
CJNE A,#20,RETI1
MOV 20H,#00H ;一秒鐘時(shí)間到
MOV A,21H
ADD A,#01H
DA A
MOV 21H,A
CJNE A,#60H,RETI1
MOV 21H,#00H ;一分鐘時(shí)間到
MOV A,22H
ADD A,#01H
DA A
MOV 22H,A
CJNE A,#60H,RETI1
MOV 22H,#00H ;一小時(shí)時(shí)間到
MOV A,23H
ADD A,#01H
DA A
MOV 23H,A
CJNE A,#24H,RETI1
MOV 23H,#00H ;到時(shí)間達(dá)到24小時(shí),清零.
RETI1: POP PSW ;恢復(fù)現(xiàn)場(chǎng)
POP ACC
RETI ;中斷返回?
NOP
NOP
;顯示子程序
DISP: ANL 2FH,#10H ;處理小數(shù)點(diǎn)
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 ;處理小時(shí)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 ;延時(shí)
DJNZ R2,$
DEC R0
MOV A,B
RR A
DJNZ R3,LOOP1 ;循環(huán)顯示
RET
TABLE: db 28h,7eh,0a2h,62h,74h,61h,21h ;不帶小數(shù)點(diǎn)
DB 7ah,20h,60h,00,00,00,00,00,00
DB 8H,5eH,82H,42H,54H,41H,1H,5aH ;帶小數(shù)點(diǎn)
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 ;無(wú)鍵按下則返回
LCALL DISP ;
LCALL DISP
MOV A,P3
CPL A
ANL A,#3CH
JZ RETX ;鍵盤去抖動(dòng)。
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 ;關(guān)中斷
LX: MOV A,R6
JB ACC.2,L1 ;是功能鍵轉(zhuǎn)L1
JB ACC.3,L2 ;是確認(rèn)鍵轉(zhuǎn)L2
JB ACC.4,L3 ;是減1鍵轉(zhuǎn)L3
JNB ACC.5,L12 ;不是增1鍵,轉(zhuǎn)L12
JB 2BH.4,L6 ;判斷使哪一位(時(shí)、分、秒)的值加1
JB 2DH.4,L8
JB 2FH.4,L9
L12: LCALL DISP
LCALL DISP
LCALL KEY ;判斷有無(wú)鍵按下。
JZ L12
LJMP LX
L2: MOV 25H,#00H ;確認(rèn)鍵處理程序
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單元是標(biāo)志位,(25H)=01H調(diào)節(jié)時(shí)單元的值
SETB 2BH.4
CLR 2DH.4
CLR 2FH.4
AJMP L12
LB3: MOV 25H,#04H ;25H單元是標(biāo)志位,(25H)=01H調(diào)節(jié)秒單元的值
SETB 2FH.4
CLR 2DH.4
CLR 2BH.4
AJMP L12
LB2: MOV 25H,#02H ;25H單元是標(biāo)志位,(25H)=01H調(diào)節(jié)分單元的值
SETB 2DH.4
CLR 2BH.4
CLR 2FH.4
AJMP L12
L61: AJMP L611 ;
L81: AJMP L811 ;
L91: AJMP L911
L6: MOV A,23H ;時(shí)加一
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 ;時(shí)減一
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