Reputation: 19
Sourced to internal clock at APB1=96 MHz, I want to start a count up from zero via software command and generate a timer interrupt when the auto-reload register value is reached. I can only get the interrupt to fire if I use the HAL function HAL_TIM_Base_Start_IT that both activates the UPDATE interrupt and starts the clock. But then it fires immediately instead of waiting for the count to be reached?
//********** timer initialization ************************************
static void MX_TIM3_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim3.Instance = TIM3;
htim3.Init.Prescaler = 1000;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 1000;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) !=
HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
//************ timer activation code in main program ******************
HAL_NVIC_DisableIRQ(TIM3_IRQn);
TIM3->CR1 |= 0;
TIM3->CNT=0;
HAL_NVIC_ClearPendingIRQ(TIM3_IRQn);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
TIM3->CR1 |= 1;
//**************** timer ISR ***************************************
void TIM3_IRQHandler(void)
{
doStuff();
HAL_NVIC_ClearPendingIRQ(TIM3_IRQn);
HAL_TIM_IRQHandler(&htim3);
}
Upvotes: 1
Views: 1679
Reputation: 381
If you don't want the interrupt to fire immediately after the start, try clearing the UPDATE flag before starting the timer:
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_TIM_CLEAR_FLAG(&htim3, TIM_IT_UPDATE);
if(HAL_TIM_Base_Start_IT(&htim3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
Upvotes: 3