还有一个实列
;=================================================
; 简单时钟
;
; 时间设定 分钟减1 SW1
; 分钟加1 SW2
; 小时减1 SW3
; 小时加1 SW4
;=================================================
LED1 EQU 021H
LED2 EQU 022H
LED3 EQU 023H
LED4 EQU 024H
JS1 EQU 025H 25MS (0-99)
JS2 EQU 026H 1S (0-39)
TimeS EQU 027H (0-59)秒
TimeM EQU 028H (0-59)分钟
TimeH EQU 029H (0-23)小时
LedT0 EQU 030H
ORG 0000H
LJMP START;转入主程序
ORG 000BH 定时器0中断入口
INT_TO:
PUSH ACC
PUSH PSW
;=============================时钟
INC JS1
MOV A,JS1
CJNE A,#100,INT_TIME
MOV JS1,#00
INC JS2
MOV A,JS2
CJNE A,#40,INT_TIME
MOV JS2,#00
===========================================
(11.0592MHz 补偿)提高精度。
(250.6510417-250)*4000/250=10.4166672
MOV JS1,#10
===========================================
秒数加1
INC TimeS
MOV A,TimeS
CJNE A,#60,INT_TIME
MOV TimeS,#00
分钟加1
INC TimeM
MOV A,TimeM
CJNE A,#60,INT_TIME
MOV TimeM,#00
小时加1
INC TimeH
MOV A,TimeH
CJNE A,#24,INT_TIME
MOV TimeH,#00
INT_TIME:
MOV A,TimeH
CALL HEXtoBCD
MOV LED1,A
MOV LED2,B
MOV A,TimeM
CALL HEXtoBCD
MOV LED3,A
MOV LED4,B
;=============================显示
MOV A,#11110000B 关数码管
ORL P2,A
MOV A,LedT0
CJNE A,#0,INT_T0_1
MOV A,LED1
MOVC A,@A+DPTR
MOV P0,A
CLR P2.7
MOV LedT0,#1
LJMP INT_T0_END
INT_T0_1:
MOV A,LedT0
CJNE A,#1,INT_T0_2
MOV A,LED2
MOVC A,@A+DPTR
MOV P0,A
CLR P2.6
MOV LedT0,#2
点小数点
MOV C,TimeS.0
MOV P0.5,C
LJMP INT_T0_END
INT_T0_2:
MOV A,LedT0
CJNE A,#2,INT_T0_3
MOV A,LED3
MOVC A,@A+DPTR
MOV P0,A
CLR P2.5
MOV LedT0,#3
LJMP INT_T0_END
INT_T0_3:
MOV A,LED4
MOVC A,@A+DPTR
MOV P0,A
CLR P2.4
MOV LedT0,#0
LJMP INT_T0_END
INT_T0_END:
POP PSW
POP ACC
RETI
;========十六进制转换为十进制,A高位,B低位
HEXtoBCD:
mov b,#0ah HEX>BCD
div ab
anl a,#00001111b
anl b,#00001111b
RET
START:
MOV SP,#60H
CLR A
MOV JS1,A
MOV JS2,A
MOV TimeH,A
MOV TimeS,A
MOV TimeM,A
MOV TMOD,#02H 设定T0为工作方式2,八位自动重载。
MOV TH0,#025
MOV TL0,#025 256-(250*11.0592/12)=256-230.4=25.6 250us(11.0592)
用25时,中断时间为250.6510417us
SETB TR0 开定时器0
SETB ET0 致能定时0中断
CLR TF0 清定时0中断标志
SETB EA 开总中断
MOV LedT0,#00H
MOV DPTR,#TABLE
MAIN:
MOV A,#00000011B
ORL A,P3
CPL A
JZ MAIN
LCALL DELAY10MS 去抖动
MOV A,#00000011B
ORL A,P3
CPL A
JZ MAIN
JB ACC.2,KEY_SW1
JB ACC.3,KEY_SW2
JB ACC.4,KEY_SW3
JB ACC.5,KEY_SW4
LJMP MAIN
;==============时间设定.分钟减1
KEY_SW1:
CLR C
MOV A,TimeM
SUBB A,#1
MOV TimeM,A
JNC KEY_SW1_END
借位,表示已经减到小于0
MOV TimeM,#59
KEY_SW1_END:
AJMP KEY_END
;==============时间设定.分钟加1
KEY_SW2:
INC TimeM
MOV A,TimeM
CJNE A,#60,KEY_SW2_END
MOV TimeM,#00
KEY_SW2_END:
AJMP KEY_END
;==============时间设定.小时减1
KEY_SW3:
CLR C
MOV A,TimeH
SUBB A,#1
MOV TimeH,A
JNC KEY_SW3_END
借位,表示已经减到小于0
MOV TimeH,#23
KEY_SW3_END:
AJMP KEY_END
;==============时间设定.小时加1
KEY_SW4:
INC TimeH
MOV A,TimeH
CJNE A,#24,KEY_SW4_END
MOV TimeH,#00
KEY_SW4_END:
AJMP KEY_END
;按键放开处理
KEY_END:
MOV A,#00000011B
ORL A,P3
CPL A
JNZ KEY_END
LJMP MAIN
DELAY10MS:
MOV R4,#20
DELAY10MSA:
MOV R5,#247
DJNZ R5,$
DJNZ R4,DELAY10MSA
RET
TABLE:
DB 00101000B 0
DB 11101011B 1
DB 00110010B 2
DB 10100010B 3
DB 11100001B 4
DB 10100100B 5
DB 00100100B 6
DB 11101010B 7
DB 00100000B 8
DB 10100000B 9
DB 01100000B A
DB 00100101B b
DB 00111100B C
DB 00100011B d
DB 00110100B E
DB 01110100B F
DB 11110111B -
DB 11111111B
END