I describe the need for software debouncing in this video. An explanation is given to explain bouncing as it pertains for push buttons (momentary switches). Pseudo code is written to show how the ARM Microcontroller will process the debouncing and ignore the voltage swings due to the mechanical behavior of the push button.
Arduino has moved their chipset to powerful advanced chips like the ARM microcontroller architecture, so following these tutorials will you greatly is you are a follower of Arduino, especially if you are on the track to become a serious developer for production. Learning the bare chip method as shown in my videos and content will prepart you for more cost effective and efficient production rather than starting with Arduino which is geared towards one off projects.
In this video, we're going to make an LED toggle on and off every time we fully depress and release a push button. We will add a bit of complexity to our simple GPIO input for push buttons. Because the push button is a mechanical component, we are going to have some problems because, mechanically, the signal is going to bounce up and down. So, we need to take the bouncing out and only consider the pure high and low values that would be created from the push button when connected to a pin on the STM32 ARM Microcontroller.
Push button switches are mechanical components. Within this push button switch, you have leads that have contacts on the leads, and then there is a conductive area on the plunger of the push button that, when depressed, makes contact with the two leads and creates a circuit flowing between the two leads. This action causes problems electrically because mechanical actions happen much slower than electrical current flow.
You can have a scenario where a high signal (or a 1 in code) will become a low signal when the push button is depressed (of the push button is connected to ground, or the low signal when the two leads are connected). Of course, than can happen in reverse where the signal starts off low and then goes high when pressed (which is how we have our circuit connected.
What is happening; however, when you are pressing a push button, it starts low and when the push button is pressed, the voltage may bounce high and low several times before it stabilizes on the high signal. In software, while the pin is being read, this will be apparent and the pin will show many highs and lows.
Our intention is to eliminate the portion that will be bouncing high and low (1 and 0). Since we have an LED that is being toggled when the push button is pressed, we don't want the LED to toggle more than one time on a single push button press. This will happen if the microcontroller reads the bouncing portion. Imagine how frustrating it would be to press the power button on your TV remote and your TV turns on then off again unpredictably.
To eliminate the bouncing, we will use a counter when the push button is pressed and there is a stable high or low signal (many 1s or 0s in a row). We want to know if the push button is being pressed with a stable high signal and if the push button is not being pressed with a stable low signal. We don't want to count the unstable stuff and that stuff will have low counts.
If you look at code online, you will notice other programmers will implement a delay from the point in time that the push button is pressed. The delay is to theoretically pass the bouncing. There are a couple of reasons why I don't like this approach. First, code cannot be executed in a delay since a delay is just a loop of empty cycles from the microcontroller. And second, the time delay is only testing two points in time. The two points could still be some erroneous bouncing samples.
Alternatively, I would like the program test if the input pin is a one or a zero. If it is a one, it will test it again in the next cycle if it is a one again. If it is a zero, then the button pressed state is eliminated as not pressed. This process is done over and over until there is enough samples that the program can say, I am confident that the button is pressed. This works also vice versa for the state if the button is not pressed where it tests for a number of zero samples. The number of samples will be a variable that is called the confidence threshold. This idea is very similar to a state machine where the program cycles through operations and collects data as it goes and responds to that data.
So, specifically, how do we do this? We're measuring a zero on the pin and then we'll have some bouncing going on, then we'll end up on a high signal level (a one). We are going to have e a few variables we will be working with. the first variable is the most obvious, ButtonPressed. This variable will be equal to either a zero or a one telling me that if the button is pressed or not. The ButtonPressed variable will be a one if there are sufficient samples of ones. If there are sufficient low signal level samples (zeros), then the ButtonPressed Variable will be zero.
For this, we will need to introduce another variable, ButtonPressedConfidenceLevel and ButtonReleasedConfidenceLevel. What we will be doing is counting up the ButtonPressedConfidenceLevel when it matches a high level or a one and keep doing this until it reaches a confidence threshold, at which point the ButtonPressed variable can be changed to a one. There is an important consideration we need to implement. We need to be counting up the confidence level variables on when the opposite button state is present. For instance, the ButtonPressedConfidenceLevel will only be counting up if the ButtonPressed varaible is zero. It doesn't make sense to count up the ButtonPressedConfidenceLevel when the ButtonPressed is already equal to one. This is also true for the other state when counting up the ButtonReleasedConfidenceLevel. When this ButtonPressed is equal to one, then we can toggle the output pin (toggle the LED).
Before we start, you will need to review the previous program (the simple GPIO Input) where we do simple polling on the input pin and the LED (output pin) is turned on when the input pin reads a one. The first modification we will do is remove the output LED pin code. We will put that code back in when we implement the LED output pin toggle.
Let's start with some pseudo-code.
In the while loop, this is what we should have:
// If the button is pressed (IDR - input data register)
if (GPIOB->IDR & GPIO_IDR_1)
{
}
else
{
}
If the condition is true, then the push button is pressed. In the code block that will be executed when true, we need to increase the Button Pressed Confidence Level variable. Then the LEDs can be toggled and the Button Pressed variable can be changed to a one. Remember that all of this part o the code should not be executed unless the Button Pressed variable is zero. Why would we need to do these tests to change the button Pressed variable if that variable is already one.
When a zero is coming into the input pin, we want to increase the Button Released Confidence Level. then when the button Released Confidence Level is greater than the confidence threshold, then the button pressed variable can be changed to zero.
// If the button is pressed (IDR - input data register)
if (GPIOB->IDR & GPIO_IDR_1)
{
if (/*button pressed variable is 0*/)
// Increase the Button Pressed Confidence Level
// Toggle LEDs once the Button Pressed Confidence has passed the confidence threshold
// Update the Button Pressed variable to 1
}
else
{
if (/*button pressed variable is 1*/)
// Increase the button Released Confidence Level
// Once the button Release Confidence Level had been achieved
// Update the Button Pressed variable to 0
}
}
All of the variables that are being updated in the while loop must be in the main function, outside of the while loop so it doesn't keep re-initializing.
// Initialize Button Pressed Variable
// Initialize Button Pressed Confidence Level
// Initialize Button Released Confidence Level
01. Arduino for Production!! Introduction to ARM Microcontrollers
02. Arduino for Production!! How to Instal and Set up the Arduino IDE (Integrated Development Environment) for the ARM Microcontroller
03. Arduino for Production!! How to Connect the ST-Link v2 ARM Programmer to your Computer
04. Arduino for Production!! How to Use the CoIDE (Adruino IDE) for ARM Microcontroller Development
05. Arduino for Production!! How to Connect the ST-Link v2 to the ARM STM32 Microcontroller
06. Arduino for Production!! How to Output to a Pin to Blink an LED on the ARM Microcontroller Part 1
07. Arduino for Production!! How to Output to a Pin to Blink an LED on the ARM Microcontroller Part 2
08. Arduino for Production!! How to Output to a Pin to Blink an LED on an ARM Microcontroller Part 3
09. Arduino for Production!! Can Not Connect to Target! How to Establish a Connection Again.
10. Arduino for Production!! How to Receive Input from a Pin for Push Button Input (GPIO) on the ARM Microcontroller
11. Arduino for Production!! How to Receive Push Button Input on the ARM Microcontroller Part 2
12. Arduino for Production!! How to Receive Stable GPIO Push Button Input on the ARM Microcontroller - Software Debouncing Part 1
13. Arduino for Production!! How to Receive Stable GPIO PUSH Button Input onthe ARM Microcontroller - Software Debouncing Part 2
14. Arduino for Production - How to Establish Software Debouncing on the ARM Microcontroller Exclusive
15. Arduino for Production!! How to Interface an LCD on the ARM Microcontroller Part 1
16. Arduino for Production!! How to Interface an LCD on the ARM Microcontroller Part 2
17. Arduino for Production!! How to Interface an LCD to an ARM Microcontroller Part 3
18. Arduino for Production!! How to Interface an LCD to the ARM Microcontroller Part 4
STM32 Microcontroller Experiments Livestream 5