Salta al contenuto
Note per Studenti PWM - Duty Cycle, Resolution and Demodulation Report

PWM - Duty Cycle, Resolution and Demodulation

In questa pagina 6

Pulse-width modulation (PWM) transmits the information of a signal, the modulating signal, using as carrier a square wave of fixed frequency and amplitude in which the duration of the pulses is proportional to the level of the modulating signal. The modulation often implies a strong power amplification: the pulses may be hundreds of volts while the comparator works at logic levels. It is a fundamental technique in switching converters (SMPS, switch-mode power supplies), in motor drives, in class D audio amplifiers (switching output driven by PWM) and in D/A converters. On Arduino the same idea gives the analog output obtained with analogWrite and the LED fading of Lab 8 - Analog Output and Sensors with ArduinoLaboratory 8 covers digital input modes, PWM analog output with analogWrite (LED fading), the 10-bit analogRead input, the map and constrain functions, trimmer and photoresistor exercises, the LM35 temperature sensor and two additional exercises (hysteresis and potentiometer-controlled data logger).Lab 8 - Analog Output and Sensors with Arduino →.

Principle

An analog PWM modulator compares with a comparator the modulating signal V∗V^* with a triangular carrier (amplitude VpV_p, period TT). The output drives the switches of a half-bridge, and the voltage V(t)V(t) is +E2+\frac E2 for t1=d Tt_1=d\,T and −E2-\frac E2 for t2=(1−d) Tt_2=(1-d)\,T. The fraction d=t1/Td=t_1/T is the duty cycle. The mean value over the period is Vmed=(d−12)E=E V∗Vp(−12Vp<V∗<12Vp).V_{med}=\Big(d-\frac12\Big)E=E\,\frac{V^*}{V_p}\qquad(-\tfrac12V_p<V^*<\tfrac12V_p). As dd varies between 0 and 1, VmedV_{med} varies proportionally between −E2-\frac E2 and +E2+\frac E2: the information of V∗V^* has moved into the mean value, and the power associated with VV can be much larger than that of V∗V^*. With a signal at levels 00 and VDDV_{DD} (instead of ±E2\pm\frac E2) Vmed=d VDDV_{med}=d\,V_{DD}. If V∗(t)V^*(t) varies slowly with respect to TT, still Vmed(t)=E V∗(t)/VpV_{med}(t)=E\,V^*(t)/V_p.

Other modulations (PFM, random, pulse density, delta) also vary the distance between pulses: they are less used.

Spectrum and demodulation

PWM is a non-linear transformation and distorts the signal strongly; but the spectrum has a characteristic form: the mean value (or the modulating signal) plus lines around the integer multiples of the carrier frequency. A low-pass filter that removes the components at the carrier frequency and multiples is therefore enough to recover the modulating signal (demodulation). It works the better, with less residual distortion, the larger the ratio between the carrier frequency and the maximum frequency of the modulating signal: this is why PWM frequencies are relatively high and simple filters suffice. The demodulated signal keeps a residual ripple at the PWM frequency and a phase shift due to the filter. In many systems (SMPS, audio) the demodulation is done by the system itself, which is low-pass.

Attenuation of a first-order filter. With pole at ωp\omega_p and a component at frequency ff the modulus is 11+(ω/ωp)2\frac1{\sqrt{1+(\omega/\omega_p)^2}}. Example: PWM at 100 kHz (ω=628 krad/s\omega=628\ \mathrm{krad/s}) with a filter of pole ωp=50 krad/s\omega_p=50\ \mathrm{krad/s}: 62850=12.6\frac{628}{50}=12.6, attenuation 20log⁡1+12.62=22.0 dB20\log\sqrt{1+12.6^2}=22.0\ \mathrm{dB}.

Ripple on an RC filter. For a wave between −VR-V_R and +VR+V_R (peak-to-peak 2VR2V_R) with duty DD, period TT and time constant τ=RC≫T\tau=RC\gg T, the peak-to-peak ripple is Δv≈2VR D(1−D) Tτ\Delta v\approx2V_R\,D(1-D)\,\dfrac T\tau (the capacitor voltage rises for DTDT with slope VR−vmedτ\frac{V_R-v_{med}}\tau and the mean value is (2D−1)VR(2D-1)V_R). For VR=3.3 VV_R=3.3\ \mathrm V, D=0.5D=0.5, T=50 μsT=50\ \mu\mathrm s, τ=100 μs\tau=100\ \mu\mathrm s: Δv≈2⋅3.3⋅0.25⋅0.5=0.825 V\Delta v\approx2\cdot3.3\cdot0.25\cdot0.5=0.825\ \mathrm V (the exact calculation with exponentials gives 0.821 V0.821\ \mathrm V). With τ≫T\tau\gg T the mean value is ⟨y⟩=(2D−1)VR\langle y\rangle=(2D-1)V_R (zero for D=0.5D=0.5).

