说明:本程序为4×4键盘扫描程序,是经过验证显示正确的。
功能:4×4键盘,总共16个键,分别依次编号,当按到相应的按键的时候,数码管显示相应的编号。
编号依次为:1,2,3,4,5,6,7,8,9,A,B,C,D,E, F
#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 temp,num;
void display();
void delay(uint z);
void main()
{
while(1)
{
display();
}
}
void display()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
wela=1;
P0=0xfe;
wela=0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:num=0;
break;
case 0xde:num=1;
break;
case 0xbe:num=2;
break;
case 0x7e:num=3;
break;
}
dula=1;
P0=table[num];
dula=0;
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
} //显示0,1,2,3
P3=0xfd;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
wela=1;
P0=0xfe;
wela=0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:num=4;
break;
case 0xdd:num=5;
break;
case 0xbd:num=6;
break;
case 0x7d:num=7;
break;
}
dula=1;
P0=table[num];
dula=0;
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;//
}
}
} //显示4,5,6,7
P3=0xfb;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
wela=1;
P0=0xfe;
wela=0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:num=8;
break;
case 0xdb:num=9;
break;
case 0xbb:num=10;
break;
case 0x7b:num=11;
break;
}
dula=1;
P0=table[num];
dula=0;
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
} //显示8,9,A,B
P3=0xf7;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
wela=1;
P0=0xfe;
wela=0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:num=12;
break;
case 0xd7:num=13;
break;
case 0xb7:num=14;
break;
case 0x77:num=15;
break;
}
dula=1;
P0=table[num];
dula=0;
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
} //显示C、D、E、F
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
文章评论(0条评论)
登录后参与讨论