|
程序修改如下:
#define KeyPort P3
unsigned char keyscan() //矩陣鍵盤掃描函數(shù)
{
unsigned char temp1,temp2,VAL; //臨時(shí)變量
static bit sign=0; //按鍵自鎖標(biāo)志
static unsigned char count=0; //消抖計(jì)數(shù)變量
KeyPort=0xf0; //先給P3賦一個初值
if(KeyPort!=0xf0) //判斷P3不等于所賦初值,說明有健按下
{
if(sign==0) //如果按鍵自鎖標(biāo)志為0
{
count++; //消抖計(jì)數(shù)
if(count>=100) //消抖計(jì)數(shù)自>=100,估算主循環(huán)周期調(diào)整
{ //摒棄Delay延時(shí)方式,提高運(yùn)行效率
count=100; //防止溢出
sign=1; //按鍵自鎖標(biāo)志置1,鍵不抬起,按其他鍵無效
temp1=KeyPort; //temp1保存高4位變化
KeyPort=0x0f; //再給P3賦值0x0f
temp2=KeyPort; //temp2保存低4位變化
VAL=temp2|temp1; //VAL=高4位+低4位
return VAL; //返回VAL值
}
}
}
else //按鍵抬起
{
sign=0; //按鍵自鎖標(biāo)志清0
count=0; //消抖計(jì)數(shù)清0
}
}
|
|