Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve analogWrite frequency specification #775

Open
@matthijskooijman

Description

@matthijskooijman

Currently, there is ananalogWriteFrequency() that sets the frequency for all futureanalogWrite() calls on all timer channels. This works, but when working with different libraries or code files (with different frequency requirements), this can be a bit cumbersome because this function hides some global state. e.g. to really make this work generically, you would need to callanalogWriteFrequency() before every call toanalogWrite() (and again afterwards to restore the default if there is also code that is not aware ofanalogWriteFrequency().

To solve this, it might be good if you could pass the frequency to use as an (optional) argument toanalogWrite() directly. This removes the hidden global state and makes theanalogWrite() calls predictable again.

Having said that, there are certainly sketches where a "set frequency for allanalogWrites"-approach is easier, so the currentanalogWriteFrequency() still makes sense (but can nicely co-exist with the above suggestion by using theanalogWriteFrequency-specified default when no frequency is passed toanalogWrite().

Currently, every timeanalogWrite() is called, the timer is configured completely, including the PWM frequency. A more obvious approach is to setup the timer frequency (prescaler and ARR value) once and then just use the existing value when callinganalogWrite(). Something like this could be added using something like:

 analogWriteFrequence(pin, 100 /* Hz */);      analogWrite(pin, PWM_FREQ_UNCHANGED);

This explicitly specifies to not change the frequency. This could also be the default, but then there is no longer any easy way to change the frequency forall futureanalogWrite() calls.

Of course, the timer frequency is shared between channels of the same timer, so this API would change the frequency of all pins on the same timer. This is expected and should be documented properly, but is not otherwise a problem AFAICS.

I just also realized that the current code produces quite unexpected results when you startanalogWrite() on one pin using one frequency, then startanalogWrite() on another pin on the same timer using a different frequency. Then the timer gets reconfigured to a new frequency, but the CC values of other channels are unchanged, so if the ARR value changed, the effective duty cycle of those other channels now changed. I'm not sure how to fix this properly (other than encouraging people to set up frequencies first and then startanalogWrite() afterwards), but it's something to keep in mind.

All of this could mostly be applied to theanalogWriteResolution as well (except for theUNCHANGED part, since the resolution is really only a property of the value passed toanalogWrite(), not so much a configuration stored in the timer which always runs at maximum resolution I think). That would suggest adding abits argument toanalogWrite() as well (probably before thefrequency argument` suggested above). This is something that the official Arduino API does not support yet, seearduino/ArduinoCore-API#33 for some thoughts about that.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp