STM32 Timer PWM mode, strange behaviour
Hello!
I am using a STM32F303 MCU. I am generating a complementary PWM signal with a dead time. During operation I am changing the dutycycle of the PWM. I noticed a very strange behaviour. As soon as the counter value reaches the compare value, the output of the timer pin is changed. This is normal, but sometimes (when i recompile my program and download it to the MCU) the PWM signal is inverted, meaning that the ON time is now the OFF time. So instead of a dutycycle of e.g. 30% I get a dutycycle of 70%. Anyone an Idea what could be the cause of that? I do not change anything in the timer setup when I recompile. I am using CubeMX and HAL.
Here my timer configuration.
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
htim20.Instance = TIM20;
htim20.Init.Prescaler = 0;
htim20.Init.CounterMode = TIM_COUNTERMODE_UP;
htim20.Init.Period = 360;
htim20.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim20.Init.RepetitionCounter = 0;
htim20.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim20) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim20, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 150;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_SET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&htim20, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
if (HAL_TIM_PWM_ConfigChannel(&htim20, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 10;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim20, &sBreakDeadTimeConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
HAL_TIM_MspPostInit(&htim20);
}
Here I start the PWM output:
//Start the pwm output
HAL_TIM_PWM_Start(&htim20, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim20, TIM_CHANNEL_2);
//Start the pwm complementary output
HAL_TIMEx_PWMN_Start(&htim20, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&htim20, TIM_CHANNEL_2);
and thats how I change my dutycycle:
__HAL_TIM_SET_COMPARE(&htim20, TIM_CHANNEL_2, BC1_dutycycle);
__HAL_TIM_SET_COMPARE(&htim20, TIM_CHANNEL_1, TIM_DUTYCYCLE_MAX);
Best regards, Benjamin
EDIT:
I am shutting down the output of the PWM signal (with HAL_TIM_PWM_Stop(&htim20, TIM_CHANNEL_2); , then this behavour is occuring). How can i just set the main output of the PWM to 0 but the complementary one should still switch?
