求一个单片机4X4矩阵键盘扫描程序,C语言的。

2024-11-05 02:25:33
推荐回答(4个)
回答(1):

淘bao 旺铺: 广州华电 单片机学习板 单片机外围学习模块/传感器模块
//4*4键盘检测程序,按下键后相应的代码显示在数码管上
#include
sbit beep=P2^3;
sbit dula=P2^6;
sbit wela=P2^7;
unsigned char i=100;
unsigned char j,k,temp,key;
void delay(unsigned char i)
{
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
display(unsigned char num)
{
P0=table[num];
dula=1;
dula=0;
P0=0xc0;
wela=1;
wela=0;
}
void main()
{
dula=0;
wela=0;
while(1)
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=0;
break;
case 0xde:
key=1;
break;
case 0xbe:
key=2;
break;
case 0x7e:
key=3;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
display(key);
P1=0xfe;
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
key=4;
break;
case 0xdd:
key=5;
break;
case 0xbd:
key=6;
break;
case 0x7d:
key=7;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
display(key);
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
key=8;
break;
case 0xdb:
key=9;
break;
case 0xbb:
key=10;
break;
case 0x7b:
key=11;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
display(key);
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
key=12;
break;
case 0xd7:
key=13;
break;
case 0xb7:
key=14;
break;
case 0x77:
key=15;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
display(key);
}
}
}
}

回答(2):

//通用键盘扫描程序
uchar kbscan(void)
{
unsigned char sccode,recode;
P3=0x0f; //发0扫描,列线输入
if ((P3 & 0x0f) != 0x0f) //有键按下
{
delay(20); //延时去抖动
if ((P3&0x0f)!= 0x0f)
{
sccode = 0xef; //逐行扫描初值
while((sccode&0x01)!=0)
{
P3=sccode;
if((P3&0x0f)!=0x0f)
{
recode=(P3&0x0f)|0xf0;
return((~sccode)+(~recode));
}
else
sccode=(sccode<<1)|0x01;
}
}
}
return 0; //无键按下,返回0
}

//调用范例
void getkey(void)
{
unsigned char key;
key=kbscan();
if(key==0){keyval=0xff;return;}
switch(key)
{
case 0x11:keyval=7;break;
case 0x12:keyval=4;break;
case 0x14:keyval=1;break;
case 0x18:keyval=10;break;
case 0x21:keyval=8;break;
case 0x22:keyval=5;break;
case 0x24:keyval=2;break;
case 0x28:keyval=0;break;
case 0x41:keyval=9;break;
case 0x42:keyval=6;break;
case 0x44:keyval=3;break;
case 0x48:keyval=11;break;
case 0x81:keyval=12;break;
case 0x82:keyval=13;break;
case 0x84:keyval=14;break;
case 0x88:keyval=15;break;
default:keyval=0xff;break;
}
}

回答(3):

uchar code KeyCodeTable[]=
{
0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x41,0x42,0x44,0x48,0x81,0x82,0x84,0x88
};
void Delay()
{
uchar i;
for(i=0;i<200;i++);
}
uchar Keys_Scan()
{
uchar sCode,kCode,i,k;
P1 = 0xf0;
if((P1&0xf0)!=0xf0)
{
Delay();
if((P1&0xf0)!=0xf0)
{
sCode = 0xfe;
for(k=0;k<4;k++)
{
P1 = sCode;
if((P1&0xf0)!=0xf0)
{
kCode = ~P1;
for(i=0;i<16;i++)
{
if(kCode == KeyCodeTable[i])
return i;
}
}
else
sCode = _crol_(sCode,1);
}
}
}
return -1;
}
以上程序来自:《单片机C语言程序设计实训100例——基于8051+Proteus仿真》

回答(4):

好久没接触单片机了。
矩阵键盘扫描的话,好像就用循环来做吧。
4X4矩阵,一共用到8个IO口,八位的,正好是一个byte,做一个循环每次读取这八位的值,只要有按键的话,这八位中总有两位是1,其余的是0,这16个按键每个都是一个固定的数字,做一个映射表对应一下就可以了。
单片机按键好像要做防抖处理,同时按键有可能处理不了。