Page 1 of 1

Button debouncing

PostPosted: Thu Apr 05, 2018 1:42 am
by ric
I received this via PM:
Frankur wrote:Hi Ric, sorry about my english, that's because i'm from Mexico, i checked some your post in the microchip forum, i'm very interested in this
''My recommendation would be to not use the pin change interrupts at all.
Rather, run a timer interrupt at around 100Hz.
Scan all your buttons on each interrupt.
Whenever one changes, set a counter related to that button to a low value, say 3 to 5.
Each following interrupt, decrement the counter if the button hasn't changed.
When you decrement the counter from 1 to 0, treat that as a new state for that button.''

I would like if you could copy and paste the code you use to debounce your buttons so i can understand it and adapt it to my proyect.
It will be so cool if you could to that please. Thank you.

Frankur, please don't ask questions via PM.
Everyone benefits (and everyone can contribute), when questions and answers are in public.
How many buttons do you have, and how are they connected? (i.e. are they multiplexed?)
What exact PIC device are you using?

Re: Button debouncing

PostPosted: Thu Apr 05, 2018 3:25 am
by AussieSusan
Also there are already a number of code examples out on the internet (including https://hackaday.com/2010/11/09/debounc ... -them-all/ which has a number of them).
Susan

Re: Button debouncing

PostPosted: Thu Apr 05, 2018 9:52 am
by Roche
There is a superb article here http://www.ganssle.com/debouncing.htm which I have found very useful.

Re: Button debouncing

PostPosted: Sat Apr 07, 2018 4:06 am
by Frankur
ric wrote:I received this via PM:
Frankur wrote:Hi Ric, sorry about my english, that's because i'm from Mexico, i checked some your post in the microchip forum, i'm very interested in this
''My recommendation would be to not use the pin change interrupts at all.
Rather, run a timer interrupt at around 100Hz.
Scan all your buttons on each interrupt.
Whenever one changes, set a counter related to that button to a low value, say 3 to 5.
Each following interrupt, decrement the counter if the button hasn't changed.
When you decrement the counter from 1 to 0, treat that as a new state for that button.''

I would like if you could copy and paste the code you use to debounce your buttons so i can understand it and adapt it to my proyect.
It will be so cool if you could to that please. Thank you.

Frankur, please don't ask questions via PM.
Everyone benefits (and everyone can contribute), when questions and answers are in public.
How many buttons do you have, and how are they connected? (i.e. are they multiplexed?)
What exact PIC device are you using?


Sorry for that, is just one button, i'm using the PIC16f887

Re: Button debouncing

PostPosted: Mon Apr 09, 2018 8:24 am
by ric
When you see the button change, initialise a variable to how many loops you want to see it stable for.
(i.e. "3 to 5" in the post you quoted)
Then each time around the loop, if the button has the same state as last time, AND if the variable is non zero, decrement the variable.
If as a result of the decrement, the value becomes zero, you have just detected a new stable keystroke.
Set a flag, or whatever, to tell your software a new keystroke is available.

At the end of the ISR, save the current state of the switch to compare against next time around.