STM32 Timer Prescaler, Counter and ARR Explained

In the previous article, STM32 Timer Types Explained, we saw that basic, general-purpose, and advanced-control timers share the same counter core. That core is built from three parts: a prescaler, a counter, and an auto-reload register, which we first introduced in the STM32 Timers Complete Guide. Whether a timer creates a periodic interrupt, a PWM signal, or a pulse measurement, it keeps time with these three parts and the clock that drives them.

Before we use any timer mode, we should be able to calculate a timer period correctly. A wrong prescaler or a wrong auto-reload value does not cause an error message. The timer simply runs at a different speed than you expected. Most of these problems come from a small set of mistakes, such as using the wrong clock frequency or forgetting that the registers hold a value that is one less than the real count.

In this article, we will first look at the timer clock and how it relates to the APB bus clock. Then we will go through the prescaler, the counter, and the auto-reload register one by one. After that, we will combine them into the update frequency formula and use it in a worked example that configures a 1 ms period.

The examples in this article use the STM32F4 series and the NUCLEO-F446RE board, and the same approach applies to other STM32 families. Clock rules and counter widths can differ between families, so anything that is not the same everywhere is marked as device-dependent. This article is part of the STM32 timer tutorial series.

STM32 Timer Clock

Every timer calculation starts with the clock frequency that enters the timer. If this value is wrong, every result that follows from it is wrong as well, even when the prescaler and the auto-reload value are set correctly. For this reason, we need to be clear about where the timer clock comes from before we look at any register.

Timer Clock Sources

A timer can receive its counting clock from several sources. The default source is the internal clock, which comes from the clock tree of the device through the APB bus that the timer is connected to. Most of the timers can also count pulses from an external pin, use a signal from another timer as the clock, or count in an external clock mode with its own input filter. These options are device-dependent, and they belong to the timer modes that we will cover in later articles.

In this article, we will always use the internal clock, because it is the source that is used for periodic interrupts and for PWM generation. The internal clock is called the timer clock in this article, and it is the input of the prescaler.


Timer Clock vs APB Clock

A common assumption is that a timer runs at the same frequency as the APB bus it is connected to. This is not always true. On the STM32F4 series, the timer clock depends on the APB prescaler in the clock tree. When the APB prescaler is set to 1, the timer clock is equal to the APB peripheral clock. When the APB prescaler is set to any other value, the timer clock is twice the APB peripheral clock.

The reason for this rule is that the APB buses often run slower than the core, since many peripherals do not need a high clock. The timers, however, benefit from a high clock because it gives them a finer time resolution and a higher PWM resolution. The doubling rule lets the timers run fast even when the bus is divided down. The diagram below shows the path of the clock from the AHB bus to the timer.

Diagram of the STM32 timer clock path from HCLK through the APB prescaler and the x2 multiplier to the timer clock

This rule is the reason why you should never read the frequency from the APB clock setting alone. In the HAL library, the function HAL_RCC_GetPCLK1Freq() returns the APB1 peripheral clock, which is not always the timer clock. When the APB1 prescaler is not 1, the timer clock is twice this value.

Some devices add more options to this rule. Some other STM32F4 devices, have a timer prescaler selection bit in the RCC registers that is called TIMPRE. It stays at its default value after reset and in the default CubeMX setup, and the rule above describes this default. Other STM32 families, such as the STM32H7, use their own selection bits, and some devices can connect the timer clock directly to the core clock. The clock tree in the reference manual of your device is always the final source for these details. If you want to review the clock tree itself, the article STM32 Clock System Explained covers it in detail.


Timer Clocks on the STM32F446

On the STM32F446, the timers are split between the two APB buses. The timers TIM2 to TIM7 and TIM12 to TIM14 are connected to APB1, and the timers TIM1, TIM8, TIM9, TIM10, and TIM11 are connected to APB2.

