2025-05-13 8:23 AM
Hi,
I'm using STM32F103RB (NUCLEO-F103RB) and I configured the clock system in STM32CubeMX.
I am using HSI as the clock source, and the APB2 timer clock is running at 64 MHz.
I want to generate 20kHz PWM signals using TIM1 Channel 1, 2, and 3, and I’ve already set all channels to Center-Aligned PWM Mode 2.
My requirements are:
PWM frequency: 20 kHz
Resolution: At least 1000 steps (preferably ARR ≥ 999)
Timer: TIM1, running at 64 MHz input clock
However, I’m not sure how to set the correct values for ARR and PSC to achieve exactly 20kHz and also meet the resolution requirement.
Questions:
What combination of ARR and PSC values should I use to achieve 20kHz PWM with ≥1000 resolution?
Is it okay to use PSC = 0 and a higher ARR to get better resolution?
Are there any known limitations with HSI or APB2 clock setup that I should be aware of in this context?
Thanks in advance for your help!
2025-05-13 8:30 AM
Use:
PSC = 0;
ARR = 1599;
This gives you exactly 20 kHz PWM in Center-Aligned Mode with 1600 steps of resolution.
Totally valid and efficient configuration.
2025-05-13 8:32 AM
Shouldn't it be PSC=1, ARR=1599?
64,000,000 / ((1+1)*(1+1599)) = 20,000
2025-05-13 11:35 AM
> Shouldn't it be PSC=1, ARR=1599?
No, @ahsrabrifat is correct.
In center aligned mode, it counts twice per pwm period.