[TUT][SOFT][HARD] Button debouncing in software

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This tutorial is about debouncing button's in software. The final described method isn't really invented by me as it ends up to be about the same as Peter Dannegger's (avr freak user danni) excellent debounce code. I start with a simple method usable for one button and try to describe the steps needed to generalize it to a solution that only uses logic operations, which have the benefit that you can debounce up to a whole port of buttons at about the same cost as the one button method. Recommended reading before you jump into this is Ganssle's text about debouncing and Bit manipulation (AKA "Programming 101") in this forum. Edit: Some small corrections and added the zip-file that was lost in the migration to the new forum.

Attachment(s): 

Last Edited: Thu. Feb 5, 2015 - 08:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thnx 4 that good illustration

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You're welcome! I'm glad that someone found it useful.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just had a quick look but that PDF looks really nice, Snigelen!

For a walkthrough of 'danni's debounce code, see this: http://www.avrfreaks.net/index.p...

Aside: What did you use to format and create the PDF?

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
I just had a quick look but that PDF looks really nice, Snigelen!
Thanks!
Quote:
For a walkthrough of 'danni's debounce code, see this: http://www.avrfreaks.net/index.p...
I have missed that thread, I'll check it out.
Quote:
Aside: What did you use to format and create the PDF?
It's written in LaTeX, with the listings package for source code.

Btw, I've updated the pdf in the first post (again), minor modifications and some spelcheckign.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

suppose we want to check debounce condition on PORTC of atmega 8 where a switch is connected on PC0 then following code can be used successfully .

 
if(PINC&(1<<0)
{
_delay_ms(5);
if(PINC&(1<<0)
{
whatever you want to do ,please do here;
like count++;
}

:)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you kidding? If I fix the bugs in that snippet and run it in a loop then push a button and release it as fast as I can, count++ will be executed at least 16 times.

If you read the tutorial you'll hopefully see that there is more than one purpose of this. One is the bounce filtering and the other one is to keep track of the state of the buttons. You can hold a button for a minute if you want, it still only counts as one button press. And the program can take action on the press event and then continue to do other things while the button is still pressed. No delays needed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No my friend ,have u check it for count++..it vl work successfully

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You think it works but it only samples the input twice. You want to sample as many times as possible to lessen the statistical risk.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Snigelen,

 

First off - thanks for the great tutorial!

 

Second - I hope this isn't against forum etiquette to bring this back from the dead, but I think its worth noting the initial "bad example" which the rest of the tutorial is built off of has a slight typo. In your while loop, to toggle the LED on and off, you're xor-ing PORTB with the LED attached to it against the BUTTON_MASK,

 

if(~BUTTON_PIN & BUTTON_MASK){
    // Toggle the LED
    PORTB ^= BUTTON_MASK;
}

 

but I think you should XOR PORTB with the LED_MASK like so to toggle the LED on and off. The code in the tutorial works as is, but this becomes apparent the case where the LED and button are on different pin numbers

 

        if (~BUTTON_PIN & BUTTON_MASK){
            // Toggle the LED
            LED_PORT ^= LED_MASK;   //LED_PORT = PORTB
        }

 

 

Hope this helps somebody who may stumble upon this like I have!

Last Edited: Thu. Jan 8, 2015 - 08:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

 

Thanks for the correction. I've updated the pdf file in the first post (and added the example files in debounce.zip that was lost in the migration to the new forum).