Datasheet

Table Of Contents
If a PWM B pin is used as an input, and is selected on multiple GPIO pins, then the PWM slice will see the logical OR
of those two GPIO inputs
4.6.2.1. Pulse Width Modulation
The PWM hardware functions by continuously comparing the input value to a free-running counter. This produces a
toggling output where the amount of time spent at the high output level is proportional to the input value. The fraction of
time spent at the high signal level is known as the duty cycle of the signal.
The counting period is controlled by the TOP register, with a maximum possible period of 65536 cycles, as the counter and
TOP are 16 bits in size. The input values are configured via the CC register.
TOP
Count
IOVDD
TOP/3
V
Input (Count)
Counter compare level
Counter
0
T 2T 3T
t
Output (Pulse)
GPIO pulse output
0
T 2T 3T
t
Figure 102. The
counter repeatedly
counts from 0 to TOP,
forming a sawtooth
shape. The counter is
continuously
compared with some
input value. When the
input value is higher
than the counter, the
output is driven high.
Otherwise, the output
is low. The output
period T is defined by
the TOP value of the
counter, and how fast
the counter is
configured to count.
The average output
voltage, as a fraction
of the IO power
supply, is the input
value divided by the
counter period (TOP +
1)
This example shows the counting period and the A and B counter compare levels being configured on one of RP2040’s
PWM slices.
Pico Examples: https://github.com/raspberrypi/pico-examples/tree/pre_release/pwm/hello_pwm/hello_pwm.c Lines 18 - 33
18 // Tell GPIO 0 and 1 they are allocated to the PWM
19 gpio_set_function(0, GPIO_FUNC_PWM);
20 gpio_set_function(1, GPIO_FUNC_PWM);
21
22 // Find out which PWM slice is connected to GPIO 0 (it's slice 0)
23 pwm_inst_t slice = pwm_gpio_to_slice(0);
24
25 // Set period of 4 cycles (0 to 3 inclusive)
26 pwm_set_wrap(slice, 3);
27 // Set channel A output high for one cycle before dropping
28 pwm_set_chan_level(slice, PWM_CHAN_A, 1);
29 // Set initial B output high for three cycles before dropping
30 pwm_set_chan_level(slice, PWM_CHAN_B, 3);
31 // Set the PWM running
32 pwm_enable(slice, true);
Figure 103 shows how the PWM hardware operates once it has been configured in this way.
RP2040 Datasheet
4.6. PWM 547