When to use it
Pulse-width modulation fakes an analog level from a digital pin: switch fast and vary the fraction of time the output is high. It's how you dim an LED, set a servo, or drive a motor from an FPGA, no DAC required.
How it works
A counter ramps 0 → 2^W-1 over and over, and the output is high while the counter is
below duty. So duty sets the on-fraction: 0 is always off, max is always on. The
demo uses duty = 5 of 16, about 31%.
Gotchas
- Update
dutyonly at the wrap (when the counter is 0), changing it mid-pulse can produce a runt pulse. The verifiedlfpga_pwmhandles this for you. - Higher
Wgives finer steps but a lower PWM frequency for a given clock, trade resolution against flicker.