第3个回答 2010-08-23
很完美的按键控制可调时钟。消抖都在里面包含。好好研究~~~~~~~
R_QH EQU 44H
R_BZ BIT 18H
R_1 EQU 26H
R_2 EQU 27H
R_3 EQU 28H
B_DEBOUNCE EQU 65H
B_HASKEY EQU 66H
ORG 0000H
LJMP L_MAIN
ORG 000BH
LJMP I_T0
ORG 001BH
LJMP I_T1
ORG 0030H
L_MAIN:
MOV SP,#62H
MOV TMOD,#11H
MOV TH0,#HIGH(62536)
MOV TL0,#LOW(62536)
MOV TH1,#HIGH(15536)
MOV TL1,#LOW(15536)
MOV IE,#10001010B
SETB TR0
SETB TR1
SETB PT1
SETB R_BZ
MOV 40H,#59H
MOV 41H,#58H
MOV 42H,#23H
MOV R_1,#20
MOV R_2,#0
MOV R_3,#55H
MOV 55H,#0
MOV 56H,#0
MOV 57H,#0
MOV 58H,#0
MOV 59H,#0
MOV 5AH,#0
MOV B_HASKEY,#00H
MOV B_DEBOUNCE,#00H
LCALL F_CHAIFEN
L_LOOP:
JB B_DEBOUNCE,L_JSJZ
MOV P1,#0FH
MOV A,P1
ANL A,#0FH
CJNE A,#0FH,L_PRESS
JNB B_HASKEY ,L_WAJ
LCALL F_KEYRELEASE
CLR B_HASKEY
L_WAJ: LJMP L_FANHUI
L_JSJZ:
CLR B_DEBOUNCE
LCALL F_KEYSCAN
LCALL F_KEYservice
SETB B_HASKEY
LJMP L_FANHUI
L_PRESS:
JNB B_HASKEY,L_GAX
LCALL F_XDYS200ms
LCALL F_KEYHOLD
LJMP L_FANHUI
L_GAX:
SETB B_DEBOUNCE
LCALL F_XDYS20ms
LJMP L_FANHUI
F_KEYRELEASE:
CPL P2.0
RET
F_KEYHOLD:
MOV A,75H
CJNE A,#02H,L_NEXT3
LJMP KEY2
RET
L_NEXT3:
CJNE A,#03H,L_FANHUI0
LJMP KEY3
L_FANHUI0:
RET
L_FANHUI:
LJMP L_LOOP
F_KEYSCAN:
MOV P1,#0FH
MOV A,P1
MOV R4,#0FFH
L_LOOP1:
INC R4;计算移动次数,得到第几位为0
RRC A
JC L_LOOP1
MOV P1,#0F0H
MOV A,P1
MOV R5,#0FFH
L_LOOP2:
INC R5;计算移动次数,得到第几位为0
RLC A
JC L_LOOP2
MOV B,#4
MOV A,R4
MUL AB
ADD A,R5
MOV 75H,A
RET
F_KEYservice:
MOV A,75H
MOV DPTR,#T_LJMP
MOV B,#3
MUL AB
JMP @A+DPTR
T_LJMP:
LJMP KEY0
LJMP KEY1
LJMP KEY2
LJMP KEY3
LJMP KEY0
LJMP KEY1
LJMP KEY2
LJMP KEY3
LJMP KEY0
LJMP KEY1
LJMP KEY2
LJMP KEY3
LJMP KEY0
LJMP KEY1
LJMP KEY2
LJMP KEY3
KEY0:
CPL R_BZ
MOV R_QH,#40H
LJMP L_D
KEY1:
JB R_BZ,L_D
MOV A,R_QH
ADD A,#1
MOV R0,A
MOV R_QH,R0
CJNE R0,#43H,L_D
MOV R_QH,#40H
LJMP L_D
KEY2:
JB R_BZ,L_D
MOV R1,R_QH
MOV A,@R1
ADD A,#1
DA A
MOV @R1,A
CJNE R1,#42H,L_LL
CJNE @R1,#24H,L_D
MOV @R1,#00H
LJMP L_D
L_LL:
CJNE @R1,#60H,L_D
MOV @R1,#00H
LJMP L_D
KEY3: JB R_BZ,L_D
MOV R0,R_QH
CJNE R0,#42H,L_FENMIAO
DEC @R0
CJNE @R0,#0FFH,L_JJ
MOV @R0,#23H
LJMP L_D
L_FENMIAO:
MOV R0,R_QH
DEC @R0
CJNE @R0,#0FFH,L_JJ
MOV @R0,#59H
LJMP L_D
L_JJ:
MOV A,@R0
ANL A,#0FH
MOV R1,A
MOV R0,R_QH
MOV A,@R0
ANL A,#0F0H
CJNE R1,#0FH,L_D
ADD A,#09H
MOV @R0,A
L_D: LCALL F_CHAIFEN
RET
F_CHAIFEN:
MOV R0,#40H
MOV R1,#55H
L_DD: MOV A,@R0
ANL A,#0FH
MOV @R1,A
INC R1
MOV A,@R0
ANL A,#0F0H
SWAP A
MOV @R1,A
INC R0
INC R1
CJNE R0,#43H,L_DD
RET
;中断T0
I_T0: PUSH PSW
PUSH ACC
SETB RS0
MOV TH0,#HIGH(62536)
MOV TL0,#LOW(62536)
MOV A,R_2
SWAP A
MOV R0,R_3
ORL A,@R0
MOV P0,A
INC R_2
INC R_3
MOV R0,R_2
CJNE R0,#06H,L_EXIT
MOV R_3,#55H
MOV R_2,#0
L_EXIT:
POP ACC
POP PSW
RETI
;中断T1
I_T1:
PUSH ACC
PUSH PSW
SETB RS1
MOV TH1,#HIGH(15536)
MOV TL1,#LOW(15536)
DJNZ R_1,L_EXIT2
MOV R_1,#20
JNB R_BZ,L_EXIT2
MOV A,40H
ADD A,#1
DA A
MOV 40H,A
CJNE A,#60H,L_LCHAIFEN
MOV 40H,#00H
MOV A,41H
ADD A,#1
DA A
MOV 41H,A
CJNE A,#60H,L_LCHAIFEN
MOV 41H ,#00H
MOV A,42H
ADD A,#1
DA A
MOV 42H,A
CJNE A,#24H,L_LCHAIFEN
MOV 42H,#00H
L_EXIT2 :
L_LCHAIFEN:
LCALL F_CHAIFEN
POP PSW
POP ACC
RETI
F_XDYS20ms:
MOV R6,#100
L_LOOP9:
MOV R5,#100
DJNZ R5,$
DJNZ R6,L_LOOP9
RET
F_XDYS200ms:
MOV R6,#50
L_LOOP11:
MOV R7,#100
L_LOOP10:
MOV R5,#40
DJNZ R5,$
DJNZ R7,L_LOOP10
DJNZ R6,L_LOOP11
RET
END