CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

debouncing problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Twitie



Joined: 08 Nov 2004
Posts: 11

View user's profile Send private message

debouncing problem
PostPosted: Wed Nov 17, 2004 10:12 pm     Reply with quote

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)

View user's profile Send private message

PostPosted: Thu Nov 18, 2004 4:27 am     Reply with quote

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!

Cool
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Nov 18, 2004 5:55 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Nov 18, 2004 7:14 am     Reply with quote

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







PostPosted: Thu Nov 18, 2004 9:19 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Nov 18, 2004 11:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Nov 18, 2004 6:02 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 19, 2004 7:34 pm     Reply with quote

Got it~~!

thx a lot everyone! Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group