Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

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

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    7

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

    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

  2. #2
    Senior Member
    Join Date
    Dec 2013
    Location
    East Stroudsburg PA.
    Posts
    306
    Hi
    You can find more information in this link: PWM Resolution (Teensy LC, 3.0 - 3.6, 4.0, 4.1).

  3. #3
    Senior Member
    Join Date
    Jul 2020
    Posts
    1,252
    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.

  4. #4
    Junior Member
    Join Date
    Jul 2021
    Posts
    7
    @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!

  5. #5
    Senior Member
    Join Date
    Dec 2016
    Location
    Wales
    Posts
    122
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •