Stopwatch

STM32 Timer Pulse Measurement Exercise

Task

The bench presses and releases SW1 on PA0 while your program runs. For as long as the board runs, time every press with TIM2 ticking once a microsecond (PSC = 15). The registers and bit fields are on the Datasheet tab.

Circuit

Block diagram: SW1 button from +3V3 into U1 STM32F4 pin PA0, R1 pulling PA0 to ground while the button is open, TIM2 inside U1 fed by a 16 MHz timer clock, and pin PA9 carrying the USART1 report to TP1, a serial monitor

Pass condition

If SW1 is released -> print how long it was held in whole ms, rounded down: "23 ms". Until then -> print nothing. TIM2 must tick once a microsecond from reset.

Expected output for the public test: SW1 held from 1 to 24.3 ms, 26 to 38.4 ms and 40 to 77.2 ms; at each release the serial monitor prints how long it was held, 23 ms, 12 ms and 37 ms, and nothing before the first release

Notes

TIM2 is clocked at 16 MHz. The given code sets up USART1 and printing. Your program is built and run at -O0, then at -O2. Both must pass.

Related challenges

Loading the interactive arena.