A timer is a counter that the clock increments. A prescaler (PSC) divides the clock first, the counter counts from 0 up to the value in ARR and wraps around, and that wrap is the update event: it can raise an interrupt or start a DMA transfer. Each channel adds a compare/capture register (CCRx): in output mode the pin changes when the counter reaches it (that is how PWM is made), in input mode the counter value is copied into it when an edge arrives on the pin (that is how you measure a pulse).
The chip has many timers of three kinds, and they differ in channels and features, not in how the counter works. Basic timers (TIM6, TIM7) only count and tick. General-purpose timers (TIM2 to TIM5, TIM12 to TIM17) add capture, compare and PWM channels. Advanced-control timers (TIM1, TIM8) add complementary outputs with dead time and a break input for motor and power stages. The beginner chapter is the one for TIM2 to TIM5; learn it once and the others read as small variations. For a plain 1 ms tick the Cortex-M7 also has SysTick built into the core.
- Running code at a fixed rate with the update interrupt (a 1 kHz control loop, a 500 ms blink)
- Dimming an LED or driving a servo or motor with PWM, with no CPU work while it runs
- Measuring the period or pulse width of an input signal with input capture
- Reading a quadrature encoder, or starting ADC conversions at an exact rate from a timer trigger
Read in this order
The shortest path through the documents. Each link opens the exact page; the line under it says what to look for.
- RM043339.2 TIM2/TIM3/TIM4/TIM5 main featuresp.1631One page: 16-bit and 32-bit counters, four channels, capture, PWM, encoder input
- RM043339.3.1 Time-base unitp.1633Counter, prescaler, auto-reload and the update event: the part every timer shares
- RM0433Table 57. Ratio between clock timer and pclkp.350How the timer clock is derived from the APB clock (TIMPRE and the APB prescaler). Read it before any timer maths
- RM043339.3.9 PWM modep.1654How CCRx and ARR give duty and period, in edge-aligned and center-aligned mode
- RM043339.3.5 Input capture modep.1650Step-by-step recipe to capture the counter on an edge and measure a pulse
- RM043338.3.16 Using the break functionp.1563Why TIM1 and TIM8 outputs stay off until MOE is set, and what the break input does
- DS12556Table 4. Timer feature comparisonp.39Which timer has how many channels and how many counter bits
- PM02534.4 System timer, SysTickp.212The SysTick registers: the core timer you can use instead of a peripheral timer for a plain tick
Registers to learn first
In the order you usually program them. The other 67 registers of the chapter can wait until you need them.
| 1 | RCC_APB1LENR | Turn on the timer clock first (TIM2 to TIM7 and TIM12 to TIM14 here; TIM1, TIM8 and TIM15 to TIM17 are in RCC_APB2ENR) | p.461 |
| 2 | TIMx_PSC | Prescaler: the counter ticks once every PSC + 1 timer clocks | p.1694 |
| 3 | TIMx_ARR | The counter counts from 0 to this value, then wraps: period = (PSC + 1) × (ARR + 1) timer clocks | p.1695 |
| 4 | TIMx_CCMR1 | Channel mode for channels 1 and 2: PWM mode, output compare, or input capture, plus the preload bit | p.1686 |
| 5 | TIMx_CCER | Connects each channel to its pin (CCxE) and sets the polarity. Without it the pin stays silent | p.1692 |
| 6 | TIMx_CCR1 | Compare value of channel 1 (sets the duty cycle) or the captured counter value | p.1695 |
| 7 | TIMx_DIER | Which events raise an interrupt or a DMA request: update, capture or compare | p.1682 |
| 8 | TIMx_CR1 | CEN starts the counter; ARPE buffers ARR; the direction and counting mode live here too | p.1676 |
| 9 | TIMx_SR | Event flags: UIF (update) and CCxIF (capture or compare). Software clears them | p.1683 |
| 10 | TIMx_BDTR | Advanced timers only: MOE enables the outputs, plus dead time and break settings | p.1610 |
All 136 registers in General-purpose timers (TIM2/TIM3/TIM4/TIM5) and Advanced-control timers (TIM1/TIM8) and General-purpose timers (TIM12/TIM13/TIM14) and General-purpose timers (TIM15/TIM16/TIM17) and Basic timers (TIM6/TIM7)
On this board
| Net | Pin | Signal | Note |
|---|---|---|---|
| ARD_D3 | PA6 | AF1 TIM1_BKIN, AF2 TIM3_CH1, AF3 TIM8_BKIN, AF9 TIM13_CH1, AF10 TIM8_BKIN_COMP12, AF12 TIM1_BKIN_COMP12 | Arduino D3 = PA6, TIM3_CH1 (AF2): the pin the PWM guide uses for an external LED |
| ARD_D5 | PA8 | AF1 TIM1_CH1, AF3 TIM8_BKIN2, AF12 TIM8_BKIN2_COMP12 | Arduino D5 = PA8, TIM1_CH1: an advanced-control timer, so MOE in TIMx_BDTR must be set |
| STMOD#14-PWM | PA3 | AF1 TIM2_CH4, AF2 TIM5_CH4, AF4 TIM15_CH2 | STMod+ pin labelled PWM = PA3, TIM2_CH4 (32-bit timer) or TIM5_CH4 |
| LCD_BL | PK0 | AF1 TIM1_CH1N, AF3 TIM8_CH3 | Backlight enable. PK0 can be TIM1_CH1N or TIM8_CH3, so the backlight can be dimmed with PWM instead of plain on/off |
Header pins you can wire to
Arduino and STMod+ header pins whose MCU pin can carry a TIM signal (from the datasheet AF tables).
On this chip
12 instances found in the datasheet’s alternate-function tables, with every pin that can carry each signal.
| Instance | Signals and the pins that can carry them |
|---|---|
| TIM1 | BKINPA6PB12PE15PK2BKIN_COMP12PA6PB12PE15PK2BKIN2PE6PG4BKIN2_COMP12PE6PG4CH1PA8PE9PK1CH1NPA7PB13PE8PK0CH2PA9PE11PJ11CH2NPB0PB14PE10PJ10CH3PA10PE13PJ9CH3NPB1PB15PE12PJ8CH4PA11PE14ETRPA12PE7PG5 |
| TIM2 | CH1PA0PA5PA15CH2PA1PB3CH3PA2PB10CH4PA3PB11ETRPA0PA5PA15 |
| TIM3 | CH1PA6PB4PC6CH2PA7PB5PC7CH3PB0PC8CH4PB1PC9ETRPD2 |
| TIM4 | CH1PB6PD12CH2PB7PD13CH3PB8PD14CH4PB9PD15ETRPE0 |
| TIM5 | CH1PA0PH10CH2PA1PH11CH3PA2PH12CH4PA3PI0ETRPA4PH8 |
| TIM8 | BKINPA6PG2PI4PK2BKIN_COMP12PA6PG2PI4PK2BKIN2PA8PG3PI1BKIN2_COMP12PA8PG3PI1CH1PC6PI5PJ8CH1NPA5PA7PH13PJ9CH2PC7PI6PJ6PJ10CH2NPB0PB14PH14PJ7PJ11CH3PC8PI7PK0CH3NPB1PB15PH15PK1CH4PC9PI2ETRPA0PG8PI3 |
| TIM12 | CH1PB14PH6CH2PB15PH9 |
| TIM13 | CH1PA6PF8 |
| TIM14 | CH1PA7PF9 |
| TIM15 | BKINPA0PE3CH1PA2PE5CH1NPA1PE4CH2PA3PE6 |
| TIM16 | BKINPB4PF10CH1PB8PF6CH1NPB6PF8 |
| TIM17 | BKINPB5PG6CH1PB9PF7CH1NPB7PF9 |
Map of the chapter
Every reference-manual chapter has the same shape. Sections marked ● are in the reading order above.
Common mistakes
- Doing the maths with the wrong clock. The timer clock is not always the APB clock: when the APB prescaler is above 1 and
TIMPREis 0, it is twice the APB clock (Table 57). WithTIMPRE= 1 it can be four times the APB clock, but never above the AHB clock. - Expecting a PWM pin to move when only the timer is set up. You also need the pin in alternate-function mode with the right AF number,
CCxEinTIMx_CCER, and on TIM1 or TIM8MOEinTIMx_BDTR, which is 0 after reset. - Off-by-one in the period. Both registers count from zero, so the period is (
PSC+ 1) × (ARR+ 1) timer clocks, notPSC×ARR. - New
PSCandARRvalues are only taken at the next update event. WriteUGinTIMx_EGRonce to load them. That also sets the update flag, so clearUIFinTIMx_SRbefore you enable the interrupt or it fires at once. - Never clearing
UIFin the interrupt handler. Hardware sets it on every update and only software clears it, so the interrupt keeps re-triggering.