---
title: Digital control of converters
heading: Sampling the output once per period, digital PWM resolution and the output steps it allows, limit cycles and how to avoid them, the delay budget, and dithering for extra resolution
eyebrow: S11 · Lesson 19 · Digital control of converters
xp: 50
---

::: hook
A growing share of converters are controlled by a microcontroller or a dedicated digital controller rather than an op amp and a comparator. Digital control brings programmable compensation, telemetry, nonlinear tricks and easy multi-phase management. It also brings quantization in two places, the ADC that reads the output and the digital PWM that sets the duty cycle, and delay from sampling and computation. Get the resolutions in the wrong order and the output hunts endlessly between levels; ignore the delay and the phase margin vanishes.
:::

::: play
- The curve is the output voltage step a digital PWM can make, $V_g f_s/f_\text{clk}$, for a buck from 12 V switching at the slider's frequency, against the PWM counter's clock.
- At 500 kHz and a 100 MHz clock the counter has only 200 steps per period: 60 mV per step at the output, coarser than many regulation specifications.
- Higher switching frequencies make it worse, which is why fast digital controllers need hundreds of megahertz of effective clock, or tricks like dithering and delay-line PWM.
:::

```sim plot
view: graph
x: c = 50..2000 log
y: V = 0.1..1000 log
slider: f = 100..2000 = 500
plot: 12*f/c | label: output step, mV, against clock in MHz
plot: 10 + 0*c | dashed | label: a 10 mV ADC step
```

::: predict
## A digital buck from 12 V switches at 500 kHz with a 100 MHz PWM clock; its ADC resolves 10 mV at the output. What happens in steady state?

- [ ] It regulates to within 10 mV
- [x] It hunts in a limit cycle: no PWM step lands the output inside the ADC's zero-error bin, so the integrator keeps toggling between duty steps 60 mV apart
- [ ] It regulates exactly, since integral action removes all error
- [ ] Nothing: quantization averages out

::: reveal
The PWM can only set the output in 60 mV steps, while the ADC's bin around the setpoint is 10 mV wide. Usually no duty step lands inside it, so the integrator sees an error of one sign, steps the duty, overshoots to an error of the other sign, and steps back: a limit cycle. The cure is a PWM step finer than the ADC step, here a clock of at least 600 MHz or a few bits of dithering.
:::
:::

::: nameit
## Sampling and delay

