10

Do you have a simple debounce routine handy to deal with a single switch input?

This is a simple bare metal system without any OS.

I would like to avoid a looping construct with a specific count, as the processor speed might fluctuate.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Benoit
  • 37,894
  • 24
  • 81
  • 116

11 Answers11

11

I think you could learn a lot about this here: http://www.ganssle.com/debouncing.pdf

Your best bet is always to do this in hardware if possible, but there are some thoughts on software in there as well.

Simple example code from TFA:

#define CHECK_MSEC 5 // Read hardware every 5 msec
#define PRESS_MSEC 10 // Stable time before registering pressed
#define RELEASE_MSEC 100 // Stable time before registering released
// This function reads the key state from the hardware.
extern bool_t RawKeyPressed();
// This holds the debounced state of the key.
bool_t DebouncedKeyPress = false;
// Service routine called every CHECK_MSEC to
// debounce both edges
void DebounceSwitch1(bool_t *Key_changed, bool_t *Key_pressed)
{
    static uint8_t Count = RELEASE_MSEC / CHECK_MSEC;
    bool_t RawState;
    *Key_changed = false;
    *Key_pressed = DebouncedKeyPress;
    RawState = RawKeyPressed();
    if (RawState == DebouncedKeyPress) {
        // Set the timer which allows a change from current state.
        if (DebouncedKeyPress) Count = RELEASE_MSEC / CHECK_MSEC;
        else Count = PRESS_MSEC / CHECK_MSEC;
    } else {
        // Key has changed - wait for new state to become stable.
        if (--Count == 0) {
            // Timer expired - accept the change.
            DebouncedKeyPress = RawState;
            *Key_changed=true;
            *Key_pressed=DebouncedKeyPress;
            // And reset the timer.
            if (DebouncedKeyPress) Count = RELEASE_MSEC / CHECK_MSEC;
            else Count = PRESS_MSEC / CHECK_MSEC;
        }
    }

}

chepseskaf
  • 664
  • 2
  • 12
  • 41
GEOCHET
  • 21,119
  • 15
  • 74
  • 98
  • Actually found it about 10 minutes after asking the question. Quite Handy. I agree with the HW solution...if only... – Benoit Sep 30 '08 at 22:21
11

Simplest solutions are often the best, and I've found that simply only reading the switch state every N millseconds (between 10 and 50, depending on switches) has always worked for me.

I've stripped out broken and complex debounce routines and replaced them with a simple slow poll, and the results have always been good enough that way.

To implement it, you'll need a simple periodic timer interrupt on your system (assuming no RTOS support), but if you're used to programming it at the bare metal, that shouldn't be difficult to arrange.

Note that this simple approach adds a delay to detection of the change in state. If a switch takes T ms to reach a new steady state, and it's polled every X ms, then the worst case delay for detecting the press is T+X ms. Your polling interval X must be larger than the worst-case bounce time T.

Roddy
  • 66,617
  • 42
  • 165
  • 277
  • I agree that a steady period works perfectly OK - I often use my display refresh cycle for debouncing "since I'm there" and that works fine. You're missing the actual "debounce logic" here, though - unless you always trigger on a state change, which simply won't work every time. – Tom Swirly Mar 11 '13 at 17:33
  • 3
    @Tom, the whole point is there *is* no debounce logic: You don't need it. Switch 'bounce' is really just an artefact caused by sampling a mechanical contact clusure faster than it was designed to operate. By dropping the sample rate down, you sidestep the problem. (and yes, video refresh frequency is spot on for most good switches) – Roddy Mar 11 '13 at 17:55
  • Very interesting, I hadn't thought of it that way. After some scribbling, I believe that you're provably correct IF the polling period is between twice the longest debounce cycle from your hardware and half the length of the shortest button press that you'll accept, as long as you're willing to accept a delay of (occasionally) up to two full polling cycles. (For a screen refresh rate of 60Hz, that implies a bounce cycle of <9ms for example.) I must have been grumpy, I marked you down - and apparently I can't change this. :-( If you change your answer slightly, I can mark it up again. – Tom Swirly Mar 13 '13 at 16:45
  • Great! Hmm, I have a suboptimal definition of "bounce time" that's not as good as yours. Perhaps a better name for the concept is "time to stability" - how long after a state change is the signal stable? - which corresponds to your idea of it. – Tom Swirly Mar 14 '13 at 17:23
2

There's no single simple solution that works for all types of buttons. No matter what someone here tells you to use, you'll have to try it with your hardware, and see how well it works. And look at the signals on a scope, to make sure you really know what's going on. Rich B's link to the pdf looks like a good place to start.

KeyserSoze
  • 2,501
  • 1
  • 16
  • 17
2

I have used a majority vote method to debounce an input. I set up a simple three state shift register type of data structure, and shift each sample and take the best two out of three as the "correct" value. This is obviously a function of either your interrupt handler, or a poller, depending on what method is used to actually read the hardware.

But, the best advice is to ask your friendly hardware designer to "latch" the value and allow you to clear this value when you get to it.

gthuffman
  • 311
  • 2
  • 3
  • That doesn't seem right to me. Surely the order they come in is important? For example, if your shift register says Up Down Up, surely you are "in the middle of a bounce" and you need to wait one more period? – Tom Swirly Mar 11 '13 at 17:34
1

To debounce, you want to ignore any switch up that lasts under a certain threshold. You can set a hardware timer on switch up, or use a flag set via periodic interrupt.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

If you can get away with it, the best solution in hardware is to have the switch have two distinct states with no state between. That is, use a SPDT switch, with each pole feeding either the R or S lines of a flip/flop. Wired that way, the output of the flip/flop should be debounced.

