cancel
Showing results for 
Search instead for 
Did you mean: 

Generate interrupt on center aligned mode at counter overflow

AKOCA.1
Associate II

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);

}

4 REPLIES 4
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".
AKOCA.1
Associate II

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...

AKOCA.1
Associate II

0693W000004IS6YQAW.jpg

TDK
Guru

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.

0693W000004IXcuQAG.png

If you feel a post has answered your question, please click "Accept as Solution".