以下面的按键处理函数作为例子讲解可能存在的bug
//下面代码段是放外部中断
if(EXTI_GetITStatus(EXTI_Line8) != RESET) { if(g_cnt_MainKey_switch == 0 && MainKey_Read() == 0)//press MainKey, and it hasn't starting counting yet { debug("press MainKey "); g_cnt_MainKey = 0; g_cnt_MainKey_switch = 1; xTimerStartFromISR( xKeyTimerCtrHandle, &xHigherPriorityTaskWoken ); }else if(g_cnt_MainKey_switch == 1 && MainKey_Read() != 0)//release MainKey, and counting is in progress { debug("release MainKey "); xTimerStopFromISR( xKeyTimerCtrHandle, &xHigherPriorityTaskWoken ); g_cnt_MainKey_switch = 0; if(g_cnt_MainKey >= 150) { xTaskNotifyFromISR( xWIFIConnectHandle, 0x01, eSetBits, &xHigherPriorityTaskWoken ); debug("send the instruction of connecting wifi "); } } EXTI_ClearITPendingBit(EXTI_Line8); }
//FreeRTOS定时器回调函数(定时器中断)
void vKeyCtrTimerCallback(TimerHandle_t xTimer) { if(g_cnt_MainKey_switch == 1) { if(g_cnt_MainKey < 1000) { g_cnt_MainKey++; } if(g_cnt_MainKey == 150) //press MainKey for more than 3s { debug("red led on, blue red twinkle slowly "); HeartbeatPacket_Switch_off(); vLedTaskRedOn_BlueSlowTwinkle(); } } }
讲解:
第一代码段:检测到按键按下的下降沿,启动定时器计数;检测到按键放开的上升沿,停止定时器计算,根据按下的时间长短进行事件处理。
第二代码段:计数到一定值LED进行相应的显示(计为A)表示已经长按了一定时间,放开按键LED进行另一种显示(计为B)(此程序不在这两段代码内)
如果第一代码段 g_cnt_MainKey = 0; 放在 xTimerStartFromISR( xKeyTimerCtrHandle, &xHigherPriorityTaskWoken ); 之后可能产生的bug:
定时器计数值 g_cnt_MainKey 计数到150后放开按键,但是由于按键抖动,很快产生了按键下降沿和上升沿,在执行了 xTimerStartFromISR( xKeyTimerCtrHandle, &xHigherPriorityTaskWoken );但还未执行 g_cnt_MainKey = 0; 定时器回调函数执行了,导致LED显示模式为A,而不是B