STM32F407 Timer Interrupts
Posted: Thu Oct 19, 2023 11:48 am
I'm having issues with STM32F407VET6 and Timer Interrupts on a professional project.
So, I created very simple code leaving nothing to the imagination, using an STM32F407 board conveniently equipped with LEDs on A6 and A7.
The program has a repeating main loop with 1s delays between A7 high and A7 low, and correctly cycles a full on/off every 2s.
Additionally for compare, I added a timer interrupt, with prescaler 1:256 and rollover of 31250 to fire once every second, so the LED on A6 intentionally replicates the flash speed of the LED on A7. The panel reveals the crystal frequency as the timer source clock.
Over time, the two LEDs can naturally be expected to slowly lose sync due to the additional processing clock cycles applicable to the LED on A7. However, the LED driven by timer interrupt (port pin A6) is running at an observed 4x the speed of the LED driven by the loop (port pin A7).
The MCU's PLL multiplier/prescaler/post-scaler registers are all configured in accordance with ST's CubeMX clock configurator, and experimental changes to them indicates that register settings declared in Flowcode are indeed being set.
It would appear that the timer clocks are actually driven from one of the bus clocks instead of the crystal clock - i.e. TMR1-7 referencing APB1 (reportedly 42MHz in CubeMX) and not the crystal clock. Thanks and best regards,
Brendan
So, I created very simple code leaving nothing to the imagination, using an STM32F407 board conveniently equipped with LEDs on A6 and A7.
The program has a repeating main loop with 1s delays between A7 high and A7 low, and correctly cycles a full on/off every 2s.
Additionally for compare, I added a timer interrupt, with prescaler 1:256 and rollover of 31250 to fire once every second, so the LED on A6 intentionally replicates the flash speed of the LED on A7. The panel reveals the crystal frequency as the timer source clock.
Over time, the two LEDs can naturally be expected to slowly lose sync due to the additional processing clock cycles applicable to the LED on A7. However, the LED driven by timer interrupt (port pin A6) is running at an observed 4x the speed of the LED driven by the loop (port pin A7).
The MCU's PLL multiplier/prescaler/post-scaler registers are all configured in accordance with ST's CubeMX clock configurator, and experimental changes to them indicates that register settings declared in Flowcode are indeed being set.
It would appear that the timer clocks are actually driven from one of the bus clocks instead of the crystal clock - i.e. TMR1-7 referencing APB1 (reportedly 42MHz in CubeMX) and not the crystal clock. Thanks and best regards,
Brendan