Button debounce routines , and approach
duffling
Posts: 73
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
·
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
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video Display Module" Available Now.
www.sxvm.com
"A problem well defined, is a problem·half solved."
·
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.
·· 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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
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
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