![]() |
Forum Index : Microcontroller and PC projects : how to attach switch ?
Author | Message | ||||
atmega8![]() Guru ![]() Joined: 19/11/2013 Location: GermanyPosts: 724 |
Hi, simple , hope not to stupid, question. Pin 7 is connected via a 10K Resistor to VCC. The Switch pulls it down to low. Is this the best way to avoid bounces? I get lots of them ;-(( How can i debounce in Software? Codesample? setpin 7, intl, keypress Do Loop keypress: print "keypress" count = count +1 print count ireturn |
||||
atmega8![]() Guru ![]() Joined: 19/11/2013 Location: GermanyPosts: 724 |
Ok, did it this way: setpin 7, intl, keypress Do Loop keypress: pause 20 if pin(7) = 0 then count = count +1 print count print "keypress" endif ireturn |
||||
atmega8![]() Guru ![]() Joined: 19/11/2013 Location: GermanyPosts: 724 |
Again, using a pause in an Interrupt causes some headache to me. But as Geoff explained in another Thread: "The pause command constantly checks for an interrupt condition and if one has occurred it will execute the interrupt routine. On return from the interrupt the pause commend will continue with the pause but any time spent in the interrupt routine will be subtracted from the pause time so that the pause would be the same with or without the interrupt. You can also use pause in an interrupt routine although that cannot be interrupted because you are already in an interrupt." ...the pause within the Interrupt Service Routine will miss other Interrupts. As i remember this Interrupt is completely lost?! So you have to decide if this will be a Problem in your code... |
||||
atmega8![]() Guru ![]() Joined: 19/11/2013 Location: GermanyPosts: 724 |
@ Geoff, seems that you have not implemented to use the PIC-internal pullups. In my case this will save me a resistor;-) Will you implement it in a next release? THX |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
If you want to eliminate bounce, add a capacitor. So a resistor from Vcc to one side of the switch and a capacitor from that same side to GND. Then the other side of the switch to GND. When the switch is pressed this will cause the capacitor to dump its charge to gnd giving you and active low. it stays low until the switch is released AND the capacitor is charged via the resistor. This will effectively giving you a delay on the logic level of the pin that is used for the switch. A debounce! No need to fix it in software. You can but it gets more tricky. My personal thinking is that if it is easy and cheap to solve in hardware do that. Unless of course there are strict cost or space restrictions. It is important to spend as little time in a interrupt routine as possible. Also look at the SUB/END SUB structure to replace using labels and the use of ireturn. Consider the use of a label and iReturn as deprecated. Microblocks. Build with logic. |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3282 |
Yes, that will be in the next version (including pull downs). Geoff Graham - http://geoffg.net |
||||
atmega8![]() Guru ![]() Joined: 19/11/2013 Location: GermanyPosts: 724 |
Thank you Geoff, looking forward to the next Version, hope it will be soon ;-) |
||||
atmega8![]() Guru ![]() Joined: 19/11/2013 Location: GermanyPosts: 724 |
Also look at the SUB/END SUB structure to replace using labels and the use of ireturn. Consider the use of a label and iReturn as deprecated. if i use end Sub or Exit Sub on a settick Interrupt i get this error: [43] Exit Sub Error: Nothing to return to > Can you help me with tis ? |
||||
atmega8![]() Guru ![]() Joined: 19/11/2013 Location: GermanyPosts: 724 |
OK, sorry.... changed Label (Label:) to Sub (SUB Label) So it works ! |
||||
Gizmo![]() Admin Group ![]() Joined: 05/06/2004 Location: AustraliaPosts: 5116 |
Interesting article about debouncing switches. The circuit in fig 3 seams to be pretty good, and only 3 extra components. Also goes into software solutions ( in C ) Its a 333k pdf. http://www.eng.utah.edu/~cs5780/debouncing.pdf Glenn The best time to plant a tree was twenty years ago, the second best time is right now. JAQ |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9590 |
I always put a 1k in series with the IO pin - copied from the PICAXE method. It limits current into or out of the IO pin, and has become my default wiring for all switches and button inputs, including relay contacts etc. Not sure if this is strictly needed with the uM, but I do it anyway, as that is what I was used to with PICAXE, and the theory is still good, even though I am on another device. Smoke makes things work. When the smoke gets out, it stops! |
||||
paceman Guru ![]() Joined: 07/10/2011 Location: AustraliaPosts: 1329 |
Interesting article Glenn. Just checked the price of those MC14490 chips (with six de-bounce circuits). Element14 still want $10 each for them which probably makes them unrealistic, but e-Bay has them for closer to $2 if you can trust the quality. Greg |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |