STM32 gotchas
289. Timer runs somewhat slower than expected, as counters run to PSC+1/ARR+1

Sometimes users complain, that a timer's period is slightly longer than expected. If all other things are set properly (e.g. the primary clock due to mis-setting is not the less-precise HSI instead of the expected HSE), this is probably due to improperly set TIMx_PSC and/or TIMx_ARR.

The counters in STM32 Timer - the prescaler and the "main" counter - count from 0 up to TIMx_PSC/TIMx_ARR respectively 1 2. This means, that the prescaler's period is (TIMx_PSC + 1) timer clock cycles, and the total timer's period is ((TIMx_PSC + 1) * (TIMx_ARR + 1)).

In other words, if user wishes to divide the clock by (N * M), where M is the division ratio for the prescaler and N the division ratio for the timer to overflow, then the registers should be loaded as

TIMx_PSC = M - 1;
TIMx_ARR = N - 1;

The same rule applies also to the repetition counter, TIMx_RCR, available in the Advanced Timers.

1. It has to be kept in mind, that TIMx_PSC is preloaded, i.e. the internal prescaler counter uses the value written into the publicly available register before the latest Update event.

2. The prescaler counter is not exposed, so technically it may count from TIMx_PSC down to zero, inclusively; nevertheless the resulting division ratio (TIMx_PSC + 1) is still the same. Similarly, if timer is set to count down (TIMx_CR1.DIR = 1), it counts from TIMx_ARR down to zero, inclusively; and again, the resulting division ratio (TIMx_ARR + 1) is the same. For up-down ("center-aligned") mode, see g290.