这个电子钟程序已经基本符合你的要求。希望能对你有用。
程序说明:
显示的时间格式为:12-00-00,既,时-分-秒
按键说明:P3.2为时调节。P3.3为分调节。P3.4为秒调节。这三个按键均是每按一次加一。
晶振说明:12MHZ
数码管说明:共阳数码管。共8个数码管。共用了两个4连共阳数码管。
其中,P0口为数码管的段选,P2口为数码管的位选。
本程序已通过实践验证!!!
汇编源程序如下:
SECOND EQU 30H
MINITE EQU 31H
HOUR EQU 32H
HOURK BIT P3.2
MINITEK BIT P3.3
SECONDK BIT P3.4
DISPBUF EQU 40H
DISPBIT EQU 48H
T2SCNTA EQU 49H
T2SCNTB EQU 4AH
TEMP EQU 4BH
ORG 00H
LJMP START
ORG 0BH
LJMP INT_T0
START:MOV SECOND,#00H
MOV MINITE,#00H
MOV HOUR,#12
MOV DISPBIT,#00H
MOV T2SCNTA,#00H
MOV T2SCNTB,#00H
MOV TEMP,#0FEH
LCALL DISP
MOV TMOD,#01H
MOV TH0,#(65536-2000) / 256
MOV TL0,#(65536-2000) MOD 256
SETB TR0
SETB ET0
SETB EA
WT:JB SECONDK,NK1
LCALL DELY10MS
JB SECONDK,NK1
INC SECOND
MOV A,SECOND
CJNE A,#60,NS60
MOV SECOND,#00H
NS60:LCALL DISP
JNB SECONDK,$
NK1:JB MINITEK,NK2
LCALL DELY10MS
JB MINITEK,NK2
INC MINITE
MOV A,MINITE
CJNE A,#60,NM60
MOV MINITE,#00H
NM60:LCALL DISP
JNB MINITEK,$
NK2:JB HOURK,NK3
LCALL DELY10MS
JB HOURK,NK3
INC HOUR
MOV A,HOUR
CJNE A,#24,NH24
MOV HOUR,#00H
NH24:LCALL DISP
JNB HOURK,$
NK3:LJMP WT
DELY10MS:MOV R6,#10
D1:MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
RET
DISP:MOV A,#DISPBUF
ADD A,#8
DEC A
MOV R1,A
MOV A,HOUR
MOV B,#10
DIV AB
MOV @R1,A
DEC R1
MOV A,B
MOV @R1,A
DEC R1
MOV A,#10
MOV @R1,A
DEC R1
MOV A,MINITE
MOV B,#10
DIV AB
MOV @R1,A
DEC R1
MOV A,B
MOV @R1,A
DEC R1
MOV A,#10
MOV@R1,A
DEC R1
MOV A,SECOND
MOV B,#10
DIV AB
MOV @R1,A
DEC R1
MOV A,B
MOV @R1,A
DEC R1
RET
INT_T0:MOV TH0,#(65536-2000) / 256
MOV TL0,#(65536-2000) MOD 256
MOV A,#DISPBUF
ADD A,DISPBIT
MOV R0,A
MOV A,@R0
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
MOV A,DISPBIT
MOV DPTR,#TAB
MOVC A,@A+DPTR
MOV P2,A
INC DISPBIT
MOV A,DISPBIT
CJNE A,#08H,KNA
MOV DISPBIT,#00H
KNA:INC T2SCNTA
MOV A,T2SCNTA
CJNE A,#100,DONE
MOV T2SCNTA,#00H
INC T2SCNTB
MOV A,T2SCNTB
CJNE A,#05H,DONE
MOV T2SCNTB,#00H
INC SECOND
MOV A,SECOND
CJNE A,#60,NEXT
MOV SECOND,#00H
INC MINITE
MOV A,MINITE
CJNE A,#60,NEXT
MOV MINITE,#00H
INC HOUR
MOV A,HOUR
CJNE A,#24,NEXT
MOV HOUR,#00H
NEXT:LCALL DISP
DONE:RETI
TABLE:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,0BFH
TAB:DB 0FEH,0FDH,0FBH,0F7H,0EFH,0DFH,0BFH,07FH
END
KEYVAL
EQU
30H
KEYTM
EQU
31H
KEYSCAN
EQU
32H
DAT
EQU
33H
SCANLED
EQU
39H
CLK
EQU
77H
SEC
EQU
78H
MIN
EQU
79H
HOUR
EQU
7AH
PAUSE
BIT
00H
DOT
BIT
01H
ORG
0000H
LJMP
MAIN
ORG
000BH
LJMP
T0ISR
;50ms定时
ORG
001BH
LJMP
T1ISR
;扫描显示
ORG
0030H
MAIN:
MOV
SP,#5FH
MOV
TMOD,#11H
MOV
TH0,#03CH
MOV
TL0,#0B0H
MOV
TH1,#0ECH
MOV
TL1,#078H
MOV
KEYVAL,#0
MOV
SCANLED,#0
MOV
33H,#10H
MOV
34H,#10H
MOV
35H,#10H
MOV
36H,#10H
MOV
37H,#10H
MOV
38H,#10H
MOV
SEC,#0
MOV
MIN,#0
MOV
HOUR,#0
MOV
CLK,#0
CLR
PAUSE
SETB
EA
SETB
ET1
SETB
TR1
LOOP:
LCALL
KEYSEL
MOV
A,KEYVAL
CJNE
A,#0FFH,LOOP1
SJMP
LOOP
LOOP1:
CJNE
A,#10,LOOP2
;“ON”启动
SETB
TR0
SETB
ET0
SETB
PAUSE
SJMP
LOOP
LOOP2:
CJNE
A,#11,LOOP3
;“=”清零
MOV
SEC,#0
MOV
MIN,#0
MOV
HOUR,#0
LCALL
DISCHG
SJMP
LOOP
LOOP3:
CJNE
A,#15,LOOP4
;“+”暂停
CLR
TR0
CLR
ET0
CLR
PAUSE
SJMP
LOOP
LOOP4:
CJNE
A,#14,LOOP5
;“-”清显示暂停
MOV
33H,#10H
MOV
34H,#10H
MOV
35H,#10H
MOV
36H,#10H
MOV
37H,#10H
MOV
38H,#10H
CLR
TR0
CLR
ET0
CLR
PAUSE
SJMP
LOOP
LOOP5:
CJNE
A,#10,LOOP6
;数字键
LOOP6:
JC
LOOP7
LJMP
LOOP
LOOP7:
JNB
PAUSE,LOOP8
;暂停状态可以输入数字键
LJMP
LOOP
LOOP8:
MOV
33H,34H
MOV
34H,35H
MOV
35H,36H
MOV
36H,37H
MOV
37H,38H
MOV
38H,KEYVAL
MOV
A,33H
SWAP
A
ORL
A,34H
LCALL
BCDH
MOV
HOUR,A
MOV
A,35H
SWAP
A
ORL
A,36H
LCALL
BCDH
MOV
MIN,A
MOV
A,37H
SWAP
A
ORL
A,38H
LCALL
BCDH
MOV
SEC,A
LJMP
LOOP
;------------------
;BCD转换为十六进制
BCDH:
MOV
B,#10H
DIV
AB
MOV
R7,B
MOV
B,#10
MUL
AB
ADD
A,R7
RET
;------------------
;十六进制转换为BCD
HBCD:
MOV
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0ISRE
MOV
CLK,#0
INC
SEC
MOV
A,SEC
CJNE
A,#60,T0ISRE
MOV
SEC,#0
INC
MIN
MOV
A,MIN
CJNE
A,#60,T0ISRE
MOV
MIN,#0
INC
HOUR
MOV
A,HOUR
CJNE
A,#24,T0ISRE
MOV
SEC,#0
MOV
MIN,#0
MOV
HOUR,#0
T0ISRE:
LCALL
DISCHG
POP
ACC
RETI
;--------------------
DISCHG:
MOV
A,HOUR
LCALL
HBCD
PUSH
ACC
ANL
A,#0FH
MOV
34H,A
POP
ACC
ANL
A,#0F0H
SWAP
A
MOV
33H,A
MOV
A,MIN
LCALL
HBCD
PUSH
ACC
ANL
A,#0FH
MOV
36H,A
POP
ACC
ANL
A,#0F0H
SWAP
A
MOV
35H,A
MOV
A,SEC
LCALL
HBCD
PUSH
ACC
ANL
A,#0FH
MOV
38H,A
POP
ACC
ANL
A,#0F0H
SWAP
A
MOV
37H,A
RET
;--------------------
T1ISR:
PUSH
ACC
CLR
TR1
MOV
TH1,#0ECH
MOV
TL1,#78H
SETB
TR1
MOV
DPTR,#LEDTAB
T100:
MOV
R0,#DAT
MOV
A,SCANLED
ADD
A,R0
MOV
R0,A
MOV
A,SCANLED
JNZ
T101
MOV
P2,#01H
CLR
DOT
SJMP
T1DIS
T101:
DEC
A
JNZ
T102
MOV
P2,#02H
SETB
DOT
SJMP
T1DIS
T102:
DEC
A
JNZ
T103
MOV
P2,#04H
CLR
DOT
SJMP
T1DIS
T103:
DEC
A
JNZ
T104
MOV
P2,#08H
SETB
DOT
SJMP
T1DIS
T104:
DEC
A
JNZ
T105
MOV
P2,#10H
CLR
DOT
SJMP
T1DIS
T105:
MOV
P2,#20H
CLR
DOT
T1DIS:
MOV
A,@R0
MOVC
A,@A+DPTR
JNB
DOT,T1DIS1
ORL
A,#01H
T1DIS1:
CPL
A
MOV
P0,A
INC
SCANLED
MOV
A,SCANLED
CJNE
A,#6,T1END
MOV
SCANLED,#0
T1END:
POP
ACC
RETI
;--------------------
CHGKEY:
MOV
A,KEYVAL
JZ
KV16
DEC
A
JNZ
KV01
MOV
KEYVAL,#7
RET
KV01:
DEC
A
JNZ
KV02
MOV
KEYVAL,#4
RET
KV02:
DEC
A
JNZ
KV03
MOV
KEYVAL,#1
RET
KV03:
DEC
A
JNZ
KV04
MOV
KEYVAL,#10
RET
KV04:
DEC
A
JNZ
KV05
MOV
KEYVAL,#8
RET
KV05:
DEC
A
JNZ
KV06
MOV
KEYVAL,#5
RET
KV06:
DEC
A
JNZ
KV07
MOV
KEYVAL,#2
RET
KV07:
DEC
A
JNZ
KV08
MOV
KEYVAL,#0
RET
KV08:
DEC
A
JNZ
KV09
MOV
KEYVAL,#9
RET
KV09:
DEC
A
JNZ
KV10
MOV
KEYVAL,#6
RET
KV10:
DEC
A
JNZ
KV11
MOV
KEYVAL,#3
RET
KV11:
DEC
A
JNZ
KV12
MOV
KEYVAL,#11
RET
KV12:
DEC
A
JNZ
KV13
MOV
KEYVAL,#12
RET
KV13:
DEC
A
JNZ
KV14
MOV
KEYVAL,#13
RET
KV14:
DEC
A
JNZ
KV15
MOV
KEYVAL,#14
RET
KV15:
DEC
A
JNZ
KV16
MOV
KEYVAL,#15
RET
KV16:
MOV
KEYVAL,#0FFH
RET
;--------------------
LEDTAB:
DB
0FCH
;"0"
00H
DB
60H
;"1"
01H
DB
0DAH
;"2"
02H
DB
0F2H
;"3"
03H
DB
66H
;"4"
04H
DB
0B6H
;"5"
05H
DB
0BEH
;"6"
06H
DB
0E0H
;"7"
07H
DB
0FEH
;"8"
08H
DB
0F6H
;"9"
09H
DB
0EEH
;"A"
0AH
DB
3EH
;"B"
0BH
DB
9CH
;"C"
0CH
DB
7AH
;"D"
0DH
DB
9EH
;"E"
0EH
DB
8EH
;"F"
0FH
DB
00H
;"
"
10H
;--------------------
END