Simple keypad library, for a 3-column x 4-row keypad.
Key arrangement is: --------- 1 2 3 4 5 6 7 8 9 * 0 # ---------
Supports two function keys ('*' and '#').
………………
static char Last_valid_key_G = KEYPAD_NO_NEW_DATA;//静态全局变量
………………
void KEYPAD_Update(void) //键盘更新函数 { char Key, FnKey;
// Scan keypad here... if (KEYPAD_Scan(&Key, &FnKey) == 0) //调用键盘扫描 { // No new key data - just return return; }
……………… ………………
bit KEYPAD_Scan(char* const pKey, char* const pFuncKey) { static data char Old_Key;
char Key = KEYPAD_NO_NEW_DATA; char Fn_key = (char) 0x00;
C1 = 0; // Scanning column 1 if (R1 == 0) Key = '1'; if (R2 == 0) Key = '4'; if (R3 == 0) Key = '7'; if (R4 == 0) Fn_key = '*'; C1 = 1;
C2 = 0; // Scanning column 2 if (R1 == 0) Key = '2'; if (R2 == 0) Key = '5'; if (R3 == 0) Key = '8'; if (R4 == 0) Key = '0'; C2 = 1;
C3 = 0; // Scanning column 3 if (R1 == 0) Key = '3'; if (R2 == 0) Key = '6'; if (R3 == 0) Key = '9'; if (R4 == 0) Fn_key = '#'; C3 = 1;
if (Key == KEYPAD_NO_NEW_DATA) { // No key pressed (or just a function key) Old_Key = KEYPAD_NO_NEW_DATA; Last_valid_key_G = KEYPAD_NO_NEW_DATA;
return 0; // No new data } // A key has been pressed: debounce by checking twice if (Key == Old_Key) //这步!初始化时 { //Key==Old_Key是理应的 //当有KEY按下后 //Key!=Old_Key //但!=Old_Key又如何 //进入IF语句??? // A valid (debounced) key press has been detected //这里小了延时语句?? // Must be a new key to be valid - no 'auto repeat' if (Key != Last_valid_key_G) { // New key! *pKey = Key; Last_valid_key_G = Key;
// Is the function key pressed too? if (Fn_key) { // Function key *is* pressed with another key *pFuncKey = Fn_key; } else { *pFuncKey = (char) 0x00; }
return 1; } }
// No new data Old_Key = Key; //当某KEY被一直按下时,由于不支持重 return 0; //复,虽然检测到Key != KEYPAD_NO_NEW_DATA } //但由于Key == Old_Key,仍然进入IF语句,再由 //Key != Last_valid_key_G 决定不是新键, //return 0;
个人认为,第一次有KEY按下后,要求Key != Old_Key进入IF语句: 但在长按情况下,却要求Key == Old_Key进入IF语句。。。如何解释???
|