Steady

Button Debounce Code in C Exercise

Task

The button on PA0 bounces as it is pressed and released while your program runs. Report the button on USART1 for as long as the board runs: if a press has settled -> print PRESS; if a release has settled -> print RELEASE. The registers and bit fields are on the Datasheet tab.

Circuit

Block diagram: SW1 pushbutton from +3V3 into U1 STM32F4 pin PA0, R1 pulling PA0 to ground while the button is open, and pin PA9 carrying the USART1 report to TP1, a serial monitor

Pass condition

A level counts once it has lasted 20 ms. Anything shorter, and the level at reset, prints nothing. The lines are compared in order.

Expected output: PA0 bounces then settles high, and after 20 milliseconds of steady level the monitor prints PRESS; the same on the release prints RELEASE

Notes

The given code sets up USART1 and TIM2. The window is timed on TIM2, which counts microseconds and does not wrap during the run.

Related challenges

Loading the interactive arena.