4*4矩阵键盘源程序
适用51单片机开发板v3.0或v4.0
;CHEN 04.02
;P3口接键盘,P3.0接键盘靠近S1的脚
;P0口接数码管段码端J7,再将任一数码管的位码接地
AD EQU 30H
org 0000h
LJMP MAIN
ORG 0030H
MAIN: mov p3,#0Fh ;p0-3输出1,作为输入位
mov a ,p3
ANL A,#0FH
CJNE A,#0FH,DELAY
SJMP MAIN
DELAY: ACALL DELAY1 ; 延时去键抖
mov a ,p3
ANL A,#0FH
CJNE A,#0FH,HAVE
SJMP MAIN
HAVE:
MOV A,#0EFH ;行扫描码
NEXT:
MOV B,A
MOV P3,A
MOV A,p3
ANL A,#0FH ;检测列
CJNE A,#0FH,YES
MOV A,B
RL A
CJNE A,#0FEH,NEXT
YES: orl a,#0f0h ;高四位置1
CPL A
mov r2,#00h
MOV R2,A ;存列码
MOV A,B ;取行码,
CPL A
ORL A,R2 ;列码+行码=键植
MOV P2,A ; 用P2口接发光二极管查看结果
MOV AD,#00H
ACALL DISPKEYV
JMP MAIN
DISPKEYV:
KEY0:CJNE A,#11H,KEY1
AJMP WORD0
KEY1:CJNE A,#12H,KEY2
AJMP WORD1
KEY2:CJNE A,#14H,KEY3
AJMP WORD2
KEY3:CJNE A,#18H,KEY4
AJMP WORD3
KEY4:CJNE A,#21H,KEY5
AJMP WORD4
KEY5:CJNE A,#22H,KEY6
AJMP WORD5
KEY6:CJNE A,#24H,KEY7
AJMP WORD6
KEY7:CJNE A,#28H,KEY8
AJMP WORD7
KEY8:CJNE A,#41H,KEY9
AJMP WORD8
KEY9:CJNE A,#42H,KEY10
AJMP WORD9
KEY10:CJNE A,#44H,KEY11
AJMP WORD10
KEY11:CJNE A,#48H,KEY12
AJMP WORD11
KEY12:CJNE A,#81H,KEY13
AJMP WORD12
KEY13:CJNE A,#82H,KEY14
AJMP WORD13
KEY14:CJNE A,#84H,KEY15
AJMP WORD14
KEY15:CJNE A,#88H,PASS
AJMP WORD15
WORD0:MOV AD,#00
ACALL DISP
Ajmp PASS
WORD1:MOV AD,#01
ACALL DISP
Ajmp PASS
WORD2:MOV AD,#02
ACALL DISP
Ajmp PASS
WORD3:MOV AD,#03
ACALL DISP
Ajmp PASS
WORD4:MOV AD,#04
ACALL DISP
Ajmp PASS
WORD5:MOV AD,#05
ACALL DISP
Ajmp PASS
WORD6:MOV AD,#06
ACALL DISP
Ajmp PASS
WORD7:MOV AD,#07
ACALL DISP
Ajmp PASS
WORD8:MOV AD,#08
ACALL DISP
Ajmp PASS
WORD9:MOV AD,#9
ACALL DISP
Ajmp PASS
WORD10:MOV AD,#10
ACALL DISP
Ajmp PASS
WORD11:MOV AD,#11
ACALL DISP
Ajmp PASS
WORD12:MOV AD,#12
ACALL DISP
Ajmp PASS
WORD13:MOV AD,#13
ACALL DISP
Ajmp PASS
WORD14:MOV AD,#14
ACALL DISP
Ajmp PASS
WORD15:MOV AD,#15
ACALL DISP
PASS: ret
DISP: MOV A,AD
MOV DPTR,#numtab
MOVC A,@A+DPTR
MOV P0,A
RET
numtab: DB 0c0H,0f9H,0a4H,0b0H,99H,92H,82H,0f8H,80H,90H,88H,83H,0C6H,0A1H,86H,8EH
DELAY1:
MOV R5,#10
D1: MOV R6,#250
DJNZ R7, $
DJNZ R5,D1
RET
END
阅读(5425) | 评论(0) | 转发(0) |