您的当前位置:首页正文

矩阵式键盘程序

2021-11-14 来源:步旅网
;30H存列号31H存行号32H装键值,要改键盘的值排列可以通过改变后面TAB的值。

;程序构思,先给键盘的接入口p1赋0F0H,通过检测p1.4-p1.7那个出现低电平,举例如果是p1.4那列有键按

;下则p1.4出现低电平,则跳到y0得到列号为0,接下来分别给p1.4-p1.7赋0,看什么时候p1.4在那出现低电

;平,则可以得到行号,最终计算行号*4+列号就可以得到键盘的值,如果要修改矩阵键盘键值排列,则可以

;通过查表,计算出的前期键值只是作为一个地址。不知表述是否清楚了

SCAN:MOV P1,#0F0H ;键盘扫描程序 JNB P1.4,Y0 ;判断列 JNB P1.5,Y1 JNB P1.6,Y2 JNB P1.7,Y3 SJMP SCAN

Y0:MOV 30H,#00H ;赋列号 MOV P1,#0FEH JNB P1.4,X0 MOV P1,#0FDH JNB P1.4,X1 MOV P1,#0FBH JNB P1.4,X2

MOV P1,#0F7H JNB P1.4,X3 Y1:MOV 30H,#01H MOV P1,#0FEH JNB P1.5,X0 MOV P1,#0FDH JNB P1.5,X1 MOV P1,#0FBH JNB P1.5,X2 MOV P1,#0F7H JNB P1.5,X3 Y2:MOV 30H,#02H MOV P1,#0FEH JNB P1.6,X0 MOV P1,#0FDH JNB P1.6,X1 MOV P1,#0FBH JNB P1.6,X2 MOV P1,#0F7H JNB P1.6,X3 Y3:MOV 30H,#03H MOV P1,#0FEH

JNB P1.7,X0 MOV P1,#0FDH JNB P1.7,X1 MOV P1,#0FBH JNB P1.7,X2 MOV P1,#0F7H JNB P1.7,X3

X0:MOV 31H,#00H ACALL DELAY MOV P1,#0F0H LJMP JISUAN X1:MOV 31H,#01H ACALL DELAY MOV P1,#0F0H LJMP JISUAN X2:MOV 31H,#02H ACALL DELAY MOV P1,#0F0H LJMP JISUAN X3:MOV 31H,#03H ACALL DELAY MOV P1,#0F0H

;赋行号 LJMP JISUAN JISUAN:MOV A,31H MOV B,#04H MUL AB ADD A,30H

MOV DPTR,#TABLE MOVC A,@A+DPTR MOV 32H,A RET

delay:MOV R4,#0C5H ;键扫延时 DEL1:MOV R5,#43H DEL0:MOV R6,#10H DJNZ R6,$ DJNZ R5,DEL0 DJNZ R4,DEL1 RET

TABLE:DB

01H,02H,03H,04H,05H,06H,07H,08H,09H,00H,0AH,0BH,0CH,0DH,0EH,0FH ;键盘值

实验十三例程:

KEYBUF EQU 30H ORG 0000H

LJMP START ORG 0100H

START: mov p2,#0ffh;灭灯 mov P1,#0f0h mov a,p1 anl a,#0f0h

xrl a,#0f0h jz start lcall dely10ms mov a,p1 anl a,#0f0h NK0: NK1: NK2: xrl a,#0f0h jz start

MOV P1,#0feH ;

clr P1.0

MOV A,P1 ANL A,#0F0H CJNE A,#0e0H,NK0 MOV KEYBUF,#0 LJMP DK1

CJNE A,#0d0H,NK1 MOV KEYBUF,#1 LJMP DK1

CJNE A,#0b0H,NK2 MOV KEYBUF,#2 LJMP DK1 CJNE A,#70H,NK3 MOV KEYBUF,#3

LJMP DK1 NK3: MOV P1,#0fdH ;

clr p1.1

MOV A,P1 ANL A,#0F0H NK4: NK5: NK6: NK7: CJNE A,#0e0H,NK4 MOV KEYBUF,#4 LJMP DK1

CJNE A,#0d0H,NK5 MOV KEYBUF,#5 LJMP DK1

CJNE A,#0b0H,NK6 MOV KEYBUF,#6 LJMP DK1 CJNE A,#70H,NK7 MOV KEYBUF,#7 LJMP DK1 MOV P1,#0fbH ;

clr p1.2

MOV A,P1 ANL A,#0F0H CJNE A,#0e0H,NK8

MOV KEYBUF,#8 LJMP DK1

NK8: CJNE A,#0d0H,NK9 MOV KEYBUF,#9 LJMP DK1

NK9: CJNE A,#0b0H,NK10 MOV KEYBUF,#10 LJMP DK1

NK10: CJNE A,#70H,NK11 MOV KEYBUF,#11 LJMP DK1 NK11 : MOV P1,#0f7H ;

clr p1.3

MOV A,P1 ANL A,#0F0H CJNE A,#0e0H,NK12 MOV KEYBUF,#12 LJMP DK1

NK12: CJNE A,#0d0H,NK13 MOV KEYBUF,#13 LJMP DK1

NK13: CJNE A,#0b0H,NK14

MOV KEYBUF,#14 LJMP DK1 NK14: MOV KEYBUF,#15 DK1: MOV A,KEYBUF ;display MOV DPTR,#TABLE MOVC A,@A+DPTR cpl a

MOV P2,A DK1A: MOV A,P1 ANL A,#0F0H XRL A,#0F0H JNZ DK1A ljmp start

DELY10MS: MOV R6,#10 D1: MOV R7,#248 DJNZ R7,$ DJNZ R6,D1 RET TABLE:

3FH,06H,5BH,4FH,66H,6DH,7DH,07H DB

7FH,6FH,77H,7CH,39H,5EH,79H,71H

DB

END

因篇幅问题不能全部显示,请点此查看更多更全内容