nsayer
  • 16,925
  • 3
  • 33
  • 51
  • I agree that HW would be better but that's not in the dice. – Benoit Sep 30 '08 at 22:22
  • If it's something a person is switching, you almost always need to debounce it, even if there's hardware debouncing. People's hands shake, they use not enough or too much force on the button, or crap has gotten into the button - they can *mechanically* create bounce that way, which your SPDT will then faithfully report. Hardware debouncing is great, however, and if nothing else, lets you keep your bounce time much smaller and get a more responsive keyboard. – Tom Swirly Mar 11 '13 at 17:37
0

What I usually do is have three or so variables the width of the input register. Every poll, usually from an interrupt, shift the values up one to make way for the new sample. Then I have a debounced variable formed by setting the logical-and of the samples, and clearing the inverse logical-or. i.e. (untested, from memory)

input3 = input2;
input2 = input1;
input1 = (*PORTA);

debounced |= input1 & input2 & input3;
debounced &= (input1 | input2 | input3);

Here's an example:

debounced has xxxx (where 'x' is "whatever")

input1 = 0110,
input2 = 1100,
input3 = 0100

With the information above,

We need to switch only bit 2 to 1, and bit 0 to 0. The rest are still "bouncing".

debounced |= (0100); //set only bit 2
debounced &= (1110); //clear only bit 0

The result is that now debounced = x1x0

JACH
  • 996
  • 11
  • 20
Justin Love
  • 4,397
  • 25
  • 36
  • I found your algorithm interesting (close to what I'm looking for). I edited it to remove the negation '~', which I think was incorrect – JACH Feb 10 '17 at 04:32
0

The algorithm from ganssle.com could have a bug in it. I have the impression the following line

static uint8_t Count = RELEASE_MSEC / CHECK_MSEC;

should read

static uint8_t Count = PRESS_MSEC / CHECK_MSEC;

in order to debounce correctly the initial press.

sorin
  • 29
  • 2
0

At the hardware level the basic debouncing routine has to take into account the following segments of a physical key's (or switch's) behavior:

Key sitting quietly->finger touches key and begins pushing down->key reaches bottom of travel and finger holds it there->finger begins releasing key and spring pushes key back up->finger releases key and key vibrates a bit until it quiesces

All of these stages involve 2 pieces of metal scraping and rubbing and bumping against each other, jiggling the voltage up and down from 0 to maximum over periods of milliseconds, so there is electrical noise every step of the way:

(1) Noise while the key is not being touched, caused by environmental issues like humidity, vibration, temperature changes, etc. causing voltage changes in the key contacts

(2) Noise caused as the key is being pressed down

(3) Noise as the key is being held down

(4) Noise as the key is being released

(5) Noise as the key vibrates after being released

Here's the algorithm by which we basically guess that the key is being pressed by a person:

read the state of the key, which can be "might be pressed", "definitely is pressed", "definitely is not pressed", "might not be pressed" (we're never really sure)

loop while key "might be" pressed (if dealing with hardware, this is a voltage sample greater than some threshold value), until is is "definitely not" pressed (lower than the threshold voltage) (this is initialization, waiting for noise to quiesce, definition of "might be" and "definitely not" is dependent on specific application)

loop while key is "definitely not" pressed, until key "might be" pressed

when key "might be" pressed, begin looping and sampling the state of the key, and keep track of how long the key "might be" pressed - if the key goes back to "might not be" or "definitely is not" pressed state before a certain amount of time, restart the procedure - at a certain time (number of milliseconds) that you have chosen (usually through experimenting with different values) you decide that the sample value is no longer caused by noise, but is very likely caused by the key actually being held down by a human finger and you return the value "pressed"


while(keyvalue = maybepressed){
//loop - wait for transition to notpressed
sample keyvalue here;
maybe require it to be "notpressed" a number of times before you assume
it's really notpressed;
}
while(keyvalue = notpressed){
//loop - wait for transition to maybepressed
sample keyvalue
again, maybe require a "maybepressed" value a number of times before you 
transition
}
while(keyvalue=maybepressed){
  presstime+=1;
  if presstime>required_presstime return pressed_affirmative
  }
}
return pressed_negative
0

use integration and you'll be a happy camper. Works well for all switches.

just increment a counter when read as high and decrement it when read as low and when the integrator reaches a limit (upper or lower) call the state (high or low).

Keith
  • 1
0

The whole concept is described well by Jack Ganssle. His solution posted as an answer to the original question is very good, but I find part of it not so clear how does it work.

There are three main ways how to deal with switch bouncing: - using polling - using interrupts - combination of interrupts and pooling.

As I deal mostly with embedded systems that are low-power or tend to be low-power so the answer from Keith to integrate is very reasonable to me.

If you work with SPST push button type switch with one mechanically stable position then I would prefer the solution which works using a combination of interrupt and pooling.
Like this: use GPIO input interrupt to detect first edge (falling or rising, the opposite direction of un-actuated switch state). Under GPIO input ISR set flag about detection.
Use another interrupt for measuring time (ie. general purpose timer or SysTick) to count milliseconds. On every SysTick increment (1 ms):
IF buttonFlag is true then call function to poll the state of push button (polling).
Do this for N consecutive SysTick increments then clear the flag.

When you poll the button state use logic as you wish to decide button state like M consecutive readings same, average more than Z, count if the state, last X readings the same, etc.

I think this approach should benefit from responsiveness on interrupt and lower power usage as there will be no button polling after N SysTick increments. There are no complicated interrupt modifications between various interrupts so the program code should be fairly simple and readable.

Take into consideration things like: do you need to "release" button, do you need to detect long press and do you need action on button release. I don't like button action on button release, but some solutions work that way.

Mayur Karmur
  • 2,119
  • 14
  • 35
msimunic
  • 1
  • 1