Why Do I Need to Halve Prescaler Values for TIMER 2 & 3 on STM32F4 to Get Correct Interrupt Periods?
Good day everyone, @Middleware & OS I'm using an STM32F4 with three timers and experiencing unexpected behavior with the timers' interrupts.
- TIMER 1 (advanced control) generates an interrupt every 1ms with (\text{PSC} = 11) and (\text{ARR} = 13999) as expected.
- TIMER 2 (general purpose) is supposed to generate an interrupt every 10ms. Calculations give (\text{PSC} = 39) and (\text{ARR} = 41999), but the interrupt occurs every 20ms. To achieve 10ms, I had to set (\text{PSC} = 19).
- TIMER 3 (general purpose) should generate an interrupt every 65.535ms. Calculations give (\text{PSC} = 167) and (\text{ARR} = 65535), but the interrupt occurs every 131.064ms. Halving the PSC to 83 gives the correct 65.535ms period.
Oscilloscope outputs:
- TIMER 3 with (\text{ARR} = 0xFFFF) and (\text{PSC} = 167): output is NOT as expected.
- TIMER 3 with (\text{ARR} = 0xFFFF) and (\text{PSC} = 83): output is as expected.
Why do I need to halve the prescaler values for TIMER 2 and TIMER 3 to get the correct interrupt periods?
Solution:Jump to solution
Oh @Sterling Another possibility is that there might be a factor of 2 difference due to the internal clock tree configuration of the STM32F4. Sometimes, the timers might be running at a different clock frequency than you expect because of prescaler settings in the RCC (Reset and Clock Control) that affect certain timer groups differently.
5 Replies
@Sterling It sounds like there might be a discrepancy in how the clock frequency is being divided. Have you checked if the timers' clock source is configured correctly and if there are any differences in the clock settings for TIMER 1 versus TIMERS 2 and 3?
Yes, I used the same clock source configuration for all three timers. I'm still not sure why the prescaler needs to be halved for TIMER 2 and TIMER 3. @UC GEE
Solution
Oh @Sterling Another possibility is that there might be a factor of 2 difference due to the internal clock tree configuration of the STM32F4. Sometimes, the timers might be running at a different clock frequency than you expect because of prescaler settings in the RCC (Reset and Clock Control) that affect certain timer groups differently.
Hmmm, that actually makes sense. I'll double-check the RCC configuration and see if there's something affecting the timers' clock frequency differently.
Yes @Sterling , also check if there are any post-scaler or pre-scaler settings specific to TIMER 2 and TIMER 3 that might not be present for TIMER 1. Sometimes advanced control timers like TIMER 1 can have different configurations compared to general-purpose timers.