PWM

We can implement PWM using a counter and a comparison. For example, to achieve a 25% duty cycle, we say:

Output = (Counter < (0.25)*Max)

Here's a diagram showing how a counter with a threshold can be used to achieve a desired duty cycle.


In [ ]:
import magma as m
m.set_mantle_target("ice40")
from loam.boards.icestick import IceStick

N = 8

icestick = IceStick()
# TODO: Configure parts (GPIO, clock)

In [ ]:
import mantle
main = icestick.main()
# TODO: Define main
m.EndDefine()

In [ ]:
m.compile('build/pwm', main)

In [ ]:
%%bash
cd build
cat pwm.pcf
yosys -q -p 'synth_ice40 -top main -blif pwm.blif' pwm.v
arachne-pnr -q -d 1k -o pwm.txt -p pwm.pcf pwm.blif 
icepack pwm.txt pwm.bin
iceprog pwm.bin

Wire up the GPIO pin to a logic analyzer to verify that our circuit produces the correct waveform.