5.5.3Embedded Systems & Real-Time Software

Timers — PWM generation, input capture, output compare

1,917 words9 min readdifficulty · medium

1. The counter — first principles

WHY a prescaler? The peripheral clock (say 72 MHz) is often too fast. A prescaler divides it so each tick lasts a usable amount of time.


2. PWM generation (a special output-compare mode)

HOW the hardware makes it: every tick it compares CNT to the Capture/Compare Register CCR:

  • While CNT < CCR → pin is HIGH
  • When CNT ≥ CCR → pin is LOW
  • At wrap (CNT = ARR+1 → 0) → pin goes HIGH again.

So CCR sets how long HIGH lasts, ARR sets the whole period.

Figure — Timers — PWM generation, input capture, output compare

3. Output Compare (general)


4. Input Capture


5. Common mistakes (Steel-man + fix)


6. Active recall

Recall Feynman: explain to a 12-year-old

Imagine a clock hand sweeping a circle over and over. PWM: you tell a light "stay on while the hand is in the first quarter, off the rest" — sweep fast and the light just looks half-bright; move the "off line" to dim it. Input capture: a friend taps the table, and you write down exactly where the clock hand was at that instant. Tap twice, look at the two hand positions, and you know how long the gap was — without ever staring at a stopwatch yourself. The clock (timer) does the watching; you just read the notes.


Connections

  • GPIO and Alternate Functions — PWM/compare pins need AF mode enabled.
  • Interrupts and NVIC — capture/compare events fire ISRs; overflow tracking.
  • Clock Tree and Prescalers — where fclkf_{clk} comes from.
  • Motor Control and H-Bridges — PWM drives speed; dead-time inserts gaps.
  • Servo and ESC Control — pulse-width interpretation of PWM.
  • Encoder Mode — timer counting quadrature; a cousin of input capture.
Timer period formula in terms of PSC, ARR, f_clk
T=(PSC+1)(ARR+1)fclkT=\frac{(PSC+1)(ARR+1)}{f_{clk}}
Why the +1 on PSC?
So that PSC=0 divides the clock by 1, not 0 (divider is PSC+1).
Why the +1 on ARR?
The counter includes count 0, giving ARR+1 ticks per cycle.
PWM duty cycle formula
D=CCRARR+1×100%D=\dfrac{CCR}{ARR+1}\times100\%.
What sets PWM period vs duty?
ARR sets the period; CCR sets the HIGH duration (duty).
Output compare data direction
Timer → Pin (CNT reaches CCR, hardware acts on pin).
Input capture data direction
Pin → Timer (edge event copies CNT into CCR).
Period from two captures (with overflow)
T=(c2c1)mod(ARR+1)ftimT=\dfrac{(c_2-c_1)\bmod(ARR+1)}{f_{tim}}.
Toggle-mode output frequency vs match rate
Output freq = match-rate / 2 (two toggles per period).
Settings for 1 kHz 50% PWM at 72 MHz
PSC=71 (1 MHz), ARR=999, CCR=500.
Servo 1.5 ms center pulse at 50 Hz, 1 µs ticks
ARR=19999, CCR=1500.
Fix for negative capture difference
Take (c2−c1) modulo (ARR+1) to account for counter wrap.

Concept Map

divided by PSC+1

sets tick rate f_tim

wraps at ARR

sets period T

compared to CCR

CNT lt CCR HIGH else LOW

D equals CCR over ARR+1

Time drives Pin

snapshots counter

Pin drives Time

done in hardware

done in hardware

Peripheral clock f_clk

Prescaler PSC

Counter CNT

Auto-Reload ARR

PWM period

Compare Reg CCR

Output pin

Duty cycle

PWM / Output Compare

External pin edge

Input Capture

CPU free, exact timing

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, timer basically ek counter hai jo har clock tick pe ek-ek karke badhta rehta hai, aur top value (ARR) pe pahunch ke wapas 0 ho jaata hai. Prescaler (PSC) clock ko slow karta hai — yaad rakho divider hamesha (PSC+1) hota hai, isliye 72 se divide karna ho to PSC=71 likhte ho. Pura period nikalta hai T=(PSC+1)(ARR+1)/fclkT=(PSC+1)(ARR+1)/f_{clk}, aur dono jagah +1 isliye hai kyunki counting 0 se shuru hoti hai.

PWM ka funda simple hai: hardware har tick pe CNT ko CCR se compare karta hai. Jab tak CNT < CCR, pin HIGH; uske baad LOW. To CCR decide karta hai kitni der HIGH rahega (duty), aur ARR pura period. Duty = CCR/(ARR+1). LED dim karni ho ya motor speed control karni ho — sab isi se. CPU ko kuch karne ki zaroorat nahi, hardware khud handle karta hai, isliye timing ekdum exact aur jitter-free.

Output compare matlab Time se Pin — counter target tak pahuncha, pin par action ho gaya. Iska ulta hai Input capture: yahan Pin se Time — external signal ka edge aata hai aur hardware turant CNT ka value CCR me copy kar deta hai. Do captures ka difference le lo, mil gaya pulse ka width ya frequency, bina kisi software delay ke. Bas ek dhyan rakhna: agar beech me counter overflow ho gaya to seedha subtract mat karo — (c2c1)mod(ARR+1)(c_2-c_1)\bmod(ARR+1) use karo, warna answer negative aa jaayega.

Short me: Compare = pin control karta hai, Capture = time copy karta hai. Ye PWM, servo, motor, frequency-measurement — embedded ki har real-time cheez ka dil hai.

Go deeper — visual, from zero

Test yourself — Embedded Systems & Real-Time Software

Connections