[qq]1187470326[/qq]交流QQ在线
改进前:
unsigned char KeyScan(void) //键盘扫描函数,使用行列逐级扫描法
{
unsigned char Val;
KeyPort=0xf0;//高四位置高,低四位拉低
if(KeyPort!=0xf0)//表示有按键按下
{
DelayMs(10); //去抖
if(KeyPort!=0xf0)
{ //表示有按键按下
KeyPort=0xfe; //检测第一行
if(KeyPort!=0xfe)
{
Val=KeyPort&0xf0;
Val+=0x0e;
while(KeyPort!=0xfe);
DelayMs(10); //去抖
while(KeyPort!=0xfe);
return Val;
}
KeyPort=0xfd; //检测第二行
if(KeyPort!=0xfd)
{
Val=KeyPort&0xf0;
Val+=0x0d;
while(KeyPort!=0xfd);
DelayMs(10); //去抖
while(KeyPort!=0xfd);
return Val;
}
KeyPort=0xfb; //检测第三行
if(KeyPort!=0xfb)
{
Val=KeyPort&0xf0;
Val+=0x0b;
while(KeyPort!=0xfb);
DelayMs(10); //去抖
while(KeyPort!=0xfb);
return Val;
}
KeyPort=0xf7; //检测第四行
if(KeyPort!=0xf7)
{
Val=KeyPort&0xf0;
Val+=0x07;
while(KeyPort!=0xf7);
DelayMs(10); //去抖
while(KeyPort!=0xf7);
return Val;
}
}
}
return 0xff;
}
unsigned char KeyPro(void)
{
switch(KeyScan())
{
case 0x7e:return 0;break;//0 按下相应的键显示相对应的码值
case 0x7d:return 1;break;//1
case 0x7b:return 2;break;//2
case 0x77:return 3;break;//3
case 0xbe:return 4;break;//4
case 0xbd:return 5;break;//5
case 0xbb:return 6;break;//6
case 0xb7:return 7;break;//7
case 0xde:return 8;break;//8
case 0xdd:return 9;break;//9
case 0xdb:return 10;break;//a
case 0xd7:return 11;break;//b
case 0xee:return 12;break;//c
case 0xed:return 13;break;//d
case 0xeb:return 14;break;//e
case 0xe7:return 15;break;//f
default:return 0xff;break;
}
}
改进后:
unsigned char pcf8574_key_number(void)
{
unsigned char key_number=0;
//key_board PCF8574 write byte
KeyPort=0xf0;
key_number=KeyPort;
if(key_number!= 0xf0)
{
delay_5us(2);//delay 10us
if(key_number!= 0xf0)
{
//key_board PCF8574 write byte
KeyPort=0x0f;
key_number |= KeyPort;//key_board PCF8574 read byte
}
}
//SendData(key_number);//test
return key_number;
}
unsigned char key_board_number(void)
{
unsigned char key_number=0;
switch(pcf8574_key_number())
{
case 0X7E:key_number=1;break;
case 0X7D:key_number=2;break;
case 0X7B:key_number=3;break;
case 0X77:key_number=4;break;
case 0XBE:key_number=5;break;
case 0XBD:key_number=6;break;
case 0XBB:key_number=7;break;
case 0XB7:key_number=8;break;
case 0XDE:key_number=9;break;
case 0XDD:key_number=10;break;
case 0XDB:key_number=11;break;
case 0XD7:key_number=12;break;
case 0XEE:key_number=13;break;
case 0XED:key_number=14;break;
case 0XEB:key_number=15;break;
case 0XE7:key_number=16;break;
default:break;
}
return key_number;
}
1