程序修改如下:
#define KeyPort P3
unsigned char keyscan() //矩陣鍵盤掃描函數
{
unsigned char temp1,temp2,VAL; //臨時變量
static bit sign=0; //按鍵自鎖標志
static unsigned char count=0; //消抖計數變量
KeyPort=0xf0; //先給P3賦一個初值
if(KeyPort!=0xf0) //判斷P3不等于所賦初值,說明有健按下
{
if(sign==0) //如果按鍵自鎖標志為0
{
count++; //消抖計數
if(count>=100) //消抖計數自>=100,估算主循環周期調整
{ //摒棄Delay延時方式,提高運行效率
count=100; //防止溢出
sign=1; //按鍵自鎖標志置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; //按鍵自鎖標志清0
count=0; //消抖計數清0
}
}
|