#include<reg52.h>
#define uc unsigned char
#define ui unsigned int
sbit wela=P2^7;
sbit dula=P2^6;
uc temp,key,a[100],i=0;
uc code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uc code table2[]={
0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(uc xms)
{
ui ii,j;
for(ii=xms;xms>0;xms--)
for(j=110;j>0;j--);
}
void display(ui we,uc num)
{
dula=1;
P0=table[num];
dula=0;
P0=table2[we];
wela=1;
wela=0;
}
void keyscan()
{ ui j;
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{case 0xee:key=1;break;
case 0xde:key=2;break;
case 0xbe:key=3;break;
case 0x7e:key=4;break;}
a[i]=key;i++;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{case 0xee:key=5;break;
case 0xde:key=6;break;
case 0xbe:key=7;break;
case 0x7e:key=8;break;}
a[i]=key;i++;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{case 0xee:key=9;break;
case 0xde:key=10;break;
case 0xbe:key=11;break;
case 0x7e:key=12;break;}
a[i]=key;i++;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{case 0xee:key=13;break;
case 0xde:key=14;break;
case 0xbe:key=15;break;
case 0x7e:key=16;break;}
a[i]=key;i++;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
for(j=i;j>0;j--)
display(j,a[j]);
}
void main()
{
while(1)
{
keyscan();
}
}