Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
04/11/06 15:16
Read: times


 
#114103 - excellent debounce for bad switches
Responding to: ???'s previous message
Erik Malund said:
using the 1 msec debounce timer
this must be excellent switches, I debounce with 10ms.
Erik

I use cheap switches, and also this excellent debounce function. I use in all my microcontroller projects and put it here for people to put in their bag of tricks. I call it every 1 msec interval while it can work for more (slower) or less (faster) with excellent results.
It returns debounced switch falling edge or rising edge.
/**************************************************************************
* NAME:        debounce                                                   *
* DESCRIPTION: The purpose of this routine is to debounce, i.e. digitally *
*              low pass filter inputs. The algorithm handles upto 8  bits *
*              at a time. An input is considered filtered if it has not   *
*              changed states in the last 4 samples.                      *
*              2-bit cyclic vertical counters count the 4 samples. As     *
*              long as there is no change, the counters are held in the   *
*              reset state of 00b. When a change is detected between the  *
*              current sample and the filtered or debounced sample, the   *
*              counters are incremented. The counting sequence is         *
*              00,01,10,11,00... When the counters roll over from         *
*              11b to 00b, the debounced state is updated. If the input   *
*              changes back to the filtered state while the counters are  *
*              counting, then the counters are re-initialized to the      *
*              reset state and the filtered state is unaffected.          *
*              In other words, a glitch or transient input has been       *
*              filtered.                                                  *
*              The 2-bit counters are arranged "vertically". In other     *
*              words 8 counters are formed with 2 bytes such that the     *
*              corresponding bits in the bytes are paired (e.g. MSBit of  *
*              each byte is paired to form one counter).                  *
*              The counting sequence is 0,1,2,3,0,1,... And the state     *
*              tables and Karnaugh maps are:                              *
*                                                                         *
*              State Table:     Karnaugh Maps:                            *
*              pres  next      B                                          *
*              SS  SS         0   1                                       *
*              AB  AB       +---+---+    +---+---+                        *
*             --------   A 0|   | 1 |    | 1 |   |                        *
*              00  01       +---+---+    +---+---+                        *
*              01  10      1| 1 |   |    | 1 |   |                        *
*              10  11       +---+---+    +---+---+                        *
*              11  00      A+ = A ^ B     B+ = ~B                         *
*                                                                         *
* PLATFORM:    AT89S8252                                                  *
* OS:          None.                                                      *
* DATE:        25th April 2004                                            *
* REF:         http://www.dattalo.com/technical...unce.html*   
**************************************************************************/
void debounce(unsigned char new_sample)
{
    unsigned char delta;
    delta = new_sample ^ debounced_state;   //Find all of the changes
    clock_A ^= clock_B;                     //Increment the counters
    clock_B  = ~clock_B;
    clock_A &= delta;                       //Reset the counters if no 
    clock_B &= delta;                       // changes were detected.
    //Preserve the state of those bits that are being filtered and 
    // simultaneously clear the states of those bits that are already filtered.
    debounced_state &= (clock_A | clock_B);
    //Re-write the bits that are already filtered.
    debounced_state |= (~(clock_A | clock_B) & new_sample);
    // debounced_state is the filtered version of new_sample
}








List of 17 messages in thread
TopicAuthorDate
LCD repeat function in C            01/01/70 00:00      
   First thought            01/01/70 00:00      
      if its roundrobin            01/01/70 00:00      
         last post            01/01/70 00:00      
      Sounds fine            01/01/70 00:00      
   the one issue            01/01/70 00:00      
      stuck key            01/01/70 00:00      
      Yes            01/01/70 00:00      
         Code Review            01/01/70 00:00      
            this must be excellent switches            01/01/70 00:00      
               excellent debounce for bad switches            01/01/70 00:00      
   better code version            01/01/70 00:00      
      no comments, captals, no indenting            01/01/70 00:00      
         It could do with some improvement!            01/01/70 00:00      
            I did not comment on that, it may very w            01/01/70 00:00      
            true....            01/01/70 00:00      
               led to so much misinformation in this fo            01/01/70 00:00      

Back to Subject List