|
unsigned char GetKey(void)
{//3個(gè)按鍵連接在P3.6、P3.3、P3.2
unsigned char KeyTemp,CheckValue,Key = 0x00;
CheckValue = P3&0x4c; //按鍵的常態(tài)值賦值變量CheckValue 01001100
if(CheckValue==0x4c) //兩者相等,說明沒有鍵按下
return 0x00; //返回鍵值0x00,不執(zhí)行以下語句
//如果if(CheckValue==0x4c)為假,說明有鍵按下或可能在抖動中,也可能是干擾信號
Delay1ms(10); //延時(shí)10ms,等待按鍵穩(wěn)定
KeyTemp = P3&0x4c; //穩(wěn)定后再將按鍵的變態(tài)值賦值變量KeyTemp 01001100
if(KeyTemp==CheckValue)//判斷兩個(gè)變量是否相等,如果相等,則不是按鍵按下
return 0x00; //返回鍵值0x00,不執(zhí)行以下語句
//兩個(gè)變量不相等,
if(!(CheckValue&0x04)) //順序判斷是哪個(gè)鍵按下或哪幾個(gè)鍵組合按下
Key|=0x01;
if(!(CheckValue&0x08))
Key|=0x02;
if(!(CheckValue&0x40))
Key|=0x04;
return Key; //返回鍵值Key
} |
|