Topics / TIM
Topic

TIM Timers (basic, general-purpose and advanced-control)

Counters driven by the clock: wake the CPU at a fixed rate, make PWM, or time the edges of an input signal.

Learn first:RCCGPIOPractise:Precise timing and PWM
What it is

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.

You need it for
  • 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.

  1. RM043339.2 TIM2/TIM3/TIM4/TIM5 main featuresp.1631One page: 16-bit and 32-bit counters, four channels, capture, PWM, encoder input
  2. RM043339.3.1 Time-base unitp.1633Counter, prescaler, auto-reload and the update event: the part every timer shares
  3. 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
  4. RM043339.3.9 PWM modep.1654How CCRx and ARR give duty and period, in edge-aligned and center-aligned mode
  5. RM043339.3.5 Input capture modep.1650Step-by-step recipe to capture the counter on an edge and measure a pulse
  6. RM043338.3.16 Using the break functionp.1563Why TIM1 and TIM8 outputs stay off until MOE is set, and what the break input does
  7. DS12556Table 4. Timer feature comparisonp.39Which timer has how many channels and how many counter bits
  8. 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.

1RCC_APB1LENRTurn 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
2TIMx_PSCPrescaler: the counter ticks once every PSC + 1 timer clocksp.1694
3TIMx_ARRThe counter counts from 0 to this value, then wraps: period = (PSC + 1) × (ARR + 1) timer clocksp.1695
4TIMx_CCMR1Channel mode for channels 1 and 2: PWM mode, output compare, or input capture, plus the preload bitp.1686
5TIMx_CCERConnects each channel to its pin (CCxE) and sets the polarity. Without it the pin stays silentp.1692
6TIMx_CCR1Compare value of channel 1 (sets the duty cycle) or the captured counter valuep.1695
7TIMx_DIERWhich events raise an interrupt or a DMA request: update, capture or comparep.1682
8TIMx_CR1CEN starts the counter; ARPE buffers ARR; the direction and counting mode live here toop.1676
9TIMx_SREvent flags: UIF (update) and CCxIF (capture or compare). Software clears themp.1683
10TIMx_BDTRAdvanced timers only: MOE enables the outputs, plus dead time and break settingsp.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)
TIMx_CR1 p.1586TIMx_CR2 p.1587TIMx_SMCR p.1590TIMx_DIER p.1592TIMx_SR p.1594TIMx_EGR p.1596TIMx_CCMR1 p.1597TIMx_CCMR1 p.1598TIMx_CCMR2 p.1601TIMx_CCMR2 p.1602TIMx_CCER p.1604TIMx_CNT p.1607TIMx_PSC p.1607TIMx_ARR p.1607TIMx_RCR p.1608TIMx_CCR1 p.1608TIMx_CCR2 p.1609TIMx_CCR3 p.1609TIMx_CCR4 p.1610TIMx_BDTR p.1610TIMx_DCR p.1613TIMx_DMAR p.1614TIMx_CCMR3 p.1615TIMx_CCR5 p.1616TIMx_CCR6 p.1617TIM1_AF1 p.1617TIM1_AF2 p.1619TIM8_AF1 p.1620TIM8_AF2 p.1622TIM1_TISEL p.1624TIM8_TISEL p.1624TIMx_CR1 p.1676TIMx_CR2 p.1677TIMx_SMCR p.1679TIMx_DIER p.1682TIMx_SR p.1683TIMx_EGR p.1684TIMx_CCMR1 p.1686TIMx_CCMR1 p.1688TIMx_CCMR2 p.1690TIMx_CCMR2 p.1691TIMx_CCER p.1692TIMx_CNT p.1693TIMx_CNT p.1694TIMx_PSC p.1694TIMx_ARR p.1695TIMx_CCR1 p.1695TIMx_CCR2 p.1696TIMx_CCR3 p.1696TIMx_CCR4 p.1697TIMx_DCR p.1698TIMx_DMAR p.1698TIM2_AF1 p.1699TIM3_AF1 p.1699TIM4_AF1 p.1700TIM5_AF1 p.1700TIM2_TISEL p.1701TIM3_TISEL p.1701TIM4_TISEL p.1702TIM5_TISEL p.1703TIM12_CR1 p.1733TIM12_CR2 p.1734TIM12_SMCR p.1735TIM12_DIER p.1737TIM12_SR p.1737TIM12_EGR p.1738TIM12_CCMR1 p.1739TIM12_CCMR1 p.1740TIM12_CCER p.1743TIM12_CNT p.1744TIM12_PSC p.1745TIM12_ARR p.1745TIM12_CCR1 p.1745TIM12_CCR2 p.1746TIM12_TISEL p.1746TIMx_CR1 p.1749TIMx_DIER p.1750TIMx_SR p.1750TIMx_EGR p.1751TIMx_CCMR1 p.1752TIMx_CCMR1 p.1753TIMx_CCER p.1755TIMx_CNT p.1756TIMx_PSC p.1757TIMx_ARR p.1757TIMx_CCR1 p.1757TIM13_TISEL p.1758TIM14_TISEL p.1758TIM15_CR1 p.1799TIM15_CR2 p.1800TIM15_SMCR p.1802TIM15_DIER p.1803TIM15_SR p.1804TIM15_EGR p.1806TIM15_CCMR1 p.1807TIM15_CCMR1 p.1808TIM15_CCER p.1811TIM15_CNT p.1814TIM15_PSC p.1814TIM15_ARR p.1814TIM15_RCR p.1815TIM15_CCR1 p.1815TIM15_CCR2 p.1816TIM15_BDTR p.1816TIM15_DCR p.1819TIM15_DMAR p.1819TIM15_AF1 p.1820TIM15_TISEL p.1821TIMx_CR1 p.1824TIMx_CR2 p.1825TIMx_DIER p.1826TIMx_SR p.1827TIMx_EGR p.1828TIMx_CCMR1 p.1829TIMx_CCMR1 p.1830TIMx_CCER p.1832TIMx_CNT p.1834TIMx_PSC p.1835TIMx_ARR p.1835TIMx_RCR p.1836TIMx_CCR1 p.1836TIMx_BDTR p.1837TIMx_DCR p.1839TIMx_DMAR p.1840TIM16_AF1 p.1841TIM16_TISEL p.1842TIM17_AF1 p.1843TIM17_TISEL p.1844TIMx_CR1 p.1854TIMx_CR2 p.1856TIMx_DIER p.1856TIMx_SR p.1857TIMx_EGR p.1857TIMx_CNT p.1857TIMx_PSC p.1858TIMx_ARR p.1858

