cancel
Showing results for 
Search instead for 
Did you mean: 

PWM shift or move question

de Haro Carbonell
Associate III
Posted on October 25, 2017 at 13:08

Dear all,

I am programing a STM32F429 for creating 4 PWM for inverter aplication. I managed to create these 4 PWM using TIMER1 and channel 1, channel 2, channel 1 N and channel 2 N.

The inverter works pretty good using Bipolar mode (channel 1 and channel 2 creates same PWM and their channels N its negative ones).

I have problems when I try to program Unipolar mode, which is channel 1 creates a PWM and channel 1 N the same but negative, BUT channel 2 shold be a PWM with same frecuency but duty cycle is different AND SHIFTED so that the channel 1 and channel 2 shold be centered. SEE ATTACHED pictures for a better understanding.

I have programed everything but still is pending the shifting (o the delay 'moved') one PWM in relation the other.

As you can see in the code (see at the end of the question), Timer 1 is fixed to 16KHz , and there is 2 duty cycles modification (channel 1 and channel 2), and its channel 1N and 2N are automatticaly created at this frecuency and duty cycles.

So my question, finally, is: is there any parameter or register or ANY WAY to shift (mode or delay) one channel in relation other channel?

Sorry for long question but i am completely lost right now!

I am thinking to use 2 different timers to do this task but the shifted problem will be still there , at least i think...

Thank you in advance,

best regards

Alex.

CODE:

 TIMER 1 CONFIGURATION:

*********************************************************************************************************************************

void MX_TIM1_Init(void)//---------------------------------------------MX_TIM1_Init__PWM-----------------------------------

{

  TIM_MasterConfigTypeDef sMasterConfig;

  TIM_OC_InitTypeDef sConfigOC;

  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;

  htim1.Instance = TIM1;

  htim1.Init.Prescaler = 0;

  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim1.Init.Period = 11249;                                                        // 180MHz / 16kHz = 11250 => 0 - 11249

  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  htim1.Init.RepetitionCounter = 0;

  if (HAL_TIM_OC_Init(&htim1) != HAL_OK)

  {

    Error_Handler();

  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)

  {

    Error_Handler();

  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;

  sConfigOC.Pulse = 5624;                                                               // 11250 / 2 = 5625 => 0 - 5624

  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

  sConfigOC.OCNPolarity = TIM_OCPOLARITY_HIGH;

  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;

  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;                                       //  PWM PIN OUT         TIMER CHANNEL

  if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)           //  PE8            ->    TIM1_CH1N

  {                                                                                       //  PE9             ->    TIM1_CH1

    Error_Handler();                                                                   //  PE10         ->     TIM1_CH2N

  }                                                                                       //  PE11         ->     TIM1_CH2

  sConfigOC.Pulse = 5624;                                                               //  Channels TIM1_CH1 = TIM1_CH2, and  TIM1_CH1N = TIM1_CH2N

  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;                                           //  Channels TIM1_CHx and TIM1_CHxN are opposite and with Dead Time programmable

  sConfigOC.OCNPolarity = TIM_OCPOLARITY_HIGH;

  if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)

  {

    Error_Handler();

  }

  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;

  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;

  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_1;

  sBreakDeadTimeConfig.DeadTime = IGBT_Dead_Time;

  sBreakDeadTimeConfig.BreakState = TIM_BREAK_ENABLE;

  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;

  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE;

  if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)

  {

    Error_Handler();

  }

  HAL_TIM_MspPostInit(&htim1);

}

*********************************************************************************************************************************

Next 2 functions are the  functions wich modify the PWM duty cycle for the 2 channels depending on the rest of the code.

             __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, PWM_sine_pos_DutyCicle);

             __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_2, PWM_sine_neg_DutyCicle);

#stm32 #stm32-stm32f4 #timer-channel #inverter #pwm
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on October 25, 2017 at 14:46

Hello!

htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

Try to use the center-aligned mode  by replacing the mode with TIM_COUNTERMODE_CENTERALIGNED1.

Take a look also insid  RM for different PWM modes.

0690X00000608kGQAQ.png

Regards

vf

View solution in original post

2 REPLIES 2
Posted on October 25, 2017 at 14:46

Hello!

htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

Try to use the center-aligned mode  by replacing the mode with TIM_COUNTERMODE_CENTERALIGNED1.

Take a look also insid  RM for different PWM modes.

0690X00000608kGQAQ.png

Regards

vf

Posted on October 25, 2017 at 16:01

THATS IT !! Channel 2 is centeder using 'TIM_COUNTERMODE_CENTERALIGNED' THANKS A LOT! now i have to check which one I must use from 3 modes of center aligned.

Apart from that, also obviously the frecuency decreased to the half, so I have to recalcule the timer, but thats easy...

It was my fault becasue i should have readed the reference manual.

Again thanks a lot.