cancel
Showing results for 
Search instead for 
Did you mean: 

Basic example for using a timer to measure time using interrupt

ihouses ihouses
Associate III
Posted on February 23, 2018 at 22:58

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!

4 REPLIES 4
henry.dick
Senior II
Posted on February 24, 2018 at 01:36

'I know that the communication as stopped.'

you don't need something that complicated to do what you want to do: in the uart ***receiving isr***, flip a pin.

that's all it takes.

if you want to be fancy, you can have a free running clock, like dwt or systick, and you can time stamp events.

ihouses ihouses
Associate III
Posted on February 24, 2018 at 10:40

I need a timer that generate an interrupt AFTER the last byte is received in the USART ISR. In each call of the USART ISR I reset the counter. When the TIMER generate his interrupt I enable a FLAG that is used in the main function. I never used systick or dwt before that I think that only used for measuring time, right?

I am using a M0 STM32.

I am looking around and I don't see any examples of 'really simple' things like I am asking for.

:(

ihouses ihouses
Associate III
Posted on February 24, 2018 at 13:20

The last byte is the one that after it the timer interrupt occurs.

I am using kind of 'time syncronization' method.

Somehow I managed to use HAL functions to do what I descrived regarding the timer and the usart.

Now my problem is that when the mass storage device pops up the virtual serial port doesnt work anymore. Looks like I can transmit to the microcontroller but there is no communication back

🙂

Posted on February 24, 2018 at 13:05

'

I don't see any examples of 'really simple' things like I am asking for.

 '

doesn't that get you to think hard about what you are trying to do?

you first will need to figure out a way to tell what the last byte is, and if it has been received. once you are in the uart receiving isr, that byte has been received. there is no use for a 'timer isr'.

ie, what you are trying to do makes very little sense logically.