Skip to main content
OEkic.1
Visitor II
January 6, 2023
Question

STM32F103C8T6 , Timer1 PWM channels have different frequencies ?

  • January 6, 2023
  • 1 reply
  • 1814 views

STM32F103C8T6 MCU is used for controlling a brushless DC Motor. Firstly I used bipolar switching for Inverter Circuit. TIM1_CH1 ,CH2 ,CH3 pins used for upper mosfets of each leg and TIM1_CH1N,CH2N,CH3N used for lower mosfets. When I want to set the pwm frequency at 18kHz , One of the legs (CH1 and CH1N - Phase A) both upper and lower mosfets aren't working (MCU doesn't create pwm signals) and when i decrease the frequency to 10.2kHz then MCU creates pwm signals on these pins but not same frequency with other Pins (CH2 , CH3 , CH2N , CH3N).

after start-up procedure pwms signals are set in interrupt.

a example of IT codes;

case GPIO_PIN_0:

if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)) 

{  

      HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);   

      HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_2);   

      HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_3);    

      HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);  

 

     HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);

     HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_1);

}

if(!HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0))  

   {  

    HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2); 

    HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_2); 

    HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_1);   

    HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3); 

    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);   

    HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_3); 

  }

 break;

This topic has been closed for replies.

1 reply

KnarfB
Super User
January 6, 2023

> and when i decrease the frequency to 10.2kHz then MCU creates pwm signals on these pins b

This is very unlikely behaviour. Double check your timer and pin configs.

For motor control or other fine-grain channel control, I would not use HAL, but low-level library (LL) or register-level instead. HAL does many things implicitly and also adds overhead.

I would also not start-stop the channels, but switch their modes like (from some 6-step code, other chip):

LL_TIM_CC_DisableChannel(TIM1, LL_TIM_CHANNEL_CH1);
LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_FORCED_INACTIVE);
LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_PWM1);
LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH3, LL_TIM_OCMODE_FORCED_INACTIVE);
LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH2);

There is also a convenient preload feature. Then, the config uses shadow registers, and the new config will only be effective when an update event is sent. This allows simultaneously changing configs without glitches introduced by software.

hth

KnarfB