Button debouncing

(instructions, reset, WDT, specifications...) PIC12F6xx, PIC16Fxxx, PIC16F6x, PIC16F7x

Button debouncing

Postby ric » Thu Apr 05, 2018 1:42 am

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?
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Button debouncing

Postby AussieSusan » Thu Apr 05, 2018 3:25 am

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
AussieSusan
Verified identity
 
Posts: 173
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Button debouncing

Postby Roche » Thu Apr 05, 2018 9:52 am

There is a superb article here http://www.ganssle.com/debouncing.htm which I have found very useful.
Roche
 
Posts: 72
Joined: Fri Jul 11, 2014 12:35 pm
PIC experience: Professional 5+ years with MCHP products

Re: Button debouncing

Postby Frankur » Sat Apr 07, 2018 4:06 am

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
Frankur
 
Posts: 1
Joined: Tue Apr 03, 2018 5:59 am
PIC experience: EE Student

Re: Button debouncing

Postby ric » Mon Apr 09, 2018 8:24 am

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.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products


Return to 14-Bit Core

Who is online

Users browsing this forum: No registered users and 5 guests

cron