Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] How To Debounce a Switch 10 ms edge triggered....

2005-09-04 by erikc

Venkat Nagappan wrote:
> Dear Friends kindly send me codes to debounce a switch
> 
> assembly language avr program is needed...,
> 
> kindly help m eout friends
> 
> 
> venkattttttttttttttt
> 

Excuse my enthusiasm, but I came across this trick a few years back; it 
is incredibly elegant, and is now just about the only debounce routine I 
use for mechanical switches.

[1] It debounces all the bits on a port at the same time.
[2] It is inline code with no loops or branches, hence it operates in 
constant time.
[3] It will basically work on anything.  I have used it on 8051, 68HC11, 
  80x86, and just recently, AVR.

but, here's the caveat:

[4] You DO have to either call it from a timer interrupt or on a polling 
basis.

Just hold your noses and look at this PIC page.  ;-)
http://www.dattalo.com/technical/software/pic/debounce.html

Here is the "C" code taken from there:

====begin code====
static unsigned clock_A,clock_B,debounced_state;
debounce(unsigned new_sample)
{
   unsigned 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 changes
   clock_B &= delta;         //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);
}
=====end code=====

Read the cited web page for an explanation.


erikc
--
"The kind of man who wants the government to adopt and enforce his
ideas is always the kind of man whose ideas are idiotic."
    -- H. L. Mencken

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.