Solved
STM32G483VET6 3-phase 120 degree phase shifted PWM with complementary channels and dead time
Hi,
I am working on a motor control project that need to generate 3 sine waves with 120 degree phase shift to power a 3-phase AC motor. The target MCU is an STM32G483VET6 with 100 pins, and I though that for the first step to generate 3 PWM with phase shift shouldn't be a problem with this MCU, but it is :)
Ok, so as stated in the reference manual, there are TIMx internal trigger connections, which I thought can be used for chaining 3 timers: TIM1, TIM8, TIM20 because they can generate complementary outputs as well:
According to Table 252, I should have easily set this up with MX. I started to process this table, TIM1 is the first timer which can trigger TIM8 through ITR0, and TIM8 can trigger TIM20 through ITR5.
TIM1 setup:
- Channel 1 -> PWM Generation CH1 CH1N
- Channel 2 -> Output Compare No Output
- Counter Settings:
- Prescaler: 47 (Because I am using 48MHz clock from HSI)
- Counter Period: 9999 (100 Hz, nothing fancy)
- Trigger Output (TRGO) Parameters:
- Trigger Event Selection TRGO: Output Compare (OC2REF)
- PWM Generation Channel 1 and 1N
- Mode: PWM Mode 1
- Pulse: 5000 (50% duty)
- Output Compare No Output Channel 2
- Mode: Active Level On Match
- Pulse: 3333 (9999 / 3 = 3333, because 360 degree / 3 = 120 degree)
TIM8 setup:
- Slave Mode: Trigger Mode
- Trigger Source: ITR0
- Channel 1 -> PWM Generation CH1 CH1N
- Channel 2 -> Output Compare No Output
- Counter Settings:
- Prescaler: 47 (Because I am using 48MHz clock from HSI)
- Counter Period: 9999 (100 Hz, nothing fancy)
- Trigger Output (TRGO) Parameters:
- Trigger Event Selection TRGO: Output Compare (OC2REF)
- PWM Generation Channel 1 and 1N
- Mode: PWM Mode 1
- Pulse: 5000 (50% duty)
- Output Compare No Output Channel 2
- Mode: Active Level On Match
- Pulse: 3333 (9999 / 3 = 3333, because 360 degree / 3 = 120 degree)
TIM20 setup:
- Slave Mode: Trigger Mode
- Trigger Source: ITR5
- Channel 1 -> PWM Generation CH1 CH1N
- Counter Settings:
- Prescaler: 47 (Because I am using 48MHz clock from HSI)
- Counter Period: 9999 (100 Hz, nothing fancy)
- PWM Generation Channel 1 and 1N
- Mode: PWM Mode 1
- Pulse: 5000 (50% duty)
My custom code:
/* USER CODE BEGIN 2 */
if (HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1) != HAL_OK)
Error_Handler();
if (HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1) != HAL_OK) // turn on complementary channel
Error_Handler();
if (HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_2) != HAL_OK)
Error_Handler();
if (HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_1) != HAL_OK)
Error_Handler();
if (HAL_TIMEx_PWMN_Start(&htim8, TIM_CHANNEL_1) != HAL_OK) // turn on complementary channel
Error_Handler();
if (HAL_TIM_OC_Start(&htim8, TIM_CHANNEL_2) != HAL_OK)
Error_Handler();
if (HAL_TIM_PWM_Start(&htim20, TIM_CHANNEL_1) != HAL_OK)
Error_Handler();
if (HAL_TIMEx_PWMN_Start(&htim20, TIM_CHANNEL_1) != HAL_OK) // turn on complementary channel
Error_Handler();
/* USER CODE END 2 */
The outcome is that TIM1 and TIM8 are basically the same, with just a few microseconds delay between each other:

TIM20 is 120 degree phase shifted from TIM8:

What am I doing wrong ?
Every idea is highly appreciated.
Bests,
Zsolt
