Datasheet

PICkit™ 3 Debug Express
DS41370C-page 30 © 2009 Microchip Technology Inc.
3.4.2 Switch Debouncing
Mechanical switches are frequently encountered in embedded processor applications,
and are inexpensive, simple and reliable. However, such switches are also often very
electrically noisy. This noise is known as switch bounce, whereby the connection
between the switch contacts makes and breaks several, perhaps even hundreds, of
times before settling to the final switch state. This can cause a single switch push to be
detected as several distinct switch pushes by a fast device, especially with an edge-
sensitive input. Think of advancing the TV channel, but instead of getting the next
channel, the selection skips ahead two or three.
Classic solutions to switch bounce involved filtering out the fast switch bounce
transitions with a resistor-capacitor circuit, or using re-settable logic shift registers.
While effective, these methods add additional cost and increase circuit board real
estate. Debouncing a switch in software eliminates these issues.
A simple way to debounce a switch is to sample the switch until the signal is stable.
How long to sample requires some investigation of the switch characteristics, but
usually 5ms is sufficiently long.
This lesson code demonstrates sampling the switch input every 1mS, waiting for 5
consecutive samples of the same value before determining that the switch was
pressed. Note that the switch on the 44-Pin Demo Board doesn’t bounce much, but it
is good practice to debounce all system switches.
FIGURE 3-21: SWITCH DEBOUNCING PROGRAM FLOW
3.4.3 Exploring the Lesson 4 Source Code
Open the lesson source code file 04 Switch Input.c in an editor window if it is not
open already.