串口实验演示分类
111.4.1串口实验演示分类
/* ==================== Core\Inc\main.h ==================== */
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.h
* @brief : Header for main.c file.
* This file contains the common defines of the application.
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/
#define LED2_Pin GPIO_PIN_5
#define LED2_GPIO_Port GPIOE
#define LED1_Pin GPIO_PIN_5
#define LED1_GPIO_Port GPIOB
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
/* ==================== Core\Inc\gpio.h ==================== */
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file gpio.h
* @brief This file contains all the function prototypes for
* the gpio.c file
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __GPIO_H__
#define __GPIO_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_GPIO_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /*__ GPIO_H__ */
/* ==================== Core\Inc\usart.h ==================== */
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.h
* @brief This file contains all the function prototypes for
* the usart.c file
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USART_H__
#define __USART_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes */
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN Private defines */
extern uint8_t u_buf[256];
#define printf(...) HAL_UART_Transmit(&huart1, (uint8_t *)u_buf, sprintf((char*)u_buf, __VA_ARGS__), 0xffff)
/* USER CODE END Private defines */
void MX_USART1_UART_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __USART_H__ */
/* ==================== Core\Src\main.c ==================== */
/* 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 <string.h>
/* 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 */
uint8_t UART_Recv[5];
uint8_t DHT11_Buf[30];
/* 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 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_UART_Receive(&huart1, UART_Recv, 4, 1000);
if(strcmp("LED1", (char *)UART_Recv)==0)
{
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
memset(UART_Recv, '\0', 4);
}
if(!strcmp("LED2", (char *)UART_Recv))
{
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
memset(UART_Recv, '\0', 4);
}
sprintf((char *)DHT11_Buf,"H2:%f T2:%f\n",30.1,36.5);
HAL_UART_Transmit(&huart1, (char *)DHT11_Buf, strlen(DHT11_Buf), 1000);
printf("Hellow world wfeng %d!\n",123);
/* 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};
/** 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.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_DIV2;
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 */
/* ==================== Core\Src\gpio.c ==================== */
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file gpio.c
* @brief This file provides code for the configuration
* of all used GPIO pins.
******************************************************************************
* @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 "gpio.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/*----------------------------------------------------------------------------*/
/* Configure GPIO */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
/*Configure GPIO pin : PtPin */
GPIO_InitStruct.Pin = LED2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED2_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : PtPin */
GPIO_InitStruct.Pin = LED1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);
}
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* ==================== Core\Src\usart.c ==================== */
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.c
* @brief This file provides code for the configuration
* of the USART instances.
******************************************************************************
* @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 "usart.h"
/* USER CODE BEGIN 0 */
uint8_t u_buf[256];
/* USER CODE END 0 */
UART_HandleTypeDef huart1;
/* USART1 init function */
void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspDeInit 0 */
/* USER CODE END USART1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
/* USER CODE BEGIN USART1_MspDeInit 1 */
/* USER CODE END USART1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
一、串口实验演示
实验使用 USART1:
| 参数 | 配置 |
|---|---|
| TX | PA9,复用推挽输出 |
| RX | PA10,输入 |
| 波特率 | 115200 bit/s |
| 数据格式 | 8 个数据位、无校验、1 个停止位,即 8N1 |
| 方向 | TX 与 RX 全双工 |
| 硬件流控 | 无 |
| 过采样 | 16 倍 |
开发板通过 CH340C 把 STM32 的 3.3 V TTL 串口转换为 USB。电脑看到的是虚拟 COM 口;数据路径为:
flowchart LR
PC[串口助手] <-->|USB 数据| CH340[CH340C USB转TTL]
CH340 <-->|USART1_TX/USART1_RX| MCU[STM32F103]
MCU --> LED1[PB5 LED1]
MCU --> LED2[PE5 LED2]
原理图中 USART1_TX 经过 LED 和电阻还带有发送指示,USART1_RX 也带接收指示。指示灯会给信号线增加负载,但在本开发板设计和常用波特率下用于观察通信活动。
1.1 实验演示控灯
程序每次阻塞接收 4 个字节:
HAL_UART_Receive(&huart1, UART_Recv, 4, 1000);
串口助手发送大写 ASCII 字符 LED1 或 LED2:
if (strcmp("LED1", (char *)UART_Recv) == 0)
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
if (strcmp("LED2", (char *)UART_Recv) == 0)
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
LED1 与 LED2 都是低电平有效,Toggle 每收到一次完整命令就改变亮灭状态。串口助手若自动附加回车换行,会额外发送 \r\n,下一次固定 4 字节接收就可能错位;演示时应关闭自动换行,或在程序中实现按分隔符解析。
接收缓冲区定义为 5 字节并由静态存储区自动清零,所以第 5 字节可作为字符串结尾。代码只清前 4 字节,第 5 字节仍为 0。更明确的写法是:
uint8_t UART_Recv[5] = {0};
if (HAL_UART_Receive(&huart1, UART_Recv, 4, 1000) == HAL_OK)
{
UART_Recv[4] = '\0';
...
}
还应检查 HAL_UART_Receive 返回值。发生超时或只收到部分字节时,直接比较可能混入上次残留数据。正式协议可采用逐字节状态机、空闲线中断或 DMA,而不是永久按固定 4 字节阻塞。
程序还演示:
sprintf((char *)DHT11_Buf, "H2:%f T2:%f\n", 30.1, 36.5);
HAL_UART_Transmit(&huart1, DHT11_Buf, strlen((char *)DHT11_Buf), 1000);
printf("Hellow world wfeng %d!\n", 123);
这里的 printf 是 usart.h 中定义的宏,先用 sprintf 写入全局 u_buf,再调用 HAL_UART_Transmit。它便于教学,但不具备重入性,ISR 和主循环同时调用会覆盖缓冲区;无限循环中没有节流还会持续占满串口。工程化实现应限制发送周期、使用 snprintf 防止越界,并通过环形缓冲区与中断/DMA异步发送。
二、串口理论知识
串口通信至少要区分三层:
信息层:字符、命令、帧格式、校验
UART/USART 控制器层:起始位、数据位、校验位、停止位、波特率
物理电气层:3.3 V TTL、RS-232、RS-485、USB 等
“串口”不能自动等同于 RS-232。STM32 PA9/PA10 是 3.3 V CMOS/TTL 电平,不能直接连接 ± 电压的传统 RS-232 接口;需要 MAX3232 一类电平转换器。连接另一块 3.3 V MCU 时,应 TX 接对方 RX、RX 接对方 TX,并共地。
2.1 USART
USART 是 Universal Synchronous/Asynchronous Receiver/Transmitter,即通用同步/异步收发器;UART 没有同步时钟能力。STM32 USART 外设可在常用异步串口模式下工作,也支持同步时钟等功能。
异步 8N1 的一帧:
空闲 起始 8个数据位,低位先发 停止
1 ─────── 0 ── D0 D1 D2 D3 D4 D5 D6 D7 ───────────── 1
以发送字符 A 为例,ASCII 码 0x41,二进制为 0100 0001;UART 低位先发,所以线上数据位顺序为 1、0、0、0、0、0、1、0。8N1 每发送 8 位有效数据,线上实际使用 10 个比特时间:
$ \eta=\frac{8}{1+8+1}=80% $
115200 bit/s 下,理论最大 8 位字符速率约为:
$ \frac{115200}{10}=11520\ \text{byte/s} $
这还没有扣除协议头、校验、软件等待和 USB 转串口调度。
2.1.1 USART百度百科
阅读百科类资料时应把术语落实到寄存器配置:
| 术语 | 本实验配置 |
|---|---|
| 波特率 | 115200 |
| 字长 | 8 bit |
| 校验 | None |
| 停止位 | 1 |
| 收发 | TX_RX |
| 流控 | None |
| 过采样 | 16 |
波特率是每秒符号数,bit/s 是每秒比特数。对普通二进制 NRZ UART,一个符号承载一个比特时,两者数值通常相等;概念仍不应混为一谈。接收端通过起始位定位,再按本地时钟在每个位中间附近采样,因此双方波特率误差不能过大,帧越长、噪声越强,容差越紧。
2.1.2 通信方式分类
通信可按多组独立维度分类:
时钟关系:同步 / 异步
数据空间:串行 / 并行
方向能力:单工 / 半双工 / 全双工
“串行”不等于“异步”,例如 SPI 是同步串行;“并行”也不决定单工还是双工。
2.1.2.1 同步与异步
同步通信由显式时钟线或从数据流恢复的时钟来确定采样时刻;异步 UART 不传独立时钟,双方事先约定波特率,每帧用起始位重新同步。
| 对比 | 同步 | 异步 UART |
|---|---|---|
| 独立时钟 | 通常有,或可从编码恢复 | 无 |
| 帧边界 | 由时钟/协议确定 | 起始位与停止位 |
| 开销 | 连续传输时可较低 | 每帧有起始/停止位 |
| 空闲间隔 | 取决于协议 | 字节间可任意空闲 |
2.1.3 同步
STM32 USART 同步模式可输出 CK 时钟,发送端与接收端按共同节拍移位。同步只描述采样时序,并不自动规定地址、包长或错误恢复;这些仍由上层协议决定。
发送端:TX ───────────────> RX:接收端
CK ───────────────> CK
每个有效时钟边沿移动/采样一位
2.1.3.1 异步
异步 UART 线路空闲通常为高。检测到下降沿后认为起始位到来,接收器按设定波特率和过采样时钟判断后续数据位。双方没有共享时钟,所以必须使用相同的数据位、校验、停止位和足够接近的波特率。
常见错误:
- 一方 115200,另一方 9600;
- 一方 8N1,另一方启用奇偶校验;
- TX/RX 没有交叉;
- 两端没有共地;
- TTL 串口直接接 RS-232;
- 3.3 V RX 接入不兼容的 5 V 输出。
2.1.3.2 串行与并行
串行通信在少量线上按时间顺序发送各位;并行通信同时使用多条数据线传输一组位。它们描述的是数据在空间和时间上的组织方式。
2.1.3.3 并行传输
并行接口在一个时钟周期可同时传输多位,常见于 MCU 内部总线、存储器接口、早期打印机口等。短距离、紧密布线时吞吐量很高,但线数多,会带来引脚、连接器、走线和功耗成本。
并行线速度提高后,各线传播延迟不同会产生 skew(偏斜);接收端可能在同一采样瞬间看到不同批次的数据位。串扰和同时翻转噪声也随之变得突出。
2.1.3.4 串行传输
串行接口使用更少的线,易于长距离连接和布线。现代高速串行并不意味着“只能一位一位慢慢传”;它可通过很高符号率、差分传输、时钟恢复、编码、均衡和多 lane 并行获得高吞吐量,例如 USB、PCIe、以太网。
2.1.3.5 效率比较
不能笼统断言串行一定比并行快,或并行一定比串行快。应同时比较:
| 指标 | 影响 |
|---|---|
| 单线符号率 | 每条线每秒承载能力 |
| 数据线数量 | 并行宽度或串行 lane 数 |
| 编码/帧开销 | 起始停止位、校验、协议头 |
| 距离和信号完整性 | 偏斜、串扰、衰减、反射 |
| 引脚与布线成本 | 连接器、PCB 层数、功耗 |
UART 8N1 的线编码效率是 80%;但系统吞吐还受软件、缓冲区和协议影响。
2.1.3.6 并行与串行的区别
| 特性 | 串行 | 并行 |
|---|---|---|
| 数据线 | 少 | 多 |
| 位传输 | 按时间顺序 | 多位同时 |
| 高速难点 | 高频损耗、时钟恢复、均衡 | 线间偏斜、串扰、同步 |
| 典型场景 | UART、USB、CAN、以太网 | 存储器总线、MCU 内部总线 |
2.1.3.7 实际应用: 串口(串行)使用广泛,并口(并行)因需接多线且不稳定,已较少使用。
在板间和外部连接中,串行接口因引脚少、连接器小、布线方便而非常普遍。并行接口并未消失,它仍大量存在于芯片内部、存储器和短距离高带宽场景。更准确的结论是:距离、速率和成本上升后,宽并行总线的偏斜与布线代价更难控制,因此许多外部接口转向高速串行。
2.1.3.8 方向与时间分类
方向分类与同步/异步、串行/并行相互独立:
flowchart TD
C[通信方向] --> S[单工]
C --> H[半双工]
C --> F[全双工]
S --> SO[只能 A→B]
H --> HO[A↔B,但不能同时]
F --> FO[A↔B,可同时]
2.1.3.9 全双工
全双工允许双方同时发送和接收。普通 USART 异步模式有独立 TX、RX 线路:
设备A TX ─────────> 设备B RX
设备A RX <───────── 设备B TX
GND ─────────── GND
两根数据线各负责一个方向,因此收发可以同时发生。
2.1.3.10 半双工
半双工允许双向通信,但同一时刻只允许一方发送,例如常见两线 RS-485 总线。系统需要方向控制、总线仲裁和发送完成后释放线路,否则两端同时驱动会冲突。
STM32 USART 也支持单线半双工模式,此时同一引脚在发送和接收之间切换。
2.1.3.11 单工
单工只有固定单向数据流,例如只发送遥测数据而永不接收命令的链路。广播电视常被用于解释单工,但具体系统可能另有回传通道;判断时应以当前数据链路是否允许反向传输为准。
本实验的 USART1 使用独立 TX/RX,配置 UART_MODE_TX_RX,因此属于异步、串行、全双工通信。