Digital control: sampling, PWM resolution and C code
A compensator that runs on a microcontroller samples, computes and updates the PWM a little late, and it stores its coefficients and the duty in a finite number of bits. You let Digitalize Compensator show what that costs on the closed-loop buck, fix the two problems it finds, and generate the C code of the control. About 20 minutes.
Pictures: a numbered red box is what you click or read, Run is a button, Design › Automate a menu path.
1. Open the closed-loop buck
Step 1 of 9 · about 1 minChoose File › Open Example › DC-DC › Buck converter, closed loop. It is the circuit of tutorial 2.
File.Open Example.DC-DC.Buck converter, closed loop.
2. Open Digitalize Compensator
Step 2 of 9 · about 1 min3. Start from the voltage-loop design
Step 3 of 9 · about 1 min- Set
Run design firsttovoltage loop. The tool runs the voltage-loop design of tutorial 2 (a Type III network crossing at 5.78 kHz with 60° of margin) and digitalizes it. It never changes the circuit. - Click
More options.
4. Describe the firmware
Step 4 of 9 · about 1 minScroll to the lower fields and fill in the ADC and the PWM timer.
Number format:q31, 32-bit fixed point (the default).Compute delay: 1 sample, the usual timing: sample, compute, write the duty at the next period.ADC bits:12.ADC full scale:4V at the ADC's top code.PWM timer counts:1000, a 100 MHz timer at 100 kHz, so the duty moves in steps of 0.1 %.Run.
5. Read the verdict
Step 5 of 9 · about 1 min✖ Check failed.Phase margin lost: 20.8° against a limit of 10°. The design planned for half a sample of PWM delay; with one sample of computation on top, the loop has 1.5 samples, and that extra sample costs360° · fc · Ts= 20.8° at 5.78 kHz. The margin falls from 60° to 39.2°.DPWM finer than the ADC: one duty step moves the output by 48 mV (48 V × 0.1 %), more than one ADC step (977 µV). The loop then cannot settle between two duty steps and hunts round the set point (a limit cycle). The note under the table says the PWM resolves 10 bits and needs 16.
Below the table are the coefficients as a C header, in float32, q15 and q31.
6. Shorten the delay and refine the PWM
Step 6 of 9 · about 1 minClose the window and open Design › Automate › Digitalize Compensator again. The form opens with the values you ran last, so Run design first, ADC bits and ADC full scale are already set. Clear PWM timer counts, because the high-resolution step replaces it, then fill in these fields:
ISR cycles:500, the length of the control interrupt.CPU clock:170e6. The interrupt then takes 2.94 µs, under a third of the 10 µs period.Duty update:end. The new duty is written as soon as the interrupt ends, not at the next period.High-resolution PWM step:184e-12, the 184 ps edge step of the STM32G474 high-resolution timer.Run.
7. Read the new verdict
Step 7 of 9 · about 1 min✔ Verified.Phase margin lost: 6.12°. The loop keeps 53.9° at the same 5.78 kHz crossover.ISR fits in the period: the interrupt uses 29 % of the period.
The last row now passes too: one duty step moves the output by 883 µV, less than one ADC step.
8. Generate the C code
Step 8 of 9 · about 1 minClose the window and choose Tools › Code Generation.
Save Files…writes the files.
The code holds the control blocks of the circuit (Ref, Err and PI), with the PI turned into a difference equation by Tustin's rule at the 10 µs sample time. Call pc_control_step() once per sample from the timer interrupt. Click Save Files… to write the files. The circuit still holds the PI: to generate the Type III network, apply it first as in tutorial 2.
9. Give the model the same PWM resolution
Step 9 of 9 · about 1 minClick the PWM block and open Advanced (2) in Properties.
PWM.Timer counts in one PWM period: with1000here, the simulation rounds every duty to 0.1 %.High-resolution PWM edge step:184e-12(orstm32g474) rounds it to 184 ps steps instead.
The same steps from a script
codegen.verify compiles the generated C with your computer's compiler, replays the simulated inputs through it and compares the outputs with the model's; here they agree within 2e-7.
powercraft-cli run \
--cmd file.openExample --params '{"name":"buckClosedLoop"}' \
--cmd design.digitalize --params '{"run":"voltageLoop","format":"q31","adcBits":12,"vFullScale":4,"dpwmCounts":1000}' \
--cmd design.digitalize --params '{"run":"voltageLoop","format":"q31","adcBits":12,"vFullScale":4,"hrpwm":184e-12,"cycles":500,"fclk":170e6,"update":"end"}' \
--cmd tools.codeGeneration --params '{}' \
--cmd codegen.verify --params '{"maxSamples":500}'
Back to the learning path.








