Voltecx is in beta. The macOS build comes first; Windows and Linux packages are still being tested.Beta. macOS comes first; Windows and Linux are still in testing. What's new
Documentation

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 min

Choose File › Open Example › DC-DC › Buck converter, closed loop. It is the circuit of tutorial 2.

The File menu open at Open Example, DC-DC, Buck converter, closed loop, marked 1 to 4

  1. File.
  2. Open Example.
  3. DC-DC.
  4. Buck converter, closed loop.

2. Open Digitalize Compensator

Step 2 of 9 · about 1 min

Choose Design › Automate › Digitalize Compensator.

The Design menu open at Automate, Digitalize Compensator, marked 1 to 3

  1. Design.
  2. Automate.
  3. Digitalize Compensator.

3. Start from the voltage-loop design

Step 3 of 9 · about 1 min

The form: Run design first set to voltage loop, marked 1 to 2

  1. Set Run design first to voltage 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.
  2. Click More options.

4. Describe the firmware

Step 4 of 9 · about 1 min

Scroll to the lower fields and fill in the ADC and the PWM timer.

The lower fields: number format, compute delay, ADC and PWM timer, marked 1 to 6

  1. Number format: q31, 32-bit fixed point (the default).
  2. Compute delay: 1 sample, the usual timing: sample, compute, write the duty at the next period.
  3. ADC bits: 12.
  4. ADC full scale: 4 V at the ADC's top code.
  5. PWM timer counts: 1000, a 100 MHz timer at 100 kHz, so the duty moves in steps of 0.1 %.
  6. Run.

5. Read the verdict

Step 5 of 9 · about 1 min

Check failed: phase margin lost and DPWM coarser than the ADC, marked 1 to 3

  1. ✖ Check failed.
  2. 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 costs 360° · fc · Ts = 20.8° at 5.78 kHz. The margin falls from 60° to 39.2°.
  3. 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 min

Close 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, CPU clock, Duty update end and the high-resolution PWM step, marked 1 to 5

  1. ISR cycles: 500, the length of the control interrupt.
  2. CPU clock: 170e6. The interrupt then takes 2.94 µs, under a third of the 10 µs period.
  3. Duty update: end. The new duty is written as soon as the interrupt ends, not at the next period.
  4. High-resolution PWM step: 184e-12, the 184 ps edge step of the STM32G474 high-resolution timer.
  5. Run.

7. Read the new verdict

Step 7 of 9 · about 1 min

Verified: the ISR fits, 6.1° of margin lost, the DPWM finer than the ADC, marked 1 to 3

  1. ✔ Verified.
  2. Phase margin lost: 6.12°. The loop keeps 53.9° at the same 5.78 kHz crossover.
  3. 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 min

Close the window and choose Tools › Code Generation.

The generated C: pc_control.h and pc_control.c, marked 1

  1. 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 min

Click the PWM block and open Advanced (2) in Properties.

The PWM block's Advanced parameters: timer counts and high-resolution step, marked 1 to 3

  1. PWM.
  2. Timer counts in one PWM period: with 1000 here, the simulation rounds every duty to 0.1 %.
  3. High-resolution PWM edge step: 184e-12 (or stm32g474) 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.