4*4矩阵键盘的实现代码
时间:10-02
整理:3721RD
点击:
题目:按下16个矩阵键盘依次在数码管上显示1-16的平方。如按下第一个显示1,第二个显示4...
代码:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uint num1;
uchar bai,shi,ge;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void display(uchar bai, uchar shi, uchar ge) //显示函数只送段选数据
{
wela=1;
P0=0xfe;
wela=0;
P0=0xff;
dula=1;
P0=table[bai];
delayms(1);
wela=1;
P0=0xfd;
wela=0;
P0=0xff;
dula=1;
P0=table[shi];
delayms(1);
wela=1;
P0=0xfb;
wela=0;
P0=0xff;
dula=1;
P0=table[ge];
delayms(1);
}
uint matrixkeyscan()
{
uchar temp,key;
P3=0xfe; //第一行键盘
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10); //消除按键抖动
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=0;
break;
case 0xde:
key=1;
break;
case 0xbe:
key=4;
break;
case 0x7e:
key=9;
break;
}
while(temp!=0xf0) //条件满足时是1;等待按键释放;
{
temp=P3; //这两句是增加程序的稳定性
temp=temp&0xf0;
}
}
}
P3=0xfd; //第二行键盘
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10); //消除按键抖动
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
key=16;
break;
case 0xdd:
key=25;
break;
case 0xbd:
key=36;
break;
case 0x7d:
key=49;
break;
}
while(temp!=0xf0) //条件满足时是1;等待按键释放;
{
temp=P3; //这两句是增加程序的稳定性
temp=temp&0xf0;
}
}
}
P3=0xfb; //第三行键盘
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10); //消除按键抖动
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
key=64;
break;
case 0xdb:
key=81;
break;
case 0xbb:
key=100;
break;
case 0x7b:
key=121;
break;
}
while(temp!=0xf0) //条件满足时是1;等待按键释放;
{
temp=P3; //这两句是增加程序的稳定性
temp=temp&0xf0;
}
}
}
P3=0xf7; //第四行键盘
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10); //消除按键抖动
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
key=144;
break;
case 0xd7:
key=169;
break;
case 0xb7:
key=196;
break;
case 0x77:
key=225;
break;
}
while(temp!=0xf0) //条件满足时是1;等待按键释放;
{
temp=P3; //这两句是增加程序的稳定性
temp=temp&0xf0;
}
}
}
return key;
}
void main()
{
P0=0; //关闭所有数码管段选
dula=1;
dula=0;
P0=0xc0; //位选中所有数码管
wela=1;
wela=0;
while(1)
{
num1=matrixkeyscan(); //不停的调用键盘扫描程序
bai=num1/100;
shi=(num1%100)/10;
ge=num1%10;
display(bai,shi,ge);
}
}
仿真电路图:
代码:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uint num1;
uchar bai,shi,ge;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void display(uchar bai, uchar shi, uchar ge) //显示函数只送段选数据
{
wela=1;
P0=0xfe;
wela=0;
P0=0xff;
dula=1;
P0=table[bai];
delayms(1);
wela=1;
P0=0xfd;
wela=0;
P0=0xff;
dula=1;
P0=table[shi];
delayms(1);
wela=1;
P0=0xfb;
wela=0;
P0=0xff;
dula=1;
P0=table[ge];
delayms(1);
}
uint matrixkeyscan()
{
uchar temp,key;
P3=0xfe; //第一行键盘
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10); //消除按键抖动
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=0;
break;
case 0xde:
key=1;
break;
case 0xbe:
key=4;
break;
case 0x7e:
key=9;
break;
}
while(temp!=0xf0) //条件满足时是1;等待按键释放;
{
temp=P3; //这两句是增加程序的稳定性
temp=temp&0xf0;
}
}
}
P3=0xfd; //第二行键盘
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10); //消除按键抖动
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
key=16;
break;
case 0xdd:
key=25;
break;
case 0xbd:
key=36;
break;
case 0x7d:
key=49;
break;
}
while(temp!=0xf0) //条件满足时是1;等待按键释放;
{
temp=P3; //这两句是增加程序的稳定性
temp=temp&0xf0;
}
}
}
P3=0xfb; //第三行键盘
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10); //消除按键抖动
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
key=64;
break;
case 0xdb:
key=81;
break;
case 0xbb:
key=100;
break;
case 0x7b:
key=121;
break;
}
while(temp!=0xf0) //条件满足时是1;等待按键释放;
{
temp=P3; //这两句是增加程序的稳定性
temp=temp&0xf0;
}
}
}
P3=0xf7; //第四行键盘
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10); //消除按键抖动
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
key=144;
break;
case 0xd7:
key=169;
break;
case 0xb7:
key=196;
break;
case 0x77:
key=225;
break;
}
while(temp!=0xf0) //条件满足时是1;等待按键释放;
{
temp=P3; //这两句是增加程序的稳定性
temp=temp&0xf0;
}
}
}
return key;
}
void main()
{
P0=0; //关闭所有数码管段选
dula=1;
dula=0;
P0=0xc0; //位选中所有数码管
wela=1;
wela=0;
while(1)
{
num1=matrixkeyscan(); //不停的调用键盘扫描程序
bai=num1/100;
shi=(num1%100)/10;
ge=num1%10;
display(bai,shi,ge);
}
}
仿真电路图:
89c52单片机实现的4*4矩阵键盘
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
现代很有驾驶的乐趣
亲,希望发表到proteus板块来!
嗯,好的,亲!这几个板块有什么区别呀?我是新加入社区的,很多东西都不是很懂,求指教!
顶顶顶顶顶顶顶顶顶顶顶
没啥区别!都是电子发烧友的论坛。只是这样会让更多的proteus爱好者能能好的得到你的资源。
编的这个程序太费劲了。判断一个按键需要这么复杂?去看看研究一下更好的算法吧。
谢谢分享
好东西,多谢小编分享