STM32F4 Timer Interrupt

In this demo, I will show you how to configure a hardware timer interrupt on STM32F4 MCU. These interrupts are quiet useful in a variety of applications.

The main system clock  is configured below to run at a frequency of: 168 MHz, and in this example, I used timer 3 interrupt and I configured it to expires at every 500ms. Every time we get an interrupt ,a green LED blinks.

Timer 3 clock frequency is 84MHz. To have this timer expires every 500ms, we set the pre-scaler at 8400-1 and period to: 5000-1.

System Clock Configuration

/** System Clock Configuration
* @brief System Clock Configuration
* The system Clock is configured as follow : 
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 168000000
* HCLK(Hz) = 168000000
* AHB Prescaler = 1
* APB1 Prescaler = 4
* APB2 Prescaler = 2
* HSE Frequency(Hz) = 8000000
* Main regulator output voltage = Scale1 mode
* Flash Latency(WS) = 5
*/
void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;

  /* Enable Power Control clock */
  __PWR_CLK_ENABLE();

  /* The voltage scaling allows optimizing the power consumption when the device is 
     clocked below the maximum system frequency, to update the voltage scaling value 
     regarding system frequency refer to product datasheet.  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /* Enable HSE Oscillator and activate PLL with HSE as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
  
  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}

Timer3 Interrupt Configuration

void Timer3_Interrupt_Config(void)
{
/* Compute the prescaler value to have TIM3 counter clock equal to 10 KHz */
uwPrescalerValue = (uint32_t) ((SystemCoreClock /2) / 10000) - 1;

/* Set TIMx instance */
Tim3Handle.Instance = TIM3;

/* Initialize TIM3 peripheral as follow:
+ Period = 5000 - 1
+ Prescaler = ((SystemCoreClock/2)/10000) - 1  //=8400-1
+ ClockDivision = 0
+ Counter direction = Up
*/
Tim3Handle.Init.Period = 5000 - 1; //expires at 500ms intervals
Tim3Handle.Init.Prescaler = uwPrescalerValue;//=8400-1
Tim3Handle.Init.ClockDivision = 0;
Tim3Handle.Init.CounterMode = TIM_COUNTERMODE_UP;

  if(HAL_TIM_Base_Init(&Tim3Handle) != HAL_OK)
  {
   /* Initialization Error */
   Error_Handler();
  }
  /*Start the TIM Base generation in interrupt mode */
  if(HAL_TIM_Base_Start_IT(&Tim3Handle) != HAL_OK)
  {
   /* Starting Error */
   Error_Handler();
  }
}

Timer3 Interrupt Handler

void TIM3_IRQHandler(void)
{
HAL_TIM_IRQHandler(&Tim3Handle);
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *Tim3Handle)
{
  HAL_GPIO_TogglePin(GPIOI,Green_LED); //toggle an LED jsut for testing/debugging
}

Green LED Demo

 

Green LED blinks every 1 second

Source Code

The source code can be downloaded from my Github repository at: TimerInterrupt This Keil project also includes an I2C configuration and interface with an STM EEPROM.

If you like this posting or have any questions, please drop us a feedback in the comments section!

References

[1] STM32F407xx datasheet, https://www.st.com/resource/en/datasheet/dm00037051.pdf

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.