;程序说明:
;显示的时间格式为:23-59-50,既,时-分-秒
;按键说明:P2.4为时调节。P2.5为分调节。P2.6为秒调节。这三个按键均是每按一次加一。
;晶振说明:12MHZ
;数码管说明:共阴数码管。共8个数码管。共用了两个4连共阴数码管。
;其中,P0口为数码管的段选,同时P0口为数码管的位选。
;本程序已通过实践验证!!!
;Author:爱因斯尼 @SCUT ;TIME:2011.05.20
;编源程序如下:
;===========================
;分别为时分秒分配一个内存
;以后就从各自的内存中读出相应的数值
;这样方便随时修改数值
SECOND EQU 30H
MINITE EQU 31H
HOUR EQU 32H
;===========================
HOUR_SET BIT P2.4
MINITE_SET BIT P2.5
SECOND_SET BIT P2.6
DISP_SMGBUF EQU 40H
DISP_SMGBIT EQU 48H
T2SCNTA EQU 49H
T2SCNTB EQU 4AH
TEMP EQU 4BH
DATASMG EQU P0 ;定义数码管的数据流入端
DUAN EQU P1.5
WEI EQU P1.6
ORG 00H
LJMP START
ORG 0BH
LJMP INT_T0
START:
CALL SYSTEM_Init
CALL DELAY10MS
; JMP $
;============
;在这里设置时钟初始的时分秒
MOV HOUR,#23
MOV MINITE,#59
MOV SECOND,#50
;============
MOV DISP_SMGBIT,#00H
MOV T2SCNTA,#00H
MOV T2SCNTB,#00H
MOV TEMP,#0FEH
LCALL DISP_SMG
MOV TMOD,#01H
MOV TH0,#(65536-2000) / 256
MOV TL0,#(65536-2000) MOD 256
SETB TR0
SETB ET0
SETB EA
LOOP:
JB SECOND_SET,NK1 ;如果秒设置按键不为低(没按下)的话,就正常跳转
LCALL DELAY10MS
JB SECOND_SET,NK1 ;延时消抖
; CLR TR0 ;先停止定时器
INC SECOND ;秒增加一
MOV A,SECOND
CJNE A,#60,NS60//是否<=59,是的话则跳转
MOV SECOND,#00H
NS60:LCALL DISP_SMG
JNB SECOND_SET,$;还没松开按键的话就在此停留
NK1:JB MINITE_SET,NK2
LCALL DELAY10MS
JB MINITE_SET,NK2
; CLR TR0 ;先停止定时器
INC MINITE
MOV A,MINITE
CJNE A,#60,NM60
MOV MINITE,#00H
NM60:LCALL DISP_SMG
JNB MINITE_SET,$
NK2:JB HOUR_SET,NK3
LCALL DELAY10MS
JB HOUR_SET,NK3
; CLR TR0 ;先停止定时器
INC HOUR
MOV A,HOUR
CJNE A,#24,NH24
MOV HOUR,#00H
NH24:LCALL DISP_SMG
JNB HOUR_SET,$
NK3:LJMP LOOP
;=========================下面是子函数和中断
DELAY10MS:MOV R6,#10
D1:MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
RET
DISP_SMG:;=====数码管显示
MOV A,#DISP_SMGBUF
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
;================================
SYSTEM_Init:
MOV P2,#0XFE;软件设置为独立按键
SETB DUAN
MOV DATASMG,#0XFF
CLR DUAN
SETB WEI
MOV DATASMG,#0X00
CLR WEI
;================================
INT_T0:MOV TH0,#(65536-2000) / 256 ;重装定时器初值
MOV TL0,#(65536-2000) MOD 256
MOV A,#DISP_SMGBUF
ADD A,DISP_SMGBIT
MOV R0,A
MOV A,@R0
MOV DPTR,#TABLE
MOVC A,@A+DPTR
SETB DUAN
MOV DATASMG,A
CLR DUAN
MOV A,DISP_SMGBIT
MOV DPTR,#TAB
MOVC A,@A+DPTR
SETB WEI
MOV DATASMG,A
CLR WEI
INC DISP_SMGBIT
MOV A,DISP_SMGBIT
CJNE A,#08H,KNA
MOV DISP_SMGBIT,#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_SMG
DONE:RETI
;======================数码管的字模
TABLE:
DB 0FCH,60H,0DAH,0F2H ;0,1,2,3,
DB 66H,0B6H,0BEH,0E0H ;4,5,6,7,
DB 0FEH,0F6H,02H ;8,9,"-"
TAB:
DB 0FEH,0FDH,0FBH,0F7H,0EFH,0DFH,0BFH,07FH
;===================程序结束
END
用户377235 2013-6-17 21:20