analogWrite changes HardwareTimer settings

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
Eldi4
Posts: 22
Joined: Fri Dec 20, 2019 8:10 am
Answers: 1
Location: Kediri, Indonesia

analogWrite changes HardwareTimer settings

Post 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.
by fpiSTM » Sun Jan 05, 2020 11:18 am
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
Go to full post
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: analogWrite changes HardwareTimer settings

Post 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
Eldi4
Posts: 22
Joined: Fri Dec 20, 2019 8:10 am
Answers: 1
Location: Kediri, Indonesia

Re: analogWrite changes HardwareTimer settings

Post by Eldi4 »

Ah i see, thank you, so i just need to analogWriteFrequency(timer.getOverflow(HERTZ_FORMAT));
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: analogWrite changes HardwareTimer settings

Post by fpiSTM »

Code: Select all

analogWriteFrequency(200);
Then the timer will be properly configured.
Eldi4
Posts: 22
Joined: Fri Dec 20, 2019 8:10 am
Answers: 1
Location: Kediri, Indonesia

Re: analogWrite changes HardwareTimer settings

Post 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.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: analogWrite changes HardwareTimer settings

Post 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
Post Reply

Return to “General discussion”