Pulse width modulation (PWM)

Pulse width modulation allows you to create a block wave with a certain duty cycle. It is useful for use cases like dimming LEDs.

             <--> pulse-width
___          ____          ____
   |        |    |        |    |           duty-cycle = pulse-width / period
   |________|    |________|    |______
             <--period--->

There are 12 PWM channels.

Each channel consists of a free-running 8-bit counter with two configurable comparators. The period comparator resets the counter value back to zero once it reaches a user-configured top value. This sets the period (number of cycles between rising-edges of pulses) of the output waveform. The pulse width comparator changes the channel output from high to low when the counter value surpasses a user-configured pulse width value. This sets the pulse width (number of cycles the output spends high) of the output waveform.

Each channel has a 64-bit section in the address space for configuration. Not every channel output has yet been connected to an external pin.

OffsetPWM channelExternal pin
0x00Channel 0mikroBUS PWM
0x08Channel 1N/C
0x10Channel 2N/C
0x18Channel 3N/C
0x20Channel 4N/C
0x28Channel 5N/C
0x30Channel 6N/C
0x38Channel 7N/C
0x40Channel 8N/C
0x48Channel 9N/C
0x50Channel 10N/C
0x58Channel 11N/C

Config

For each channel, there is a 32-bit register defining the pulse width and another 32-bit register above it defining how long the complete wave is (counter top). These registers are write-only, and will return a value of zero if read.

OffsetDescriptionRead/Write
0x0Pulse widthWrite-only
0x4Counter topWrite-only

A channel is enabled by setting an non-zero counter top value.

To generate an always-low signal, set a counter top value of zero. To generate an always-high signal, set a pulse-width value greater than the (non-zero) counter top value.