Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Any simple debounce code?

Status
Not open for further replies.

NewGeek

New Member
Can somebody help me add some simple debounce code to this. Ive got a switch on PORTA, that will break out of this sub when pressed. If possible, can I keep the debounce code within this sub? (PIC16F84a)
This needs to be as simple as possible.

Nigel I cant get on to your site right now, otherwise Id try there first.

Im sure you experts will find lots of other elementary problems with my code to. Thanks in advance.


Code:
IntSub

     bcf	PORTA, 0
     btfsc	PORTA, 0 
     return
     goto	IntSub
 
Just reduce the rate you're polling porta to about 10-20 times a second and key bounce should not be problem.
 
You may or may not be able to debounce within the routine. It depends on what comes before and after it. First off, you need to be sure that the code inside this routine will not trigger on a bounce from a previous hit.

Be aware the switch bounces on both push and release, and you don't know how long the user's going to hold that button down. It may be 50mS and it may be 3 sec. If you only debounce after it falls, then they release it 1 sec later, you will get a false negative edge.

Generally I code that a button press is responded to immediately, but any change is then disregarded for the next 50mS or so, then disregarded while the line remains low, then disregarded for another 50mS. You can use a tmr interrupt to decrement a counter. Or if most of the rest of the code is just looping, you could decrement inside the code. Exactly how much is not critical, 50mS to 500mS is pretty typical.
 
One thing that helps is using a pin that has a Smitt Trigger(ST) input, usualy port C.

Use portB's interrupt on change, read the port, wait x ms's check agin, and compare.

Other things that matter is what type of switch is used.

Kent
 
kentken said:
One thing that helps is using a pin that has a Smitt Trigger(ST) input, usualy port C.

How would a schmitt trigger help button debouncing?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top