ORG 0000H
LJMP MAIN
ORG 000BH
LJMP TIM01 ;0.1s T0中断
ORG 0300H
MAIN: ACALL INIT ;调用初始化函数
LOOP: LCALL KEYIN ;键盘输入
AJMP LOOP
INIT: MOV 7FH,#7EH ;7FH存当前输入位置79-7EH
MOV 79H,#0 ;初始化显示
MOV 7AH,#0
MOV 7BH,#0
MOV 7CH,#0
MOV 7DH,#0
MOV 7EH,#0
MOV 70H,#0 ;初始化初始时间0h0m0s
MOV 71H,#0
MOV 72H,#0
MOV 73H,#0
SETB 20H.0 ;20H.0存储当前输入状态,闹钟输入,或初始值输入KEYIN: LCALL KEY ;键盘输入函数,循环对79-7EH输入,或是命令输入CLR C
PUSH ACC ;入栈,保存A值
SUBB A,#10 ;和10比较
JNC CONTRL ;大于等于10,命令键
POP ACC ;A出栈数字键,放到显示缓存
MOV R0,7FH ;A放到7FH内容指向地址处
MOV @R0,A
MOV A,7FH ;是否出了79H-7EH范围
CJNE A,#79H,RU ;出范围,循环到7EH
MOV 7FH,#7FH
RET
RU: DEC 7FH ;范围内自减1
RET
CONTRL: POP ACC ;控制键,执行相应控制操作
CJNE A,#0DH,N0C
LCALL KJUD ;D 控制计时开始,KJUD判断是否在有效时间范围内
JNC N0 ;控制操作完成退出
LCALL TIMINIT ;定时器及相关内容初始化
N0C: CJNE A,#0CH,N0B ;C 暂停开始键
CPL EA ;中断开关控制计时起止
N0B: CJNE A,#0BH,N0 ;B 选择输入是闹钟还是起始值
CPL 20H.0 ;20H.0为其标志0 起始1 闹钟
MOV C,20H.0 ;指示灯表示输入闹钟还是起始值
MOV P1.0,C
N0: RET
TIMINIT:MOV TH0,#03CH ;定时器及相关初始化
MOV TL0,#0B0H ;初值
SETB TR0 ;运行
SETB ET0 ;允许定时器中断
SETB EA ;总中断允许
MOV 70H,#0 ;载入计时初值
MOV 71H,74H
MOV 72H,75H
MOV 73H,76H
RET
DISP: MOV R0,#79H ;显示函数从右至左显示
MOV R3,#01H
MOV A,R3
DIO: MOV DPTR,#0FFDDH
MOVX @DPTR,A
MOV A,@R0
MOV DPTR,#TAB1
MOVC A,@A+DPTR ;得到键值
MOV DPTR,#0FFDCH
DIR1: MOVX @DPTR,A
LCALL DELAY1
INC R0
MOV A,R3
JB ACC.5,LD1
RL A
MOV R3,A
AJMP DIO
LD1: RET
TAB1: DB 0C0H,0F9H,0A4H,0B0H,099H,092H,082H,0F8H ;显示表A灭B显示P(错误信号)
DB 080H,090H,0FFH,08CH,0C6H,0A1H,086H,08EH
TAB2: DB 07H,04H,08H,05H,09H,06H,0AH,0BH,01H,00H ;键值校正表DB 02H,0FH,03H,0EH,0CH,0DH
DELAY1: MOV R7,#02H ;延时函数12M时精确延时1ms
DE1: MOV R6,#0FFH
DE2: DJNZ R6,DE2
DJNZ R7,DE1
RET
JUDGE: MOV DPTR,#0FFDDH ;判断是否有效MOV A,#00H
MOVX @DPTR,A
INC DPTR
MOVX A,@DPTR
CPL A
ANL A,#0FH
RET
KEY: LCALL JUDGE
JNZ LK1
LCALL DISP;
AJMP KEY
LK1: LCALL DISP;
LCALL JUDGE
JNZ LK2
LCALL DISP
AJMP KEY
LK2: MOV R2,#0FEH
MOV R4,#00H
LK3: MOV DPTR,#0FFDDH
MOV A,R2
MOVX @DPTR,A
INC DPTR
MOVX A,@DPTR
LINE0: JB ACC.0,LINE1
MOV A,#00H
AJMP LKP
LINE1: JB ACC.1,LINE2
MOV A,#08H
AJMP LKP
LINE2: JB ACC.2,LINE3
MOV A,#10H
AJMP LKP
LINE3: JB ACC.3,NEXT
MOV A,#18H
LKP: ADD A,R4
PUSH ACC
NT: LCALL DISP
LCALL JUDGE
JNZ NT
POP ACC
CJNE A,#10H,N
N: JNC EXIT
MOV DPTR,#TAB2
MOVC A,@A+DPTR
EXIT: RET
NEXT: INC R4
MOV A,R2
JNB ACC.7,KND ;4*8
RL A
MOV R2,A
AJMP LK3
KND: AJMP KEY
TIM01: PUSH PSW ;0.1s定时中断PUSH ACC ;保护现场
MOV TH0,#03CH ;载入初值
MOV TL0,#0B0H
MOV A,70H ;0.1s计时值存到70H
CJNE A,#59,EX01
MOV 70H,#0
MOV A,71H ;秒值放入71H
CJNE A,#59,EX1S
MOV 71H,#0
MOV A,72H ;分钟放入72H
CJNE A,#59,EX1M
MOV 72H,#0
MOV A,73H
CJNE A,#23,EX1H
MOV 73H,#0 ;小时73H
EX01: INC 70H
AJMP IEXIT
EX1S: INC 71H
ACALL ALJUD ;判断报警信号函数
JNZ ALARM
SETB P1.1
AJMP IEXIT ;报警
ALARM: CLR P1.1
AJMP IEXIT
EX1M: INC 72H
EX1H: INC 73H
AJMP IEXIT
IEXIT: LCALL DISTIM
POP ACC ;恢复现场
POP PSW
RETI
ALJUD: CLR C ;报警判断
MOV A,73H
SUBB A,66H ;闹钟值和现在时间值比较
JC ALEX
CLR C
MOV A,72H
SUBB A,65H
JC ALEX
SETB C
MOV A,71H
SUBB A,64H
JC ALEX
CLR A
RET
ALEX: MOV A,#1
RET
DISTIM: MOV B,#10 ;把当前时间值放入79-7EH 显示MOV A,71H
DIV AB
MOV 79H,B
MOV 7AH,A
MOV A,72H
MOV B,#10
DIV AB
MOV 7BH,B
MOV 7CH,A
MOV A,73H
MOV B,#10
DIV AB
MOV 7DH,B
MOV 7EH,A
RET
KJUD: CLR C ;键值判断,判断输入是否有效
MOV B,#10
MUL AB
ADD A,7DH
JB 20H.0,ALH
MOV 76H,A
AJMP GOH
ALH: MOV 66H,A ;初始值放入74-76H 闹钟放入64-66H GOH: SUBB A,#24
JNC EX
MOV A,7CH
MOV B,#10
MUL AB
ADD A,7BH
JB 20H.0,ALM
MOV 75H,A
AJMP GOM
ALM: MOV 65H,A
GOM: SUBB A,#60
JNC EX
MOV A,7AH
MOV B,#10
MUL AB
ADD A,79H
JB 20H.0,ALS
MOV 74H,A
AJMP GOS
ALS: MOV 64H,A
GOS: SUBB A,#60
JNC EX
RET
EX: MOV 79H,#0BH ;输入值无效显示PPPPPP
MOV 7AH,#0BH
MOV 7BH,#0BH
MOV 7CH,#0BH
MOV 7DH,#0BH
MOV 7EH,#0BH
RET
END。