• AVR Freaks

Hot!Push Button bouncing with timer interrupt

Author
abhi143
Senior Member
  • Total Posts : 125
  • Reward points : 0
  • Joined: 2018/05/20 09:06:52
  • Location: 0
  • Status: offline
2020/09/24 18:58:49 (permalink)
0

Push Button bouncing with timer interrupt

Hello
 
I work with PIC16F877A.  I want to toggle LED by Push Button using timer interrupt. I do not want the complete code I just need some explanation  How timer interrupt can be useful for Button bouncing?
#1

15 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28677
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Push Button bouncing with timer interrupt 2020/09/24 19:05:18 (permalink)
    +3 (3)
    When you see a button change, start counting interrupts.
    If the button is the same in the next interrupt, increment your count.
    Otherwise, clear the count back to zero if the button has changed.
    Once you reach a certain count (five counts at 50Hz would indicate the button had been the same for one tenth of a second), you can say that is a new stable state for the button, so you can count that as one press.
    Then wait until you see the button released, then pressed again for the next operation.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    upand_at_them
    Super Member
    • Total Posts : 681
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/24 19:07:47 (permalink)
    +3 (3)
    Good article, and debouncing routine, at http://www.ganssle.com/debouncing.htm
    #3
    abhi143
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/24 20:11:14 (permalink)
    0
    ric
    When you see a button change, start counting interrupts. 

    When Push Button will pressed, It would change from low to high  
    When Push Button will released, It would change from high to low
     
     
    ric
    When you see a button change, start counting interrupts.
    If the button is the same in the next interrupt, increment your count.
    Otherwise, clear the count back to zero if the button has changed.
    Once you reach a certain count (five counts at 50Hz would indicate the button had been the same for one tenth of a second),
     

     
    I can understand you want  timer interrupt for specific time and checking button state
    How many ms of timer interrupt do I need for 1 count ?
    #4
    ric
    Super Member
    • Total Posts : 28677
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Push Button bouncing with timer interrupt 2020/09/24 20:50:53 (permalink)
    +1 (1)
    abhi143
    ric
    When you see a button change, start counting interrupts. 

    When Push Button will pressed, It would change from low to high  
    When Push Button will released, It would change from high to low

    Yes, so you need to save the state observed each time you get an interrupt, and check if it different to last time.
     

    ric
    When you see a button change, start counting interrupts.
    If the button is the same in the next interrupt, increment your count.
    Otherwise, clear the count back to zero if the button has changed.
    Once you reach a certain count (five counts at 50Hz would indicate the button had been the same for one tenth of a second),
     

     
    I can understand you want  timer interrupt for specific time and checking button state
    How many ms of timer interrupt do I need for 1 count ?

    Your first post is not clear. Do you want the LED to toggle once on each press (which is what I assumed), or do you want it to keep toggling while the button is down?
    If it is once per press, do you want it to happen when the kjey is pressed, or when it is released (or both)?

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    abhi143
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/24 21:20:26 (permalink)
    0
    ric

    Your first post is not clear. Do you want the LED to toggle once on each press (which is what I assumed), or do you want it to keep toggling while the button is down?
    If it is once per press, do you want it to happen when the kjey is pressed, or when it is released (or both)?



    I want the LED to toggle once on each press. I want it to happen when the push button is pressed
    #6
    Murton Pike Systems
    Senior Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/25 04:55:56 (permalink)
    +3 (3)
    Simplest push button routine with debounce is:
    1/ wait for button pressed.
    2/ wait for button released.
    3/ set debounce counter to 20.
    4/ is button still released ? if no goto 2. if yes debouce counter-1.
    5/ wait 1ms.
    6/ has debounce counter reached zero ? yes accept key press. No goto 4.
    #7
    upand_at_them
    Super Member
    • Total Posts : 681
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/25 06:12:18 (permalink)
    0
    And pick an appropriate timer interrupt interval.  I'm using 5ms currently.
    #8
    abhi143
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/25 06:45:57 (permalink)
    0
    upand_at_them
    And pick an appropriate timer interrupt interval.  I'm using 5ms currently.


    let's assume we have interrupt  set for 1 ms

    void main (void)
    {
     
      while (1)
     
       {
     
       }
     
     }
     
     
    //set interrupt for 1 ms
    void interrupt(void)
    {
        if(TimerFlagSet)
        {
           count++;
     
           ReloadTimer;
        }
     if (count == 20)
       {
     
         count = 0;
       }
     
      ClearTimerFlag
      }
    }

    post edited by abhi143 - 2020/09/25 06:59:43
    #9
    abhi143
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/26 10:28:39 (permalink)
    0
    ric
    When you see a button change, start counting interrupts.
    If the button is the same in the next interrupt, increment your count.
    Otherwise, clear the count back to zero if the button has changed.
    Once you reach a certain count (five counts at 50Hz would indicate the button had been the same for one tenth of a second), you can say that is a new stable state for the button, so you can count that as one press.
    Then wait until you see the button released, then pressed again for the next operation.
     


    Will it have interrupt for button and interrupt for timer ? When interrupt generate on the pin then start timer interrupt
    #10
    davea
    Super Member
    • Total Posts : 389
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/26 10:44:13 (permalink)
    0
    NO the button should be tested in the timer interrupt
    2 states
    1 is button pressed and for how long
    2 button released and for how long
    #11
    PStechPaul
    Super Member
    • Total Posts : 2951
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/26 13:50:54 (permalink)
    0
    You can use an interrupt on change to detect the contact closing, set a debounce count, and exit. Then in the timer interrupt, decrement the debounce count and check the contact status after 10-50 mSec or so. If it is now open, do nothing - otherwise set a "contact closed" flag which is checked in the main loop, and perform whatever you need while the contact stays closed. Reset the flag when the contact opens.
     
    It is often better to have switches connect a digital input to GND through a pull-up, and a small capacitor across the contacts. This may provide better noise immunity, as the input must transition from Vdd to less than 1 volt threshold, which will be a 4 volt change on a 5V supply, whereas a high-going signal will change logic state with a 1 volt change. You can also use the weak internal pull-ups, although 4.7-10k is more robust.
     
     

     
    #12
    upand_at_them
    Super Member
    • Total Posts : 681
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/26 13:58:31 (permalink)
    +3 (3)
    Ganssle recommends against using an interrupt-on-change.  Interrupt only for the 1-5ms testing.
    #13
    ric
    Super Member
    • Total Posts : 28677
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Push Button bouncing with timer interrupt 2020/09/26 17:57:02 (permalink)
    +1 (1)
    abhi143
    ric
    When you see a button change, start counting interrupts.
    If the button is the same in the next interrupt, increment your count.
    Otherwise, clear the count back to zero if the button has changed.
    Once you reach a certain count (five counts at 50Hz would indicate the button had been the same for one tenth of a second), you can say that is a new stable state for the button, so you can count that as one press.
    Then wait until you see the button released, then pressed again for the next operation.
     


    Will it have interrupt for button and interrupt for timer ? When interrupt generate on the pin then start timer interrupt

    Everything I described is done inside the timer interrupt.
    I strongly recommend NOT using "interrupt on change" etc. on pushbuttons.
     
    When I say "wait", I mean defer until a later timer interrupt. Never ever stay inside an ISR waiting for something.
     
     
    post edited by ric - 2020/09/26 17:58:03

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #14
    abhi143
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/26 20:16:29 (permalink)
    0
    ric
    Everything I described is done inside the timer interrupt.
    I strongly recommend NOT using "interrupt on change" etc. on pushbuttons.
     
    When I say "wait", I mean defer until a later timer interrupt. Never ever stay inside an ISR waiting for something.

     
    How to convert your points in code ?
     
    If the button is the same in the next interrupt, increment your count.
    Otherwise, clear the count back to zero if the button has changed.
    Once you reach a certain count (five counts at 50Hz would indicate the button had been the same for one tenth of a second), you can say that is a new stable state for the button, so you can count that as one press.
    Then wait until you see the button released, then pressed again for the next operation.

     

     




    // CONFIG
    #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.


    #define _XTAL_FREQ 20000000
    #include <xc.h>

    void main(void)
    {
         PORTB = 0b00000001;  //RB0 pin for button
         TRISC = 0b00000000;  //RC7 pin  for LED
     
         OPTION_REG = 0b00000101;
       
        TMR0 = 101;    // Load the time value
        TMR0IE = 1;    //Enable timer interrupt bit
        GIE = 1;       //Enable Global Interrupt
       
        while(1)
        {
     
        }
        return;
    }
     
    //1ms timer interrupt ()
    void __interrupt() Timer(void){
    {
        if(TMR0IF==1)
        {
            TMR0 = 101;     //Load the timer Value
            TMR0IF=0;       // Clear timer interrupt flag
               
             if(RB0 == 1)  //  check button pressed
             {
                 
             }
           
        }
    }
    }



    post edited by abhi143 - 2020/09/26 20:17:43
    #15
    PStechPaul
    Super Member
    • Total Posts : 2951
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Push Button bouncing with timer interrupt 2020/09/26 21:34:38 (permalink)
    0
    Something like this:

    #define DEBOUNCE_COUNT 5
    volatile char debounce = DEBOUNCE_COUNT;
    volatile char keypressed = FALSE;
     
    //1ms timer interrupt ()
    void __interrupt() Timer(void){
    {
        if(TMR0IF==1)
        {
            TMR0 = 101;     //Load the timer Value
            TMR0IF=0;       // Clear timer interrupt flag
               
             if(!keypressed && RB0 == 1)  //  check button pressed
               {
               debounce--;
               if( debounce <= 0 )
                 keypressed = TRUE; 
               }
             else
               {
               keypressed = FALSE;
               debounce = DEBOUNCE_COUNT;
               }
        }
    }
    void main(void) {
      while(1) {
        while(keypressed)
          do_something();
        stop_doing_it();
      }
    }

    Not elegant, and could be improved, as an exercise for the OP...

     
    #16
    Jump to:
    © 2020 APG vNext Commercial Version 4.5