2024-09-11 10:05 AM
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();
}
}
Solved! Go to Solution.
2024-09-12 08:07 AM
@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.
2024-09-12 11:13 AM
update event = fosc / [(1 + ARR) * (1 + PSC)]
= 32000000 / [(1 + 7811) * (2047)]
= 2 Hz
it's a half-second blip.
2024-09-12 12:20 PM
@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.
2024-09-12 12:32 PM
I am using TIM2 with something else, so sometimes using HAL_GetTick breaks that feature. You're not being very helpful at all.
2024-09-12 01:06 PM
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