debouncing switches with a timer interrupt?


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    Could it be that you don't need a debouncer at all?

    If you are polling the pin that is connected the switch every program loop, and the loop is longer than the switch bounce time, you don't need a debounce, since it will see the transition, and the "bounces" will be over before you check the switch again.
    Charles Linquist

  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    Charles, I do just exactly that. In my interrupt routine. I copy th state of my button and add it to a shifted result. When the button is connected to ground with a pullup to VDD, the value after the 8 cycle (interrupt) debounce is equal to zero. When the button is released the value after the 8 cycle (interrupt) debounce is equal to 255. I use a 10 millisecond interrupt timer so the debounce time is 80 milliseconds. That way all the main has to do is interrogate debounce variable for it's value.

    Dave Purola,
    N8NTA

  3. #3
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    if i remove the pauses, there's no debounce, and one push of a button cycles through alot of menus.
    I would apply a flag condition to control the key state.

    Code:
    False        CON  0
    True         CON  1
    KEYPRESS  VAR portXx
    KeyFlag      VAR BIT
    Counter      VAR WORD
    
    LOOP:
    
    PAUSE 1
    
    IF KEYPRESS = False then
    
    IF KeyFlag = False Then
    Counter = Counter + 1
    KeyFlag = True
    ENDIF
    
    ELSE
    KeyFlag = False
    ENDIF
    .
    .
    .
    .
    .
    GOTO LOOP
    In the above snippet every time you press the key "KEYPRESS" the word variable "Counter" will increment of one unit, no matter how long you keep the key down. If you remove the flag control using pauses, it will be rather complicate to reach the same results.



    Al.
    Last edited by aratti; - 25th July 2010 at 16:12.
    All progress began with an idea

  4. #4
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by aratti View Post
    I would apply a flag condition to control the key state.
    This would still require some kind of debouncing. The logic state can "bounce" a number of times when pressing a button, so if it toggles back and forth a few times it could still advance the counter more than once for a single key press if the loop is quick enough.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  5. #5
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    This would still require some kind of debouncing. The logic state can "bounce" a number of times when pressing a button
    Contact's debouncing should be solved with proper RC filter! If proper filtering is not possible an additional IF/THEN statement should reduce greatly the debouncing effect.

    Code:
    False        CON  0
    True         CON  1
    KEYPRESS  VAR portXx
    KeyFlag      VAR BIT
    Counter      VAR WORD
    
    LOOP:
    
    PAUSE 1
    
    IF KEYPRESS = False then
    
    IF KeyFlag = False Then
    Counter = Counter + 1
    KeyFlag = True
    ENDIF
    
    ELSE
    KeyFlag = False
    IF  KEYPRESS = False THEN  KeyFlag = True    ' re-checks the key logic state
    ENDIF
    .
    .
    .
    .
    .
    GOTO LOOP
    Al.
    Last edited by aratti; - 26th July 2010 at 00:01.
    All progress began with an idea

  6. #6
    Join Date
    Dec 2009
    Location
    Kalamazoo
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    thanks guys.

    i tried the capacitor method, and that did not work well. still skipping menu items.

    im using proteus simulation, and i tried every value of cap, 1000uf to 0.000001 uf., and everything in between.

    ill try integrate the software flag idea into the problem, and hope that it fixes it. thanks
    NAG CON WIFE!
    WIFE VAR MOOD

  7. #7
    Join Date
    Dec 2009
    Location
    Kalamazoo
    Posts
    42


    Did you find this post helpful? Yes | No

    Default still not functioning

    hi all
    i tried to implement the flag idea, but still the same result. menu item cycles through 3 to 6 items with one push of a button. here's the code snippet
    Code:
    RB_MainMenuLoop:
    ' BTN_PLUS is the Next Choice button
        PAUSE 1
          IF BTN_PLUS=0 THEN
          ;PAUSE 200
          IF FLAG_PLUS = 0 THEN
          bMenuPos=bMenuPos+1
          FLAG_PLUS = 1
          ELSE 
          FLAG_PLUS = 0
          IF BTN_PLUS = 0 THEN FLAG_PLUS = 1
          ;ENDIF
          IF bMenuPos>22 THEN
             bMenuPos=1
          ENDIF
          ENDIF
          Goto DisplayMainMenuLoop:
       ENDIF
    im beginning to think that debouncing is not the problem. is there any other way to skin this cat?
    NAG CON WIFE!
    WIFE VAR MOOD

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts