cancel
Showing results for 
Search instead for 
Did you mean: 

TIM3 periodElapsedCallback never called

JustSomeGuy
Senior

In my code, after setting an on-board LED, the timer is started like this.

 __HAL_TIM_SET_COUNTER(&htim3, 0);
while(HAL_TIM_Base_Start_IT(&htim3) != HAL_BUSY);

In debugging mode, I saw that the CNT register was in fact incrementing, however in would always rollover and would never trigger this callback function:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIM3)
	{
		while(HAL_TIM_Base_Stop_IT(htim) != HAL_BUSY);
		HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, RESET);
	}
}

 I saw on another forum post that someone suggested to put that code in stm32l0xx.c in the function TIM3_IRQHandler(). However when I did that, I found that the function was called instantly after HAL_TIM_Base_Start_IT(&htim3). 

What is going on here? I opened a new project with the EXACT same settings for timer 3, and it worked perfectly. This project has become far too massive to move everything over to a new project without a lot of headache. I would prefer to find out how to solve this in the project I am currently working on. Here is the cubeMX configuration:

static void MX_TIM3_Init(void)
{
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 2047;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 7811;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
}
14 REPLIES 14

@JustSomeGuy wrote:

Some more information: I removed the function HAL_TIM_BASE_Stop_IT(), moved HAL_TIM_BASE_Start_IT() to the top of main(), and set a boolean value to indicate that the LED is set when I cleared the CNT register. in the interrupt callback, I check the timer instance and that boolean value to check whether the LED has been set. Now it's working flawlessly.


I never understood what you're doing with the LED in your first post. The timer interrupt would be so quick, that you would probably see a blip of the LED going On, then Off.

Now i'm more confused about the LED.

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

update event = fosc / [(1 + ARR) * (1 + PSC)]

                      = 32000000 / [(1 + 7811) * (2047)]

                      = 2 Hz

it's a half-second blip.


@JustSomeGuy wrote:

update event = fosc / [(1 + ARR) * (1 + PSC)]

                      = 32000000 / [(1 + 7811) * (2047)]

                      = 2 Hz

it's a half-second blip.


At that rate, you could have just used the System tick instead of creating another timer.

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

I am using TIM2 with something else, so sometimes using HAL_GetTick breaks that feature. You're not being very helpful at all.

I didn't say use HAL_GetTick. Think outside of the box. Just use the System Tick interrupt to your advantage. 

https://www.youtube.com/watch?v=o0qhmXR5LD0

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.