Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 12:38 07 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : how to attach switch ?

Author Message
atmega8

Guru

Joined: 19/11/2013
Location: Germany
Posts: 724
Posted: 12:46am 12 Oct 2014
Copy link to clipboard 
Print this post

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: Germany
Posts: 724
Posted: 12:52am 12 Oct 2014
Copy link to clipboard 
Print this post

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: Germany
Posts: 724
Posted: 01:00am 12 Oct 2014
Copy link to clipboard 
Print this post

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: Germany
Posts: 724
Posted: 01:03am 12 Oct 2014
Copy link to clipboard 
Print this post

@ 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?

THXEdited by atmega8 2014-10-13
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 01:19am 12 Oct 2014
Copy link to clipboard 
Print this post

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.

Edited by TZAdvantage 2014-10-13
Microblocks. Build with logic.
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3282
Posted: 01:35am 12 Oct 2014
Copy link to clipboard 
Print this post

  atmega8 said   @ 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

Yes, that will be in the next version (including pull downs).
Geoff Graham - http://geoffg.net
 
atmega8

Guru

Joined: 19/11/2013
Location: Germany
Posts: 724
Posted: 02:51am 12 Oct 2014
Copy link to clipboard 
Print this post

  Geoffg said  
  atmega8 said   @ 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

Yes, that will be in the next version (including pull downs).


Thank you Geoff,

looking forward to the next Version, hope it will be soon ;-)
 
atmega8

Guru

Joined: 19/11/2013
Location: Germany
Posts: 724
Posted: 03:26am 12 Oct 2014
Copy link to clipboard 
Print this post

  TZAdvantage said  

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: Germany
Posts: 724
Posted: 03:32am 12 Oct 2014
Copy link to clipboard 
Print this post

OK, sorry....

changed Label (Label:) to Sub (SUB Label)

So it works !
 
Gizmo

Admin Group

Joined: 05/06/2004
Location: Australia
Posts: 5116
Posted: 12:05pm 12 Oct 2014
Copy link to clipboard 
Print this post

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 Zealand
Posts: 9590
Posted: 12:30pm 12 Oct 2014
Copy link to clipboard 
Print this post

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: Australia
Posts: 1329
Posted: 05:04pm 12 Oct 2014
Copy link to clipboard 
Print this post

  Gizmo said   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 )Glenn

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
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025