On this board

NetPinSignalNote
ARD_D3PA6AF1 TIM1_BKIN, AF2 TIM3_CH1, AF3 TIM8_BKIN, AF9 TIM13_CH1, AF10 TIM8_BKIN_COMP12, AF12 TIM1_BKIN_COMP12Arduino D3 = PA6, TIM3_CH1 (AF2): the pin the PWM guide uses for an external LED
ARD_D5PA8AF1 TIM1_CH1, AF3 TIM8_BKIN2, AF12 TIM8_BKIN2_COMP12Arduino D5 = PA8, TIM1_CH1: an advanced-control timer, so MOE in TIMx_BDTR must be set
STMOD#14-PWMPA3AF1 TIM2_CH4, AF2 TIM5_CH4, AF4 TIM15_CH2STMod+ pin labelled PWM = PA3, TIM2_CH4 (32-bit timer) or TIM5_CH4
LCD_BLPK0AF1 TIM1_CH1N, AF3 TIM8_CH3Backlight 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.

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 TIMPRE is 0, it is twice the APB clock (Table 57). With TIMPRE = 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, CCxE in TIMx_CCER, and on TIM1 or TIM8 MOE in TIMx_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, not PSC × ARR.
  • New PSC and ARR values are only taken at the next update event. Write UG in TIMx_EGR once to load them. That also sets the update flag, so clear UIF in TIMx_SR before you enable the interrupt or it fires at once.
  • Never clearing UIF in the interrupt handler. Hardware sets it on every update and only software clears it, so the interrupt keeps re-triggering.

Where to go next