51单片机程序——4乘4矩阵键盘使用方法
2016-01-22 来源:eefocus
#include
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xa7,0xa1,0x86,0x8e,0xff};
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
进入单片机查看更多内容>>
#define uint unsigned int
#define uchar unsigned char
#define dula P0
#define wela P1
unsigned char code DuanMa[]={0xc0,0xf9,0xa4,0xb0,
uchar num,temp;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar keyscan();
void display(uchar aa);
void main()
{
num=16;
while(1)
{
display(keyscan());
}
}
void display(uchar aa)
{
wela=0xff;
dula=DuanMa[aa];
}
uchar keyscan()
{
P2=0xef;
temp=P2;
temp=temp&0x0f;
while(temp!=0x0f) //确认是否按下
{
delay(5);
temp=P2;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P2;
switch(temp)
{
case 0xee:num=0;
case 0xed:num=1;
case 0xeb:num=2;
case 0xe7:num=3;
}
while(temp!=0x0f) //松手检测
{
temp=P2;
temp=temp&0x0f;
}
}
}
P2=0xdf;
temp=P2;
temp=temp&0x0f;
while(temp!=0x0f)
{
delay(5);
temp=P2;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P2;
switch(temp)
{
case 0xde:num=4;
case 0xdd:num=5;
case 0xdb:num=6;
case 0xd7:num=7;
}
while(temp!=0x0f)
{
temp=P2;
temp=temp&0x0f;
}
}
}
P2=0xbf;
temp=P2;
temp=temp&0x0f;
while(temp!=0x0f)
{
delay(5);
temp=P2;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P2;
switch(temp)
{
case 0xbe:num=8;
case 0xbd:num=9;
case 0xbb:num=10;
case 0xb7:num=11;
}
while(temp!=0x0f)
{
temp=P2;
temp=temp&0x0f;
}
}
}
P2=0x7f;
temp=P2;
temp=temp&0x0f;
while(temp!=0x0f)
{
delay(5);
temp=P2;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P2;
switch(temp)
{
case 0x7e:num=12;
case 0x7d:num=13;
case 0x7b:num=14;
case 0x77:num=15;
}
while(temp!=0x0f)
{
temp=P2;
temp=temp&0x0f;
}
}
}
return num;
}
下一篇:51单片机程序——数码管动起来
相关文章