View previous topic :: View next topic |
Author |
Message |
Twitie
Joined: 08 Nov 2004 Posts: 11
|
debouncing problem |
Posted: Wed Nov 17, 2004 10:12 pm |
|
|
Hello all~
I am doing a project implementing button RBO ext interrupt. Now I am facing debouncing problem.
Can anyone show me how to write the algorithm of debouncing?
Thx a lot!! |
|
|
YulL
Joined: 29 Sep 2004 Posts: 39 Location: Paris (France)
|
|
Posted: Thu Nov 18, 2004 4:27 am |
|
|
Yes it is a well known problem... I think the use of a state machine should be appropriate for that.
Unfortunatly I haven't sample code to post... Hope it helps!
|
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Thu Nov 18, 2004 5:55 am |
|
|
There are lots of ways to do this. One way I commonly use is have a timer interrupt every 20ms. The interrupt is responsible for maintaining three variables and setting a flag when a new debounced value is available. The three variables are the debounced value, the previous reading, the current reading. The main line is responsible for clearing the flag. debounced always contains the last "debounced" reading.
here is an crude pseudo code example for debouncing an entire port at once.
Code: |
//......
// here in the timer interrupt handler
current = input_a();
if (last == current)
{
if (debouced != last)
{
new_key = true;
debounced = current;
}
}
else
last = current; |
_________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Last edited by asmallri on Thu Nov 18, 2004 9:08 am; edited 1 time in total |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Nov 18, 2004 7:14 am |
|
|
Quote: | I think the use of a state machine should be appropriate for that. |
A state machine is not the solution for the problem.
Basically you read the value over a period of time. If the value doesn't change over that period it is considered debounced. If it does change, then keep reading until it doesn't. |
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 18, 2004 9:19 am |
|
|
Mark wrote: | Quote: | I think the use of a state machine should be appropriate for that. |
A state machine is not the solution for the problem.
Basically you read the value over a period of time. If the value doesn't change over that period it is considered debounced. If it does change, then keep reading until it doesn't. |
A state machine, _is_ 'a solution to the problem', if used in conjunction with a timer interrupt as the poster says. In the timer interrupt, in the first 'state', you look for the signal changing to the active level. If it has, you advance the state, and exit the interrupt. On the next timer interrupt, in state 2, you check that it is still at the active level. If it is, you advance to state 3, otherwise you drop back to state 0. On state 3, you check again for it being at the active level, and if so, trigger the 'key detected' output. If not you drop back to state 1.
A state machine, is an efficient way to do this (involving only a branch on state, and a single test, in each interrupt), for a system doing other jobs, where you do not want to sit 'polling' the key.
It is by no means the 'easiest' route to code, nor is it perhaps really applicable if you are using the RB0 interrupt on change. In this situation, the change detection, implies a key has changed state. If it has, I'd trigger a timer interrupt to occur in a number of mSec. When the timer interrupt triggers if the key is still true, you could consider it 'debounced'.
Best Wishes |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Nov 18, 2004 11:23 am |
|
|
Quote: | A state machine is not the solution for the problem.
|
Let me revise that to say that it is not the "best" solution for the problem. State machines can be used for all sorts of things but a simplier solution is to have a debounce counter. The counter is incremented each time the state remains active. If it hits the threshold then consider it debounced. If it goes back inactive, then reset it back to zero. I usually create a task that reads the input periodically and then evaluates the changes. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Nov 18, 2004 6:02 pm |
|
|
The subject of debouncing has been discussed many times on this forum.
A favourite is this small debounce rountine from Neutone. |
|
|
Twitie
Joined: 08 Nov 2004 Posts: 11
|
|
Posted: Fri Nov 19, 2004 7:34 pm |
|
|
Got it~~!
thx a lot everyone! |
|
|
|