STM32F446 block diagram showing TIM2 to TIM7 and TIM12 to TIM14 on APB1 and TIM1 and TIM8 to TIM11 on APB2

The maximum APB1 clock is 45 MHz and the maximum APB2 clock is 90 MHz. With the core running at its maximum speed of 180 MHz, a typical setting divides the APB1 clock by 4 and the APB2 clock by 2.

STM32CubeMX clock configuration for the STM32F446 showing a 45 MHz APB1 clock and 90 MHz APB1 timer clocks

With these settings, the APB1 peripheral clock is 45 MHz, and since the prescaler is not 1, the APB1 timers receive 90 MHz. In the same way, the APB2 peripheral clock is 90 MHz, and the APB2 timers receive 180 MHz. STM32CubeMX shows these results in the Clock Configuration tab as โ€œAPB1 Timer clocksโ€ and โ€œAPB2 Timer clocksโ€, so you can read the correct timer frequency directly from there.


STM32 Timer Prescaler (PSC)

The prescaler is the first stage that the timer clock passes through. Its job is to divide the timer clock by a chosen factor and to produce a slower clock for the counter. Without a prescaler, a 90 MHz timer clock would make a 16-bit counter overflow in less than a millisecond, which is too fast for most practical tasks. The prescaler makes it possible to count at a rate that fits the application.

The prescaler is controlled by the PSC register, which is 16 bits wide on all STM32 timers. The value in the register is not the same as the division factor. The timer divides the clock by the register value plus one, which means that a PSC value of 0 gives a division by 1, and a PSC value of 89 gives a division by 90. This design means that a PSC value of 0 is still valid and lets the counter run at the full timer clock. The largest division factor is 65536, which comes from a PSC value of 65535.

The output of the prescaler is called the counter clock, and it is calculated like this:

Counter clock = Timer clock / (PSC + 1)

For example, assume that the timer clock is 90 MHz and the PSC register contains the value 89. The division factor is 90 (89+1), so the counter clock is 1 MHz (90 MHz / 90). Each counter tick then lasts exactly 1 microsecond. This is a very convenient setting, because the counter value can be read directly as a time in microseconds.

The PSC register is also buffered. When the software writes a new value into it, the new value does not take effect immediately. The timer keeps using the old value until the next update event, and only then does the new value move into the active prescaler. This avoids glitches while the timer is running, but it also means that a change can appear to have no effect for a while. The update event can be forced by software with the UG bit in the event generation register, which is what the HAL initialization code does when it configures a timer.

Diagram of the STM32 prescaler dividing a 90 MHz timer clock by 90 with PSC equal to 89 to produce a 1 MHz counter clock

STM32 Timer Counter (CNT)

The counter is the part of the timer that does the actual counting. The CNT register holds the current count, and it changes by one with each tick of the counter clock. Since the register can be read at any time, it works as a running record of how many ticks have passed since the counter started or since it was last reset. Software can also write to it, which allows the count to be set to any value.

Counter Width

The counter has a fixed width, and this width decides how far it can count before it must start again. A 16-bit counter can hold values from 0 to 65535. A 32-bit counter can hold values from 0 to 4294967295. On the STM32F4 series, TIM2 and TIM5 have 32-bit counters, while the other timers have 16-bit counters. The width is device-dependent, so it should always be checked in the datasheet table of timers.


Up-Counting and Down-Counting

In up-counting mode, the counter starts at 0 and increases by one with each tick until it reaches the value in the auto-reload register. On the next tick, it returns to 0 and generates an update event. This is the mode that is used in most periodic timer examples, and it is the default mode of all timers.

In down-counting mode, the counter starts at the auto-reload value and decreases by one with each tick until it reaches 0. On the next tick, it reloads the auto-reload value and generates an update event. Down-counting is available on the general-purpose and advanced-control timers, but it is not available on basic timers. There is also a center-aligned mode, where the counter counts up and then down in turn, and we will treat it separately later in this article.