Digital PWM modulator

It is built with digital circuits: the carrier is a binary counter and the analog comparator is a binary comparator. The counter counts up to N−1N-1 (modulation period) and the output is high while the count is below the value of the compare register. The compare register is C=d⋅NC=d\cdot N: it has the meaning of duty cycle.

  • Counts per period: N=fclkfPWMN=\dfrac{f_{clk}}{f_{PWM}} (with prescaler: fclk/(PSC⋅fPWM)f_{clk}/(PSC\cdot f_{PWM})).
  • Duty-cycle resolution: the step of dd is 1/N1/N; the effective bits are neff=log⁡2Nn_{eff}=\log_2N. The maximum modulation frequency for nn bits is fPWM,max=fclk2nf_{PWM,max}=\dfrac{f_{clk}}{2^n}. It is hard to have high resolution and high modulation frequency together.
  • Asymmetric modulation (counter that counts up and restarts from 0: pulses aligned to an edge, "leading/trailing edge") and symmetric (up-down: the pulse is centred in the period; needed for many converters). With up-down counting the counts per period are 2⋅ARR2\cdot ARR (resolution halves at equal frequency).
  • Dead time: delay between the turn-off of one switch and the turn-on of the other, to avoid short circuits between the supplies.

Numerical examples

  1. Maximum frequency for 10 bits. Clock 10 MHz, duty on 10 bits: fPWM,max=10 MHz/1024=9.77 kHzf_{PWM,max}=10\ \mathrm{MHz}/1024=9.77\ \mathrm{kHz}. At fPWM=200 kHzf_{PWM}=200\ \mathrm{kHz} the counts are N=50N=50: at least 6 bits are needed to represent the duty cycle (26=64≥502^6=64\ge50) and the effective resolution is log⁡250=5.64\log_250=5.64 bits. With theoretical duty 0.3750.375 (modulation between −5-5 and +5 V+5\ \mathrm V) the register is 0.375⋅50=18.75→180.375\cdot50=18.75\to18 by truncation: d=0.36d=0.36. Ideal mean value (2⋅0.375−1)⋅5=−1.25 V(2\cdot0.375-1)\cdot5=-1.25\ \mathrm V, real (2⋅0.36−1)⋅5=−1.40 V(2\cdot0.36-1)\cdot5=-1.40\ \mathrm V: error −0.15 V-0.15\ \mathrm V.
  2. Clock 170 MHz, square wave at 100 kHz, resolution >10>10 bits. N=170 MHz/100 kHz=1700N=170\ \mathrm{MHz}/100\ \mathrm{kHz}=1700 counts with prescaler 1: log⁡21700=10.73>10\log_21700=10.73>10. Period register 1700=0x06A41700=\texttt{0x06A4}; for d=50%d=50\% the duty register is 850=0x0352850=\texttt{0x0352}.
  3. 16-bit timer, 170 MHz, carrier 20 kHz, at least 10 bits. With clock 170 MHz: N=8500N=8500 (13.0513.05 bits). Increasing the counter clock frequency improves the time resolution, but N≤216N\le2^{16}: N=8500⋅k≤65535⇒k≤7.7N=8500\cdot k\le65535\Rightarrow k\le7.7. Among 1×,2×,3×,4×1\times,2\times,3\times,4\times the maximum is 4×\times (680 MHz680\ \mathrm{MHz}): N=34000=0x84D0N=34000=\texttt{0x84D0}, log⁡234000=15.05\log_234000=15.05 effective bits; for a duty of 75%75\%: 0.75⋅34000=25500=0x639C0.75\cdot34000=25500=\texttt{0x639C}.
  4. Clock 200 MHz, 16 bits, ≥12\ge12 bits of resolution: fPWM,max=200 MHz/4096=48.8 kHzf_{PWM,max}=200\ \mathrm{MHz}/4096=48.8\ \mathrm{kHz}; for a carrier near 15 kHz: N=13333=0x3415N=13333=\texttt{0x3415} (13.713.7 bits); d=50%d=50\%: 6667=0x1A0B6667=\texttt{0x1A0B} (or 6666). Leading edge modulation: the pin is set at the timer reset (start of the period) and reset at the match on the duty cycle; the match on the period coincides with the counter reset.
  5. PWM as a DAC: 16-bit data (0.16 unsigned) generated at 1 kHz, PWM between 0 and 5 V at 20 kHz with counter clock 16 MHz: N=800N=800, equivalent bits log⁡2800=9.64\log_2800=9.64; for the datum 0x6A00\texttt{0x6A00}: d=2713665536=0.414d=\frac{27136}{65536}=0.414 (41.4%), mean voltage 5⋅0.414=2.07 V5\cdot0.414=2.07\ \mathrm V; for a resolution of 10 bits: fPWM=16 MHz/1024=15.6 kHzf_{PWM}=16\ \mathrm{MHz}/1024=15.6\ \mathrm{kHz}.

