STM32F103VBT6 LED BLINKING WITH TIMER INTERRUPT (USING KEIL & STM32CUBEMX)

In this tutorial, we will be learning how to blink a LED with timer interrupt. The softwares KEIL and STM32CUBEMX is used for the programming.

                                              stm103fvbt6

STM32F103VBT6 consists of 3 synchronizable general purpose 16- bit timers embedded in it.

Now, let’s learn how the board works in this example…….

Components Required

  • Development board using STM32F103VBT6.
  • Softwares: KEIL, STM32CUBEMX.
  • RS232 Serial cable.

Procedure

Step1: Installing the Softwares

Step2:  Create Project Using STM32CubeMX

STM32F103VBT6 board is used here. The main features of the board are:

  • ARM® 32-bit Cortex®-M3 CPU Core.
  • Memories.
  • 4-to-16 MHz crystal oscillator.
  • Low-power.
  • DMA.
  • Up to 80 fast I/O ports.
  • 7 timers.
  • Up to 9 communication interfaces.

In order to start a new project and to select the device see the figure below.

 

 

new project

SELECT DEVICE

Step3:  Set the LED in STM32CubeMX

set led in stm32cubemx

Step4: Set the timer1 for interrupt.

  • Set the parameter settings as shown in the figure below.

parameter settings

  • Set the NVIC settings as shown in the figure below.

nvic settings

Step4: Program

Here we are setting PE5, to which the LED is connected. Timer1 is used for interrupt purpose. In the program, when the counter value is greater than or equal to 500, only the LED will start to toggle. Otherwise, the timer 1 interrupt function will be carried out.

#include "stm32f1xx_hal.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

TIM_HandleTypeDef htim1;

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

void Error_Handler(void);

static void MX_GPIO_Init(void);

static void MX_TIM1_Init(void);

/* USER CODE BEGIN PFP */

/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main(void)

{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_TIM1_Init();

  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  HAL_TIM_Base_Start_IT(&htim1); //START TIMER 1 IN INTERRUPT MODE

  while (1)

  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  }

  /* USER CODE END 3 */

}

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct;

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

  RCC_OscInitStruct.HSIState = RCC_HSI_ON;

  RCC_OscInitStruct.HSICalibrationValue = 16;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

  {

    Error_Handler();

  }

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)

  {

    Error_Handler();

  }

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */

  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* TIM1 init function */

static void MX_TIM1_Init(void)

{

  TIM_ClockConfigTypeDef sClockSourceConfig;

  TIM_MasterConfigTypeDef sMasterConfig;

  htim1.Instance = TIM1;

  htim1.Init.Prescaler = 64000;

  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim1.Init.Period = 1;

  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  htim1.Init.RepetitionCounter = 0;

  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)

  {

    Error_Handler();

  }

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

  if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)

  {

    Error_Handler();

  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)

  {

    Error_Handler();

  }

}

/** Configure pins as

        * Analog

        * Input

        * Output

        * EVENT_OUT

        * EXTI

*/

static void MX_GPIO_Init(void)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */

  __HAL_RCC_GPIOE_CLK_ENABLE();

  /*Configure GPIO pin Output Level */

  HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET);

  /*Configure GPIO pin : PE5 */

  GPIO_InitStruct.Pin = GPIO_PIN_5;

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**

  * @brief  This function is executed in case of error occurrence.

  * @param  None

  * @retval None

  */

void Error_Handler(void)

{

  /* USER CODE BEGIN Error_Handler */

  /* User can add his own implementation to report the HAL error return state */

  while(1)

  {

  }

  /* USER CODE END Error_Handler */

}

#ifdef USE_FULL_ASSERT

/**

   * @brief Reports the name of the source file and the source line number

   * where the assert_param error has occurred.

   * @param file: pointer to the source file name

   * @param line: assert_param error line source number

   * @retval None

   */

void assert_failed(uint8_t* file, uint32_t line)

{

  /* USER CODE BEGIN 6 */

  /* User can add his own implementation to report the file name and line number,

    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* USER CODE END 6 */


}


#endif


/**

  * @}

  */


/**

  * @}

*/


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

 

Also include the below code in ‘STM32Fxx_it.c’

static uint32_t counter = 0;

if (counter >= 500)

{

        HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);

        counter = 0;

 }

 counter++;

Press F7 or build button to compile the program. Generate the hex file.

Step5: Burn to the development board.

Press the load button or F8. Load the hex file to the board using Flash loader demo. The program will be loaded to your board.

Step6: Output.

LED will be toggling into ON or OFF condition based on a time interval.

User Review (2)

SUJITH MATHEW   Kochi,Kerala

STM32F103VBT6 OR STM32F103V8T??

Reply  Report Abuse   13 June,2016 11:06

Mepits

We use STM32F103VBT6 here

Reply   Report Abuse   13 June,2016 02:06
Related Items