#include<AT89X52.H> //包含AT89X52.H头文件 /*定义0~9,A~F十六个字符的字型码表*/
unsigned char table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
void delay10ms(void)
{
unsigned char i,j;
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}
void delay()
{
unsigned char i;
for(i=0;i<255;i++);
}
unsigned char scan_key(void)
{
unsigned char scan,col,rol,tmp;
bit flag=0;
scan=0xef;
for(col=0;col<4;col++)
{
P0=scan;
tmp=P0;
switch(tmp&0x0f)
{
case 0x0e:rol=0;flag=1;break;
case 0x0d:rol=1;flag=1;break;
case 0x0b:rol=2;flag=1;break;
case 0x07:rol=3;flag=1;break;
}
if(flag==1)
break;
scan=(scan<<1)+1;
}
while(tmp!=0x0f)
{
P0=0x0f;
tmp=P0;
}
return(rol*4+col);
}
void scan()
{
unsigned char tmp,key;
while(tmp!=0x0f)
{
P1=table[0];
P2=0xfe;
delay();
P2=0xff;
P1=table[6];
P2=0xfd;
delay();
P2=0xff;
}
}
void main()
{
unsigned char tmp,key;
P0=0x0f;
while(1)
{
while(tmp==0x0f)
{
P0=0x0f;
tmp=P0;
}
delay10ms();
P0=0x0f;
tmp=P0;
if(tmp==0x0f)
continue;
key=scan_key();
P1=table[key];
P2=0xf7;
delay();
scan();
}
}
|