PDA

View Full Version : Switch Debounce using a 10F chip



modifyit
- 20th March 2006, 18:32
I know this is a simple question, but I am so used to using the "Button" command I can't think of the correct method for debouncing a switch.

The pic10F206 chip I am using does not seem to like the "Button" command. I'm guessing that is because of the reduced instruction set with the 10F.

The price of the 10F chips makes them hard to pass up for some projects.

modifyit
- 20th March 2006, 18:56
by the way right now my switch is wired in with the pin pulled down to ground through a 10k resistor. Please excuse my poor ASCII art below:



+5v_____/ switch__________pic input
.............................|
.............................|_____10k resistor______GND

Dwayne
- 20th March 2006, 19:43
Debouncing is just the matter of putting a little "Pause" in the program. This little pause will allow accidental "double pushing", "electrical connections", that are iffy, and other uncontrollable human factors to be "softened" out.

Loop:

if GPIO.1 = 0 Not pushed goto Loop (or use some kind of interupt)
pauseus 1000 (Pause 100 microseconds)
while GPIO.1=1 endwhile (Button released)
Do your program stuff here
goto Loop


You can adjust your pauseus to whatever fits your need to your project.
When you use a interput, you dont want to interupt a bunch of times, so you can "debounce" the button, so that the interput only works 1 time ever 1000 microseconds.

Dwayne

modifyit
- 20th March 2006, 20:47
Thanks for the response, I am still a little confused though…does this code snippet basically do what you are suggesting? This code should toggle the state of an LED when I press the switch and not be effected if the switch is held down.




SW var GPIO.1
LED1 var GPIO.2
OnOff var bit
Swset var bit

Startup:
Led1=0
OnOff=0
GOTO Main

Main:
…….
…..do regular code stuff
……
if OnOFF=1 then
led1=1
else
led1=0
endif
GOSUB Debounce:
GOTO Main

Debounce:
If SW=0 then
Swset=0 ;used to determine if switch is held down
RETURN ;if switch is not pressed return to main
Endif
If swset=1 then RETURN ;this returns to the main loop if SW is held down
If SW=1 then PAUSEUS 100 ;if switch is pressed pause 100us
IF SW=0 then RETRN ;if switch is now not pressed return to main
IF SW=1 then
OnOff=OnOff+1 ;if switch is still pressed toggle OnOff
Swset=1 ;used to determine if switch is held down
Endif
RETURN

Dwayne
- 20th March 2006, 23:18
SW var GPIO.1
1. LED1 var GPIO.2
2. OnOff var bit
3. Swset var bit

4. Startup:
5. Led1=0
6. OnOff=0
7. GOTO Main

8. Main:
9…….
10…..do regular code stuff
11……
12. if OnOFF=1 then
led1=1
else
led1=0
endif
13. GOSUB Debounce:
14. GOTO Main

Debounce:
If SW=0 then
Swset=0 ;used to determine if switch is held down
RETURN ;if switch is not pressed return to main
Endif
If swset=1 then RETURN ;this returns to the main loop if SW is held down
If SW=1 then PAUSEUS 100 ;if switch is pressed pause 100us
IF SW=0 then RETRN ;if switch is now not pressed return to main
IF SW=1 then
OnOff=OnOff+1 ;if switch is still pressed toggle OnOff
Swset=1 ;used to determine if switch is held down
Endif
RETURN


Ok, Lets look at your code. The First goto Main (Line 7) is worthless... your program will automatically fall through to the beginning.

Lets Look at something similar...

SW var GPIO.1
LED1 var GPIO.2
OnOff var bit
Swset var bit

Startup:
Led1=0
OnOff=0

Main:
…….
…..do regular code stuff
……
if SW=1 then
led1=1
else
led1=0
endif

Pauseus 1000
Goto Main

Looking at this, as long as OnOff button is pushed (On or equal to 1) your LED will stay on. (assuming you are pulling the OnOff pin to ground, and when you push the button, you are at 5 volts).

Another way...............

SW var GPIO.1
LED1 var GPIO.2
OnOff var bit
Swset var bit

Startup:
Led1=0
OnOff=0

Main:
…….
…..do regular code stuff
……
if SW=1 then
Toggle Switchset
pauseus 100 ; debounce timing
endif

If SwitchSet=1 then
led=1
else
led=0
endif

Goto Main

This one, will keep the LED on until you press the switch the second time.



Realize, that I have not tested these programs, but it will give you an idea...
Also, make sure you correctly assign the GPIO pins and turn off things like comparitors... else your program will not work!

Dwayne

modifyit
- 21st March 2006, 03:26
I'm not sure why, but when I try your second method I do not get reliable switch debounce, or my "OnOff" variable is constantly toggling keeping the result from being reliable.

I tried my overkill code and it works like a champ, so I'm going to go with it now.

I appreciate the help, and I understand why your code method should work, but I can't get it reliable.

Bruce
- 21st March 2006, 04:10
The pin you have your switch on is by default an analog comparator input on
power up. Do you have CMCON0.3=0 in your program somewhere, and just
forgot to show it in your example?

modifyit
- 21st March 2006, 05:22
yeah, I've got CMCON0=%00000001 set at the top of the code.

Thanks