|
|
View previous topic :: View next topic |
Author |
Message |
whmeade Guest
|
Incorporating switch debounce |
Posted: Thu Jul 22, 2004 8:08 am |
|
|
I have a program that has a extensive menu system and uses 5 switches to get around the menu. I have coded what I thought would work for debouncing.
Code: |
do
{
If (!input(enter_btn)){ // wait for Enter Button to be pressed
while (!input(enter_btn)); // wait for Enter Button to be released
delay_ms(150); // delay 150 ms
process_menu(1);} // display menu
} While (true); // loop
|
This still isn't working as expected. I still get bouncing and the menu goes a little crazy. I have searched the forum and found the following example for debouncing a switch input in several locations.
Code: |
int1 C0;
signed int8 debounce_counter_C0;
if (input(PIN_C0))
{
if ( ++debounce_counter_C0 > 15 )
{
C0= 1;
debounce_counter_C0 = 15;
}else{
if ( --debounce_counter_C0 < -15 )
{
C0 = 0;
debounce_counter_C0 = -15;
}
}
}
|
How do I incorporate what I am trying to do above with the debounce code? I am not using C0, but that doesn't matter, I can change it to the actual pin I am using. I can find just about any answer to my questions in this forum, but need a little help with this one. Thanks for taking the time to read my post. |
|
|
Ttelmah Guest
|
Re: Incorporating switch debounce |
Posted: Thu Jul 22, 2004 8:36 am |
|
|
whmeade wrote: | I have a program that has a extensive menu system and uses 5 switches to get around the menu. I have coded what I thought would work for debouncing.
Code: |
do
{
If (!input(enter_btn)){ // wait for Enter Button to be pressed
while (!input(enter_btn)); // wait for Enter Button to be released
delay_ms(150); // delay 150 ms
process_menu(1);} // display menu
} While (true); // loop
|
This still isn't working as expected. I still get bouncing and the menu goes a little crazy. I have searched the forum and found the following example for debouncing a switch input in several locations.
Code: |
int1 C0;
signed int8 debounce_counter_C0;
if (input(PIN_C0))
{
if ( ++debounce_counter_C0 > 15 )
{
C0= 1;
debounce_counter_C0 = 15;
}else{
if ( --debounce_counter_C0 < -15 )
{
C0 = 0;
debounce_counter_C0 = -15;
}
}
}
|
How do I incorporate what I am trying to do above with the debounce code? I am not using C0, but that doesn't matter, I can change it to the actual pin I am using. I can find just about any answer to my questions in this forum, but need a little help with this one. Thanks for taking the time to read my post. |
Read the key.
If it is pressed, wait for a fixed time (say 1mSec). Then read the key again. If the key is still pressed, accept it, otherwise exit. Alternatively repeat the test at higher frequency, for a fixed number of times.
In your code, something like:
Code: |
in keycount;
keycount=0;
while (!input(enter_btn)) {
//Here key is pressed
delay_us(200);
if (input(enter_btn)) break;
else {
if (keycount++ == 8) {
//here key has been 'seen'
//Remember you must wait at the end for the button
//to be released, or code a 'flag' that the button has been
//seen, or if it continues to be held, it will be seen again.
}
}
}
|
Personally, Id do this type of scanning using a timer, rather than 'sitting waiting'. If (for instance), you have a timer triggered every 1/100th second, then you can check if the key is 'set' on the interrupt, and if it is present, set a counter to '2' say, then if it is still set, decrement the counter. If it is not set, you clear the counter. If the counter reaches '0', you know it has been pressed for 3/100th second, and treat the key as 'seen'.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
RE |
Posted: Sat Jul 31, 2004 10:52 pm |
|
|
Hi,
Set up an interrupt to check the button for changes, when the button is pressed set a flag, and when the interrupt occurs again, check whether the flag is set, finally clear the flag when button is released.
This way only one key press will be accepted, you also get a interrupt based time delay.
thanks
arun |
|
|
|
|
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
|