键盘扫描驱动程序4x4
#include "hardware.h"
#include "public.h"
/******************************************************************************************
* 函数名称 :单一按键选择函数
* 功能描述 :
* 参数 :无
* 返回值 :
******************************************************************************************
bit chk_key(void)
{
if(!key1)
{
delayms(20);
if(!key1) //key=p1.7
{
while(!key1);
delayms(20);
return(1);
}
}
return(0);
}
/******************************************************************************************
* 函数名称 :键扫描函数
* 功能描述 :
* 参数 :无
* 返回值 :
******************************************************************************************/
unsigned char keyscan(void)
{
unsigned char scancode,tmpcode;
key_port = 0xf0; // 发全0行扫描码
if ((key_port&0xf0)!=0xf0) // 若有键按下
{
delayms(60); // 延时去抖动 时间太短按键特灵 太长按键迟钝
if ((key_port&0xf0)!=0xf0) // 延时后再判断一次,去除抖动影响
{
scancode = 0xfe;
while((scancode&0x10)!=0) // 逐行扫描
{
key_port = scancode; // 输出行扫描码
if ((key_port&0xf0)!=0xf0) // 本行有键按下
{
tmpcode = (key_port&0xf0)|0x0f; // 返回特征字节码,为1的位即对应于行和列
return((~scancode)+(~tmpcode));
}
else scancode = (scancode<<1)|0x01; // 行扫描码左移一位
}
}
}
return(0); // 无键按下,返回值为0
}