Shop OBEX P1 Docs P2 Docs Learn Events
Button debounce routines , and approach — Parallax Forums

Button debounce routines , and approach

dufflingduffling Posts: 73
edited 2005-04-16 15:00 in General Discussion
How can i debounce a button using Assembly, and also SX/B· without using a pause or delay loop?

Will using a Pause or delay loop mess with my interupts? , i need to keep a steady interrupt going but also

capture button presses etc as the main program loop is working away
·

Comments

  • BeanBean Posts: 8,129
    edited 2005-04-06 11:16
    The Pause command will have no effect on your interrupt routine, however your interrupt routine WILL have an effect on the pause command (it will make it take longer than you have specified).
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video Display Module" Available Now.

    www.sxvm.com

    "A problem well defined, is a problem·half solved."
    ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-04-06 15:33
    To do software debouncing, you'll need to take at least two samples of the button a minimum amount of time between them. If you don't want to pause between them, you can insert the button debounce code into your interrupt by keeping a copy of the button state last ISR.

    A hardware method of debouncing is using a capacitor attached to the SX pin which prevents rapid changes occuring and effectively debounces the switch for you.
  • dufflingduffling Posts: 73
    edited 2005-04-07 02:49
    Ok ,ill give both a try .. I assume the cap is connected to Gnd and Pin ?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-04-07 03:47
    duffling,

    ·· One thing you can do on the stamp since it's a single-tasking MCU is to use what I call a passive delay.· I've used these in Interrupt routines in the past on various CPUs/MCUs.· The idea is that if you need to wait for an event to happen in a loop, but you can't just stop there (Because you have other things you're doing, such as key input), you use a counter/flag to enable the event at the right time.· I will try to explain...

    ·· Within a loop let's say you're checking for a keypress, updating a counter, etc. but you need to leave a pin high for a set time then low.· You can do this one of two ways.· The first would be used if you have an external reference source, such as an RTC to draw on for a counter.

    ·· In this case you could load the time value, convert it to all seconds, add your offset (The delay time in seconds) to that in another variable (The flag value) and enter your loop.· During the loop you will get the time data, convert to seconds, and compare to the flag value.· When the values match, do the event.· In this case set that pin low but remain in the loop.

    ·· The other is what you'll most likely need, and may require more variables if the delay is more than a few milliseconds (This will depend on the speed/length of the loop/mcu).· You will clear the counter variable(s) prior to entering the loop.· Each pass through the loop increment the counter(s) and compare the inner counter (If used, otherwise the deeper of the 2) to 0.· When that happens your time is elapsed, without PAUSEing.

    ·· I hope I was able to make that clear, it's kind of hard to explain without code.· The latter example is used in a lot of different things, just implemented in various ways.· Oh, and before I get to relate it to the original question, you can test the status of the button before entering the loop, and when the flag counter reaches zero, and test the button status again.· If it hasn't changed, that's your debounce.




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Macgman2000Macgman2000 Posts: 59
    edited 2005-04-07 04:36
    I usually capture the button press between levels in a loop. IE, when I get a logic high I decrement a counter until it expires·if the key changed levels coming out of the counter·then it was a false trigger, if it has persistance then I look for a falling edge. It seems to work well, especially with cheap switches that have a lot of bounce.

    Nick
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-04-07 04:42
    Nick,

    ·· That does work.· But even for a short time, it does stop the code until the timer/counter expires.· That's not to say you shouldn't do that, but I just wanted to make sure the OP realizes the difference.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • dufflingduffling Posts: 73
    edited 2005-04-07 05:28
    ok .. i follow your idea there Chris .. thanks again .
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-04-07 21:16
    ·· Just remember, there is almost awlays more than one way to solve something.· Often when there are replies, if I feel there is another avenue worth mentioning, I will post it.· If not, I don't.· Take Care!



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2005-04-08 17:50
    Another way to do this, especially effective if you have to debounce multiple buttons or a keypad, is a state machine using a "vertical counter". There is a PBASIC example of this (easily translated to SX/B or assembly) at

    www.emesystems.com/BS2fsm.htm#keypad

    The state machine is executed as a short subroutine that your main program has to call on a regular schedule, say, ~10 millisecond intervals. The state variables hold the past states of the buttons in parallel. The current state and several past states of a button have to agree before it registers as really up or down,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2005-04-16 15:00
    Avoid the bounce with a hall effect switch, and have cleaner code.

    While there are several hardwire solutions, all require a Vcc and Vdd - so the hall effect switch remains just about as clean and compact as any.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    G. Herzog in Taiwan
Sign In or Register to comment.