5.5.3 · D2Embedded Systems & Real-Time Software

Visual walkthrough — Timers — PWM generation, input capture, output compare

2,055 words9 min readBack to topic

Step 1 — A counter is just a dot climbing a staircase

WHAT: Picture a dot that hops one stair higher on every tick.

WHY: Everything a timer does — blinking LEDs, spinning motors, measuring pulses — is built on this one boring climb. If we understand the climb, the rest is bookkeeping.

PICTURE: In the figure the horizontal axis is time measured in ticks (one tick = one clock pulse). The red dot is the current value of CNT. Watch it climb one step per tick.

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

Each tick lasts a fixed amount of real time, . For now just know it is constant — Step 6 shows where it comes from.


Step 2 — The staircase has a ceiling: ARR

WHAT: We add a ceiling line at height ARR. The dot climbs, hits the ceiling, and drops to the floor.

WHY: This wrap is what makes the pattern repeat. A PWM wave is a repeating thing, so we need a repeating counter. The ceiling is what turns a one-way climb into an endless loop.

PICTURE: The red saw-tooth: climb, drop to , climb again. One climb-and-drop is one period. Count the little steps in a single climb — that number matters next.

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

Step 3 — A second number watches the climb: CCR

WHAT: Draw a second horizontal line at height CCR, lower than ARR. Now the staircase is split into two zones: below the line and above the line.

WHY: A comparison ("is the dot below the line yet?") is the only new idea PWM needs. The timer compares CNT against CCR every single tick — for free, in hardware, while the CPU sleeps. This is why timers beat delay() loops.

PICTURE: The red line is CCR. Below it = the "before" zone; on/above it = the "after" zone. The dot spends part of each climb below and part above.

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

Step 4 — Turn the two zones into a pin: HIGH and LOW

WHAT: Below the map of the climbing dot, we draw the pin's voltage as a flat line that jumps between HIGH and LOW exactly at the moments the dot crosses CCR and wraps.

WHY: This is the entire trick. The counter's height has been converted into a pin's on/off state. Time drives the pin automatically — no software in the loop. That converted square wave is PWM (Pulse-Width Modulation).

PICTURE: Top half — the red dot climbing past the CCR line. Bottom half — the resulting square wave. Notice the HIGH stretch lines up exactly with the "below CCR" part of the climb.

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

Step 5 — Measure the HIGH fraction: the duty cycle appears

WHAT: We measure two lengths on the square wave — the HIGH part and the whole period — and divide.

WHY: An LED at looks dim; a motor at turns slowly. The number that controls brightness or speed is precisely this ratio, so we need it as a formula.

Now count ticks instead of guessing lengths:

PICTURE: The HIGH span (red) sits above the full period. Their ratio is the duty.

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

Step 6 — Where a "tick" actually comes from (the time axis, grounded)

We have been counting ticks. But how long is a tick in real seconds? This turns our tick-counts into a real frequency you can put on an oscilloscope.

WHAT: One fast clock pulse arrives every seconds; the prescaler passes only one tick to the counter for every of them.

WHY: Without slowing down, a 72 MHz counter would fly through its whole climb in microseconds — too fast for a servo or an LED. The prescaler sets how "chunky" each tick is.

PICTURE: Fast clock pulses on the left get funnelled through a "divide by " gate into slower, evenly spaced ticks on the right.

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

This tick-time links to Clock Tree and Prescalers (where is born) and the pin needs GPIO and Alternate Functions switched on to leave the chip.


Step 7 — Walk the edge cases (never leave a gap)

A formula you trust is a formula tested at its ends. Set CCR to each extreme and read the picture.

WHAT / WHY: These three panels prove the formula behaves at both ends and in the middle — no surprise value can appear that we did not show.

PICTURE: Three stacked square waves — flat LOW, half-and-half, flat HIGH — one accent bar marking the HIGH span in each.

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

The one-picture summary

Everything above, compressed: the climbing dot, the ceiling ARR, the threshold CCR, the pin below it, and the duty fraction it produces — all in one frame.

Figure — Timers — PWM generation, input capture, output compare
Recall Feynman retelling — explain it to a friend

Picture a lift that rides from the ground floor up to floor ARR, then instantly drops to the ground and rides up again, forever. On the wall there is a marked floor, CCR. We wire a lamp with one rule: lamp ON while the lift is below the mark, OFF once it reaches or passes it. As the lift rides up, the lamp glows; the moment the lift crosses the mark, the lamp dies; when the lift drops back to the ground, the lamp lights again. So every round trip, the lamp is ON for the lower part and OFF for the upper part. If the mark is low, the lamp barely blinks on — dim. If the mark is high, the lamp is on almost the whole ride — bright. The brightness is just how much of the trip is below the mark: the CCR floor divided by the total number of floors, and the total is ARR+1 because we counted the ground floor too. That single ratio is the duty cycle — and the lift did all the work while we sat and watched.


Connections

  • Timers — PWM generation, input capture, output compare — the parent note (words + more modes).
  • Clock Tree and Prescalers — where and the tick time come from.
  • GPIO and Alternate Functions — the pin must be in AF mode to emit the wave.
  • Interrupts and NVIC — compare/overflow events can fire ISRs.
  • Motor Control and H-Bridges — duty sets motor speed.
  • Servo and ESC Control — pulse-width reading of this same wave.
  • Encoder Mode — the counter used as a position tracker.