Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

analogWriteFrequency() not implemented #937

Open
Art-ut-Kia opened this issue Oct 24, 2024 · 1 comment
Open

analogWriteFrequency() not implemented #937

Art-ut-Kia opened this issue Oct 24, 2024 · 1 comment

Comments

@Art-ut-Kia
Copy link

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?

@rogerclarkmelbourne
Copy link
Owner

PWM uses hardware timers

However different pins use different timers, as the STM32 has multiple hardware timers

analogWrite() calls

pwmWrite()

void pwmWrite(uint8 pin, uint16 duty_cycle) {

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

  1. Change the 'prescaler' for the timer. This is a divider , i.e the higher the number the lower the PWM speed
  2. Change values like the reload and compare values for the timer
  3. Change both of the above

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants