51单片机矩阵键盘程序:想把按键的值一一存到数组里,最后按下某个按键时,把数组里的某个数显示出来,

51单片机矩阵键盘程序:想把按键的值一一存到数组里,最后按下某个按键时,把数组里的某个数显示出来,,第1张

给一个键盘扫描程序参考:

void delay(unsigned int a)

{

unsigned int i,j

for(i=0i<ai++)

for(j=0j<120j++)

}

uchar kbscan(void)

{

unsigned char sccode,recode

P1=0x0f  //发0扫描,列线输入

if ((P2 & 0x0f) != 0x0f)  //有键按下

{

delay(20)   //延时去抖动

if ((P1&0x0f)!= 0x0f)

{

sccode = 0xef    //逐行扫描初值

while((sccode&0x01)!=0)

{

    P1=sccode

if((P1&0x0f)!=0x0f)

{

    recode=(P1&0x0f)|0xf0

while((P1&0x0f)!=0x0f)//等待键抬起

return((~sccode)+(~recode))

 }

        else

sccode=(sccode<<1)|0x01

}

}

}

return 0  //无键按下,返回0

}

uchar getkey(void)

{

unsigned char key,keyval=0xff

key=kbscan()

switch(key)

{

case 0x11:keyval=1break

case 0x12:keyval=2break

case 0x14:keyval=3break

case 0x18:keyval=10break //+

case 0x21:keyval=4break

case 0x22:keyval=5break

case 0x24:keyval=6break

case 0x28:keyval=11break //-

case 0x41:keyval=7break

case 0x42:keyval=8break

case 0x44:keyval=9break

case 0x48:keyval=12break //清除

case 0x81:keyval=13break

case 0x82:keyval=0break

case 0x84:keyval=14break //小数点

case 0x88:keyval=15break //确认

default:keyval=16break

}

return(keyval)

}

KEYVAL EQU 30H

KEYSCAN EQU 31H

KEYTM EQU 32H

KEYSEL:

MOV KEYVAL,#0

MOV KEYSCAN,#0EFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS1

MOV KEYVAL,A

SJMP KEYRTN

KEYS1:

MOV KEYSCAN,#0DFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS2

CLR C

ADD A,#4

MOV KEYVAL,A

SJMP KEYRTN

KEYS2:

MOV KEYSCAN,#0BFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS3

CLR C

ADD A,#8

MOV KEYVAL,A

SJMP KEYRTN

KEYS3:

MOV KEYSCAN,#7FH

LCALL GETKEY

MOV A,KEYTM

JZ KEYRTN

CLR C

ADD A,#12

MOV KEYVAL,A

KEYRTN:

RET

--------------------

GETKEY:

MOV KEYTM,#0

MOV A,KEYSCAN

MOV P3,A

NOP

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JZ NOKEY

MOV R2,#10

LCALL DELAY

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JZ NOKEY

MOV A,P3

ANL A,#0FH

MOV R7,A

SF:

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JNZ SF

MOV A,R7

CJNE A,#0EH,NK1

MOV KEYTM,#1

SJMP NOKEY

NK1:

CJNE A,#0DH,NK2

MOV KEYTM,#2

SJMP NOKEY

NK2:

CJNE A,#0BH,NK3

MOV KEYTM,#3

SJMP NOKEY

NK3:

CJNE A,#07H,NOKEY

MOV KEYTM,#4

NOKEY: RET


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11803597.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存