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.

switch debouncing

Status
Not open for further replies.

bigfarmerdave

New Member
A while back, I wrote a very simple, reliable switch debouncing routine similar to what I've posted below. Problem is, I cannot find or locate that code and would like to re-create it. What I've got below is close but not reliable. If I remember correctly, there should have been 4 groups of 2 IF,THEN,ENDIF groups. Can anyone lend a hand?

tester var bit
switch var byte

TRISA.5 = %1 'Sets TRISA.5 to an input
TRISC.5 = %0 'Sets TRISC.5 to an output

Tester = 0
switch = 0

LOOP:

if (PORTA.5 = 0) and (tester = 0) THEN
tester = 0
endif

if (PORTA.5 = 1) and (tester = 0) THEN
tester = 1
switch = switch + 1
endif

if (PORTA.5 = 1) AND (tester = 1) then
tester = 1
endif

if (PORTA.5 = 0) AND (tester = 1) then
tester = 0
endif

IF SWITCH > 1 THEN
SWITCH = 0
ENDIF
if switch = 0 then
LOW PORTC.5
ENDIF
IF switch = 1 then
HIGH PORTC.5
ENDIF

Goto loop
 
I'll tell you what happened - this probably worked fine twenty years ago. Now, the chips so dang fast it executes the decision tree a couple of times before it's done bouncing.

Got to put a delay in. If you poll the switches, that will take care of it for you.
 
Just a point - I don't recall in BASIC, but I know that in C = is the assignment operator, and == is the equality operator, so when you say:

Code:
if (PORTA.5 = 0) and (tester = 0) THEN

what you are doing is assigning 0 to PORTA.5 and 0 to tester. You might want to check, if it should be:

Code:
if (PORTA.5 == 0) and (tester == 0) THEN

As duffy says, it is common to delay about 20 milli-Seconds and if the contacts are closed, your switch is debounced.
 
Debounce is really very simple, if it was not pressed 10mS ago and is now pressed then we have a new key press.

Code:
previous var bit
tester   var bit

Loop:
	previous=tester				;keep old state
	DelaymS(10)				;delay for debounce
	tester=PORTA.5				;read key
	if(tester=1) AND (previous=0) THEN

                 ;this code will be executed each new key press.

	endif

If your key is active low then swap around the 1 and 0 in the if statement.

Mike.
 
Thanks guys! I put the delay in as you suggested and it's working great. My only concern is that if I have to put delays in for this thing and that thing throughout my program, it will begin to slow each cycle down. Maybe it's nothing to worry about? I'm still learning...
 
Thanks guys! I put the delay in as you suggested and it's working great. My only concern is that if I have to put delays in for this thing and that thing throughout my program, it will begin to slow each cycle down. Maybe it's nothing to worry about? I'm still learning...

You can debounce using a counter for each switch. When you detect a switch set its counter to N. Each time through the loop you subtract 1 from the counter instead of reading the switch. When the counter reaches 0 the switch should be done bouncing and you can start reading it again.

You can play with the code to see how large N needs to be.

I looks a bit like this which is in no actual language

Code:
N=10

counter0=counter1=counter2=0

do forever  // main loop
   if counter0 > 0
   then 
      counter0=counter0-1
   else
     if switch0 is pressed
     then counter0 = N

  if counter1 > 0
  then
    counter1=counter1-1
    ...
done
3v0
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top