標題: 這是一段鍵盤掃描程序,我知道作用,但不知道每條為什么這樣寫,煩請哪位給逐句解.... [打印本頁]

作者: hanxingyijian    時間: 2023-2-28 20:04
標題: 這是一段鍵盤掃描程序,我知道作用,但不知道每條為什么這樣寫,煩請哪位給逐句解....
unsigned char GetKey(void)
{
        unsigned char KeyTemp,CheckValue,Key = 0x00;
        CheckValue = P3&0x4c;
        if(CheckValue==0x4c)
                return 0x00;
        Delay1ms(10);
        KeyTemp = P3&0x4c;
        if(KeyTemp==CheckValue)
                return 0x00;

        if(!(CheckValue&0x04))
                Key|=0x01;
        if(!(CheckValue&0x08))
                Key|=0x02;
        if(!(CheckValue&0x40))
                Key|=0x04;
        return Key;
}


作者: wulin    時間: 2023-3-1 09:22
unsigned char GetKey(void)
{//3個按鍵連接在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);          //延時10ms,等待按鍵穩(wěn)定
        KeyTemp = P3&0x4c;     //穩(wěn)定后再將按鍵的變態(tài)值賦值變量KeyTemp 01001100
        if(KeyTemp==CheckValue)//判斷兩個變量是否相等,如果相等,則不是按鍵按下
                return 0x00;        //返回鍵值0x00,不執(zhí)行以下語句
                               //兩個變量不相等,
        if(!(CheckValue&0x04)) //順序判斷是哪個鍵按下或哪幾個鍵組合按下
                Key|=0x01;
        if(!(CheckValue&0x08))
                Key|=0x02;
        if(!(CheckValue&0x40))
                Key|=0x04;
        return Key;            //返回鍵值Key
}
作者: hanxingyijian    時間: 2023-3-1 16:57
wulin 發(fā)表于 2023-3-1 09:22
unsigned char GetKey(void)
{//3個按鍵連接在P3.6、P3.3、P3.2
        unsigned char KeyTemp,CheckValue,Key  ...

非常感謝!有老師指導(dǎo)才能使我不斷進步!




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1