Page 1 of 1

analogWrite changes HardwareTimer settings

Posted: Sun Jan 05, 2020 10:16 am
by Eldi4
Hello there,
I have some problem related to HardwareTimer, when i call analogWrite(PB8,x) on blue pill it somehow changes the TIM4 settings, I set the timer interrupt firing frequency of 200Hz, but when i called analogWrite(PB8,x) the interrupt fire at 1kHz, that's not what i want, does anyone know what is the problem?.

I use STM32F103C8 (Blue Pill), STM32Core, PB8 is T4C3.

Re: analogWrite changes HardwareTimer settings

Posted: Sun Jan 05, 2020 11:18 am
by fpiSTM
That is normal. Analogwrite uses the HardwareTimer and set it to get 1kHz.
To change the freq of the PWM signal use the analogWriteFrequency() before call the analogWrite

Re: analogWrite changes HardwareTimer settings

Posted: Sun Jan 05, 2020 11:40 am
by Eldi4
Ah i see, thank you, so i just need to analogWriteFrequency(timer.getOverflow(HERTZ_FORMAT));

Re: analogWrite changes HardwareTimer settings

Posted: Sun Jan 05, 2020 12:29 pm
by fpiSTM

Code: Select all

analogWriteFrequency(200);
Then the timer will be properly configured.

Re: analogWrite changes HardwareTimer settings

Posted: Sun Jan 05, 2020 12:34 pm
by Eldi4
My timer overflow value acually is variative, so it needs to adapt to it, if i explicitly call it like that, then it will unsynchronized once the overflow value changed.

Re: analogWrite changes HardwareTimer settings

Posted: Sun Jan 05, 2020 2:45 pm
by fpiSTM
If you use Timer4 for another stuff, then you should not use the Arduino API analogWrite which reconfigure it.
You can check those examples to configure the PWM as desired:
https://github.com/stm32duino/STM32Exam ... dwareTimer