cancel
Showing results for 
Search instead for 
Did you mean: 

How to reset Timer interrupt counter?

Naos Y
Associate II

I'd like to turn LED on or off according to UART receiving situation.

What I'd like to do is following,

1. If there is no receiving during 3 sec from last data, the LED is turned on (showing NO RECEIVING).

2. When UART RX interrupt comes, the LED is turned off (showing RECEIVING) .

I tried following code, but sometimes the LED turned on during receiving with 1 Hz rate  (one byte comes each 1 sec).

// UART RX interruption (UART is 460800bps)

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)

{

 <...turn LED off...>

 // I'd like to restart the timer interruption from 0 again.

 HAL_TIM_Base_Stop_IT(&htim2);

 HAL_TIM_Base_Start_IT(&htim2);

}

// htim2 has been set to 3s interval and started.

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

 HAL_TIM_Base_Stop_IT(&htim2);

 <...turn LED on...>

}

I tried it with both of TIM2 or TIM4. The results were same.

I think I should reset something when restart (stop and start) the timer inturruption, but I don't understand about it.

I use STM32L496RGT6, TM32CubeMX V5.4.0, and IAR Embedde Workbench for ARM V8.4.0 with Windows 10 PC.

Could you give me some advice?

Sorry for the basic question again.

Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions

I don't use Cube.

Assuming TIMx_PSC*TIMx ARR is set to given timeout (3 seconds) and update interrupt is enabled both in TIMx_DIER and in NVIC:

  • when UART byte arrives, clear TIMx_CNT (i.e. write 0 to it) and set TIMx_CR1.EN and turn LED off;
  • in update interrupt, clear TIMx_CR1.EN and turn LED on

JW

View solution in original post

3 REPLIES 3

I don't use Cube.

Assuming TIMx_PSC*TIMx ARR is set to given timeout (3 seconds) and update interrupt is enabled both in TIMx_DIER and in NVIC:

  • when UART byte arrives, clear TIMx_CNT (i.e. write 0 to it) and set TIMx_CR1.EN and turn LED off;
  • in update interrupt, clear TIMx_CR1.EN and turn LED on

JW

Thanks to your kind instruction, I understood that well.

I modified the code as follwing, and it works as intended.​

// UART RX interruption (UART is 460800bps)

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)

{

 __HAL_TIM_SET_COUNTER(&htim2, 0);

 __HAL_TIM_ENABLE(&htim2);

 <...turn LED off...>

}

// htim2 has been set to 3s interval and started.

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

__HAL_TIM_DISABLE(&htim2);

 <...turn LED on...>

}

Glad it helped.

JW