Resolution and limit cycles

With fPWMf_{PWM} too high the resolution drops below 8 bits and limit cycles can appear in a closed-loop control. For typical resolutions of 12 bits the modulation frequency does not usually exceed 500 kHz. If the frequency is not fixed by the specifications it is best to choose it as high as possible (simpler demodulation) compatibly with the resolution.

Applications

Control of power converters (SMPS, inverters): the converter acts as a voltage buffer with large power gain and the passive part (inductor and capacitor) is the demodulator (second-order low-pass), while the microcontroller computes the duty cycle values with a PID. Class D audio amplifiers. D/A converters. Example: an LED driven in PWM above 100 Hz is not perceived by the human eye as flicker; with 16-bit PWM, 10 MHz clock, frequency 200 Hz and at least 10 bits: N=10 MHz/200=50000N=10\ \mathrm{MHz}/200=50000 counts (15.6 bits): PSC=1PSC=1 and match 5000050000.

Common mistakes

  • Computing the effective bits with the processor clock frequency instead of the counter clock (after the prescaler).
  • Applying Vmed=d VDDV_{med}=d\,V_{DD} to a bipolar signal: for levels ±E2\pm\frac E2 the mean is (d−12)E(d-\frac12)E.
  • Swapping the meaning of the period register and the compare/duty-cycle register.
  • Demanding both high frequency and high resolution with a slow clock.

Versione ripasso

Key statements and formulas

  • Principle: the information V∗V^* goes into the pulse width; duty cycle d=t1/Td=t_1/T. Levels ±E2\pm\frac E2: Vmed=(d−12)E=EV∗VpV_{med}=\big(d-\frac12\big)E=E\frac{V^*}{V_p}; levels 00 and VDDV_{DD}: Vmed=d VDDV_{med}=d\,V_{DD}.
  • Demodulation: a low-pass filter removes the carrier harmonics, better when the carrier frequency is much larger than the signal bandwidth; residual ripple and phase shift remain. First-order attenuation 11+(ω/ωp)2\frac{1}{\sqrt{1+(\omega/\omega_p)^2}}.
  • RC ripple (τ≫T\tau\gg T): Δv≈2VRD(1−D)Tτ\Delta v\approx2V_RD(1-D)\frac{T}{\tau}; mean (2D−1)VR(2D-1)V_R.
  • Digital modulator: counter plus comparator; N=fclkfPWMN=\frac{f_{clk}}{f_{PWM}} (with prescaler fclkPSC fPWM\frac{f_{clk}}{PSC\,f_{PWM}}); compare register C=dNC=dN; effective bits log⁡2N\log_2N; fPWM,max=fclk2nf_{PWM,max}=\frac{f_{clk}}{2^n}. Up-down counting halves the resolution; dead time avoids shoot-through.
  • Limit cycles: below about 8 bits in closed loop; with 12 bits fPWM≲500 kHzf_{PWM}\lesssim500\ \mathrm{kHz}; choose the frequency as high as the resolution allows.

Main steps of the examples

  • PWM at 100 kHz with ωp=50 krad/s\omega_p=50\ \mathrm{krad/s}: attenuation 22.0 dB22.0\ \mathrm{dB}. RC ripple VR=3.3 VV_R=3.3\ \mathrm V, D=0.5D=0.5, T=50 μsT=50\ \mu\mathrm s, τ=100 μs\tau=100\ \mu\mathrm s: 0.825 V0.825\ \mathrm V.
  • 10 MHz clock, 10 bits: 9.77 kHz9.77\ \mathrm{kHz}. At 200 kHz200\ \mathrm{kHz}: N=50N=50, 5.645.64 bits, register 18.75→1818.75\to18, mean −1.40 V-1.40\ \mathrm V instead of −1.25 V-1.25\ \mathrm V.
  • 170 MHz, 100 kHz: N=1700=0x06A4N=1700=\texttt{0x06A4}, d=50%d=50\% gives 850=0x0352850=\texttt{0x0352}.
  • 16-bit timer, 170 MHz, 20 kHz: clock 4×4\times (680 MHz680\ \mathrm{MHz}), N=34000=0x84D0N=34000=\texttt{0x84D0}, 75%75\% gives 25500=0x639C25500=\texttt{0x639C}.
  • 200 MHz, 15 kHz: N=13333=0x3415N=13333=\texttt{0x3415}; leading edge: set at reset, reset at the duty match.
  • PWM as DAC, 16 MHz clock, 20 kHz: N=800N=800 (9.649.64 bits); datum 0x6A00\texttt{0x6A00} gives d=0.414d=0.414, 2.07 V2.07\ \mathrm V.

Typical mistakes

  • Bits computed with the processor clock instead of the counter clock.
  • d VDDd\,V_{DD} used for a bipolar signal.
  • Period and compare registers swapped.
  • High frequency and high resolution demanded together with a slow clock.

Teoria collegata