你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32G431RBTx 基本模块 UART

[复制链接]
STMCU小助手 发布时间:2023-3-1 21:21
前言
串口可以实现单片机和上位机之间的通信,是生活中常见的通讯接口,本篇文章将以十二届蓝桥杯嵌入式部分真题为例,实现串口通信。

a9b719d7969246e8a6acf4eb4956986a.png


UART
1.原理图以及配置元素

363e6dd03ad94d89b6c28614106c82ed.png

分析:由图可知PA9为串口的TX端,PA10为串口的RX端。


2.CubeMx的配置步骤

64f3da945d6f40679ab958a20f0496f1.png

配置两个引脚为红框中的模式,一定要先配置引脚模式再激活串口1,否则当你没有选定PC4与PC5的模式时,串口1激活时会默认选中PC4和PC5为TX端和RX端。
955dd70487b544f083f9a632b1a39f9a.png

配置号PA9和PA10之后,激活串口1(题目要求波特率为9600)。


3.生成工程
点击GENERATE CODE生成代码后点击Open Project即可。

831d98f946924eb2bc3184255eca4227.png

4.测试代码
interrupt.h:
  1. #ifndef __INTERRUPT_H__
  2. #define __INTERRUPT_H__

  3. #include "main.h"

  4. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);

  5. #endif

复制代码

interrupt.c
  1. #include "interrupt.h"
  2. #include "usart.h"

  3. char RxBuffer[30];
  4. unsigned char BufIndex = 0;
  5. unsigned char Rxdat = 0;

  6. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  7. {
  8.         if(huart->Instance == USART1)
  9.         {
  10.                 RxBuffer[BufIndex++] = Rxdat;
  11.                 HAL_UART_Receive_IT(&huart1, &Rxdat, 1);
  12.         }
  13. }

复制代码

