cancel
Showing results for 
Search instead for 
Did you mean: 

Timer interrupt immediately

joachim2
Associate II
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
4 REPLIES 4
chen
Associate II
Posted on October 20, 2014 at 11:49

Hi

''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''

How are you coming to this conclusion?

10us is faster than you can blink!

Are you trying to debug it?

Have you checked with an oscilloscope or logic analyser that you get the pulse?

(FYI, you might have to manually set the Count in TIM7 CNT register before starting the timer.)

joachim2
Associate II
Posted on October 28, 2014 at 11:23

Hi,

Actually, I can blink pretty fast ! ;)

The reason I know I have a pulse is because I receive a (correct) distance reading. This means the device receives the pulse.

I do clear the counter, it is the 1st instruction in my while(1) loop.

I know the interrupt occur immediately (no distance reading in that case and without the update interrupt disable instruction) because if I put a LED in GPIOG 2, it stays SET. The reason for this is that the MCU enter the interrupt handler (which reset the pin) BEFORE the pin is actually SET in the main while(1) loop.

When the ''update interrupt'' bit is reset, my code is working (almost) perfectly but why do I have to reset that bit ?

Thanks 

chen
Associate II
Posted on October 28, 2014 at 11:53

''I do clear the counter, it is the 1st instruction in my while(1) loop.''

So you are constantly re-initialising TIM7 CNT register and not allowing the Timer to work correctly.

joachim2
Associate II
Posted on October 30, 2014 at 16:04

Well you are absolutely right, I just happened to forgot to copy the delay of 1sec at the end of the while loop which gives plenty of time for the interrupt to complete.