2020-10-01 07:21 AM
Hi,i am using STM32F334C6T.
i try to set my PWM as Center Aligned mode and generate interrupt at counter overflow. my code as below.whatever i do in my code, i can not take just one interrupt which is counter overflow. everytime it generates two times which are overflow and underflow.
is there anyone who has any idea about this?
void MX_TIM2_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
htim2.Init.Period = 2400;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 500;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim2);
}
2020-10-01 09:00 AM
The interrupt is generated on both overflow and underflow. This is what the hardware does, you can't change it. You need to adapt your code appropriate with a state variable or by reading the counter or some other method.
2020-10-01 10:47 PM
Thank you fro your answer,
are you sure about it? because it look like from papers, it depends on CMS...the interrrupt is changing with Center aligned modes, it can be set as onderflow,or overflow or both...
2020-10-01 10:49 PM
2020-10-02 07:59 AM
It looks like TIMx->RCR can be used to trigger on every other update, but is only available on some counters. TIM2 is not one of them.