Question
Cant Setup Timer Interrupt with HAL and STM32CubeMX.
Im using an STM32L452RE dev. board. and Im trying to set an interrupt once after some time. I have the following subroutines.
#define MICROSECOND_TICK_COUNT 80 //clock = 80 MHZ i.e. 1e-6/(1/80e6)=80
TIM_HandleTypeDef myTimer;
void setupTimer(void){
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
myTimer.Instance = TIM2;
myTimer.Init.Prescaler =0;
myTimer.Init.CounterMode = TIM_COUNTERMODE_UP;
myTimer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
myTimer.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
myTimer.Init.Period = 0;
HAL_TIM_Base_Init(&myTimer);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&myTimer, &sClockSourceConfig)
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&myTimer, &sMasterConfig) != HAL_OK
}
void startTimer(uint32_t microseconds){
myTimer.Init.Period = microseconds*MICROSECOND_TICK_COUNT;
HAL_TIM_Base_Init(&myTimer)
HAL_TIM_Base_Start_IT(&myTimer);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
if(htim->Instance==myTimer.Instance){
stopTimer2();
}
}The interrupt is generated but the timing is incorrect. I checked with a scope and independently of the values I set for Period, Prescaler and ClockDivision from the HAL struct "TIM_HandleTypeDef " in my subroutines I always get the interrupt at a same time. . Is there anything else Im missing ?