
This project is a PWM (pulse width modulation) generator with 8 selectable fixed duty cycles and a selectable clock divider.
An 8-bit counter runs from 0 to 255. The PWM output is high while the counter is below a threshold, and the
threshold is chosen by the ui[7:0] inputs. The PWM frequency is:
f_pwm = f_clk / (divider * 256)
Each ui input selects one fixed duty cycle. If several inputs are active at the same time, the one with the
lowest index wins (ui[0] has the highest priority). If no input is active, the PWM output stays low.
| Input | Nominal duty | Threshold (of 256) | Actual duty |
|---|---|---|---|
ui[0] |
15 % | 38 | 14.84 % |
ui[1] |
25 % | 64 | 25.00 % |
ui[2] |
35 % | 89 | 34.77 % |
ui[3] |
50 % | 128 | 50.00 % |
ui[4] |
65 % | 166 | 64.84 % |
ui[5] |
75 % | 192 | 75.00 % |
ui[6] |
85 % | 217 | 84.77 % |
ui[7] |
95 % | 243 | 94.92 % |
SEL1:SEL0 (uio[2:1]) select how many clock cycles each counter step lasts. The table shows the resulting PWM
frequency with a 25 MHz clock:
| SEL1:SEL0 | Divider | Period (clock cycles) | PWM frequency @ 25 MHz |
|---|---|---|---|
| 00 | /1 | 256 | 97.66 kHz |
| 01 | /8 | 2048 | 12.21 kHz |
| 10 | /64 | 16384 | 1.526 kHz |
| 11 | /512 | 131072 | 190.7 Hz |
uio[0]): while low, the counter and the prescaler are held at 0 and the PWM output is low. When EN goes
high, the first PWM period starts immediately and has the exact full length.ui[7:0] or SEL1:SEL0 while running takes effect at the start of the next full period,
so no period is ever shortened or stretched.ui, EN, SEL) and the reset go through 2-flip-flop synchronizers, so the
response to an input change has a latency of a few clock cycles.| Pin | Signal |
|---|---|
uo[0] |
PWM |
uo[1] |
PWM inverted |
uo[2] |
End-of-period pulse (1 clock cycle, last cycle of the period) |
uo[3] |
Divider tick (one pulse per counter step) |
uo[4] |
EN (synchronized) |
uo[5] |
Counter MSB |
uo[6] |
At least one duty input is active |
uo[7] |
More than one duty input is active |
The bidirectional pins uio[7:3] repeat PWM, PWM inverted, end-of-period pulse, counter MSB and divider tick.
uio[2:0] are inputs (EN, SEL0, SEL1).
uio[0] (EN) to 1.ui[7:0] inputs high, for example ui[3] for 50 %. The PWM appears on uo[0] (and on
uio[3]).uio[2:1] (SEL1:SEL0) to select the PWM frequency.ui inputs at once: the lowest index wins and uo[7] goes high as a warning. With all
ui inputs low, the PWM stays low and uo[6] is low.
A cocotb testbench is included in the test folder. It checks the duty cycle of each input, the priority logic,
the four clock dividers, EN and reset behavior, changes while running, and the pin mapping. Run it with make -B
inside test.None is required. Useful for checking the result:
uo[0].uo[0] to see the brightness change with the duty cycle (use the /64 or /512
divider if you want to see it blink).uo[0] to get an analog voltage proportional to the duty cycle.| # | Input | Output | Bidirectional |
|---|---|---|---|
| 0 | Duty 15% | PWM | EN (input) |
| 1 | Duty 25% | PWM inverted | SEL0 (input) |
| 2 | Duty 35% | End of period pulse | SEL1 (input) |
| 3 | Duty 50% | Divider tick | PWM |
| 4 | Duty 65% | Enable (synchronized) | PWM inverted |
| 5 | Duty 75% | Counter MSB | End of period pulse |
| 6 | Duty 85% | Duty selected (any input active) | Counter MSB |
| 7 | Duty 95% | Multiple duty inputs active (flag) | Divider tick |