#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit beep=P1^5;
unsigned char num,t,temp,keynum;
unsigned char code table1[]={0xff,0xfb,0xf7,0xf3,0xef,0xeb,0xe7,0xe3};
unsigned char code table2[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83};
void delay(unsigned int z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void key()
{
P0=0xfd;
temp=P0;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P0;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P0; //这一步赋值绝对不能少啊
switch(temp)
{
case 0xed:keynum=1;
break;
case 0xdd:keynum=2;
break;
case 0xbd:keynum=3;
break;
case 0x7d:keynum=4;
break;
}
while(temp!=0xf0)
{
temp=P0;
temp=temp&0xf0;
}
}
}
P0=0xfb;
temp=P0;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P0; //这里一定是把P0赋给temp
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P0; //这一步赋值绝对不能少啊
switch(temp)
{
case 0xeb:keynum=5;
break;
case 0xdb:keynum=6;
break;
case 0xbb:keynum=7;
break;
case 0x7b:keynum=8;
break;
}
while(temp!=0xf0)
{
temp=P0;
temp=temp&0xf0;
}
}
}
P0=0xf7;
temp=P0;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P0;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P0; //这一步赋值绝对不能少啊
switch(temp)
{
case 0xe7:keynum=9;
break;
case 0xd7:keynum=0;
break;
case 0xb7:keynum=10;
break;
case 0x77:keynum=11;
break;
}
while(temp!=0xf0)
{
temp=P0;
temp=temp&0xf0;
}
}
}
//return keynum;
}
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
keynum=0;
P2=table1[0];
while(1)
{
key();
P1=table2[keynum];
// if(keynum==10)
// {
// key();
// if(num==3)
// P1=table2[0];
// }
// switch(num)
// {
// case 1:if(keynum==1) P1=table2[0];
// break;
// }
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
}
评论0