please point me in the direction of some debounce code

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

Can some one gimme a link to some debounce code. im sure there should be some available here. but could not find. i confess i have not searched properly.

I have a single switch. (The type that is used for domestic house appliances). This switch seems to be bouncing a lot.

I have tryed using delays. but not too sure how to make a debounce routine.

Thanks Rodney

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

Add the word "danni" to you search for "debounce"

(Oh and I guess you read Ganssle already?)

EDIT: For example here's one thread where Peter's code is presented:

https://www.avrfreaks.net/index.p...

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

I use something along the lines of the technique described here:
http://www.dattalo.com/technical...

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

I know you're asking for code, but this might be a good time to try writing one yourself. There are lots of ways to proceed, but I like this common one:

Give yourself a regular timer interrupt, maybe 1 ms for example, and a global volatile unsigned int. Initialize it to some garbage value, maybe alternating ones and zeroes.

On every interrupt, read your switch's pin and convert it down to a 0 or 1. Shift the int left one and OR in this value.

Then in the main routine you can check this int any time you want to know the value of the switch. If it's zero, the switch is 0 (whether that is open or closed depends on your circuitry). If it's 0xffff, the switch is 1. Anything else means the switch is in transition.

This example gives a 16 ms debounce which is probably excessive. You can change the time interval by changing the timer frequency, and/or by masking the int either in the ISR or before checking (that is, looking at fewer than 16 bits).

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

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

I second the vote for Danni's code.

It is somewhat intricate, but is both compact and fast, does eight switches if you need it, and will do auto-repeat. It needs a timer tick, but you can hook it up to a timer that you already use for other things.

I used it for a toy clock that ran up to four clocks (RTC or stopwatch or timer) with 1/100 s resolution and the debouncing, all driven by one AVR timer/counter. Works like a charm!

Get a clean sheet of paper and go though what happens in the algorithm. After that you will be more than proficient in bitwise operations too!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"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

Quote:

I second the vote for Danni's code.

Geez I thought that I proved beyond any reasonable doubt that my approach was like 1 cycle faster and 1 byte of SRAM less than danni's.

[Actually I don't remember who "won", but the horizontal vs. vertical approach was like that close. the thread was a challenge that debouncing could not be done in "a few microseconds", and both danni & I responded.]

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Lee Theusch :

Quote:
[Actually I don't remember who "won", but the horizontal vs. vertical approach was like that close. the thread was a challenge that debouncing could not be done in "a few microseconds", and both danni & I responded.]

I tried searching AVRfreaks "danni AND theusch AND debounce" got no hits. Tried "danni AND theusch" got only one thread of March 2005 not connected with debouncing. Please may I humbly request the URL for the quoted reference. Many thanks for the same.

India_AVR

-----------------------------------------
Wonderful world of "0"s & "1"s
-----------------------------------------

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

danni has posted his code several times. One version of mine is in
https://www.avrfreaks.net/index.p...
linked from
https://www.avrfreaks.net/index.p...

There is one thread that I cannot find where danni and I went back-and-forth byte-counting, but I cannot quickly search it out.

Also follow the links in
https://www.avrfreaks.net/index.p...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Lee Theusch :
Many thanks to your kind self for posting the urls.
India_AVR

-----------------------------------------
Wonderful world of "0"s & "1"s
-----------------------------------------