AnsweredAssumed Answered

Basic example for using a timer to measure time using interrupt

Question asked by ihouses ihouses on Feb 23, 2018
Latest reply on Feb 24, 2018 by ihouses ihouses

Hello,

 

I need to implement a very basic timer function, the idea is:

 

I receive a character in the USART interrupt. Each time that I receive the character I enable a TIMER. When the serial communication stops the TIMER generates an interrupt and then I know that the communication as stopped. If the communication continues the TIMER is reset in the USART interrupt function.

 

Timer21 it suppose to trigger an interrupt each  1 ms.

 

In order to do I prepared some "demo" code like:

 

in Main()

 

 HAL_TIM_Base_Start_IT(&htim21);

 

  while(1)
  {
     HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin,GPIO_PIN_SET);
     __HAL_TIM_SET_COUNTER(&htim21, 0);
     __HAL_TIM_ENABLE_IT(&htim21, TIM_IT_UPDATE);
     HAL_Delay(100);
  }

 

In the TIMER interrupt:

 

void TIM21_IRQHandler(void)
{
  /* USER CODE BEGIN TIM21_IRQn 0 */
    HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin,GPIO_PIN_RESET);
  /* USER CODE END TIM21_IRQn 0 */
  HAL_TIM_IRQHandler(&htim21);
  /* USER CODE BEGIN TIM21_IRQn 1 */

  __HAL_TIM_DISABLE_IT(&htim21, TIM_IT_UPDATE);

  /* USER CODE END TIM21_IRQn 1 */
}

 

With this code I was expecting see the LED3 blinking but this is only happening one time.

 

Actually I don't know if this the proper way todo that.

 

Any ideas?

 

Thanks!

Outcomes