;***************************************************
;* 用單片機89C2051制作的延時控制器 *
;* T0、TT0配合構(gòu)成為秒脈沖發(fā)生器 *
;* TSBIN 為秒計數(shù)器(二進制,0~60) ;21H單元 *
;* TMBIN 為分計數(shù)器(二進制,0~60) ;22H單元 *
;* THBIN 為時計數(shù)器(二進制,0~24) ;23H單元 *
;* KEYCODE 為鍵盤值;29H單元 *
;* KEYPRE 為按鍵標志字;PSW.5 *
;* 當?shù)褂嫊r至00H00M00S 時,P3.0, P3.1端輸出 *
;* 反相信號。 *
;* S1----對時按鈕;S2--對分按鈕; *
;* S3--對秒按鈕;S4--退出對時 *
;***************************************************
TMSBIN EQU 20H
TSBIN EQU 21H
TMBIN EQU 22H
THBIN EQU 23H
KEYCODE EQU 29H
TT0 EQU 2BH
OUT EQU 2DH
KEYPRE BIT PSW.5
DPS1 DATA 11001111B ;S4
DPM0 DATA 11010111B ;S3
DPM1 DATA 11011111B ;S2
DPH0 DATA 11100111B ;S1
DPH1 DATA 11101111B
HIDE DATA 11111111B ;消隱字
ORG 0000H
AJMP MAIN
ORG 000BH
SJMP I_T0
ORG 001BH
I_T1: AJMP MAIN1
RETI
;*********** T0中斷服務(wù) ***********
ORG 0030H
I_T0: PUSH ACC
PUSH PSW
MOV TH0,#0DCH ;0.01s時間,常數(shù)因子為DC00H
;MOV TL0,#00H
CLR C
MOV A,TMSBIN
SUBB A,#01H
MOV TMSBIN,A
MOV R3,TT0
DJNZ R3,I_T01 ;中斷次數(shù)<100(每次中斷0.01S),轉(zhuǎn)出
CPL KEYPRE
MOV R3,#100 ;中斷次數(shù)=100次,重新下一個計數(shù)
MOV TMSBIN,#100
CLR C
MOV A,TSBIN ;秒計數(shù)器加1
SUBB A,#01H
MOV TSBIN,A
;SUBB A,#60 ;秒計數(shù)器>59? Y,秒計數(shù)器=00
JNC I_T01
MOV A,TMBIN
ADD A,THBIN
JNZ M1
MOV OUT,#11111101B
CLR TR0
MOV TSBIN,#00H
SJMP I_T01
M1: MOV TSBIN,#59
CLR C
MOV A,TMBIN ;分計數(shù)器加1
SUBB A,#01H
MOV TMBIN,A
JNC I_T01
MOV A,THBIN
JZ I_T01
MOV TMBIN,#59
CLR C
MOV A,THBIN ;時計數(shù)器加1
SUBB A,#01H
MOV THBIN,A
;SUBB A,#24 ;時計數(shù)器>23? Y,時計數(shù)器=00
JNC I_T01
MOV THBIN,#00H
I_T01: MOV TT0,R3
POP PSW
POP ACC
RETI
ORG 00F0H
MAIN: MOV P1,#0FFH
MOV OUT,#11111110B ;P3.0輸出低電平
MOV P3,OUT
MOV SP,#40H ;初始化堆棧指針
MOV TMOD,#11H ;T0、T1工作于16為定時器
MOV TT0,#100
MOV TH0,#0DCH ;T0初值
MOV TL0,#00H
MOV TH1,#00H ;T1初值
MOV TL1,#00H
MOV TSBIN,#59
MOV TMBIN,#59
MOV THBIN,#23
CLR EX0
CLR EX1
CLR ES
SETB ET0 ;開T0、T1中斷
SETB ET1
SETB TR0
SETB TR1
SETB EA
MAIN1: MOV R6,#80H ;主處理過程
DISP: MOV KEYCODE,#00H
MOV TH1,#00H
MOV TL1,#00H
ACALL DISPLY ;調(diào)用顯示子程序
DJNZ R6,DISP
MOV DPTR,#KEY
MOV A,KEYCODE ;讀鍵盤值
JMP @A+DPTR ;按鍵值跳相應(yīng)處理程序
KEY: SJMP MAIN1
SJMP KEY1 ;依次為:KEY值+2,4,6,8,因為SJMP占2個字節(jié)
SJMP KEY2 ;S1的KEYCODE=02H,S2: KEYCODE=04H
SJMP KEY3 ;S3: KEYCODE=06H, S4: KEYCODE=08H
SJMP KEY4
KEY1: CLR TR0 ;時計數(shù)器加1
MOV A,THBIN
CLR C
ADDC A,#01H
MOV THBIN,A
SUBB A,#24 ;24D
JC KEY11
MOV THBIN,#00H
KEY11: SJMP MAIN1
NOP
KEY2: CLR TR0 ;分計數(shù)器加1
MOV A,TMBIN
CLR C
ADDC A,#01H
MOV TMBIN,A
SUBB A,#60 ;60D
JC KEY21
MOV TMBIN,#00H
KEY21: SJMP MAIN1
NOP
KEY3: CLR TR0 ;秒計數(shù)器加1
MOV A,TSBIN
CLR C
ADDC A,#01H
MOV TSBIN,A
SUBB A,#60 ;60D
JC KEY31
MOV TSBIN,#00H
KEY31: SJMP MAIN1
NOP
KEY4: MOV TH0,#0DCH ;退出校時狀態(tài)
MOV TL0,#00H
SETB TR0
SJMP MAIN1
; ********** 顯示子程序 **************
ORG 0200H
DISPLY: MOV A,TSBIN ;秒計數(shù)器調(diào)入A
MOV B,#0AH
DIV AB ;秒計數(shù)值÷10 (結(jié)果十位存于A,個位存于B)
SWAP A
ORL A,#07H ;顯示秒的十位
MOV P1,A
MOV A,#DPS1
ANL A,OUT
MOV P3,A
ACALL DSPDEL ;延時(顯示時)0.5ms
MOV C,P3.7 ;讀S4按鍵狀態(tài)
MOV A,#HIDE
ANL A,OUT
MOV P3,A
JC NP1
MOV KEYCODE,#08H ;S4按下,鍵盤值=8
NP1: MOV P1,#0F7H ;消隱
NOP
NOP
NOP
MOV A,B ;顯示秒的個位
SWAP A
ORL A,#07H
MOV P1,A
MOV A,#DPS0
ANL A,OUT
MOV P3,A
ACALL DSPDEL
MOV A,#HIDE
ANL A,OUT
MOV P3,A
NOP
NOP
NOP
MOV A,TMBIN ;
MOV B,#0AH
DIV AB ;
SWAP A ;顯示分的十位
ORL A,#07H
MOV P1,A
MOV A,#DPM1
ANL A,OUT
MOV P3,A
ACALL DSPDEL
MOV C,P3.7 ;檢測S2是否按下
MOV A,#HIDE
ANL A,OUT
MOV P3,A
JC NP2
MOV KEYCODE,#04H ;S2按下,鍵盤值=4
NOP
NP2: MOV P1,#0F7H ;消隱
NOP
NOP
MOV A,B
SWAP A ;顯示分的個位
ORL A,#07H
MOV P1,A
;MOV C,KEYPRE
;MOV P1.3,C
SETB P1.3
MOV A,#DPM0
ANL A,OUT
MOV P3,A
ACALL DSPDEL
MOV C,P3.7 ;判斷S3是否按下
MOV A,#HIDE
ANL A,OUT
MOV P3,A
JC NP3
MOV KEYCODE,#06H ;若S3按下,鍵盤值=6
NOP
NP3: MOV P1,#0F7H ;消隱
NOP
NOP
MOV A,THBIN ;
MOV B,#0AH
DIV AB ;
SWAP A ;顯示時的十位
JNZ DISPLY1
MOV A,#0F7H
DISPLY1:ORL A,#07H
MOV P1,A
MOV A,#DPH1
ANL A,OUT
MOV P3,A
ACALL DSPDEL
MOV A,#HIDE
ANL A,OUT
MOV P3,A
NOP
MOV A,B
SWAP A ;顯示時的個位
ORL A,#07H
ANL A,OUT
MOV P1,A
SETB P1.3
MOV A,#DPH0
ANL A,OUT
MOV P3,A
ACALL DSPDEL
MOV C,P3.7 ;判斷S1是否按下
MOV A,#HIDE
ANL A,OUT
MOV P3,A
JC NP4
MOV KEYCODE,#02H ;S1按下,鍵盤值=2
NOP
NP4: MOV P1,#0F7H ;消隱
NOP
NOP
NOP
RET
;********** 延時子程序 **************8
DSPDEL: MOV R7,#0FFH ;延時程序0.5ms
DJNZ R7,$
RET
END