cancel
Showing results for 
Search instead for 
Did you mean: 

formula for pwm generation

Srinath_03
Associate II

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.

1 REPLY 1
Javier1
Principal


*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:


Center-Aligned PWM Mode:

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.


Frequency Calculation Formula:

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:

  • fPWMf_{\text{PWM}}: Desired PWM frequency (5 kHz in your case)
  • fTIMCLKf_{\text{TIMCLK}}: Timer clock frequency (170 MHz in your case)
  • ARR\text{ARR}: Auto-Reload Register value (to be calculated)

Rearranging for ARR:

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


Substituting Your Values:

Given:

  • fTIMCLK=170 MHz=170,000,000 Hzf_{\text{TIMCLK}} = 170 \, \text{MHz} = 170,000,000 \, \text{Hz}
  • fPWM=5 kHz=5,000 Hzf_{\text{PWM}} = 5 \, \text{kHz} = 5,000 \, \text{Hz}

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


ARR Value:

Set ARR = 16,999 in the TIM1 configuration.


Steps in Code:

  1. Configure the timer for center-aligned mode (TIM_CounterMode_CenterAligned1 or equivalent HAL/LL setting).
  2. Set the ARR value to 16,999.
  3. Configure the duty cycle using the Capture/Compare register (CCR1, CCR2, etc.).

Validation:

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!

we dont need to firmware by ourselves, lets talk