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 support@ccsinfo.com

Incorporating switch debounce

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







Incorporating switch debounce
PostPosted: Thu Jul 22, 2004 8:08 am     Reply with quote

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
PostPosted: Thu Jul 22, 2004 8:36 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jul 22, 2004 2:17 pm     Reply with quote

Here is code for the simple type of debouncing that you were
trying to do. I tested it just now, and it works OK with a
push-button switch. This is really intended for just one button.
http://www.ccsinfo.com/forum/viewtopic.php?t=19874

This other post shows a rough outline of how to poll switches
every 10 ms. It doesn't show everything that's needed
in the debounce routine.
http://www.ccsinfo.com/forum/viewtopic.php?t=17189
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

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

RE
PostPosted: Sat Jul 31, 2004 10:52 pm     Reply with quote

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
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