Let's start off with learning how to program the uC from the beginning .
1. Adding Button support using board BSP API.
In order to use the button on the board which is labelled as 'user', i found it in the user manual. And the schematic of it was as follows.
· When the button is pressed, PA0 is connected to ground.
· If not, it is by-default connected to Vdd.
· Question: Why this cap in between?
Let's understand the concept of Key Debouncing
As we press the button, the pin connected to it should read 0V immediately. In real scenario, it does not! It bounces between 5V and 0V upto some time and then goes to constant 0V. This problem can not be neglected. Why this happens?
As we push the button, like any other physical collision (Conservation of Momentum), the button bounces back a little and does so for a little amount of time. This is analogous to the an example of releasing the ball that we are holding in our hands. It goes to stationary state after bouncing a little.
This debouncing occurs for some milliseconds, which should not be a problem, isn't it? NO, our microproccessor work on MHz or Ghz, which is around 1 million operations in one millisecond.
One hardware solution for Switch debouncing for a SPDT (Single Pole Double Throw) switch is using SR(Bar) latch {SR Latch is made up using NOR Gates, and SR(Bar) is made of NAND Gates}. As follows :-
We are looking into the SPDT in this case
Discussed Below
Solution for Floating Point Problem :
Actual Output contains bouncing. We will use a debouncing circuit to remove this.
Current State of input is 0
0,1
As we press the switch, it moves to a state where output becomes 1,0
Even if the bouncing occurs, state 1,1 remembers 1,0 state and our output remains Bouncing Free
This remembers the 1,0 even in the bouncing state
Yellow being the output and Red being the input
This completes the section for switch debouncing using SR(Bar) latch. Can the debounce circuit be more simpler and cost-effective?
The cap charges and discharges slowly that removes the high freq components from the circuit which in our case is noise due to Bouncing.
The choice of R1,R2 and C values is a matter of great concern over here. As we can see, the charging circuit equivalent resistance is R1+R2 while for discharging it is R2. Charging Time constant is the time taken to charge the capacitance which in our case is (R1+R2.) Similarly discharging time constant is time taken to discharge the capacitor which in our case is (R2)*C.
To avoid the problem of not being able to discharge is solved by taking R1>>R2, which ensures rapid discharge. As the frequency of switching in our case is not too high, 100 ms time constant for discharging should suffice. With R1=1k ohms, we get the cap value to be 10uF. Let's compare by putting the yellow probe to switch and red probe to the capacitor.
Hysteresis is a very interesting concept in Basic Electronics. Here, from a real life example, if we were to make an oven that maintains 200 temperature. In case 1, we'll end up switching rapidly while in case 2, switching would be lesser.
The hysteresis loop is what that solves our problem here. When the Vin to schmitt trigger is 0, the Vout is high (this is an inverting schmitt trigger), as we carry along, the vout will remain high till the time Vin becomes Vt+. Then it becomes low and remains there till the time we decrease the voltage to Vt-. The quantity Vt+ - Vt- is called Hysteresis Voltage Vh . This makes the output remains const for a longer range which removes the slope due to capacitance. Trade off being just a little delay.
Expected Signal
Displaying noisy button on red probe and Schmitt output on yellow probe