STM32F103VBT6 UART TRANSMISSION USING KEIL & STM32CUBEMX

UART transmission allows serial communication between the PC and the board. Here, lets learn how to perform serial communication using our ARM development board with STM32F103VBT6.

Procedure

Step1: Components Required

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

Step2: Installing the Softwares

Step3:  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.

 

stm32cubevbtx

 

SELECT DEVICE

Step4:  Set the LED in STM32CubeMX

Step5: Set UART2

set uart

Set PA2 as the UART_Tx and PA3 as the UART_Rx.

Step6: Program

#include "stm32f1xx_hal.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

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

UART_HandleTypeDef huart2;

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

  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

 char buff[50] = "WWW.MEPITS.COM";

 HAL_UART_Transmit(&huart2,buff,strlen(buff),1000);

  }

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

}

/* USART2 init function */

static void MX_USART2_UART_Init(void)

{

  huart2.Instance = USART2;

  huart2.Init.BaudRate = 115200;

  huart2.Init.WordLength = UART_WORDLENGTH_8B;

  huart2.Init.StopBits = UART_STOPBITS_1;

  huart2.Init.Parity = UART_PARITY_NONE;

  huart2.Init.Mode = UART_MODE_TX_RX;

  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart2.Init.OverSampling = UART_OVERSAMPLING_16;

  if (HAL_UART_Init(&huart2) != HAL_OK)

  {

    Error_Handler();

  }

}

/** Pinout Configuration

*/

static void MX_GPIO_Init(void)

{

  /* GPIO Ports Clock Enable */

  __HAL_RCC_GPIOA_CLK_ENABLE();

}

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

/**

  * @}

  */

/**

  * @}

*/

Here, the text “www.mepits.com” will be printed.

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

Step7: Burn to the development board.

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

Now, to view the UART transmission output, download docklight software. https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwiQzK7H65fNAhVBgI8KHQl-A7QQFggbMAA&url=http%3A%2F%2Fdocklight.de%2Fdownloads%2F&usg=AFQjCNGBhkz3Nz6LFgCSF8hAHuKXAgtNRA&sig2=W_5AoCPf2ZYphV3HP3uCog&bvm=bv.124088155,d.c2I&cad=rja

Set the com port and the baud rate.

Also, place the jumpers in the correct position. Once it is set, press the reset button on the development board.

www.mepits.com  will be displayed on the serial monitor.

mepits output

User Review (0)
Related Items