分享一个键盘扫描代码,嵌入式c代码,拿去可用,简短有效。
int task_fxn_key_check(void *p_arg) { int port,trigger=0; static int i=0,continues=0,cnt=0; static int trigger_long=0; static uint32_t last_keys=KEY_LOC_VAL_NULL,this_keys=KEY_LOC_VAL_NULL; p_arg = p_arg; for(port=0xFF,i=0;i<ARRAY_SIZE(g_keys);i++){ if(GPIO_ReadInputDataBit(g_keys[i].port, g_keys[i].gpio)==1){ port &= ~g_keys[i].val; } } this_keys = port; if(last_keys == this_keys){ port = port ^ 0xFF; trigger = port & (port^continues); continues = port; /* 只要按键没有松开,这个值永远是0x01 */ if(trigger){ trigger_long |= trigger; } if(continues){ if(++cnt == 160){ /* 5s */ trace_debug(MID_KEY,"long press 0x%X\r\n",continues); key_convert_to_car(1,continues); trigger_long = 0; } }else{ cnt = 0; if(trigger_long){ trace_debug(MID_KEY,"press 0x%X\r\n",trigger_long); key_convert_to_car(0,trigger_long); trigger_long = 0; } } } last_keys = this_keys; return trigger; }