本贴用于记录蓝桥杯嵌入式准备的模块。本博客使用printf输出函数串口1,STM32CubeMX和keil的初始配置这里不再重复,有问题的同学可以看模块一LED那篇博客。
(该系列文章为公开免费,若文章被设置为VIP,请私信作者,我会重新免费发布)
目录
一、UART配置
在蓝桥杯嵌入式STM32G431的电路板上串口1使用的是引脚PA10和PA9,这里先将这两引脚配置为串口的RX和TX,如下图所示,然后打开左边USART1选项,模式选择异步模式即Asynchronous,之前的赛题都是设置为9600的波特率,这里也设置为9600,最后开启串口中断生成工程,如第二幅图所示。
二、代码编写
STM32CubeMX已经配置好串口初始化代码,不需要我们再对其进行配置,需要编写的代码是中断接收数据,这里为了方便数据发送,重定向printf。下面将介绍printf的重定向和中断接收数据。
1、重定向printf
这里需要添加头文件stdio.h,并且需要再keil的魔术棒内勾选下面第二个选项,这样就可以使用printf来实现串口发送数据
#include "stdio.h"
int fputc(int ch,FILE *f)//重定向
{
HAL_UART_Transmit(&huart1,(unsigned char *)&ch,1,50);
return ch;
}
这里对其进行测试,串口助手能显示hello
/* Infinite loop */
/* USER CODE BEGIN WHILE */
printf("hello\r\n");
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
2、中断接收
(1)单个数据接收
使用串口接收中断服务函数,用于将接收到的数据保存至变量中,并且每次只能保存一位数据
unsigned char RX;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance==USART1)
{
HAL_UART_Receive_IT(huart,&RX,1);//将串口接收缓冲区的数据保存到RX中
}
}
接收数据前要先开启串口接收中断,这样接收单个数据完成。
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1,&RX,1);//开启串口接收中断
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
// printf("hello\r\n");
while (1)
{
printf("%c\r\n",RX);
HAL_Delay(500);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
(2)多个数据接收
若要接收一串字符串,参考b站up主01Studio的代码
视频链接uart串口通信(完结)_哔哩哔哩_bilibili
添加两个变量分别是数组用于存储接收数据和指针用于将接收到的数据指向数组的哪一位
unsigned char RX;
unsigned char rx_data[20];//用于将数据保存数组中
unsigned char rx_Point=0;//将读取到的数据保存到数组的哪一位中
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance==USART1)
{
rx_data[rx_Point++]=RX;//将串口接收的数据保存至数组中
HAL_UART_Receive_IT(huart,&RX,1);//将串口接收缓冲区的数据保存到RX中
}
}
主函数部分
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1,&RX,1);//开启串口接收中断
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
// printf("hello\r\n");
while (1)
{
if(rx_Point>0)
{
int temp=rx_Point;//为保证数据完全接收,这里定义变量保存上一时刻的指针
HAL_Delay(1);
if(temp==rx_Point)//当上一时刻的指针和这一时刻的指针相等时,则表示数据接收完成
{
rx_Point=0;//将指针归位
printf("%s\r\n",rx_data);//将数组中的数据打印出来
for(unsigned char a=0;a<20;a++)//将数组中数据清除
{
rx_data[a]=0;
}
}
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
演示效果
附录(完整main.c代码)
为了方便用csdn查看,将所有代码写在main.c文件中。读者可自行分文件编写。
特别感谢b站up主01Studio分享的教程
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
int fputc(int ch,FILE *f)//重定向
{
HAL_UART_Transmit(&huart1,(unsigned char *)&ch,1,50);
return ch;
}
unsigned char RX;
unsigned char rx_data[20];//用于将数据保存数组中
unsigned char rx_Point=0;//将读取到的数据保存到数组的哪一位中
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance==USART1)
{
rx_data[rx_Point++]=RX;//将串口接收的数据保存至数组中
HAL_UART_Receive_IT(huart,&RX,1);//将串口接收缓冲区的数据保存到RX中
}
}
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1,&RX,1);//开启串口接收中断
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
// printf("hello\r\n");
while (1)
{
if(rx_Point>0)
{
int temp=rx_Point;//为保证数据完全接收,这里定义变量保存上一时刻的指针
HAL_Delay(1);
if(temp==rx_Point)//当上一时刻的指针和这一时刻的指针相等时,则表示数据接收完成
{
rx_Point=0;//将指针归位
printf("%s\r\n",rx_data);//将数组中的数据打印出来
for(unsigned char a=0;a<20;a++)//将数组中数据清除
{
rx_data[a]=0;
}
}
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
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_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#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 /* USE_FULL_ASSERT */