It seems as if you can set the duty cycle to a 16-bit value giving you 65535 different settings, however, this is only true in a few cases. The Pico’s PWM hardware implements the following algorithm:
set GPIO line high
for count in range(wrap):
wait one clock cycle
if count>level:
set GPIO line low
In other words, the frequency of the PWM signal depends on the value of wrap and the clock frequency and duty cycle depend on level. When you set the frequency of the PWM signal that you want, the software works out the best value for the clock frequency and wrap value. In an ideal wold the wrap would always be the maximum 16-bit value, 65535, but to achieve the desired frequency it often has to be less. Clearly you can only set the duty cycle between 0 and wrap and this can limit the precision that you can set. For example, if wrap is 4 then you can only set duty cycles of 0, 25%, 75% and 100%, i.e. level= 0, 1, 2, 3, and such low values of wrap do occur.
In other words, the frequency you select affects the accuracy of the setting of the duty cycle. In most cases the wrap value is large enough to ignore the problem, but sometimes it is important. MicroPython doesn’t provide any way to explicitly set or get the wrap value, but it is easy to add a function to get it:
Problems with small wrap values usually occur at high frequencies. The reason is that the clock runs at 125MHz and so if you want a PWM frequency of say 30MHz then the wrap value has to be 4 and hence the duty cycle can only be 0,1,2,3. You can see that this is the case in the following example:
You will see the wrap is 2 and if you examine the output using a logic analyzer you will see that the duty cycle is not 50%.
At lower frequencies the duty cycle precision is usually close to 16-bits. Also notice that the duty cycle is always set as if it was a full 16-bit value and MicroPython scales this into the range that wrap makes possible.
There is an additional effect related to the clock that you need to consider. The clock divider can be fractional and in this case the fractional division is achieved by varying the division between two integer dividers so that the average frequency is as requested. This can lead to jitter in the pulse positions.
In Chapter But Not In This Extract
Uses Of PWM – Digital To Analog
Frequency Modulation
Controlling An LED
What Else Can You Use PWM For?
Summary
PWM, Pulse Width Modulation, has a fixed repetition rate but a variable duty cycle, i.e. the amount of time the signal is high or low changes.
PWM can be generated by software simply by changing the state of a GPIO line correctly, but it can also be generated in hardware so relieving the processor of some work.
As well as being a way of signaling, PWM can also be used to vary the amount of power or voltage transferred. The higher the duty cycle, the more power/voltage.
The Pico has eight hardware PWM generators and these are capable of a range of operational modes.
The PWM lines are controlled by a counter and two values wrap which gives the frequency and level which gives the duty cycle. This is mostly hidden from the MicroPython programmer and you can specify frequency and duty cycle without worrying too much about how they are generated.
You can generate phase-correct PWM or allow the phase to vary with the duty cycle.
The higher the wrap value, the higher the resolution of the duty cycle. It is possible to work out the best value for the clock frequency for any PWM frequency to maximize the duty cycle resolution.
PWM can be used to implement digital to analog conversion simply by varying the duty cycle. In the same way, by varying the duty cycle, you can dim an LED.
Programming the Raspberry Pi Pico/W In MicroPython Third Edition
Edera has announced an open source version of Sprout, a bootloader built in Rust. The security specialists also announced success in penetration testing and integration with Falco. The announceme [ ... ]
Itential, which specializes in intelligent network and infrastructure orchestration, has announced FlowAI. This is an extension to the existing Intential Automation Platform which is intended to [ ... ]