#define KEY P1 //键盘所连接的I/O接口组定义
*********************************************************************************************
函数名:16个阵列式键盘驱动程序
调 用:? = Key ();
参 数:无
返回值:unsigned char 键值0~16
结 果:有键按下时返回值为键值1~16,无键按下时返回值为0
备 注:在主函数中不断调用
**********************************************************************************************/
unsigned char Key (void){ //4*4阵列键盘处理程序
unsigned char a,b,c;
KEY = 0x0f; //设定键盘初始电平状态
if (KEY != 0x0f){ //读取键盘状态是否改变
Delay (20); //延时20ms去抖动
if (KEY != 0x0f){ //重新读取
a = KEY; //寄存状态值到a
}
KEY = 0xf0; //设定键盘反向电平状态
c = KEY; //读取反向电平状态值到c
a = a|c; //a与c相或
switch(a){ //键盘状态查表
case 0xee: b = 1; break;
case 0xed: b = 2; break;
case 0xeb: b = 3; break;
case 0xe7: b = 4; break;
case 0xde: b = 5; break;
case 0xdd: b = 6; break;
case 0xdb: b = 7; break;
case 0xd7: b = 8; break;
case 0xbe: b = 9; break;
case 0xbd: b = 10; break;
case 0xbb: b = 11; break;
case 0xb7: b = 12; break;
case 0x7e: b = 13; break;
case 0x7d: b = 14; break;
case 0x7b: b = 15; break;
case 0x77: b = 16; break;
default: b = 0 ; break;
}
}
return (b); //将b中的键值代号送入函数返回值
}