it'is my logic for debounce in interrupt external, when the microcontroller wake up.
Basictly i have a timer (TIMER 4 STM8L), that overflow 1ms. is only activated when an external interruption occurs.
INTERRUPT_HANDLER(EXTI1_IRQHandler,9)
{
debounceTimerSet(TIMEOUT_DEBOUNCE); // Load timeout 30ms. TIMEOUT_DEBOUNCE = 30
TIM4_Cmd(ENABLE); // Enable timer 4 (overflow 1ms)
EXTI_ClearITPendingBit(EXTI_IT_Pin1); // Clean flag
}
While there is a variation in the key, the timer is being loaded. if end variation, the timeout will overflow.
INTERRUPT_HANDLER(TIM4_UPD_OVF_TRG_IRQHandler,25) //1ms
{
debounceTimerProc();
TIM4_ClearITPendingBit(TIM4_IT_Update);
}
void debounceTimerProc(void)
{
if(timeOutDebounce)
{
if(!(--timeOutDebounce) )
{
readButtons();
TIM4_Cmd(DISABLE); // timer off
}
}
}
// When call readbutton dont has VARIATIONS.
void readButtons (void)
{
if(GPIO_ReadInputDataBit(GPIOA, BTN1_PIN) == 0)
{
btn |= BTN_D0;
}
if(GPIO_ReadInputDataBit(GPIOB, BTN2_PIN) == 0)
{
btn |= BTN_D1;
}
if(GPIO_ReadInputDataBit(GPIOC, BTN3_PIN) == 0)
{
btn |= BTN_D2;
}
if(GPIO_ReadInputDataBit(GPIOB, BTN4_PIN) == 0)
{
btn |= BTN_D3;
}
if( (GPIO_ReadInputDataBit(GPIOA, BTN1_PIN))&&
(GPIO_ReadInputDataBit(GPIOB, BTN2_PIN))&&
(GPIO_ReadInputDataBit(GPIOC, BTN3_PIN))&&
(GPIO_ReadInputDataBit(GPIOB, BTN4_PIN)) )
{
btn = 0; // Without press
}
}
That's the logic to debounce. Has something wrong?