STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

 前言:时基是定时器的最基本的功能:

本节详细叙述STM32的时基的功能。


Time-base unit

The main block of the programmable advanced-control timer is a 16-bit counter with its related auto-reload register. The counter can count up, down or both up and down. The counter clock can be divided by a prescaler.

The counter, the auto-reload register and the prescaler register can be written or read by software. This is true even when the counter is running.

The time-base unit includes:

Counter register (TIMx_CNT)

Prescaler register (TIMx_PSC) 分频设定寄存器值

Auto-reload register (TIMx_ARR) 自动装载寄存器值

Repetition counter register (TIMx_RCR) 自动装载重复计数寄存器

The auto-reload register is preloaded. Writing to or reading from the auto-reload register accesses the preload register. The content of the preload register are transferred into the shadow register permanently or at each update event (UEV), depending on the auto-reload preload enable bit (ARPE) in TIMx_CR1 register.

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

每个UEV,ARPE的值是可以改变的。

The update event is sent when the counter reaches the overflow (or underflow when downcounting) and if the UDIS bit equals 0 in the TIMx_CR1 register.

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

It can also be generated by software. The generation of the update event is described in detailed for each configuration. The counter is clocked by the prescaler output CK_CNT, which is enabled only when the counter enable bit (CEN) in TIMx_CR1 register is set (refer also to the slave mode controller description to get more details on counter enabling).

Note that the counter starts counting 1 clock cycle after setting the CEN bit in the TIMx_CR1 register.


Prescaler description

The prescaler can divide the counter clock frequency by any factor between 1 and 65536. It is based on a 16-bit counter controlled through a 16-bit register (in the TIMx_PSC register). It can be changed on the fly(即时生效) as this control register is buffered. The new prescaler ratio is taken into account at the next update event.

案:由于改变是实时的,分频器的改变可以在下一个事件更新时候就生效

Figure 53 and Figure 54 give some examples of the counter behavior when the prescaler ratio is changed on the fly:

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode


Counter modes

In upcounting mode, the counter counts from 0 to the auto-reload value (content of the TIMx_ARR register), then restarts from 0 and generates a counter overflow event.

ARR是将被实际装载的寄存器的值

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

If the repetition counter(重复计数使能) is used, the update event (UEV) is generated after upcounting is repeated for the number of times programmed in the repetition counter register plus one (TIMx_RCR+1).

在重复计数模式下,UEV的产生:在重复计数器寄存器达到重复寄存器值+1的时候产生,注意这时候的计数是计的upcounting is repeated,也就是自动装载计数器重复装载的数值。

RCR是重复计数值,

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

简单说明一下RCR,RCR就是重复装载计数,这个在比较寄存器那里,理解为更新率。

REP的计数都是向下计数到0的,那么,每一次更新事件就会重新计数到之前REP的值。同时产生 U_RC的事件。

写到 RCR的值都只会在下一个周期的时候起作用。

Else the update event is generated at each counter overflow.

如果repetition counter不计数,每次自动装载都会产生溢出事件。


Setting the UG bit in the TIMx_EGR register (by software or by using the slave mode controller) also generates an update event(产生事件但是不一定产生中断). The UEV event can be disabled by software by setting the UDIS bit in the TIMx_CR1 register. This is to avoid updating the shadow registers while writing new values in the preload registers. Then no update event occurs until the UDIS bit has been written to 0.

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

However, the counter restarts from 0, as well as the counter of the prescaler (but the prescale rate does not change). In addition, if the URS bit (update request selection) in TIMx_CR1 register is set, setting the UG bit generates an update event UEV but without setting the UIF flag (thus no interrupt or DMA request is sent). This is to avoid generating both update and capture interrupts when clearing the counter on the capture event.

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

这个位控制事件的发生时候,中断是否产生。

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

When an update event occurs, all the registers are updated and the update flag (UIF(Update interrupt flag ) bit in TIMx_SR register) is set (depending on the URS bit):

The repetition counter is reloaded with the content of TIMx_RCR register,

The auto-reload shadow register is updated with the preload value (TIMx_ARR),

• The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC register).

The following figures show some examples of the counter behavior for different clock frequencies when TIMx_ARR=0x36.

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode