# STM32 - Timer Interrupts
[TOC]
## References
### [Getting Started with STM32 and Nucleo Part 6: Timers and Timer Interrupts | Digi-Key Electronic](https://youtu.be/VfbW6nfG4kw)
{%youtube VfbW6nfG4kw %}
To avoid busy-checking timestamp to determine if an event should fire, in stead of something like this:
```c
int main()
{
HAL_TIM_Base_Start_IT(&htim10);
while (1)
{
if (__HAL_TIM_GET_COUNTER(&htim10) - last > THRESHOLD) {
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
last = __HAL_TIM_GET_COUNTER(&htime10);
}
}
}
```
you can implement callbacks for timer-related events listed **TIM Callbacks functions** of **TIM Firmware driver API description** in HAL manuals, and then start timer with interrupt-based APIs like `HAL_TIM_Start_Base_IT`. For example, after setting appropriate timer parameters for `TIM10` and implement a `HAL_TIM_PeriodElapsedCallback`:
```c
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
int main()
{
HAL_TIM_Base_Start_IT(&htim10);
while (1) {}
}
void HAL_TIM_PeriodElapsedCallback (TIM_HandleTypeDef *htim)
{
if (htim->Instance == htim10.Instance)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
}
```
The `HAL_TIM_PeriodElapsedCallback` is triggered whenever its counter period gets reset (e.g. count over the Counter Period). Note that those two examples may require different settings on `TIM10`, so they are not really equivqlent.
### [HAL #10: HowTo Timer with Interrupt](https://youtu.be/YPJlhYm5T8Y)
{%youtube YPJlhYm5T8Y %}