earlephilhower / arduino-pico

Raspberry Pi Pico Arduino core, for all RP2040 and RP2350 boards
GNU Lesser General Public License v2.1
1.96k stars 406 forks source link

Difference between the documentation and the code for analogWriteFreq #2334

Closed jlbirccyn closed 3 weeks ago

jlbirccyn commented 3 weeks ago

Hello.

The documentation gives a maximum frequency of 1MHz for PWMs, but in the analogWriteFreq function, the test is performed with a value of 10MHz.

Documentation : https://arduino-pico.readthedocs.io/en/latest/analog.html#void-analogwritefreq-uint32-t-freq

Code : https://github.com/earlephilhower/arduino-pico/blob/bd64b97a20dd5da484572788fd76f92cd6491768/cores/rp2040/wiring_analog.cpp#L48

Best regards

earlephilhower commented 3 weeks ago

PWM is basically a counter with a configurable reset value and a clock. Physically, yes, you can run 10MHz. But at that PWM period with a 133MHZ PWM clock you only can count to 133M/10M = ~13, or less than 4 bits (log2(13)) of resolution. Not too useful.

A much more useful PWM frequency would be 1M, as specified in the docs. Then you have slightly more than 7 bits of resolution to work with.

So, if you really want 10M, go for it, but the docs are specifying a much more reasonable config.