5.5.3 · D4Embedded Systems & Real-Time Software

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

2,268 words10 min readBack to topic
Figure — Timers — PWM generation, input capture, output compare

L1 — Recognition

Recall Solution 1.1

The divider is , not PSC. To divide by 8: .

Recall Solution 1.2
  • Output Compare / PWM: Timer → Pin (the count reaches CCR, hardware drives the pin).
  • Input Capture: Pin → Timer (an edge on the pin copies CNT into CCR). Mnemonic: Compare = Control the pin; Capture = Copy the time.
Recall Solution 1.3

ARR sets the period (the whole saw-tooth length). CCR sets the HIGH duration (how far up the plum line sits). Frequency stays fixed as you change duty — only the orange split moves.


L2 — Application

Recall Solution 2.1

(a) → each tick is . (b) The counter sweeps , i.e. ticks per period, so . (c) .

Recall Solution 2.2

Keep ticks ⇒ . Period ticks. Because the sweep takes ticks, . Duty: .

Recall Solution 2.3

ticks ⇒ . Frame ticks . Servos read the pulse width directly, so ticks. → CCR=2000. (See Servo and ESC Control for why width, not duty %, is what the servo decodes.)


L3 — Analysis

Recall Solution 3.1

The scope is right; the student divided by the wrong number. Duty is , not . The counter has distinct tick-slots per period (it spends one tick at each of ), and HIGH occupies CCR=500 of them → exactly half. Using (=999) in the denominator invents a phantom error that the hardware never had.

Recall Solution 3.2

The counter wrapped once between the two edges (it hit 65535, reset to 0, then kept counting). Raw subtraction goes negative because it ignores the wrap. Here means "the non-negative remainder after removing whole counter cycles of length " — it adds back one full cycle to a negative difference. So: At /tick: , so .

Recall Solution 3.3

MHz, so overflows occur every ticks → overflow rate kHz. But toggle flips the pin each overflow, and it takes two flips (HIGH→LOW→HIGH) for one full wave. So .


L4 — Synthesis

Recall Solution 4.1

Use ticks → , MHz. Frame ticks . Idle pulse ticks ⇒ . Full pulse ticks ⇒ . Fits? Longest pulse (2000) is well under the frame (2500) → yes, comfortable margin. (See Servo and ESC Control.)

Recall Solution 4.2

With ( MHz), a 1 Hz period needs — far above the 16-bit ceiling of 65535. Impossible. How to choose the divisor: the total division is . We must split this product into two factors that each fit in 16 bits (). A tidy split is to make a round number: pick (tick ), leaving () — both comfortably under 65536. (Many splits work; this one keeps the tick a clean .) Period 1 Hz ticks ⇒ (fits!). Duty 10 %: . Check: . ✓


L5 — Mastery

Recall Solution 5.1

HIGH holds while , across ( slots).

  • 0 %: → the condition is never true → pin never HIGH.
  • 100 %: need HIGH for all 1000 slots → . Wait — can CCR exceed ARR? Yes. The compare register is not clamped to ARR; it is just a 16-bit value the hardware compares against. If , the counter never reaches or exceeds CCR during its sweep, so the "go LOW" condition never fires and the pin stays HIGH for the whole cycle — exactly the 100 % we want. Then is true for every count .
  • leaves the single slot LOW → duty , not 100 %. The missing tick is the classic "almost-but-not-quite full brightness" bug.
Recall Solution 5.2

Let = number of full counter overflows between the two edges; here . Each full cycle is ticks, so the true gap adds whole cycles onto the raw difference: At /tick: , so . Single-wrap modulo would have under-counted by ticks — you must track the overflow count for multi-cycle gaps.

Recall Solution 5.3

The counter wrapped once. Recall means "add back one full cycle to a negative difference": At /tick the HIGH pulse is . (This is exactly how H-bridge current-sense timing gets measured.)

Recall Solution 5.4

Resolution = one tick . Longest single-cycle span = ticks . Anything longer wraps and demands overflow bookkeeping — the trade-off is fine resolution vs short unambiguous range.


Connections

  • Servo and ESC Control — L2/L4 pulse-width design problems.
  • Clock Tree and Prescalers — L4 balancing PSC vs ARR for out-of-range periods.
  • Interrupts and NVIC — L3/L5 overflow-counting for long captures.
  • Motor Control and H-Bridges — L5 dual-edge width sensing.
  • Encoder Mode — a cousin of the capture logic used above.