SA said:
The best way to debounce a switch is to use a form C switch, and
cross-connected logic gates to create a Set-Reset Flip-Flop.
In your case, you have only a form A switch. Your pullup resistor is
something like 5K. (5v/1mA)
I'm assuming that the voltage across the switch goes into a CMOS input
with a logic threshold of ~2V.
Most switches "make" multiple times for durations of 10 to 250us or so
with "breaks" of durations of ~100us of when pushed.
Attached is an LTSpice simulation which uses an RC network to establish
some relevant time constants which filter out multiple transitions. This
is not 100%; but is better than nothing. Run the simulation and look at
Node S.
If you have a better guess at the switch bounce behavior, then you can
edit the PWL. The 47 Ohm resistor is there to limit the capacitor
discharge current to prevent damaging the switch.
MikeM, PhD EE
--------- Cut and paste into a file called DeBounce.asc -----------
Version 4
SHEET 1 2164 680
WIRE -176 240 -176 416
WIRE -176 416 -80 416
WIRE 96 416 96 304
WIRE -128 64 -176 64
WIRE -176 64 -176 160
WIRE -48 64 -16 64
WIRE 96 64 96 96
WIRE 96 176 96 224
WIRE -16 384 -16 416
WIRE -16 416 96 416
WIRE 48 240 -80 240
WIRE -80 240 -80 416
WIRE -80 416 -16 416
WIRE -16 304 -16 288
WIRE -16 288 0 288
WIRE 96 64 144 64
WIRE -16 64 96 64
WIRE -16 416 -16 448
WIRE -16 96 -16 64
WIRE -16 160 -16 192
WIRE 224 -32 96 -32
WIRE 96 -32 96 64
WIRE 288 -32 432 -32
WIRE 0 288 48 288
FLAG 144 64 s
IOPIN 144 64 Out
FLAG -16 192 0
FLAG -16 448 0
FLAG 432 -32 B
IOPIN 432 -32 Out
FLAG 0 288 c
SYMBOL res 80 80 R0
SYMATTR InstName R1
SYMATTR Value 47
SYMBOL Misc\\battery -176 144 R0
WINDOW 123 0 0 Left 0
WINDOW 39 0 0 Left 0
SYMATTR InstName V1
SYMATTR Value 5
SYMBOL sw 96 208 R0
SYMATTR InstName S1
SYMATTR Value msw
SYMBOL voltage -16 288 R0
WINDOW 3 126 68 Left 0
WINDOW 123 0 0 Left 0
WINDOW 39 0 0 Left 0
SYMATTR Value PWL(0 0 99u 0 100u 1 200u 1 205u 0 308u 0 310u 1 510u 1
515u 0 718u 0 720u 1 822u 1 825u 0 928u 0 930u 1 1200u 1 1205u 0 1310u 0
1330u 1 1425u 0 14000u 1)
SYMATTR InstName V2
SYMBOL res -32 48 R90
WINDOW 0 0 56 VBottom 0
WINDOW 3 32 56 VTop 0
SYMATTR InstName R2
SYMATTR Value 5k
SYMBOL cap -32 96 R0
SYMATTR InstName C1
SYMATTR Value 0.22µ
SYMBOL Digital\\buf1 224 -96 R0
WINDOW 3 22 98 Left 0
SYMATTR InstName A1
SYMATTR Value Vhigh=5
TEXT 214 402 Left 0 !.tran 3m
TEXT 176 312 Left 0 !.model msw SW(Ron=0.1 Roff=100Meg Vt=0.5)
TEXT 224 64 Left 0 ;Switch Debounce simulation
TEXT 224 112 Left 0 ;Note: R2 is the pullup\n R1 limits
discharge current\n Node S goes to the CMOS input