When we set PWM ratio once we can get PWM to operate relatively stable way anyway when we update the pulsewidth with the same value we get unstable output. Below is the example:
Real problem starts when in asynchronic thread in it’s main loop I add pulse width manipulation - with very same values being written every loop of the thread.
Why you need to update PWM pulse width periodically? It will set the PWM HW again and again and might cause some problems, one of them might like what you’ve observed.
I think we all agree that PWM is not about fixed division factor but is used as the pact of the control loops where some inputs produce changed PWM which (as for the example) impact the hardware etc. As so the PWM hardware once set should remind stable when H/L ratio or pulse width is changed. Otherwise we loose purpose of the PWM.
First of all, I’m not a mbed expert, so my comment is just a developer’s point of view.
Nothing bad in changing PWM pulse width periodically, however what the reason for setting the same value? It can be avoided by an additional check (compare previous value to the current one).
Theoretically, this check could sit inside the pulsewidth method, though I’m not sure that it definitely should.
The issue I shown is for updating the same value or not. Just the update of the ratio or the PWM make PWM be reprogrammed from the scratch what causes the issue.
I agree that in real code such update with same value could be eliminated - anyway when ‘hunting’ for the cause of strange HW behavior I simplified the control loop eliminating all what might is not much controllable - leaving update with more controlled values (so constant ones as presented in the example). Please also understand that sample code purpose was to eliminate discussions on ‘you probably wrongly compute values you use in the control of the PWM so better buy some XYZ company SW/HW which use real operating system and better CPU etc.’
If you are interested in this how I use it - here is short description - data is collected from a total of 14 measurement channels in one thread, based on the input from the user and network in other thread we compute values for outputs (3 channels) and the values computed are passed to HW. Other thread do self checking etc. providing ability to check for typical issues/failures. There is also part doing using interface. Last part is watchdog and validity checking.