top of page

STM32 Timer Interrupt HAL Example


Previous Tutorial

Tutorial 13

Next Tutorial


1. Explain about Example:

In this example, we'll utilize the TIMER peripheral of the STM32 NUCLEO L476RG microcontroller to generate periodic interrupts. We'll configure the timer to generate interrupts at a specified interval, and upon each interrupt, an LED connected to the board will toggle its state.


2. Required Parts For Example:

  • STM32 NUCLEO L476RG board

  • LED (and appropriate resistor)

  • USB cable for connecting the board to your computer

  • CUBEIDE software for STM32 development

3. CUBEIDE Configuration Steps:

  1. Open CUBEIDE and create a new project for the STM32 NUCLEO L476RG board.

  2. Configure the TIMER peripheral to generate interrupts at the desired frequency.

  3. Enable the GPIO peripheral for controlling the LED.

  4. Configure the NVIC (Nested Vector Interrupt Controller) for handling TIMER interrupts.

  5. Generate code and open the main.c file for editing.


--> Configure GPIO: Configure PA5 As An Output




--> Enable The Timer Interrupt Signal In NVIC Tab




--> Configure Timer2 Peripheral: Prescaler:8000, Counter Period:1000




#include "stm32l4xx_hal.h"

TIM_HandleTypeDef htim;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM_Init(void);

int main(void) {
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_TIM_Init();

  HAL_TIM_Base_Start_IT(&htim);

  while (1) {
    // Main loop
  }
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
  if (htim->Instance == TIM2) {
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // Toggle LED
  }
}

void SystemClock_Config(void) {
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 10;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);
}

static void MX_GPIO_Init(void) {
  __HAL_RCC_GPIOA_CLK_ENABLE();

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  GPIO_InitStruct.Pin = GPIO_PIN_5;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

static void MX_TIM_Init(void) {
  __HAL_RCC_TIM2_CLK_ENABLE();

  htim.Instance = TIM2;
  htim.Init.Prescaler = 7999;
  htim.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim.Init.Period = 999;
  htim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  HAL_TIM_Base_Init(&htim);
}

5. Result:

The LED connected to pin PA5 will toggle its state every time the TIMER interrupt occurs, indicating that the interrupt is functioning correctly.

Remember to connect an LED with an appropriate resistor to pin PA5 on the STM32 NUCLEO L476RG board.



Previous Tutorial

Tutorial 13

Next Tutorial


 
 
 

Recent Posts

See All

Comments


bottom of page