#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar Key_num;
uint a[17]={0x00,0x0f,0xf0,0xdb,0xbd,0x7f,0x3f,0x1f,0x07,0xcd,0x8d,0xd5,0x7c,0x6e,0x7e,0xbf,0xff};
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void display()
{
P0=a[Key_num-1];
}
void Keyboard(void)
{
uchar temp;
P2=0x7f;
temp=P2;
temp=temp&0xf0;
if(temp!=0x0f)
{
delay(10);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
switch(temp)
{
case 0xe0:Key_num=1;break;
case 0xd0:Key_num=2;break;
case 0xb0:Key_num=3;break;
case 0x70:Key_num=4;break;
}
}
if(temp==0xf0)
{
Key_num=17;
}
}
P2=0xbf;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(10);
temp=P2;
temp=temp&0x0f;
while(temp!=0x0f)
{
switch(temp)
{
case 0xde:Key_num=5;break;
case 0xdd:Key_num=6;break;
case 0xdb:Key_num=7;break;
case 0xd7:Key_num=8;break;
}
}
if(temp==0x0f)
{
Key_num=17;
}
}
P2=0xfb;
temp=P2;
temp=temp&0x0f;
if(temp!=0x0f)
{
delay(10);
temp=P2;
temp=temp&0x0f;
while(temp!=0x0f)
{
switch(temp)
{
case 0xbe:Key_num=9;break;
case 0xbd:Key_num=10;break;
case 0xbb:Key_num=11;break;
case 0xb7:Key_num=12;break;
}
}
if(temp==0x0f)
{
Key_num=17;
}
}
P1=0xf7;
temp=P2;
temp=temp&0x0f;
if(temp!=0x0f)
{
delay(10);
temp=P2;
temp=temp&0x0f;
while(temp!=0x0f)
{
switch(temp)
{
case 0x7e:Key_num=12;break;
case 0x7d:Key_num=14;break;
case 0x7b:Key_num=15;break;
case 0x77:Key_num=16;break;
}
}
if(temp==0x0f)
{
Key_num=17;
}
}
}
void main(void)
{
while(1)
{
Keyboard();
display();
}
}不知道哪里错了 |