debouncing switches with a timer interrupt?


Closed Thread
Results 1 to 28 of 28

Hybrid View

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

    Default debouncing switches with a timer interrupt?

    hi everyone,
    im experimenting with the "menu builder" application on a 18f2431 and im having a bit of a problem. it inserts a lot of "pause 200" commands, to debounce the switches, which is causing havoc on my real time timer on an lcd. if i remove the pauses, there's no debounce, and one push of a button cycles through alot of menus. i tried the portb interrupt on change, and im getting the same results. i've read some rumours that debouncing can be done using a timer interrupt. is it possible? im using 4 buttons: up, down, enter, exit. any help/idea on pseudocode will really be appreciated; portb interrupt, timer interrupt, software debounce, etc
    NAG CON WIFE!
    WIFE VAR MOOD

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


    Did you find this post helpful? Yes | No

    Default

    If you can change the hardware, one option would be to use capacitors across the switches. Software denounce would no longer be required.

    Otherwise you do do this:

    port b change interrupt: detects a button press and takes action, an the end of the interrupt it sets a timer so that it will overflow in say 50ms, and then disables the port b interrupt.

    when the timer interrupt goes off 50ms later it simply needs to re-enable the port b interrupt and stop the timer.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  3. #3
    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

  4. #4
    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

  5. #5
    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

  6. #6
    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

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