cancel
Showing results for 
Search instead for 
Did you mean: 

changing timer output polarity fail in toggle mode

ABucs
Associate II

I use the STM32G0 Timer2 to generate phase controlled signals. For negative phases the output needs to be toggled; After reset the state is always correct, but during runtime the OC polarity is not set correctly.

Here is my function. Can someone told me what is missing?

void tim2Phase(int16_t phase1, int16_t phase2, int16_t phase3)

{

TIM_OC_InitTypeDef sConfigOC = {0};

  sConfigOC.OCMode = TIM_OCMODE_TOGGLE;

sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

  

if (phase1 >= 0) 

{

phase1 = phase1*2 + 1;

sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;

}

else 

{

phase1 = 360 + phase1*2 + 1;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

}

sConfigOC.Pulse = phase1;

  HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);

  

if (phase2 >= 0) 

{

phase2 = phase2*2 + 1;

sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;

}

else 

{

phase2 = 360 + phase2*2 + 1;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

}

sConfigOC.Pulse = phase2;

HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3);

if (phase3 >= 0) 

{

phase3 = phase3*2 + 1;

sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;

}

else 

{

phase3 = 360 + phase3*2 + 1;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

}

sConfigOC.Pulse = phase3;

  HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4);

HAL_TIM_OC_Start(&htim2,TIM_CHANNEL_2);

HAL_TIM_OC_Start(&htim2,TIM_CHANNEL_3);

HAL_TIM_OC_Start(&htim2,TIM_CHANNEL_4); 

}

Thanks,

Andrei B

0 REPLIES 0