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

Button debounce function - how long?

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



Joined: 02 Feb 2004
Posts: 17

View user's profile Send private message

Button debounce function - how long?
PostPosted: Tue May 25, 2004 2:01 pm     Reply with quote

I have seen this routine posted several places for debouncing a pin. Its supposed to be a lot faster than using delays to wait for the pin to debounce.
If I was checking my buttons every 10ms (using a timer), will this code work for me- or should I only use it on an interrupt-on-change type button? Any comments on this routine would be great. Thanks.
Code:

int1 B2;
   signed int8 debounce_counter_B2;

   if (input(PIN_B2)) {
      if ( ++debounce_counter_B2 > 15 ) {
         B2 = 1;
         debounce_counter_B2 = 15;
      } else {
         if ( --debounce_counter_B2 < -15 ) {
            B2 = 0;
            debounce_counter_B2 = -15;
         }
      }
   }
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Tue May 25, 2004 2:42 pm     Reply with quote

This one compiles better but operates on the same idea.

Code:
   
   int1 Overload_Alarm;
   int8 debounce_counter_OVERLOAD;

   if(PIN_OVERLOAD)                                         // Test the inputs status at this time
   {  if(bit_test(++debounce_counter_OVERLOAD,CounterSize)) // Aproach and test for true status
      {  --debounce_counter_OVERLOAD;                       // Remove counter overflow state
         Overload_Alarm=1;                                  // Set true status
      }                                                     // Make adjustments to the tested bit
   }                                                        // to change the counter size
   else                                                     // The input was false
   {  if(--debounce_counter_OVERLOAD==0)                    // Aproach and test for false status
      {  ++debounce_counter_OVERLOAD;                       // Remove counter from zero state
         Overload_Alarm=0;                                  // Set false status
      }
   }


I suggest CounterSize = 4; to debounce in 6 passes (60mS at 10mS per pass)
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