STM32F103VBT6 LED BLINKING (USING KEIL & STM32CUBEMX)

STM32F103VBT6 is a 32 bit ARM Cortex M3 microcontroller useful for many embedded applications.  Here, let’s learn how to blink a LED with STM32F103VBT6 using KEIL & STM32CUBEMX.

                                                              STM32F103VBT6

LED is a simple semiconductor diode with two leads. This pn junction diode emits the light when it gets activated. A suitable voltage applied will cause the electrons and holes to combine, thereby generating energy in the form of light.

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 in STM32CUBEMX

SELECT DEVICE

Step3:  Set the LED’s in STM32CubeMX

SET LED

Step4: Program

Here we are setting PE5, PE6, to which the LED’s are connected.  PE5 is given the label Ld1 and PE6 is given the label Ld2.

LABEL

In the program Leds will be toggling for an interval.

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

#include "stm32f1xx_hal.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

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

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

/* 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();

  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

HAL_GPIO_TogglePin(Ld1_GPIO_Port,Ld1_Pin); //Toggle LED

HAL_Delay(1000); //Delay 1 Seconds

HAL_GPIO_TogglePin(Ld2_GPIO_Port,Ld2_Pin); //Toggle LED

HAL_Delay(1000); //Delay 1 Seconds

  }

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

}

/** 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, Ld1_Pin|Ld2_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pins : Ld1_Pin Ld2_Pin */

  GPIO_InitStruct.Pin = Ld1_Pin|Ld2_Pin;

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

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.

The LED blinks.

User Review (2)

SUJITH MATHEW   Kochi,Kerala

STARTED WITH STM32F103VBT6, BUT THE PICTURES BELOW SHOWS STM32F103V8T. IS IT CORRECT?

Reply  Report Abuse   13 June,2016 11:06

Mepits

We are using STM32F103VBT6..

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