Skip to main content
Associate III
January 19, 2026
Question

NUCLEO F411RE timer in order to generate an interrupt

  • January 19, 2026
  • 3 replies
  • 571 views

Learning on how to code 4x4 keypad with F411RE I came to need to learn 

timer in order to generate an interrupt (saying simply C function should be called lets say every 1 second) 
I created STM32 project CubeIDE+CubeMX  selected TIM3 (in Timers)  -> set Clock Source to InternalClock, set Prescaler ( PSC... ) to 8399 ( googled, found example ),
set Counter Period to 9999 and set NVIC checkmark for TIM3 (enabled)
 Saved IOC file and CubeMX generated some code for me.
In stm32f4xx_it.c file I added 

void TIM3_IRQHandler(void)
{
 /* USER CODE BEGIN TIM3_IRQn 0 */

 /* USER CODE END TIM3_IRQn 0 */
 HAL_TIM_IRQHandler(&htim3);
 /* USER CODE BEGIN TIM3_IRQn 1 */
 HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
 /* USER CODE END TIM3_IRQn 1 */
}

and running the code I expected to see blinking LED ( at least wanted no matter 1 sec or 0.5 sec )

but nothing happens
Did I miss something ? ( I'm learning this stuff )

 

3 replies

TDK
Super User
January 19, 2026

Did you start the timer with interrupts enabled? Is global TIM3 interrupt enabled in IOC?

Should be a HAL_TIM_Base_Start_IT or similar statement in your user code to start the timer.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Roman_EAuthor
Associate III
January 19, 2026

Is TIM3 interrupt enabled in IOC?   Yes , I checkmarked in NVIC CubeMX
I found HAL_TIM_Base_Start_IT  in Drivers/STM32F4xx_HAL_Driver folder header and c source files with HAL_TIM_Base_Start_IT  function
Should I add where ? to main.c file? Logically Yes , I should add "staring TIM3"

before while loop

 

Associate II
January 19, 2026

(Edit: comment out HAL_TIM_Base_Start())

Probably you missed in main.c:

static void MX_TIM3_Init(void)
{
...
 /* USER CODE BEGIN TIM3_Init 2 */
 HAL_TIM_Base_Start_IT(&htim3);
 // HAL_TIM_Base_Start(&htim3);
 /* USER CODE END TIM3_Init 2 */

}

 

Associate II
January 20, 2026

Great to hear it works.

 

Besides your initial problem a suggestion:

You modified void TIM3_IRQHandler(void) in stm32f4xx_it.c to toggle your LED pin.

The (unmodified) function calls HAL_TIM_IRQHandler(&htim3) from stm32f4xx_hal_tim.c, which in turn calls HAL_TIM_PeriodElapsedCallback(htim).

The latter is a weak function defined empty in stm32f4xx_hal_tim.c:

/**
 * @brief Period elapsed callback in non-blocking mode
 * @PAram htim TIM handle
 * @retval None
 */
__weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
 /* Prevent unused argument(s) compilation warning */
 UNUSED(htim);

 /* NOTE : This function should not be modified, when the callback is needed,
 the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
 */
}

HAL_TIM_PeriodElapsedCallback is declared weak as it is intended to be defined somewhere else with more functionality.

 

Long story short: You may override the weak callback function in your code, e.g. in main.c without fiddling with stm32f... files.

main.c might then look like this:

...
/* USER CODE BEGIN 4 */

void HAL_TIM_PeriodElapsedCallback( TIM_HandleTypeDef *htim )
{
 if( htim == &htim3 )
 {
 HAL_GPIO_TogglePin( LED_GPIO_Port, LED_Pin );
 }
}

/* USER CODE END 4 */
...

 This keeps your code together and apart from ST defined code.

Roman_EAuthor
Associate III
January 20, 2026

Thank you for your advice !
As I'm learning on how to use 4x4 keypad with F411re, I understand , that this interrupt function should be called periodically. Frequency it should be called based on Prescaler&Period ( at simple case + can be configured via CubeMX) 

htim3.Init.Prescaler = 8399;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 4499;

What would be your suggestion on "frequency to call the interrupt function" ?
I understand that in this function I need to add code "to scan 4x4 rows and columns". But how often this function should be called ? Every 10 ms ? 20ms?

Andrew Neil
Super User
January 20, 2026

I think your questions here - generating a timer interrupt - has now been answered?

Is so, please mark the solution on the post which provided the answer.

 

You already have a separate thread on 4 x 4 keypad with Nucleo 411RE and HAL, and @gbm has already given a suggestion for the scan rate there...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Technical Moderator
January 20, 2026

Hello @Roman_E 
Hope you enjoy your time learning about timers
you can follow the steps of this article. It will help you learn how to do 1 second interruptions with timers
BR
Gyessine

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.