5.5.3 · D1Embedded Systems & Real-Time Software

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

2,166 words10 min readBack to topic

This page assumes you have seen none of the notation in the parent note. We build every letter, every subscript, and every from the ground up, in the order they depend on each other. If a symbol appears, it was defined a line earlier. Read top to bottom.


0. The mental model before any symbol

Picture a clock with a single hand sweeping around a dial. Each tick the hand jumps forward one notch. When it reaches the last notch it snaps back to the start and begins again. That is the whole machine. Our job is to name its parts.

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

Look at the figure: the sweeping hand is the counter, the notches are the ticks, the last notch (amber) is the ceiling. Hold this picture — every symbol below is a label on some part of it.


1. Frequency and period — the metronome

WHY the topic needs this: every timer symbol is built out of a frequency (how fast the counter is fed) or a period (how long a full sweep or a pulse lasts). Without and nothing else can be stated.


2. The clock frequency — where ticks are born

The subscript "clk" is just a label meaning clock. It comes from the chip's Clock Tree and Prescalers. For now: is the raw, usually-too-fast heartbeat.

WHY: 72 million ticks per second is far too fast for most jobs (a 1 kHz LED blink would need the counter to reach 72,000 — awkward). So we need a way to slow it down. That is the next symbol.


3. The prescaler — a gearbox that slows the clock

The number we program is called (short for prescaler). But here is the crucial twist that trips everyone up:

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

Look at the figure: 72 white incoming ticks on the left, one amber tick surviving on the right — that surviving-tick rate is the timer clock, our next symbol.


4. The counter — the sweeping hand itself

WHY: is the cause of every output event and the thing copied in every capture. It is the heart the rest of the machine listens to.


5. The ceiling — where the hand snaps back

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

Look at the figure: 4 slots on the dial for , because is four numbers, not three. That off-by-one is the single most common bug in timer code.


6. The threshold — the "off line" on the dial

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

Look at the figure: the amber "off line" splits the sweep into a HIGH stretch (left, cyan) and a LOW stretch (right, dim). Slide the line right → more HIGH → brighter LED / faster motor.


7. Two captures and the wrap — the symbol

When input capture takes two snapshots, we get two counter values. Call them (first edge) and (second edge). The elapsed ticks are . But what if the counter wrapped (§5) between the edges? Then is smaller than and the raw subtraction goes negative — clearly wrong, since time never runs backwards.


8. Putting the symbols on one diagram

Now every symbol has a meaning AND a picture. Here is how they feed the topic:

Frequency f and period T

Source clock f_clk

Prescaler PSC divide by PSC plus 1

Timer clock f_tim and tick time

Counter CNT counts up

Top value ARR wraps to zero

Period T equals ARR plus 1 times tick

Threshold CCR

PWM and duty D

Input capture copies CNT into CCR

Two captures use mod for wrap

Timers topic 5.5.3

Read it top-down: frequency/period ideas feed the clock, the clock is geared down by the prescaler, that drives the counter, the counter plus its ceiling give the period, the counter plus a threshold give either PWM (out) or capture (in), and both flow into the full topic.


9. Where each symbol shows up in real peripherals

  • comes from the chip's Clock Tree and Prescalers — you don't invent it, you inherit it.
  • The output pin driven by compares needs GPIO and Alternate Functions switched to alternate function mode, so the timer (not your code) owns the pin.
  • Overflow and capture events raise Interrupts and NVIC — that is how software counts extra laps for §7's wrap handling.
  • PWM duty feeds speed in Motor Control and H-Bridges; pulse width (a raw in µs) feeds Servo and ESC Control.
  • The counting-both-directions cousin of capture lives in Encoder Mode.
  • Prefer the Hindi walk-through? See the Hinglish note.

Everything above is the vocabulary the parent Timers note speaks in. You now own every letter.


Equipment checklist

Cover the right side and answer aloud. If any stalls, reread its section.

What does mean in plain words?
Frequency and period are the same fact flipped — beats-per-second vs seconds-per-beat.
What is ?
The raw source clock frequency feeding the timer (e.g. 72 MHz), usually too fast on purpose.
Why does the prescaler divide by instead of ?
So the smallest register value divides by 1, avoiding a divide-by-zero.
To divide a 72 MHz clock down to 1 MHz, what is ?
(divides by 72).
What is and how is it built?
The seconds per surviving tick, .
What is ?
The counter register — a number ticking up , the sweeping clock hand.
Why are there ticks in one sweep, not ?
Counting includes the value , so is numbers.
What does do in PWM mode vs capture mode?
PWM: you set it, it's the HIGH-to-LOW threshold (Time→Pin). Capture: hardware writes it, snapshotting on a pin edge (Pin→Time).
What is the duty cycle formula?
.
What does fix?
A counter wrap between two captures that would otherwise make the elapsed time negative.
Full period formula from first symbols?
.