#include
#define uchar unsigned char
#define uint unsigned int
uchar buff,times,j;
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f, //0,1,2,3
0x66,0x6d,0x7d,0x07, //4,5,6,7
0x7f,0x6f,0x77,0x7c, //8,9,A,B
0x39,0x5e,0x79,0x71,0x00}; //C,D,E,F,null
unsigned char idata value[8];
void delay1ms(void) //消抖动
{ uchar i;
for(i=200;i>0;i--);
}
void delay5ms(void) //延时5ms
{
unsigned char i,j;
for(i=5;i>0;i--)
for(j=248;j>0;j--);
}
void key_scan(void) //键盘输入扫描函数
{ uchar hang,lie,key;
P3=0xf0;
if((P3&0xf0)!=0xf0) //行码为0,列码为1
{ delay1ms();
if((P3&0xf0)!=0xf0) //有键按下,列码变为0
{ hang=0xfe; //逐行扫描
times++;
if(times==2)
times=1;
while((hang&0x10)!=0) //扫描完4行后跳出
{ P3=hang;
if((P3&0xf0)!=0xf0) //本行有键按下
{ lie=(P3&0xf0)|0x0f;
buff=((~hang)+(~lie));
switch(buff)
{ case 0x11: key=0;break;
case 0x21: key=1;break;
case 0x41: key=2;break;
case 0x81: key=3;break;
case 0x12: key=4;break;
case 0x22: key=5;break;
case 0x42: key=6;break;
case 0x82: key=7;break;
case 0x14: key=8;break;
case 0x24: key=9;break;
case 0x44: key=10;break;
case 0x84: key=11;break;
case 0x18: key=12;break;
case 0x28: key=13;break;
case 0x48: key=14;break;
case 0x88: key=15;break;
}
value[times-1]=key; //按下的键值
}
else hang=(hang<<1)|0x01; //下一行扫描
}
}
}
}
void main(void)
{ uchar key;
while(1)
{ key_scan();
P0=0;
for(j=times;j>0;j--)
{
P2=j-1; //数码管列扫描
P0=dispcode[value[times-j]]; //A-G数据
delay5ms();
}
}
}
1