|
|||||||||||||||||||||||||||||||||||||||||
This site uses Google Analytics to track visits.
Privacy Statement |
Switch DebouncingSwitch debouncing is one of those things you generally have to live with when playing with switches and digital circuits. If you want to input a manual switch signal into a digital circuit you'll need to debounce the signal so a single press doesn't appear like multiple presses. There are already a considerbale number of pages on this topic. So here's another!
• What is Switch Bounce?The left-hand image below shows a simple push switch with a pull-up resistor. The right hand image shows the trace at the output terminal, Vout, when the switch is pressed. As can be seen, pressing the switch does not provide a clean edge. If this signal was used as an input to a digital counter, for example, you'd get multiple counts rather than the expected single count. Note that the same can also occur on the release fo a switch. The problem is that the contacts within the switch don't make contact cleanly, but actually slightly 'bounce'. The bounce is quite slow, so you can recreate the trace, and the problem quite easily.
In the switch waveform the bouncing lasts for about 150us.
• A Switch Debouncer CircuitThere are many different approaches to cleaning up switch bounce. Below is a debouncing circuit. The basic idea is to use a capacitor to filter out any quick changes in the switch signal.
The circuit's operation can be explained by looking at the equivalent circuits formed in the two switch states, open and closed.
Starting with the switch open.
Now close the switch
But what about bounce conditions? If bounce occurs and there are short periods of switch closure or opening, the capacitor will stop the voltage at Vb immediately reaching Vcc or GND. Although, bouncing will cause slight charging and discharing of the capcitor, the hysteresis of the schmitt trigger input will stop the output from switching. What about the diode? Well the resistor R2 is required as a discharge path for the capacitor, without it, C1 will be shorted when the switch is closed. Without the diode, D1, both R1 and R2 would form the capacitor charge path when the switch is open. The combination of R1 and R2 would increase the capacitor charge time, slowing down the circuit. So, can't you just make R1 smaller? Ideally no, when the switch is closed, R1 is connected across the supply rails, so too small a resistor value would lead to unwanted wasted current.
• Software DebounceDebouncing a switch in software is very simple. The basic idea is to sample the switch signal at a regular interval and filter out any glitches. There are a couple of approaches to achieving this listed below. Both approaches assume a swtch circuit like that shown in the explanation of switch bounce: a simple push switch with a pull-up resistor. Approach 1The first approach uses a counter to time how long the switch signal has been low. If the signal has been low continuously for a set amount of time, then it is considered pressed and stable. 1 Setup a counter variable, initialise to zero. 2 Setup a regular sampling event, perhaps using a timer. Use a period of about 1ms. 3 On a sample event: 4 if switch signal is high then 5 Reset the counter varaible to zero 6 Set internal switch state to released 7 else 8 Increment the counter variable to a maximum of 10 9 end if 10 if counter=10 then 11 Set internal switch state to pressed 12 end if Approach 2The second approach is similar to the first, but uses a shift register instead of a counter. The algorithm assumes an unsigned 8bit register value, such as that found it 8-bit microcontrollers. 1 Setup a variable to act as a shift register, initialise it to xFF. 2 Setup a regular sampling event, perhaps using a timer. Use a period of about 1ms. 3 On a sample event: 4 Shift the variable towards the most significant bit 5 Set the least significant bit to the current switch value 6 if shift register val=0 then 7 Set internal switch state to pressed 8 else 9 Set internal switch state to released 10 end if
• Digital Switch DebouncingDigital debouncing is achieved in basically the same way as the software approaches shown above. However, they are implemented in dedicated hardware.
• Switch Debouncing ICsFor such a common problem, you'd think there would be a plethora of ICs for performing switch debounce. However, there don't seem to be that many. Below is a list of those I've found so far. Unfortunately, one is not easily aquired, the other is expensive.
|
||||||||||||||||||||||||||||||||||||||||