
/**************************************************************************
* 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/software/pic/debounce.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
}
