扫描键盘,应该是按键从1~16数码管显示从0到f.
然而我这个程序上来数码管就显示0,按其他键没用、
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
sbit wela=P2^7;
sbit dula=P2^6;
void scanekey();
void display(uchar num);
void delayms(uint z);
void main()
{
P0=0;
dula=1;
dula=0;
P0=0xc0;
wela=1;//È«²¿Ñ¡ÖÐ
wela=0;
while(1)
{
display(scanekey);
}
}
void delayms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void display(uchar num)
{
dula=1;
P0=table[num];
dula=0;
}
void scanekey()
{
uchar num,temp;
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:num=0;break;//0xee=1110 1110
case 0xde:num=1;break;//0xde=1101 1110
case 0xbe:num=2;break;//0xbe=1011 1110
case 0x7e:num=3;break;//0x7e=0111 1110
}
while(temp!=0xf0)
{
P3=temp;
temp=temp&0xf0;
}
display(num);
}
}
P3=0xfd;//0xfd=1111 1101
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:num=4;break;//1110 1101
case 0xdd:num=5;break;//1101 1101
case 0xbd:num=6;break;//1011 1101
case 0x7d:num=7;break;//0111 1101
}
while(temp!=0xf0)
{
P3=temp;
temp=temp&0xf0;
}
display(num);
}
}
P3=0xfb;//1111 1011
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P3;
temp=temp&0xf0;
if(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;
}
while(temp!=0xf0)
{
P3=temp;
temp=temp&0xf0;
}
display(num);
}
}
P3=0xf7; //1111 0111
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(5);
temp=P3;
temp=temp&0xf0;
if(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;
}
while(temp!=0xf0)
{
P3=temp;
temp=temp&0xf0;
}
display(num);
}
}
}
|