单片机4*4矩阵键盘结合数码管
时间:10-02
整理:3721RD
点击:
名称:按下键盘矩阵后,在六个数码管上依次显示所按下的键所代表的数字。
按复位键可以清空显示为0
#include<reg51.h>
#define uchar unsigned char
uchar a0=16,b0=16,c0=16,d0=16,e0=16,f0=16,wei,temp,key,i,j,k;
sbit dula=P2^6;
sbit wela=P2^7;
sbit beep=P2^3;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
void delay(uchar i)
{
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void display(uchar a,uchar b,uchar c,uchar d,uchar e,uchar f)
//显示函数
{
dula=0;
P0=table[a];
dula=1;
dula=0;
wela=0;
P0=0xfe; //选通第一个数码管
wela=1;
wela=0;
delay(5);
P0=table;
dula=1;
dula=0;
P0=0xfd; //第二个
wela=1;
wela=0;
delay(5);
P0=table[c];
dula=1;
dula=0;
P0=0xfb; //第三个
wela=1;
wela=0;
delay(5);
P0=table[d];
dula=1;
dula=0;
P0=0xf7; //第四个
wela=1;
wela=0;
delay(5);
P0=table[e];
dula=1;
dula=0;
P0=0xef; //第五个
wela=1;
wela=0;
delay(5);
P0=table[f];
dula=1;
dula=0;
P0=0xdf; //第六个
wela=1;
wela=0;
delay(5);
}
void keyscan() //4*4键盘扫描
{
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=0;
wei++;
break;
case 0xde:
key=1;
wei++;
break;
case 0xbe:
key=2;
wei++;
break;
case 0x7e:
key=3;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
key=4;
wei++;
break;
case 0xdd:
key=5;
wei++;
break;
case 0xbd:
key=6;
wei++;
break;
case 0x7d:
key=7;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
key=8;
wei++;
break;
case 0xdb:
key=9;
wei++;
break;
case 0xbb:
key=10;
wei++;
break;
case 0x7b:
key=11;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
key=12;
wei++;
break;
case 0xd7:
key=13;
wei++;
break;
case 0xb7:
key=14;
wei++;
break;
case 0x77:
key=15;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
}
}
void main()
{
while(1)
{
keyscan();
if(key!=20)
{
switch(wei)
{
case 1: a0=key;
b0=16;
c0=16;
d0=16;
e0=16;
f0=16;
break;
case 2: b0=key;
c0=16;
d0=16;
e0=16;
f0=16;
break;
case 3: c0=key;
d0=16;
e0=16;
f0=16;
break;
case 4: d0=key;
e0=16;
f0=16;
break;
case 5: e0=key;
f0=16;
break;
case 6: f0=key;
wei=0;
break;
}
key=20;
}
display(a0,b0,c0,d0,e0,f0);
}
}
加下划线的怎么理解,搞不懂,求高人指点
按复位键可以清空显示为0
#include<reg51.h>
#define uchar unsigned char
uchar a0=16,b0=16,c0=16,d0=16,e0=16,f0=16,wei,temp,key,i,j,k;
sbit dula=P2^6;
sbit wela=P2^7;
sbit beep=P2^3;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
void delay(uchar i)
{
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void display(uchar a,uchar b,uchar c,uchar d,uchar e,uchar f)
//显示函数
{
dula=0;
P0=table[a];
dula=1;
dula=0;
wela=0;
P0=0xfe; //选通第一个数码管
wela=1;
wela=0;
delay(5);
P0=table;
dula=1;
dula=0;
P0=0xfd; //第二个
wela=1;
wela=0;
delay(5);
P0=table[c];
dula=1;
dula=0;
P0=0xfb; //第三个
wela=1;
wela=0;
delay(5);
P0=table[d];
dula=1;
dula=0;
P0=0xf7; //第四个
wela=1;
wela=0;
delay(5);
P0=table[e];
dula=1;
dula=0;
P0=0xef; //第五个
wela=1;
wela=0;
delay(5);
P0=table[f];
dula=1;
dula=0;
P0=0xdf; //第六个
wela=1;
wela=0;
delay(5);
}
void keyscan() //4*4键盘扫描
{
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=0;
wei++;
break;
case 0xde:
key=1;
wei++;
break;
case 0xbe:
key=2;
wei++;
break;
case 0x7e:
key=3;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
key=4;
wei++;
break;
case 0xdd:
key=5;
wei++;
break;
case 0xbd:
key=6;
wei++;
break;
case 0x7d:
key=7;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
key=8;
wei++;
break;
case 0xdb:
key=9;
wei++;
break;
case 0xbb:
key=10;
wei++;
break;
case 0x7b:
key=11;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
key=12;
wei++;
break;
case 0xd7:
key=13;
wei++;
break;
case 0xb7:
key=14;
wei++;
break;
case 0x77:
key=15;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
}
}
void main()
{
while(1)
{
keyscan();
if(key!=20)
{
switch(wei)
{
case 1: a0=key;
b0=16;
c0=16;
d0=16;
e0=16;
f0=16;
break;
case 2: b0=key;
c0=16;
d0=16;
e0=16;
f0=16;
break;
case 3: c0=key;
d0=16;
e0=16;
f0=16;
break;
case 4: d0=key;
e0=16;
f0=16;
break;
case 5: e0=key;
f0=16;
break;
case 6: f0=key;
wei=0;
break;
}
key=20;
}
display(a0,b0,c0,d0,e0,f0);
}
}
加下划线的怎么理解,搞不懂,求高人指点
首先明白是个switch...case...语句就好理解了;
然后理解wei就是数码管的位,通俗点就是代表第1~6个数码管,选中哪个那个就亮;
最后就是a0~f0分别对应第1~6个数码管显示的按键值了。
电子乐屋 switch...case...就是由wei的值,决定程序的执行。
恩恩,我看了懂了,谢谢你回复,要就是按键扫描后利用key的值而使数码管显示段选,位选是通过wei的自增来实现的逐步位移,还有就是在while大循环中,key=20这个没有什么意义,显示完之后接着再在while 大循环中再次扫描,再次显示谢谢你的回答!
谢谢你的帮助!