2024-02-29 12:27 AM
Hi,
I am using the STM32F411 MCU and i am trying to generate PWM signal with fixed dutycycle and frequency.
clock configuration and timer settings is attached below.
When I set the PWM frequency to 10KHz and the duty cycle to 50%, I get the output as 10 KHz frequency and the dutycycle toggles between 50.00% and 49.97%. When setting PWM frequency to 40KHz and duty cycle to 50%, I get output as 40 KHz frequency and dutycycle toggles between 48.00% and 47.97%.
Program code:
Could you please explain why getting like this?
Thank you
2024-02-29 01:28 AM
Hello @vrindar,
I would check the clock source, but since you're using HSE, I don't think that would be the problem
Try to get the prescaler to zero, and do all the division in the period
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2024-02-29 01:02 PM - edited 2024-03-01 01:01 AM
Does the program run otherwise, e.g. if you blink a LED (toggle an output) with a simple loopdelay in main loop, does it blink?
Read out and check/post content of TIM and relevant GPIO registers.
[EDIT] sorry I misread the question [/EDIT]
JW
2024-02-29 08:26 PM
Hello @Sarra.S
I tried the same in the Evaluation board(STM32F429ZI) using HSI but got the same variation in the duty cycle.
2024-03-01 02:01 AM
Hi @vrindar ,
If you set prescaler so that timer's counter clock is 1ms, and you set ARR so that PWM frequency is 40kHz, the period is 1/40kHz = 25ms i.e. ARR=25-1 and the granularity of pulse width setting is 1ms. If you want 50% PWM, the calculation you made above (i.e. ((PWM_Dutycycle*Auto_Reload_Value)/100) = (50*25)/100 =1250/100 = 12 (notice the rounding in integer division), i.e. the pulse is ON for 12 ms and off for 13ms. That's 12ms/25ms=48% (as consequence of the rounding). With 1ms granularity (i.e. prescaler set to 72-1) you can't set the PWM more precisely; if you'd set CCRx to 13, you'd have 13ms long pulse i.e. 13ms/25ms = 52% ratio.
The solution is, as @Sarra.S said above, not to set prescaler to result in 1ms timer counter clock. As she said, simply set prescaler to 0, and then for 40kHz PWM you'll have ARR=(72 * 25) - 1, and the granularity is 1/72ms.
JW
2024-03-01 12:16 PM
@vrindar wrote:...the dutycycle toggles between 50.00% and 49.97%....
How did you measure that values ? Did you just read these values from oscilloscope or logic analyzer "automatic measurment" ?