What is a/the use case for analogWriteResolution with resolution < 16?

Status
Not open for further replies.

wootie11

Member
Hi there,

I'm studying the analogWrite and related functions (writeFrequency, writeResolution) on the Teensy 4.0 and after looking at the source code it seems like all the analogWriteResolution value does is scale the count value used by the PWM module... I'm just curious as to what use case a resolution less than 16 would have? Could it be useful for optimizing code somehow?

From pwm.c in the Teensy 4 core:
Code:
Line 143: 	uint32_t cval = ((uint32_t)val * (modulo + 1)) >> analog_write_res;

This is the only spot I could find any mention of analog_write_res that wasn't just being passed along from analogWriteResolution.

Hopefully this question hasn't been asked already - I couldn't find anything about it on this forum, but I'm also not sure my pjrc-search-fu is up to snuff... analogWriteResolution did not return many results.

Thanks in advance :)

Best,
Will
 
If you want to generate very high frequency PWM perhaps?

I've used limited resolution PWM at high speed to allow noise-shaping for instance. The noise
shaping has the side effect of increasing the effective low-frequency resolution to much more
than the actual resolution (the wonder of noise-shaping!) This is one way to directly output
class D waveforms.
 
@Chris O. Thanks for the documentation - I remember looking that over, but I'm really glad I had another look - I guess the best use case is matching frequency to resolution so that each possible value used with analogWrite has a distinct output.

@MarkT That's really interesting! I researched noise shaping a bit and it's still a bit over my head but it seems like a good use for analogWriteResolution. Thanks for sharing!
 
It's just a function of how the timers work out. A simple view of a PWM is a counter that counts up to number, turns on at zero, turns off at X, and resets at Y back to zero. Let say your counter can increase at a rate of 1 a millisecond. You could have a max count of 3, giving you two bits resolution and an update of 250Hz, or a max count of 255, 8 bits of resolution and an update rate of ~4Hz, and so on.

By changing the count rate and values, various update rates and frequencies are possible, see the table. AFAIK there's no scope to optimize the code for using a lower resolution, as there's still the same requirement to set the timer up in the same way. If you didn't need a very high update rate, combining this with a low resolution could make it easier to bit-bash a PWM signal if you really needed to use a non-PWM capable pin.

MarkT's use is pretty similar to what I've done, where I've used PWM to create a crude DAC with output filters. Increasing the PWM update rate, by sacrificing resolution, can make it easier. If your PWM update rate is now significantly higher than the bandwidth of whatever it's driving, then any switching noise that propogates has less of an issue.
 
Status
Not open for further replies.
Back
Top