Question
Timer interrupt immediately
Posted on October 19, 2014 at 00:56
Hi,
I'm experimenting with an ultrasonic range finder (HC-SR04 for info). In order to generate a measurement from the device, the mcu must generate 1 pulse of +/-10us. My idea is to set a GPIO to high, start a timer (tim7 here) and when the timer reach 10us, an interrupt is generated and I reset the GPIO. At first, as soon as I call HAL_TIM_Base_Start_IT(&htim7);, the interrupt was generated immediately. I found that I needed to disable the update interrupt enable bit for this to work. !?!?!? How is it that I have to disable the interrupt to use it ? Something to do with the difference between event and interrupt ? (I still didn't catch the difference between those two... ) Thanks for all your input !
void main()
{
//
.....
MX_TIM7_Init();
MX_GPIO_Init();
......
//
while(1)
{
__HAL_TIM_SetCounter(&htim7,0);
HAL_TIM_Base_Start_IT(&htim7);
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_SET);
}
}
void MX_TIM7_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig;
htim7.Instance = TIM7;
htim7.Init.Prescaler = (SystemCoreClock/2/1000000) - 1 ; // Config for us count
htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
htim7.Init.Period = 10;
HAL_TIM_Base_Init(&htim7);
///////////////////////////////////////////////////////////////////
// THIS IS THE update interrupt enable BIT
__HAL_TIM_CLEAR_IT(&htim7, TIM_IT_UPDATE);
///////////////////////////////////////////////////////////////////
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig);
/* Peripheral clock enable */
__TIM7_CLK_ENABLE();
/* Peripheral interrupt init*/
HAL_NVIC_SetPriority(TIM7_IRQn, 0, 0);
HAL_NVIC_ClearPendingIRQ(TIM7_IRQn);
HAL_NVIC_EnableIRQ(TIM7_IRQn);
}
/**
* @brief This function handles TIM7 global interrupt.
*/
void TIM7_IRQHandler(void)
{
HAL_TIM_IRQHandler(&htim7);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance = TIM7)
{
HAL_TIM_Base_Stop_IT(htim);
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_RESET);
}
}
#hal-tim-stm32-cube-interrupt