2025-01-17 06:45 AM
hi im using stm32g474vet6 mcu, i want generate centrealigned mode pwm of 5khz
how to generate 5khz pwm in centre aligned mode, how to calculate the ARR value for 5khz, since my timer1 clk is 170mhz im not doing any prescalar.
can some share the formula for calculating period value and frequency in center aligned mode.
2025-01-17 08:05 AM - edited 2025-01-17 08:06 AM
*from chatgpt*, use it dude.
To generate a 5 kHz PWM in center-aligned mode on the STM32G474VET6 with TIM1 clock running at 170 MHz and no prescaler, we need to calculate the Auto-Reload Register (ARR) value. The formula and steps are as follows:
In center-aligned mode, the timer counts up and down alternately. This means the period of the PWM signal corresponds to the full up and down count, effectively doubling the count cycle compared to edge-aligned mode.
The period of the PWM signal in center-aligned mode is given by:
TPWM=2×(ARR+1)fTIMCLKT_{\text{PWM}} = \frac{2 \times (\text{ARR} + 1)}{f_{\text{TIMCLK}}}
The frequency of the PWM signal is:
fPWM=fTIMCLK2×(ARR+1)f_{\text{PWM}} = \frac{f_{\text{TIMCLK}}}{2 \times (\text{ARR} + 1)}
Where:
To calculate the ARR value for a given fPWMf_{\text{PWM}}:
ARR=fTIMCLK2×fPWM−1\text{ARR} = \frac{f_{\text{TIMCLK}}}{2 \times f_{\text{PWM}}} - 1
Given:
ARR=170,000,0002×5,000−1\text{ARR} = \frac{170,000,000}{2 \times 5,000} - 1 ARR=170,000,00010,000−1\text{ARR} = \frac{170,000,000}{10,000} - 1 ARR=17,000−1=16,999\text{ARR} = 17,000 - 1 = 16,999
Set ARR = 16,999 in the TIM1 configuration.
The period in seconds is:
TPWM=2×(16,999+1)170,000,000=0.0002 s=200 μsT_{\text{PWM}} = \frac{2 \times (16,999 + 1)}{170,000,000} = 0.0002 \, \text{s} = 200 \, \mu\text{s}
The frequency is:
fPWM=1TPWM=5,000 Hzf_{\text{PWM}} = \frac{1}{T_{\text{PWM}}} = 5,000 \, \text{Hz}
This confirms the calculations are correct for a 5 kHz PWM.
Let me know if you need help implementing this in STM32CubeMX or HAL/LL code!