因为我是自己搞得板子,原本的下程序和串口1有问题,所以我用的是串口2,用的PA2和PA3
一,使用CubeMX配置串口
选择A开头的这个是异步通信。
配置串口参数,往届的题基本用的9600波特率,所以我这里设置为9600,其他参数照着设置即可。
接下来设置开启串口中断,把他勾选上。
下一步,配置基本GPIO,B站很多博主都更改过配置,我默认的使用没什么问题
接下来把程序导出即可。
二,发送数据
我们在主程序写一个发送程序,首先需要添加两个头文件
#include "stdio.h"
#include "string.h"
然后写发送程序:
char temp1[30];
sprintf(temp1,"%d\r\n",123456);
HAL_UART_Transmit(&huart2,(uint8_t *)temp1,strlen(temp1),50);
sprintf函数:包含在stdio.h里面,他在作用是将sprintf函数打印到字符串中(要注意字符串的长度要足够容纳打印的内容,否则会出现内存溢出),这里我们就是将123456放到temp1这个数组里面。
HAL_UART_Transmit是HAL库提供的串口发送函数。四个参数分别是:
* @param huart UART句柄。
* @param pData指向数据缓冲区(u8或u16数据元素)。
* @param Size要发送的数据元素数量(u8或u16)。
* @param Timeout超时时间。
串口接收:
三,接收数据
需要自己写两个函数
uint8_t rx_porint=0;
uint8_t rx_dat;
char rx_data[64];
char car_1[10];
char car_2[10];
char car_3[20];
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) //Rx传输完成回调,这个就是HAL库中其中一个回调函数,接受完成回调。
{
rx_data[rx_porint++]=rx_dat;
HAL_UART_Receive_IT(&huart2,&rx_dat,1);
}
void Uart_Rx_Receiving_Processvoid(void) //接受数据处理函数
{
if(rx_porint>0)
{
sscanf(rx_data,"%4s:%4s:%14s",car_1,car_2,car_3);
}
rx_porint = 0;memset(rx_data,0,30);
}
sscanf:通常被用来解析并转换字符串,可以实现很强大的字符串解析功能。
memset:作用数组清零。
接下来是我的main.c和usart.c和usart.h程序全部。
//main.c
#include "main.h"
#include "gpio.h"
#include "lcd.h"
#include "usart.h"
#include "stdio.h"
#include "string.h"
void SystemClock_Config(void);
unsigned char key;
char temp1[30];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
LCD_Init();
LED_Init();
Key_Init();
MX_USART2_UART_Init();
sprintf(temp1,"%d\r\n",123456);
HAL_UART_Transmit(&huart2,(uint8_t *)temp1,strlen(temp1),50);
while (1)
{
if(rx_porint != 0)
{
int temp=rx_porint;
HAL_Delay(1);
if(temp==rx_porint)
{
Uart_Rx_Receiving_Processvoid();
}
}
sprintf(temp1,"val=%s",car_1);
LCD_SetBackColor(Yellow);
LCD_DisplayStringLine(Line1,(unsigned char *)temp1);
sprintf(temp1,"va2=%s",car_2);
LCD_SetBackColor(Yellow);
LCD_DisplayStringLine(Line3,(unsigned char *)temp1);
sprintf(temp1,"va3=%s",car_3);
LCD_SetBackColor(Yellow);
LCD_DisplayStringLine(Line5,(unsigned char *)temp1);
}
}
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 */
//usart.c
#include "usart.h"
UART_HandleTypeDef huart2;
uint8_t rx_porint=0;
uint8_t rx_dat;
char rx_data[64];
char car_1[10];
char car_2[10];
char car_3[20];
void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
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;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
{
Error_Handler();
}
HAL_UART_Receive_IT(&huart2,&rx_dat,1);
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
if(uartHandle->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspInit 0 */
/* USER CODE END USART2_MspInit 0 */
/** Initializes the peripherals clocks
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/* USART2 clock enable */
__HAL_RCC_USART2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART2 GPIO Configuration
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART2 interrupt Init */
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);
/* USER CODE BEGIN USART2_MspInit 1 */
/* USER CODE END USART2_MspInit 1 */
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
rx_data[rx_porint++]=rx_dat;
HAL_UART_Receive_IT(&huart2,&rx_dat,1);
}
void Uart_Rx_Receiving_Processvoid(void)
{
if(rx_porint>0)
{
sscanf(rx_data,"%4s:%4s:%14s",car_1,car_2,car_3);
}
rx_porint = 0;memset(rx_data,0,30);
}
//usart.h
#ifndef __USART_H__
#define __USART_H__
#include "main.h"
extern unsigned char rx_porint;
extern uint8_t rx_dat;
extern char rx_data[64];
extern char car_1[10];
extern char car_2[10];
extern char car_3[20];
extern UART_HandleTypeDef huart2;
void MX_USART2_UART_Init(void);
void Uart_Rx_Receiving_Processvoid(void);
#endif