This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSPM0G1505: button debounce

Part Number: MSPM0G1505
I have 3 buttons. Want to increment the button count according to the button pressed . All three mcu pins are internally pulled-up and interrupt is enabled on falling edge so when the button is pressed, ground is connected and interrupt will be called. Problem is I cant properly filter out multiple presses i.e debounces. Can someone suggest what else should I add in the code? Is there any Ti inbuilt function that can help with this problem?
void GROUP1_IRQHandler(void)
{
    switch (DL_Interrupt_getPendingGroup(DL_INTERRUPT_GROUP_1))
    {
        case button_INT_IIDX:
           
                            if (DL_GPIO_readPins( button_PORT, button_bt1_PIN ))
                            {
                                bt1_count++;
                             
                            }
                           
                           
                            if (DL_GPIO_readPins( button_PORT, button_bt2_PIN ))
                            {
                                 bt2_count++;
                           
                            }
                           
                            
                            if (DL_GPIO_readPins( button_PORT, button_bt3_PIN ))
                            {
                                bt3_count++;
                           
                            }
                             break;
    }
}
                           
                         
  • You need to put in a delay after you detect the button press.

    I find it easier to handle buttons in the main code as opposed to an interrupt.

  • In main code as in continuous polling? I added the delay but I am also running a 4 digit seven segment display and reading analog signals and it stops while the delay function is running and it is visible enough to cause problems? Is there any solution for this?

  • Hi SURESHAN,

    Keith is correct. A simple way is that you can add a delay in the "if(...){;}" and then check the input again, if it still got the same value, then it is a valid input.

    However, it is quite common requirments in button detection, you can search it in the Internet, there should be some workable solutions that you can refer to.

    B.R.

    Sal

  • You need to use a timer to set up a non-blocking delay. I use SysTick to set up an Arduino-like "Millis" function. Then when I see a button press, I store the starttime of the press. After a certain amount of time has past (15 ms or so for my Cherry buttons) , I then register the button press.

    something like this in pseduocode:

    if (button1pressed)

    then

    if (button1start == 0)

    then

    button1start = Millis(); if button1start == 0 then button1start = 1; // In case Millis happens to be 0!

    else

    if (Millis() - button1start > 15)

    then register keypress

    endif

    endif