Open Art-ut-Kia opened 1 month ago
PWM uses hardware timers
However different pins use different timers, as the STM32 has multiple hardware timers
analogWrite() calls
pwmWrite()
In pwmWrite() the timer device for the pin is retrieved
timer_dev *dev = PIN_MAP[pin].timer_device;
and then
timer_set_compare(dev, cc_channel, duty_cycle);
is called
So what you need to do is to call another timer control function which changes the timer.
Depending on exactly what level of control you want, you can do various things
I don't know what prescaler value is used by default, you would need to look through the code, or perhaps more easily write a sketch which printed the prescaler value for each pin.
using timer_get_prescaler()
e.g.
timer_get_prescaler(PIN_MAP[pin].timer_device);
where you need to define the pin number
I can't remember the specifics of the STM32F1 or STM32F4, but I think different hardware timers may be on different peripheral clocks, so you may not simply be able to use the same prescaler number for all pins.
If the prescaler is already set to its lowest value, you'd need to change the compare and reload values etc
I intended to implement fast PWM for a motor control. Unfortunately, the analogWriteFrequency() function is not implemented. Is there a workaround to change the PWM speed?