📄 final version of timer.asm
字号:
#include <sfr51.inc>
#include <util51.inc>
;---------------------MAIN--------------------
ORG 00H
SJMP MAIN
ORG 03H
LJMP RESET
ORG 0BH
LJMP TIMER0
MAIN: SETB P2.0
CLR P2.1
MOV R2,#00H ;initialization
MOV R4,#00H
MOV A,#00H
LUD: ACALL DISPLAY ; wait for the num input and the operation input
JB P1.0,IM1
JB P1.1,IM2
CJNE R4,#01H,SCAN1
JB P1.2,SHIFT
SJMP LUD
SHIFT: JB P1.2,$ ;To input two digit, a shift is need
RL A
RL A
RL A
RL A
CLR Acc.3
CLR Acc.2
CLR Acc.1
CLR Acc.0
MOV R4,#00H
SJMP LUD
IM1: JB P1.0,$ ;a intermidiate state to jump to UP, because JB is not enough
AJMP UP
IM2: JB P1.1,$ ;a intermidiate state to jump to DOWN, because JB is not enough
AJMP DOWN
;----------------------SCAN INPUT----------------------------
SCAN1: MOV P3,#FFH ;check the first colume
CLR P3.4
MOV R5,P3
CJNE R5,#EEH,SCAN4 ;Scan the input of 1
ADD A,#01H
MOV P0,A
INC R4
LJMP LUD
SCAN4: CJNE R5,#EDH,SCAN7 ;Scan the input of 4
ADD A,#04H
MOV P0,A
INC R4
LJMP LUD
SCAN7: CJNE R5,#EBH,SCAN2 ;Scan the input of 7
ADD A,#07H
MOV P0,A
INC R4
LJMP LUD
SCAN2: MOV P3,#FFH ;Check the second colume
CLR P3.5
MOV R5,P3
CJNE R5,#DEH,SCAN5 ;Scan the input of 2
ADD A,#02H
MOV P0,A
INC R4
LJMP LUD
SCAN5: CJNE R5,#DDH,SCAN8 ;Scan the input of 5
ADD A,#05H
MOV P0,A
INC R4
LJMP LUD
SCAN8: CJNE R5,#DBH,SCAN0 ;Scan the input of 8
ADD A,#08H
MOV P0,A
INC R4
LJMP LUD
SCAN0: CJNE R5,#D7H,SCAN3 ;Scan the input of 0
ADD A,#00H
MOV P0,A
INC R4
LJMP LUD
SCAN3: MOV P3,#FFH ;check the third colume
CLR P3.6
MOV R5,P3
CJNE R5,#BEH,SCAN6 ;Scan the input of 3
ADD A,#03H
MOV P0,A
INC R4
LJMP LUD
SCAN6: CJNE R5,#BDH,SCAN9 ;Scan the input of 6
ADD A,#06H
MOV P0,A
INC R4
LJMP LUD
SCAN9: CJNE R5,#BBH,IM ;Scan the input of 9
ADD A,#09H
MOV P0,A
INC R4
LJMP LUD
IM: AJMP LUD
;--------------------------------------------------------------
DOWN: MOV R2,#00H ;Set count down
SJMP LSTART
UP: MOV R2,#FFH ;Set count up
LSTART: JB P1.0,START ;Check start buttom
LJMP LSTART
LSP: JB P1.2,STOP ;Check stop and pause buttom
JB P1.1,PAUSE
AJMP LSP
START: JB P1.0,$ ;Set the timer0 interrupt start to count
SETB EA
SETB ET0
MOV TMOD,#0 ;Set Mode 0
MOV R0,#200 ;0.005*200=1sec
MOV TH0,#63H ;Set the interrupt to count 0.005 sec
MOV TL0,#18H
SETB TR0 ;Start the interrupt
SJMP LSP
PAUSE: JB P1.1,$ ;Pause the counting
CLR TR0
LSR: JB P1.2,STOP ;Check stop and restart buttom
JB P1.3,RESTART
AJMP LSR
STOP: JB P1.2,$ ;Stop the counting and jump back to main
CLR TR0
AJMP MAIN
RESTART: JB P1.3,$ ;Restart the count after pause
SETB TR0 ;Start to interrupt
LJMP LSP
DISPLAY: MOV P0,A ;Display the value to the 7segment LED
RET
;------------------INTERRUPT---------------------
TIMER0: CLR TR0 ;Check wheather it reach 1 sec or not
DJNZ R0,EXIT; ;if not yet 1 sec , jump to exit
MOV R0,#200;
LCHECK: CJNE R2,#00H,INCREASE ;Check count up or count down
MOV 20H,A ;use to compare the less significant digit
MOV 21H,A ;use to compare the most significant digit
CLR 20H.7 ;clear the most significant digit
CLR 20H.6
CLR 20H.5
CLR 20H.4
MOV R1,#20H
CJNE @R1,#00H,NMALS ;compare the less significant digit, if > 0,jump NMALS
CLR 21H.3 ;Clear the less significant digit
CLR 21H.2
CLR 21H.1
CLR 21H.0
MOV R1,#21H
CJNE @R1,#00H,NMALT ;compare the most significant digit,if > 0,jump NAMLT
SETB Acc.0 ;if the digit is "00" move "99" to the display in next second
CLR Acc.1
CLR Acc.2
SETB Acc.3
SETB Acc.4
CLR Acc.5
CLR Acc.6
SETB Acc.7
ACALL BUZZ ;Call a buzz sound
SJMP DIS
NMALS: DEC A ;A=A-1
SJMP DIS
NMALT: SUBB A,#10H ;Decrease the most significant digit by 1
SETB Acc.0 ;Set the less significant digit to '9'
CLR Acc.1
CLR Acc.2
SETB Acc.3
SJMP DIS
INCREASE: MOV 20H,A ;use to compare the less significant digit
MOV 21H,A ;use to compare the most significant digit
CLR 20H.7 ;Clear the most significant digit
CLR 20H.6
CLR 20H.5
CLR 20H.4
MOV R1,#20H
CJNE @R1,#09H,SNMAL ;compare the lesss significant digit,if < 9,jump SNMAL
CLR 21H.3 ;Clear the less significant digit
CLR 21H.2
CLR 21H.1
CLR 21H.0
MOV R1,#21H
CJNE @R1,#90H,TNMAL ;compare the most significant digit,if < 9, Jump TNMAL
CLR A ;if the digit is "99", Move a "00" to display in the next second
ACALL BUZZ ;Call a Buzz sound
SJMP DIS
TNMAL: CLR Acc.0 ;Increase the most significant digit by 1 and clear the less significant digit
CLR Acc.1
CLR Acc.2
CLR Acc.3
ADD A,#10H
SJMP DIS
SNMAL: INC A ;Increase the less significant digit by 1
DIS: ACALL DISPLAY
EXIT: MOV TH0,#63H ;Set the interrupt to continue counting
MOV TL0,#18H
SETB TR0
RETI
BUZZ: CLR P2.0 ;Output to the buzzer
CLR P2.2
MOV R6,#FFH
DELAY1: MOV R5,#FFH
DJNZ R5,$
DJNZ R6,DELAY1
SETB P2.0
SETB P2.2
RET
RESET: LJMP MAIN
RETI
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -