;
; press 4*4 key and display number(0 - F) in the led
;
;
KEY_ENCOD_NUMBER EQU 30H ;save the encoding key number
KEY_NUMBER EQU 31H ;保存实际的key number(0-F)
;There is one key is pressed
onekey:
acall DELAY5 ;delay 10ms
acall DELAY5
acall CheckKey
cjne a, #0f0h, key1 ;ensure if the key is pressed
ret
key1:
mov r1, a ;保存垂直方向的数据
mov p3, #0fh ;反转检查水平方向的数据
mov a, p3
anl a, #0fh
cjne a, #0fh, key2
wait: ;等待key 释放后才返回
mov p3, #0fh
mov a, p3
cjne a, #0fh, wait
ret
key2:
orl a, r1 ;将两次数据合并,下面开始查表,也可以直接比较得到key number
MOV KEY_ENCOD_NUMBER, a
mov dptr, #1520h
mov r0, #0ffh ;查表的偏移量
Lp: ;双重循环
inc r0
cjne r0, #0fh, cmp ;循环16次
ret
cmp:
mov a, r0
movc a, @a+dptr
;inc r0
cjne a, KEY_ENCOD_NUMBER, Lp
mov KEY_NUMBER, r0 ;找到对应的number,保存在31h中
ret
;;check if a key is pressed
CheckKey:
mov p3, #0f0h
mov a, p3
anl a, #0f0h
ret
;;display the key number
display:
setb p2.6 ;送段选信号
mov a, KEY_NUMBER
mov dptr, #1500h
movc a, @a+dptr
mov p0, a
clr p2.6
ret
DELAY5:
MOV R7, #100 ;Delay1ms: 2+2*100+2*100*X+2 = 5000
D1: MOV R6, #24
DJNZ R6, $
DJNZ R7, D1 ;Loop for 250 times
RET
DELAY1:
MOV R7, #100 ;Delay1ms: 2+2*100+2*100*X+2 = 1000
D2: MOV R6, #4
DJNZ R6, $
DJNZ R7, D2 ;Loop for 250 times
RET
org 1500H
DB 3FH, 06H, 5BH, 4FH, 66H, 6DH, 7DH, 07H, 7FH, 6FH
DB 77H, 7CH, 39H, 5EH, 79H, 71H
org 1520H ;look up in the table and find the key number
DB 0EEH, 0DEH, 0BEH, 7EH, 0EDH, 0DDH, 0BDH, 7DH
DB 0EBH, 0DBH, 0BBH, 7BH, 0E7H, 0D7H, 0B7H, 77H
end