本帖最后由 hutians 于 2015-2-10 17:59 编辑
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e
};
uint num;
uchar temp;
sbit duan=P1^0;
void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=20;j>0;j--);
}
void display()
{
duan=1;
P0=table[num];
duan=0;
delay(50);
}
uchar keyboard(void)
{
{
P2=0xfe;
temp=P2;
temp&=0xf0;
if(temp!=0xf0)
{
delay(50);
temp=P2;
temp&=0xf0;
switch(temp)
{
case 0x70:num=0;break;
case 0xb0:num=1;break;
case 0xd0:num=2;break;
case 0xe0:num=3;break;
}
while(temp!=0xf0)
{
temp=P2;
temp&=0xf0;
}
display();
}
}
{
P2=0xfd;
temp=P2;
temp&=0xf0;
if(temp!=0xf0)
{
delay(50);
temp=P2;
temp&=0xf0;
switch(temp)
{
case 0x70:num=4;break;
case 0xb0:num=5;break;
case 0xd0:num=6;break;
case 0xe0:num=7;break;
}
while(temp!=0xf0)
{
temp=P2;
temp&=0xf0;
}
display();
}
}
{
P2=0xfb;
temp=P2;
temp&=0xf0;
if(temp!=0xf0)
{
delay(50);
temp=P2;
temp&=0xf0;
switch(temp)
{
case 0x70:num=8;break;
case 0xb0:num=9;break;
case 0xd0:num=10;break;
case 0xe0:num=11;break;
}
display();
}
}
{
P2=0xf7;
temp=P2;
temp&=0xf0;
if(temp!=0xf0)
{
delay(50);
temp=P2;
temp&=0xf0;
switch(temp)
{
case 0x70:num=12;break;
case 0xb0:num=13;break;
case 0xd0:num=14;break;
case 0xe0:num=15;break;
}
display();
}
}
}
void main()
{
while(1)
{
keyboard();
}
}
为自己加油!