Yahoo Groups archive

AVR-Chat

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

Message

Re: [AVR-Chat] ATtiny2313 button handler

2009-11-03 by Marc R.J. Brevoort

Hi Erik,

> Who ever came up with this orignally was a frickin' genius.
> Sure, it's for PIC but the code is in C.
> A little helpful cut-n-paste to give you the basic idea.
>
> ;static unsigned clock_A,clock_B,debounced_state;
> ;unsigned debounce(unsigned new_sample)
> ;{
> ;  unsigned delta;
> ;  unsigned changes;
> ;
> ;  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.
> ;
> ;  changes = ~(~delta | clock_A | clock_B);
> ;  debounced_state ^= changes;
> ;
> ;  return changes;
> ;} /* debounce */
> ;
>
> The cool thing is that this can debounce a whole port at once.  It
> runs in an interrupt routine.

Thanks. I've got a single button debounce by now, but this one is 
quite nice due to its multi-input nature. I've saved it to my favs for 
future reference.

Best,
Marc

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.