Thursday, October 19, 2023

STM32F103R6 UART1 And Character LCD Example

UART can be interfaced with some module such as GSM, Bluetooth, Nextion display, GPS module, etc. Communicating with a PC via an RS-232 interface or a USB to UART converter module is common.

In this example, I will use a UART terminal to input ASCII characters that will send to the STM32F103R6 micro-controller. Those characters will display on a 20x4 LCD.

STM32F103R6 UART1 And Character LCD Example
Simulating Program

In the Code Configuration Tool, please set the following parameters.

STM32F103R6 UART1 And Character LCD Example
GPIO Settings

On SYS tab select Serial Wire as Debug interface if you wish to use the ST-Link.

STM32F103R6 UART1 And Character LCD Example
Debug Tool Selection

On USART1 tab select Asynchronous Mode to get UART.

STM32F103R6 UART1 And Character LCD Example
UART Settings

Source code is very easy to write with the aid of code configuration tool in the STM32CubeIDE. LCD library is already written in previous post. 

  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file : main.c
  5.   * @brief : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   * opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "lcd4bits.h"
  23.  
  24. /* Private variables ---------------------------------------------------------*/
  25. UART_HandleTypeDef huart1;
  26.  
  27. /* Private function prototypes -----------------------------------------------*/
  28. void SystemClock_Config(void);
  29. static void MX_GPIO_Init(void);
  30. static void MX_USART1_UART_Init(void);
  31.  
  32. /**
  33.   * @brief The application entry point.
  34.   * @retval int
  35.   */
  36. int main(void)
  37. {
  38.  
  39. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  40. HAL_Init();
  41.  
  42. /* Configure the system clock */
  43. SystemClock_Config();
  44.  
  45. /* Initialize all configured peripherals */
  46. MX_GPIO_Init();
  47. MX_USART1_UART_Init();
  48.  
  49. HAL_UART_Transmit(&huart1,"STM32F103R6 LCD And UART1 Example\r\n",35,100);
  50. lcdInit();
  51. lcdClear();
  52.  
  53. lcdXY(2,1);
  54. lcdStr("STM32F103R6 HUART1");
  55.  
  56. lcdXY(2,2);
  57. lcdStr("And Character LCD");
  58.  
  59. lcdXY(2,3);
  60. lcdStr("Programming Example");
  61.  
  62. lcdXY(5,4);
  63. lcdStr("STM32CubeIDE");
  64.  
  65. HAL_Delay(2000);
  66. lcdClear();
  67. uint8_t rcvData=0,charNum=1,lineNum=1;
  68.  
  69. /* Infinite loop */
  70. /* USER CODE BEGIN WHILE */
  71. while (1)
  72. {
  73. HAL_UART_Receive(&huart1,&rcvData,1,100);
  74.  
  75. if(rcvData!=0){
  76. HAL_UART_Transmit(&huart1,&rcvData,1,100);
  77. if(rcvData==0x0D){
  78. if(lineNum<4) lineNum++;
  79. else { lcdClear(); lineNum=1;}
  80. charNum=1;
  81. lcdXY(charNum,lineNum);
  82. }else if(lineNum<=4&&charNum<=20){
  83. if(rcvData==0x08){
  84.  
  85. if(charNum>0) charNum-=1;
  86. lcdXY(charNum,lineNum);
  87. lcdDat(' ');
  88. lcdXY(charNum,lineNum);
  89. }else{
  90. lcdDat(rcvData);
  91. charNum++;
  92. }
  93. }
  94.  
  95. if(charNum>20&&lineNum<4){
  96. charNum=1;
  97. lineNum++;
  98.  
  99.  
  100. lcdXY(charNum,lineNum);
  101. }
  102. rcvData=0;
  103. }
  104. }
  105. /* USER CODE END 3 */
  106. }
  107.  
  108. /**
  109.   * @brief System Clock Configuration
  110.   * @retval None
  111.   */
  112. void SystemClock_Config(void)
  113. {
  114. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  115. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  116.  
  117. /** Initializes the RCC Oscillators according to the specified parameters
  118.   * in the RCC_OscInitTypeDef structure.
  119.   */
  120. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  121. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  122. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  123. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  124. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  125. {
  126. Error_Handler();
  127. }
  128. /** Initializes the CPU, AHB and APB buses clocks
  129.   */
  130. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  131. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  132. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  133. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  134. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  135. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  136.  
  137. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  138. {
  139. Error_Handler();
  140. }
  141. }
  142.  
  143. /**
  144.   * @brief USART1 Initialization Function
  145.   * @param None
  146.   * @retval None
  147.   */
  148. static void MX_USART1_UART_Init(void)
  149. {
  150.  
  151. /* USER CODE BEGIN USART1_Init 0 */
  152.  
  153. /* USER CODE END USART1_Init 0 */
  154.  
  155. /* USER CODE BEGIN USART1_Init 1 */
  156.  
  157. /* USER CODE END USART1_Init 1 */
  158. huart1.Instance = USART1;
  159. huart1.Init.BaudRate = 115200;
  160. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  161. huart1.Init.StopBits = UART_STOPBITS_1;
  162. huart1.Init.Parity = UART_PARITY_NONE;
  163. huart1.Init.Mode = UART_MODE_TX_RX;
  164. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  165. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  166. if (HAL_UART_Init(&huart1) != HAL_OK)
  167. {
  168. Error_Handler();
  169. }
  170. /* USER CODE BEGIN USART1_Init 2 */
  171.  
  172. /* USER CODE END USART1_Init 2 */
  173.  
  174. }
  175.  
  176. /**
  177.   * @brief GPIO Initialization Function
  178.   * @param None
  179.   * @retval None
  180.   */
  181. static void MX_GPIO_Init(void)
  182. {
  183. GPIO_InitTypeDef GPIO_InitStruct = {0};
  184.  
  185. /* GPIO Ports Clock Enable */
  186. __HAL_RCC_GPIOC_CLK_ENABLE();
  187. __HAL_RCC_GPIOA_CLK_ENABLE();
  188.  
  189. /*Configure GPIO pin Output Level */
  190. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  191. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
  192. |GPIO_PIN_8, GPIO_PIN_RESET);
  193.  
  194. /*Configure GPIO pins : PC0 PC1 PC2 PC3
  195.   PC4 PC5 PC6 PC7
  196.   PC8 */
  197. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  198. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
  199. |GPIO_PIN_8;
  200. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  201. GPIO_InitStruct.Pull = GPIO_NOPULL;
  202. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  203. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  204.  
  205. }
  206.  
  207. /* USER CODE BEGIN 4 */
  208.  
  209. /* USER CODE END 4 */
  210.  
  211. /**
  212.   * @brief This function is executed in case of error occurrence.
  213.   * @retval None
  214.   */
  215. void Error_Handler(void)
  216. {
  217. /* USER CODE BEGIN Error_Handler_Debug */
  218. /* User can add his own implementation to report the HAL error return state */
  219. __disable_irq();
  220. while (1)
  221. {
  222. }
  223. /* USER CODE END Error_Handler_Debug */
  224. }
  225.  
  226. #ifdef USE_FULL_ASSERT
  227. /**
  228.   * @brief Reports the name of the source file and the source line number
  229.   * where the assert_param error has occurred.
  230.   * @param file: pointer to the source file name
  231.   * @param line: assert_param error line source number
  232.   * @retval None
  233.   */
  234. void assert_failed(uint8_t *file, uint32_t line)
  235. {
  236. /* USER CODE BEGIN 6 */
  237. /* User can add his own implementation to report the file name and line number,
  238.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  239. /* USER CODE END 6 */
  240. }
  241. #endif /* USE_FULL_ASSERT */
  242.  
  243. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  244.  

For more information about USART1 and 4-bit LCD Interfacing please see my previous posts. Click here to download its source file.



No comments:

Post a Comment