#define KEYSCAN_TICK 5 //50ms
#define KEY_PRESS_INTERVAL 4
#define KEY_SHORTUP_TICK 18 //900ms #define KEY_LONGUP_TICK 30 //1000ms
#define DOWN_MASK_BIT 16 #define PRESS_MASK_BIT 17 #define SHORTUP_MASK_BIT 18 #define LONGUP_MASK_BIT 19 INT32U Key = 0; INT32U preKeyValue = 0xffffffff; INT32U keyTimer = 0; void KeyScan(void)//Interrup Service Routin { INT32U keyValue; Key = 0; keyValue = GetKeyValue(); if(keyValue) { if(keyTimer == 0) Key = keyValue + ((INT32U)1<<16);//down else if(keyTimer >= KEY_SHORTUP_TICK ) { if(keyTimer % KEY_PRESS_INTERVAL == 0) { Key = keyValue + ((INT32U)1<<17);//press } } } else { if(keyTimer > 0 && keyTimer < KEY_SHORTUP_TICK) { Key = preKeyValue + ((INT32U)1<<18);//short up } else if(keyTimer >= KEY_LONGUP_TICK) { Key = preKeyValue + ((INT32U)1<<19);//long up } keyTimer = 0; } if(keyValue == preKeyValue ) { keyTimer++; } if(keyValue) preKeyValue = keyValue; } 第一个写按键扫描程序,欢迎大侠拍转,指出偶程序的bug! |