Timekeeper
STM32 Timer Delay Exercise
Task
Anything that blinks a light or takes a reading on a steady beat needs a clock it can trust. A counting loop won't do, because its beat changes whenever the code around it changes. A hardware timer counts the chip's own clock, so its beat stays exact.
Set up TIM2 to update every 20 ms, and toggle the LED on PA5 on every update for as long as the board runs. On the logic analyzer, PA5 should look like this: low until 20 ms, then a change every 20 ms: high at 20, low at 40, high at 60 TIM2's registers are on the Datasheet tab.
Circuit
Pass condition
PA5 toggles every 20 ms, first at 20 ms after reset. TIM2 divides by exactly 16000 and then by exactly 20.
Notes
TIM2 runs on a 16 MHz clock. Divide it by 16000 and it counts once a millisecond. Update every 20 counts and it updates every 20 ms. An update is the moment the count wraps back to 0.