Problems

14. PWM generator

MediumWrite the moduleAbout 20 min
sequentialcounterspwm

Generate a pulse-width modulated signal with a period of 16 clock cycles.

A 4-bit counter runs 0 to 15 and wraps. pwm is 1 while the counter is less than the captured duty value, so a duty of 0 is always off and a duty of 15 is on for 15 cycles out of 16.

To avoid odd-length pulses when duty changes in the middle of a period, the duty input is captured only at the start of a period: on the edge where the counter wraps from 15 to 0, and also on a reset edge. pwm uses the captured value, never the live input.

rst (synchronous, active high) sets the counter to 0 and captures duty.

Ports

Module pwm4

PortDirBitsWhat it is
clkin1Clock.
rstin1Synchronous reset; also captures duty.
dutyin4Requested on-time in cycles per 16.
pwmout1PWM output.

Example

With duty = 4 captured, pwm is 1 for counter values 0 to 3 and 0 for 4 to 15. Changing duty to 10 when the counter is at 4 changes nothing until the counter wraps to 0.

Stuck? . Sign in to save your progress. You can solve the free problems without an account.

Verilogpwm4.v
14px
Loading the editor…

Submit your answer to have it graded. The hidden test runs on the server; you see whether it passes and, if not, the first case it gets wrong.

PWM generator: Verilog interview question | breadboardhub