STM32 Auto-Reload Register (ARR)

The auto-reload register defines the end of the counting range. It holds the highest value that the counter reaches in up-counting mode, which is also called the terminal count. When the counter value is equal to the terminal count and the next tick arrives, the counter returns to 0 and an update event is generated. In this way, the ARR register sets the length of one timer period.

The important point is how many ticks one period contains. In up-counting mode, the counter passes through every value from 0 up to and including the ARR value. This means that the period has ARR + 1 ticks. For example, an ARR value of 999 gives a counter that goes through the values 0 to 999, which is a total of 1000 ticks. The same +1 relationship that we saw with the prescaler also applies here, and for the same reason: the register holds the highest count, not the number of counts.

The diagram below shows an up-counting timer with an ARR value of 4. The counter passes through the values 0, 1, 2, 3, and 4, which are five ticks, and then it returns to 0 and produces an update event.

Up-counting timer waveform with ARR equal to 4, showing a period of five ticks and an update event at each return to zero

Like the prescaler, the auto-reload register can be buffered. This behavior is controlled by the auto-reload preload enable bit, which is called ARPE, in the first control register of the timer. When this bit is set, a new ARR value is loaded into the active register only at the next update event, so a change in the middle of a period cannot cut that period short. When the bit is cleared, a new value takes effect immediately. The width of the register matches the width of the counter, so ARR is 16 bits wide on most timers and 32 bits wide on TIM2 and TIM5 of the STM32F4 series.


Timer Update Frequency Formula

We now have all the parts that we need to describe the complete counting chain, which was introduced in the STM32 Timers Complete Guide. The timer clock enters the prescaler, the prescaler produces the counter clock, the counter counts at that rate, and the update event happens each time the counter passes the terminal count. The diagram below shows this chain.

STM32 timer counting chain from the timer clock through the prescaler and counter to the ARR comparison and update event

For the common case of an up-counting timer, the two formulas that describe this chain are as follows.

Counter tick frequency = Timer clock / (PSC + 1)

Update frequency = Timer clock / ((PSC + 1) ร— (ARR + 1))

The update period is the inverse of the update frequency. It can also be written as the number of ticks in a period multiplied by the time of one tick, which gives the same result in a form that is often easier to use.

Update period = (PSC + 1) ร— (ARR + 1) / Timer clock

These formulas rest on a few assumptions. The timer must use its internal clock, the counter must count up from 0 to ARR, and the prescaler and auto-reload values must not change while the timer runs. The repetition counter of advanced-control timers must also be at 0, because it delays the update event by a number of periods when it is set to a higher value.

Other counting modes need different approach. In down-counting mode, the period contains the same number of ticks, since the counter goes from ARR down to 0, so the formula stays the same. In center-aligned mode, the counter goes up and then down, which means that a complete cycle takes about 2 ร— ARR ticks, and the number of update events per cycle depends on the selected center-aligned mode. Modes such as the one-pulse mode or the encoder mode use the counter in other ways, and they will be explained in their own articles.


1 ms Timer Example

In this example, we will configure a timer on the NUCLEO-F446RE so that it generates an update event every 1 millisecond, which is an update frequency of 1 kHz. We will use TIM3, which is a 16-bit general-purpose timer on the APB1 bus, and we will assume the clock setup that we saw earlier, with the core at 180 MHz and the APB1 prescaler set to 4. The calculation has the following steps.

  1. Find the timer clock. The core clock is 180 MHz, and dividing it by the APB1 prescaler of 4 gives a peripheral clock of 45 MHz. The prescaler is not 1, so the timer clock is twice this value, which is 90 MHz.
  2. Calculate the total number of ticks. A 1 ms period at a frequency of 1 kHz needs 90 MHz / 1 kHz = 90,000 timer clock cycles per period. This number must be equal to (PSC + 1) ร— (ARR + 1).
  3. Choose the prescaler. We want a counter clock of 1 MHz, so that one tick equals 1 microsecond. The division factor is 90 MHz / 1 MHz = 90, so the prescaler will be PSC = 90 โˆ’ 1 = 89.
  4. Choose the auto-reload value. The period must contain 1000 ticks of 1 microsecond, so ARR + 1 = 1000, and ARR = 1000 โˆ’ 1 = 999.
  5. Verify the result. The update frequency is 90,000,000 / (90 ร— 1000) = 1000 Hz, which gives a period of exactly 1 ms.

