AnsweredAssumed Answered

Timer interrupt immediately

Question asked by duquesne.joachim on Oct 19, 2014
Latest reply on Oct 30, 2014 by duquesne.joachim
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 !

01.void main()
02.{
03.    //
04.    .....
05.    MX_TIM7_Init();
06.    MX_GPIO_Init();
07.    ......
08.    //
09.     
10.    while(1)
11.    {
12.        __HAL_TIM_SetCounter(&htim7,0);
13.        HAL_TIM_Base_Start_IT(&htim7);
14.        HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_SET);
15.    }
16. }



17.void MX_TIM7_Init(void)
18.{
19. 
20.  TIM_MasterConfigTypeDef sMasterConfig;
21. 
22.  htim7.Instance = TIM7;
23.  htim7.Init.Prescaler = (SystemCoreClock/2/1000000) - 1 ; // Config for us count
24.  htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
25.  htim7.Init.Period = 10;
26.      HAL_TIM_Base_Init(&htim7);
27. 
28.   ///////////////////////////////////////////////////////////////////
29.      // THIS IS THE update interrupt enable BIT
30.  __HAL_TIM_CLEAR_IT(&htim7, TIM_IT_UPDATE);  
31.   ///////////////////////////////////////////////////////////////////

32.  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
33.  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
34.  HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig);
35. 
36. 
37.    /* Peripheral clock enable */
38.    __TIM7_CLK_ENABLE();
39. 
40.    /* Peripheral interrupt init*/
41. 
42.    HAL_NVIC_SetPriority(TIM7_IRQn, 0, 0);
43.    HAL_NVIC_ClearPendingIRQ(TIM7_IRQn);
44.    HAL_NVIC_EnableIRQ(TIM7_IRQn);
45. 
46.}
47. 
48. 



49./**
50.* @brief This function handles TIM7 global interrupt.
51.*/
52.void TIM7_IRQHandler(void)
53.{
54.    HAL_TIM_IRQHandler(&htim7);
55.}
56. 
57.void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
58.{
59.    if(htim->Instance = TIM7)
60.    {
61.        HAL_TIM_Base_Stop_IT(htim);
62.        HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_RESET);
63. 
64.    }
65. 
66.}

Outcomes