51单片机汇编语言编程独立按键

2024-11-22 06:25:22
推荐回答(1个)
回答1:

四个按键,定义如下:

KEY1 BIT P3.0
KEY2 BIT P3.1
KEY3 BIT P3.2
KEY4 BIT P3.3

按键处理程序:
;-----------------------------------
READ_KEY:

JB KEY1, K2
CALL DELAY
JB KEY1, K2
JNB KEY1, $

PUSH ACC
INC 75H
MOV A, 75H
CJNE A, #10, ZINT01
MOV 75H, #0
ZINT01:
POP ACC

JMP K_EXIT
;---------------
K2: JB KEY2, K3
CALL DELAY
JB KEY2, K3
JNB KEY2, $

PUSH ACC
INC 76H
MOV A, 76H
CJNE A, #10, ZINT01
MOV 76H, #0
ZINT01:
POP ACC

JMP K_EXIT
;---------------
K3: JB KEY3, K4
CALL DELAY
JB KEY3, K4
JNB KEY3, $

PUSH ACC
DEC 75H
MOV A, 75H
CJNE A, #255, ZINT03
MOV 75H, #9
ZINT03:
POP ACC

JMP K_EXIT
;---------------
K4: JB KEY4, K_EXIT
CALL DELAY
JB KEY4, K_EXIT
JNB KEY4, $

PUSH ACC
DEC 76H
MOV A, 76H
CJNE A, #255, ZINT04
MOV 76H, #9
ZINT04:
POP ACC

K_EXIT:
RET
;---------------
DELAY:
DJNZ R7, $
DJNZ R7, $
RET
;-----------------------------------