The controller samples the output once per switching period, often mid-pulse to avoid switching noise, computes a new duty cycle and loads it for the next period. The **delay** from sample to effect is the computation time $T_c$ plus about half a period for the PWM's hold, so at crossover the loop loses
$$\Delta\phi = \omega_c(T_c + T_s/2)$$
of phase margin. Crossover is usually kept below $f_s/10$ for this reason. The compensator itself is designed in the s-domain and mapped to a difference equation (S9 lesson 41's Tustin method) or designed directly in z.

## Digital PWM resolution

A counter clocked at $f_\text{clk}$ can set the pulse width in steps of $1/f_\text{clk}$, so there are $f_\text{clk}/f_s$ steps per period, $\log_2(f_\text{clk}/f_s)$ bits. For a buck each step moves the output by
$$\Delta V_\text{DPWM} = V_g\frac{f_s}{f_\text{clk}}$$

## Limit cycles

With integral action, the output can only settle where the ADC reads zero error. If no achievable PWM level falls inside that ADC bin, the loop toggles between levels forever. The no-limit-cycle conditions are a PWM step finer than the ADC step, $\Delta V_\text{DPWM} < \Delta V_\text{ADC}$, and modest integral gain.

## Dithering

Alternating the duty cycle between adjacent steps over a pattern of $2^k$ periods averages to intermediate values, adding k bits of effective resolution at the cost of a small, slow ripple that the output filter removes.
:::

::: example
## A buck makes 1.2 V from 12 V at 500 kHz with a 100 MHz DPWM clock. What output step does the DPWM allow, and what clock would get below a 10 mV ADC step?

::: solution
1. **Steps per period.**
   $$\frac{100\ \text{MHz}}{500\ \text{kHz}} = 200$$
   which is 7.6 bits.
2. **Output step.**
   $$\begin{aligned} \Delta V &= \frac{12\ \text{V}}{200} \\ &= 60\ \text{mV} \end{aligned}$$
3. **Clock for a 10 mV step.**
   $$\begin{aligned} f_\text{clk} &= \frac{V_gf_s}{\Delta V} \\ &= \frac{12 \times 500\text{ kHz}}{10\text{ mV}} \\ &= 600\ \text{MHz} \end{aligned}$$
4. **How it's done.** Hybrid DPWMs combine a counter with a delay line to reach such effective clocks without a 600 MHz counter.

**Answer:** the DPWM moves the output in 60 mV steps, and getting below 10 mV would take a 600 MHz clock.
:::
:::

::: example
## A digital loop samples once per 200 kHz period, takes 2 µs to compute, and crosses over at 20 kHz. How much phase does the delay cost?

::: solution
1. **Total delay.**
   $$\begin{aligned} T_d &= T_c + T_s/2 \\ &= (2 + 2.5)\ \mu\text{s} \\ &= 4.5\ \mu\text{s} \end{aligned}$$
2. **Phase lost at crossover.**
   $$\begin{aligned} \phi &= 2\pi \times 20\text{ kHz} \times 4.5\ \mu\text{s} \\ &= 0.565\ \text{rad} \\ &= 32.4° \end{aligned}$$
3. **What it means.** A compensator designed for 60° in the analog world would be left with under 30°, so the delay must be included in the design, or the crossover lowered.

**Answer:** the 4.5 µs delay costs 32.4° of phase at crossover.
:::
:::

::: example
## The 200-step DPWM above adds a 4-period dither pattern. What effective resolution results?

::: solution
1. **Intermediate values.** Four periods let the average duty take three intermediate values between adjacent steps: 2 more bits.
2. **Effective resolution.**
   $$\begin{aligned} N &= 200 \times 4 \\ &= 800 \\ \Delta V &= 12\ \text{V}/800 \\ &= 15\ \text{mV} \end{aligned}$$
3. **The cost.** The dither leaves a small ripple at
   $$\begin{aligned} f_s/4 &= 500\text{ kHz}/4 \\ &= 125\ \text{kHz} \end{aligned}$$
   well above the loop's crossover and attenuated by the LC filter.

**Answer:** 800 effective steps (2 more bits), 15 mV per step at the output.
:::
:::

::: check
kind: parts
given: c = 100 | 200 | 500; f = 250 | 500 | 1000; g = 5 | 12 | 48

## A digital PWM counts at {c} MHz for a buck switching at {f} kHz from {g} V.

::: part
answer: c*1e3/f
unit: steps
tolerance: 0.5%

### How many duty steps per period?

::: misses
- [any] $f_\text{clk}/f_s$.
:::
:::

::: part
answer: g*f/(c*1e3)*1e3
unit: mV
tolerance: 0.5%

### What output step does each give?

::: misses
- [any] $V_gf_s/f_\text{clk}$.
:::
:::
:::

::: check
kind: parts
given: g = 5 | 12 | 48; f = 200 | 500 | 1000; A = 2 | 5 | 20

## A digital buck from {g} V at {f} kHz has an ADC step of {A} mV at the output.

::: part
answer: g*f*1e3/(A*1e-3)/1e6
unit: MHz
tolerance: 0.5%

### What DPWM clock is needed to avoid limit cycles without dithering?

::: misses
- [any] $V_gf_s/\Delta V_\text{ADC}$.
:::
:::

::: part
answer: ceil(log(g*f*1e3/(A*1e-3)/(100e6))/log(2) - 1e-9)
unit: bits
tolerance: 0

### With a 100 MHz counter, how many bits of dithering would close the gap?

::: misses
- [any] The smallest k with $2^k \ge$ the required clock over 100 MHz.
:::
:::
:::

::: check
kind: parts
given: f = 100 | 200 | 500; c = 0.5 | 2 | 4; x = 5 | 10 | 20

## A digital loop samples once per {f} kHz period, computes in {c} µs and crosses over at {x} kHz.

::: part
answer: 2*pi*x*1e3*(c*1e-6 + 0.5/(f*1e3))*180/pi
unit: °
tolerance: 0.5%

### How much phase does the delay cost?

::: misses
- [2*pi*x*1e3*c*1e-6*180/pi] Include half a switching period for the PWM's hold.
- [any] $\omega_c(T_c + T_s/2)$.
:::
:::
:::

::: check
kind: parts
given: N = 100 | 200 | 1000; k = 2 | 3 | 4; g = 12 | 48

## A {N}-step DPWM on a {g} V buck adds a dither pattern lasting 2 to the power {k} periods.

::: part
answer: N*2^k
unit: steps
tolerance: 0.5%

### How many effective steps result?

::: misses
- [any] $N \cdot 2^k$.
:::
:::

::: part
answer: g/(N*2^k)*1e3
unit: mV
tolerance: 0.5%

### What effective output step?

::: misses
- [any] $V_g/(N2^k)$.
:::
:::
:::

::: check
kind: parts
given: c = 100 | 250; f = 100 | 400 | 1000

## A {c} MHz counter drives a PWM at {f} kHz.

::: part
answer: log(c*1e3/f)/log(2)
unit: bits
tolerance: 0.5%

### How many bits of duty resolution is that?

::: misses
- [any] $\log_2(f_\text{clk}/f_s)$.
:::
:::
:::

::: check
kind: parts
given: f = 100 | 300; P = 50 | 60; c = 1 | 3

## A loop samples at {f} kHz and computes in {c} µs. Its compensator was designed for {P}° of margin ignoring delay, at a crossover of {f}/10 kHz.

::: part
answer: P - 2*pi*f*1e3/10*(c*1e-6 + 0.5/(f*1e3))*180/pi
unit: °
tolerance: 0.5%

### What margin remains?

::: misses
- [any] Subtract $\omega_c(T_c + T_s/2)$.
:::
:::
:::

::: check
kind: choice

## Why does a digitally controlled converter with an integrator need a DPWM finer than its ADC?

- [ ] To reduce switching loss
- [x] An integrator can only rest where the ADC reads zero error; if every achievable duty step puts the output outside that bin, the loop can't settle and limit-cycles between steps, while a finer DPWM guarantees some step lands inside it
- [ ] The ADC aliases otherwise
- [ ] It speeds up the loop

::: misses
- [1] Resolution doesn't change switching loss.
- [3] Aliasing is a separate sampling issue.
- [4] Speed is set by the compensator and delay.
:::
:::

::: check
kind: derivation
vars: g = 1..400, f = 10000..2000000, c = 10000000..2000000000, T = 0.0000001..0.00001, x = 100..100000

## A digital buck from g volts switches at f with a DPWM clock c, computes in T and crosses over at x (hertz).

::: step
answer: g*f/c

### What output step does one DPWM count give?
:::

::: step
answer: 2*pi*x*(T + 0.5/f)

### What phase does the delay cost, in radians?
:::

::: right
Right. Faster switching worsens both: coarser DPWM steps and, unless the computation shrinks too, more delay per period.
:::
:::

::: check
kind: python

## Write `settle(target, dpwm_step, adc_step, ki, n)`: simulate an integrating controller over n periods. Each period the output is the duty count times dpwm_step; the ADC reports the error rounded to a multiple of adc_step; an accumulator adds ki times the reported error, and the count is the accumulator rounded to a whole number of dpwm_steps. Start from zero and return the number of distinct outputs over the last 50 periods.

```python starter
def settle(target, dpwm_step, adc_step, ki, n):
    return 1
```

```python solution
def settle(target, dpwm_step, adc_step, ki, n):
    acc = 0.0
    count = 0
    outs = []
    for _ in range(n):
        v = count * dpwm_step
        err = round((target - v) / adc_step) * adc_step
        acc += ki * err
        count = round(acc / dpwm_step)
        outs.append(round(count * dpwm_step, 9))
    return len(set(outs[-50:]))
```

```python tests
assert settle(1.23, 0.06, 0.01, 0.5, 400) > 1, "Coarse PWM against a fine ADC: it never settles."
assert settle(1.23, 0.005, 0.01, 0.5, 400) == 1, "A PWM step finer than the ADC step settles."
```

::: right
Right. The simulation shows the rule: the PWM's step must be smaller than the ADC's zero-error bin for the integrator to find a resting place.
:::
:::

::: explain
## Why do digital converter controllers limit their crossover to a tenth of the switching frequency or less?

::: model
A digital controller sees the output only once per switching period, needs time to compute, and can only change the duty cycle at the start of the next period, so its correction always lags the measurement by a computation time plus about half a period. That delay costs phase in proportion to frequency, so at a crossover near the switching frequency it would consume all the phase margin. Keeping the crossover well below the switching frequency keeps the delay's phase cost to some tens of degrees, which the compensator can be designed around, and it also keeps the averaged model on which the design rests accurate.
:::
:::

::: practice
::: check
given: c = 100 | 400; f = 200 | 800; g = 12 | 48
answer: g*f/(c*1e3)*1e3
unit: mV
tolerance: 0.5%

## DPWM output step: {g} V, {f} kHz, {c} MHz?

::: misses
- [any] $V_gf_s/f_\text{clk}$.
:::
:::

::: check
given: f = 150 | 500; c = 1 | 3; x = 8 | 15
answer: 2*pi*x*1e3*(c*1e-6 + 0.5/(f*1e3))*180/pi
unit: °
tolerance: 0.5%

## Delay phase at {x} kHz: {f} kHz sampling, {c} µs compute?

::: misses
- [any] $\omega_c(T_c + T_s/2)$.
:::
:::

::: check
given: N = 128 | 500; k = 2 | 4
answer: N*2^k
unit: steps
tolerance: 0.5%

## Effective steps: {N} with {k} bits of dither?

::: misses
- [any] $N2^k$.
:::
:::
:::