The result of this calculation is the pair PSC = 89 and ARR = 999. In STM32CubeMX, you enter these two numbers in the Parameter Settings of TIM3, in the fields Prescaler (PSC โ€“ 16 bits value) and Counter Period (AutoReload Register โ€“ 16 bits value).

STM32CubeMX TIM3 parameter settings with prescaler 89 and counter period 999 for a 1 ms period

CubeMX expects the register values, which means that you must enter 89 and 999, and not the division factors 90 and 1000. The same values can be written directly in the registers or in the HAL initialization structure, as shown below.

Verify Against the Timer Limits

Now we must check that the values are valid for this timer. Both the PSC and ARR registers of TIM3 are 16 bits wide, so each of them can hold a value up to 65535. The PSC value of 89 and the ARR value of 999 are both well inside this limit. The timer clock of 90 MHz comes from the default TIMPRE setting that we assumed earlier, and it is within the maximum timer clock that the datasheet allows for the APB1 timers. For this reason, the setup is valid.

The pair 89 and 999 is not the only solution. Any pair of values with (PSC + 1) ร— (ARR + 1) = 90,000 gives the same period. For example, PSC = 8999 and ARR = 9 gives a counter clock of 10 kHz with only 10 ticks per period. In the other direction, PSC = 0 and ARR = 89,999 gives a counter clock of 90 MHz with 90,000 ticks per period. This last pair does not fit into the 16-bit ARR register of TIM3, but it is valid for TIM2, which has a 32-bit counter. We prefer PSC = 89 and ARR = 999 because a counter clock of 1 MHz is easy to read as microseconds, and because 1000 ticks per period leave room for a finer adjustment later.

One more detail is worth knowing when you start the timer with the HAL library. The initialization code generates one update event, so that the prescaler value moves into its active register. This event can set the update flag before the timer starts, and an interrupt can then occur immediately after the start. Clearing the update flag before you start the timer with interrupts avoids this extra interrupt, and we will use this in the article about the periodic update interrupt.


Conclusion

Every timer period on an STM32 device is the result of four elements that work together. The timer clock enters the prescaler, which divides it by PSC + 1. The counter counts the resulting ticks from 0 up to the value in the auto-reload register, which gives a period of ARR + 1 ticks. The update event at the end of each period is the signal that drives interrupts, DMA requests, and trigger outputs. For an up-counting timer, this is summarized in the formula: update frequency = timer clock / ((PSC + 1) ร— (ARR + 1)).

The most important point is to start from the correct timer clock, because on the STM32F4 series the timer clock is twice the APB clock whenever the APB prescaler is not 1. After that, the +1 relationship in both registers and the width of the counter are the two things that must always be checked. When these points are clear, a timer period can be planned on paper and verified before any code is written. In the next article, we will use this calculation in practice and run code at a fixed interval with the periodic update interrupt and the HAL library.

What to Read Next

Article by Yashmiti

Yashmiti is a technical writer at STM32Guide, where she creates tutorials and guides that help embedded-systems learners and developers work confidently with STM32 microcontrollers. Her writing covers tools like STM32CubeIDE and STM32CubeMX, along with peripherals and communication protocols โ€” always with an emphasis on clear explanations and hands-on examples that take readers from concept to working project.

She's also part of the effort to build STM32Guide into an organized, easy-to-navigate resource for the STM32 community.

Leave a Comment