[ Log In ]
3 pin slide switch

SPDT Slide Switch 3 pin 30V

$1.49
Qty:
Tactile momentary push button switch 6 mm x 6 mm (through hole)
LED button tactile switch

LED Button Tactile Switch

$1.89
Qty:
thumbnail: 8 channel logic analyzer - iLogic

4 Channel Logic Analyzer

$149.00
Qty:
Thumbnail: quad buffer line driver 74HC126E

Quad Buffer Line Driver (Through Hole)

$0.69
Qty:
Image of the Atmega324p

Atmega324P

$8.50
Qty:
Serial to USB converter with Micro USB cable

USB to Serial Converter

$10.95
Qty:
Thumbnail: Crystal Oscillator 18.432 MHz for UART

18.432 MHz Crystal Oscillator 18pf 30ppm

$0.94
Qty:
Thumbnail: 22 pF Capacitor

22 pF Multilayer Ceramic Capacitor

$0.43
Qty:
Thumbnail: Quartz crystal oscillator - 16 MHz

16 MHz Crystal Oscillator 20 pF Through Hole

$0.75
Qty:
Skip Navigation Links

Microcontroller - A Beginners Guide - Button Debouncing Through Software

Ok, so now your saying to yourself, we JUST went over debouncing, and it seemed to work fine... Why go deeper into this subject?!? Well, basically, software debouncing, if the program space and microcontroller cycles will allow it, is essentially free. It's just a few lines of code, and you can provide much more control of how the debouncing methods work with the particular button you are using. Debouncing in hardware may add a cost to each board that is developed, and it is more difficult to determine a good debouncing for all the push button switches that will be used. However, if you want to preserve program execution cycles, it's best to go the hardware route.

My method of debouncing with software uses only two variables that measure the confidence level of the actual button press. With the help of the Pressed variable introduced in the Button Debouncing, there will be a stream of 1's when the button is pressed, and a stream of 0's when the button is released. If there is bouncing going on, the stream of 1's or 0's will be very short, so we can take advantage of this by introducing two variables that measure the length of these streams. The variables are called, Pressed_Confidence_Level, to measure the button's pressed state, and Released_Confidence_Level to measure the button's released state.

So, if the button is pressed, the Pressed_Confidence_Level will rise, and the same with Released_Confidence_Level when the button is released. But, these variables will also become reset to 0 if the opposite condition exists. For instance, say the button was pressed for a time and the Pressed_Confidence_Level became a very large number, like 153,356. If the button was released (or a bouncing happened), the variable would be reset to 0. The trick to these variables is to determine a good threshold to determine a good button press or release state. Say, if the Pressed_Confidence_Level shows that after rising to 500, that this number s a strong indication of a button press, then the LEDs will toggle once. The same goes for the Released_Confidence_Level because bouncing could also happen on a button release. So, let's see how we do this in code:

int main(void)
{
DDRB |= 1 << PINB0; //For Notes on what these actions mean
PORTB ^= 1 << PINB0;
DDRB |= 1 << PINB2;
DDRB &= ~(1 << PINB1);
PORTB |= 1 << PINB1;

int Pressed = 0;
int Pressed_Confidence_Level = 0; //Measure button press cofidence
int Released_Confidence_Level = 0; //Measure button release confidence

while (1)
{
if (bit_is_clear(PINB, 1))
{
Pressed_Confidence_Level ++; //Increase Pressed Conficence
Released_Confidence_Level = 0; //Reset released button confidence since there is a button press
if (Pressed_Confidence_Level >500) //Indicator of good button press
{
if (Pressed == 0)
{
PORTB ^= 1 << PINB0;
PORTB ^= 1 << PINB2;
Pressed = 1;
}
//Zero it so a new pressed condition can be evaluated
Pressed_Confidence_Level = 0;
}
}
else
{
Released_Confidence_Level ++; //This works just like the pressed
Pressed_Confidence_Level = 0; //Reset pressed button confidence since the button is released
if (Released_Confidence_Level >500
{
Pressed = 0;
Released_Confidence_Level = 0;
}
}
}
}