cancel
Showing results for 
Search instead for 
Did you mean: 

NUCLEO F411RE timer in order to generate an interrupt

Roman_E
Associate II

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 )

 

6 REPLIES 6
TDK
Super User

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".

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

 

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 */

}

this is what I found in my main.c ( CubeMX generated )

and looks like there is no "start TIM3 code"

static void MX_TIM3_Init(void)
{

  /* USER CODE BEGIN TIM3_Init 0 */

  /* USER CODE END TIM3_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM3_Init 1 */

  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 8399;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 9999;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */

  /* USER CODE END TIM3_Init 2 */

}

yes.

The generated code only configures the timer, but does not start it.

See my post above...

I added code ( as per your suggestion ) 

 /* USER CODE BEGIN TIM3_Init 2 */
  HAL_TIM_Base_Start_IT(&htim3);
  HAL_TIM_Base_Start(&htim3);
  /* USER CODE END TIM3_Init 2 */

and it works ! thank you !
And BTW , do I need to add both ?