1
\$\begingroup\$

For timers on the stm32, there is the option of setting prescaler and counter period. I understood that the prescaler is dividing the frequency before using it for the counter. However, it seems to me that setting the counter value does basically the same because if I set the counter to N the resulting frequency f should be f' = f / N which results actually in the same as prescaler. What did I get wrong?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$
  1. Prescaler defines the frequency of the counter unit. AR (auto reload) is just a cap for maximum count unit and reg update; in PWM or a simple time-base, they work the same. but what about a input capture? What about PWM without sacrificing resolution?
  2. More flexibility. Usually the prescalers are just a power of 2 like 2,4,8,... and despite the fact that there are some microcontrollers with no limit on that, but adding both makes it more flexible.
\$\endgroup\$
1
\$\begingroup\$

The prescaler is used to set the frequency in which counter is changed.

Example:

If your timer clock is 100MHz and you want the timer to overflow every 2ms you need to do some math.

  1. For 2ms your counter will have to count to 100MHZ * 2ms = 200000. Most STM32 timers counters are 16 bits and this value will not fit. The maximum value is 65535.

  2. 200000 / (65535 + 1) = 3.05. The closest integer number greater than this is 4.

  3. 100MHz / 4 = 25MHz. The counter will need to count to 50000 and this value will fit the 16 bits register.

So your prescaler register (PSC) will need to be set to 3 (4-1) and counter register (ARR) to 49999 (50000-1). The offsets of one are necessary because it is how those timers work.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.