main.c:
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© 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 "usart.h"
  23. #include "gpio.h"

  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "stdlib.h"
  27. #include "stdio.h"
  28. #include "string.h"
  29. #include "interrupt.h"
  30. #include "lcd.h"
  31. /* USER CODE END Includes */

  32. /* Private typedef -----------------------------------------------------------*/
  33. /* USER CODE BEGIN PTD */

  34. /* USER CODE END PTD */

  35. /* Private define ------------------------------------------------------------*/
  36. /* USER CODE BEGIN PD */
  37. /* USER CODE END PD */

  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */

  40. /* USER CODE END PM */

  41. /* Private variables ---------------------------------------------------------*/

  42. /* USER CODE BEGIN PV */
  43. extern unsigned char BufIndex;
  44. extern char RxBuffer[30];
  45. extern unsigned char Rxdat;
  46. /* USER CODE END PV */

  47. /* Private function prototypes -----------------------------------------------*/
  48. void SystemClock_Config(void);
  49. /* USER CODE BEGIN PFP */
  50. void Rx_Proc(void);
  51. /* USER CODE END PFP */

  52. /* Private user code ---------------------------------------------------------*/
  53. /* USER CODE BEGIN 0 */

  54. /* USER CODE END 0 */

  55. /**
  56.   * @brief  The application entry point.
  57.   * @retval int
  58.   */
  59. int main(void)
  60. {
  61.   /* USER CODE BEGIN 1 */

  62.   /* USER CODE END 1 */

  63.   /* MCU Configuration--------------------------------------------------------*/

  64.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  65.   HAL_Init();

  66.   /* USER CODE BEGIN Init */

  67.   /* USER CODE END Init */

  68.   /* Configure the system clock */
  69.   SystemClock_Config();

  70.   /* USER CODE BEGIN SysInit */

  71.   /* USER CODE END SysInit */

  72.   /* Initialize all configured peripherals */
  73.   MX_GPIO_Init();
  74.   MX_USART1_UART_Init();
  75.   /* USER CODE BEGIN 2 */
  76.         LCD_Init();
  77.         LCD_Clear(Black);
  78.         LCD_SetBackColor(Black);
  79.         LCD_SetTextColor(White);
  80.         HAL_UART_Receive_IT(&huart1, &Rxdat, 1);
  81.   /* USER CODE END 2 */

  82.   /* Infinite loop */
  83.   /* USER CODE BEGIN WHILE */
  84.   while (1)
  85.   {
  86.     /* USER CODE END WHILE */

  87.     /* USER CODE BEGIN 3 */
  88.                 if(BufIndex != 0)
  89.                 {
  90.                         unsigned char temp = BufIndex;
  91.                         HAL_Delay(1);
  92.                         if(BufIndex == temp)
  93.                                 Rx_Proc();
  94.                 }
  95.   }
  96.   /* USER CODE END 3 */
  97. }

  98. /**
  99.   * @brief System Clock Configuration
  100.   * @retval None
  101.   */
  102. void SystemClock_Config(void)
  103. {
  104.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  105.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  106.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  107.   /** Configure the main internal regulator output voltage
  108.   */
  109.   HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  110.   /** Initializes the RCC Oscillators according to the specified parameters
  111.   * in the RCC_OscInitTypeDef structure.
  112.   */
  113.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  114.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  115.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  116.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  117.   RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
  118.   RCC_OscInitStruct.PLL.PLLN = 20;
  119.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  120.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  121.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  122.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  123.   {
  124.     Error_Handler();
  125.   }
  126.   /** Initializes the CPU, AHB and APB buses clocks
  127.   */
  128.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  129.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  130.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  131.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  132.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  133.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  134.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  135.   {
  136.     Error_Handler();
  137.   }
  138.   /** Initializes the peripherals clocks
  139.   */
  140.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  141.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  142.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  143.   {
  144.     Error_Handler();
  145.   }
  146. }

  147. /* USER CODE BEGIN 4 */
  148. int fputc(int ch, FILE *f)
  149. {
  150.         HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, HAL_MAX_DELAY);
  151.         return ch;
  152. }

  153. void Rx_Proc(void)
  154. {
  155.         if(BufIndex == 22)
  156.         {
  157.                 char car_type[5];
  158.                 char car_data[5];
  159.           char car_time[13];
  160.                 sscanf(RxBuffer, "%4s:%4s:%12s", car_type, car_data, car_time);
  161.                 LCD_DisplayStringLine(Line0, (unsigned char*)car_type);
  162.                 LCD_DisplayStringLine(Line1, (unsigned char*)car_data);
  163.                 LCD_DisplayStringLine(Line2, (unsigned char*)car_time);
  164.         }
  165.         else
  166.         {
  167.                 printf("Error\n");
  168.         }
  169.         BufIndex = 0;
  170.         memset(RxBuffer, 0, 30);
  171. }
  172. /* USER CODE END 4 */

  173. /**
  174.   * @brief  This function is executed in case of error occurrence.
  175.   * @retval None
  176.   */
  177. void Error_Handler(void)
  178. {
  179.   /* USER CODE BEGIN Error_Handler_Debug */
  180.   /* User can add his own implementation to report the HAL error return state */
  181.   __disable_irq();
  182.   while (1)
  183.   {
  184.   }
  185.   /* USER CODE END Error_Handler_Debug */
  186. }

  187. #ifdef  USE_FULL_ASSERT
  188. /**
  189.   * @brief  Reports the name of the source file and the source line number
  190.   *         where the assert_param error has occurred.
  191.   * @param  file: pointer to the source file name
  192.   * @param  line: assert_param error line source number
  193.   * @retval None
  194.   */
  195. void assert_failed(uint8_t *file, uint32_t line)
  196. {
  197.   /* USER CODE BEGIN 6 */
  198.   /* User can add his own implementation to report the file name and line number,
  199.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  200.   /* USER CODE END 6 */
  201. }
  202. #endif /* USE_FULL_ASSERT */

  203. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码



————————————————
版权声明:火花页.


收藏 评论0 发布时间:2023-3-1 21:21

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版