'Best routine for debouncing a switch'
|Cesare Lavazza wrote:
> (A newbie question)
> I need a very small routine for debouncing a switch.
> The PIC model is 16C84. Thanks.
You need 1 byte an 1 bit of RAM. Call it DebounceCount (byte)
and Input_1_Debounced (bit).
Use timer 0 to interrupt every 20ms or so. In the interrupt
routine, check to see if the input is at the state you want.
If it is not, clear counter DebounceCount. If the input is
at the state you want, increment DebounceCount. Check now to
see if DebounceCount is greater than, say 15. If it is not,
just jump out of the interrupt service routine. If DebounceCount
is greater than 15, set flag Input_1_Debounced.
In your main routine, you only have to check if Input_1_Debounced
It may seem like more work than scan-wait-scan methods, but it
will simplify your main code considerably.
PS. If RAM is at a premium, you can use one of the bits of
DebounceCount, say bit 4, as the flag.
Tjaart van der Walt
| Another sun-deprived R&D Engineer slaving away in a dungeon |
| WASP International http://wasp.co.za |
| GSM and GPS value-added applications |
| Voice : +27-(0)11-622-8686 | Fax : +27-(0)11-622-8973 |
If you need a *very* small program to debounce a switch, how about none
Could you use a couple of resistors, a cap, and an (optional) inverter to
do an analog debouce rather than a software one?
| --- C1
| 0 --- .1uFd
| | |\ Any left over NAND gate, etc.
SW1 -| |--------------| >O-------[OUTPUT>
N.O. | | |/ Of course a Schmitt Trigger
| 0 / R1 would give the best results
| \ 470K
In fact, the inverter is a luxury that you may be able to do without.
The values of the components can be altered to give you the amount of
charge/discharge that you want. The values in my (poor) ASCII art are
nothing special. Just some from an old project that seemed to work OK.
Just remember that, when the switch is closed, R2 is across your power
supply, so make it large enough to not burden Vcc.
When the way of the Tao is forgotten, kindness and ethics must be taught.
Men must learn to pretend to be wise and good. -- Lao Tzu
More... (looser matching)
- Last day of these posts
- In 1997
, 1998 only
- New search...