#define uint unsigned int
#define uchar unsigned char
#define Key_Scanf P1 //4X4按键显示;
#define Print_Out P0 //显示数字P0;
#define PORT P2 //数码选择;
#define SYSLED P0^7; //系统指示灯;
uchar code table[]={0xff,0x4f,0x12,0x06,0x4c,0x24,0x20,0x0f,0x00,0x0c,0x01}; //0~9数字
void display(uchar); //显示
void delay_50us(uint t); //延时50微秒,防抖动
uchar keyscan(void); //键盘
void SYS_LED(void); //指示灯
uchar a=0;
uchar keynum;
void delay_50us(uint t)
{
uchar j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void SYS_LED()
{
SYSLED = 0;
delay_50us(100);
SYSLED = 1;
}
void display( uchar keynum) //根据keynum返回值显示数字
{
PORT=0xfe;
Print_Out=table[keynum];
}
uchar keyscan()
{
Key_Scanf=0xbf; // 扫描第一横行,后四位为1,前四位线与,然后跟原来数值比较,如果不等,则把对应的数字赋给keynum
if((Key_Scanf&0xbf)!=0xbf)
{
delay_50us(100);
if((Key_Scanf&0xbf)!=0xbf)//消抖
{
SYS_LED();
a=Key_Scanf&0xbf;
switch(a)
{
case 0x77:keynum=1;break;
case 0x7b:keynum=2;break;
case 0x7d:keynum=3;break;
default:break;
}
}
}
Key_Scanf=0xdf; //扫描第二横行,后四位为1,前四位线与;
if((Key_Scanf&0xdf)!=0xdf)
{
delay_50us(100);
if((Key_Scanf&0xdf)!=0xdf)
{
a=Key_Scanf&0xdf;
switch(a)
{
case 0x7e:keynum=4;break;
case 0xb7:keynum=5;break;
case 0xbb:keynum=6;break;
default: break;
}
}
}
Key_Scanf=0xef; //扫描第三横行,后四位为1,前四位线与;
if((Key_Scanf&0xef)!=0xef)
{
delay_50us(100);
if((Key_Scanf&0xef)!=0xef)
{
a=Key_Scanf&0xef;
switch(a)
{
case 0xbd:keynum=7;break;
case 0xbe:keynum=8;break;
case 0xe7:keynum=9;break;
default: break;
}
}
}
Key_Scanf=0xf7; //扫描第四横行,后四位为1,前四位线与;
if((Key_Scanf&0xf7)!=0xf7)
{
delay_50us(100);
if((Key_Scanf&0xf7)!=0xf7)
{
a=Key_Scanf&0xf7;
switch(a)
{
case 0xeb:keynum=10;break;
default: break;
}
}
}
return keynum;
}
void main(void)
{
while(1)
{
keyscan();
display(keynum);
}
}
淘bao 旺铺: 广州华电 单片机学习板 单片机外围学习模块/传感器模块
//44键盘检测程序,按下键后相应的代码显示在数码管上
#include<reg51h>
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);
}
}
}
}
有问题,当按下多个按键时,消抖都没用了。有计数法,但是需要消耗的内存。可以识别具体的键位,键的按下d起状态,消抖。每个按键状态独立识别,无需等待延时。typedefenum{Key_No,Key_Down,Key_Up,}KeyStateEnum;vkey[4]={0};//4组按键ucharkeyDownNum[16]={0};//按下计数ucharkeyDownUp[16]={0};//d起计数//判断键状态ucharstateKey(ucharkeySta,uchardownNum,ucharupNum,uchardel){ucharsta=Key_No;if(keySta){upNum[0]=0;if(downNum[0]==del){sta=Key_Down;}if(downNum[0]>4);}}voidmain(){while(1){Readkey();//ScanKey();}}
以上就是关于设计一个3 行×4列的矩阵键盘,采用行扫描法编写程序,当有按键动作时,能够获得按键全部的内容,包括:设计一个3 行×4列的矩阵键盘,采用行扫描法编写程序,当有按键动作时,能够获得按键、求一个单片机4X4矩阵键盘扫描程序,C语言的。、如何用汇编语言实现4*3矩阵键盘程序设计,当任意一个按键按下后使用四等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)