2021-09-17 12:07 PM
I'm using an STM32G431 using differential outputs on channel 1. On the scope, my deadtime is set where the channels overlap instead of having deadtime:
channel1
___-------___
------__-------
channel1_n
How can I reverse the direction of the deadtime?
Here is my PWM setup:
#define PWM_TICKS_PER_ISR_TICK ((uint16_t)(1))
#define PWM_FREQ_KHZ ((float_t)(27.0))
#define PWM_SRC_FREQ_MHz ((float_t)(170.0))
obj->pwmHandle->Instance = TIM1;
obj->pwmHandle->Init.Prescaler = 0;
obj->pwmHandle->Init.CounterMode = TIM_CR1_CMS_0;
obj->pwmHandle->Init.Period = (uint32_t)(pwmSourceClock_MHz * (float_t)1000.0 * (float_t)0.5 / pwmFrequency_kHz);
obj->pwmHandle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
obj->pwmHandle->Init.RepetitionCounter = PWM_TICKS_PER_ISR_TICK * 2 - 1;
obj->pwmHandle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(obj->pwmHandle) != HAL_OK)
{
obj->error = error_HAL_TIM_Base_Init;
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(obj->pwmHandle, &sClockSourceConfig) != HAL_OK)
{
obj->error = error_HAL_TIM_ConfigClockSource;
Error_Handler();
}
if (HAL_TIM_PWM_Init(obj->pwmHandle) != HAL_OK)
{
obj->error = error_HAL_TIM_PWM_Init;
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(obj->pwmHandle, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
obj->error = error_HAL_TIM_PWM_ConfigChannel;
Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = ((uint16_t)(0.3 * PWM_SRC_FREQ_MHz));
sBreakDeadTimeConfig.BreakState = TIM_BREAK_ENABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(obj->pwmHandle, &sBreakDeadTimeConfig) != HAL_OK)
{
obj->error = error_HAL_TIMEx_ConfigBreakDeadTime;
Error_Handler();
}
HAL_TIM_PWM_Start(obj->pwmHandle, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(obj->pwmHandle, TIM_CHANNEL_1);
Solved! Go to Solution.
2021-09-24 10:25 AM
This works great! Switching both OCPolarity, OCNPolarity and TIM_OCMODE_PWM1 did what I needed. Thank you!
2021-09-17 12:16 PM
Did you measure those waveforms directly at the mcu pins?
Read out and check/post the timer registers' content.
JW
2021-09-17 01:12 PM
Here are the timer registers:
I have managed to change the symptoms by switching the polarity of:
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
This does not fully solve my issue though, because in this case, the PWMs have reverse polarity.
2021-09-17 02:44 PM
I don't quite know what do you mean by this, but try using PWM2 mode instead of PWM1.
JW
2021-09-24 10:25 AM
This works great! Switching both OCPolarity, OCNPolarity and TIM_OCMODE_PWM1 did what I needed. Thank you!