---
title: 串行总线协议
url: https://doc.liz6.com/hardware/06-interfaces-and-communication/02-serial-bus-protocols
locale: zh
area: hardware
tags:
- hardware
- 接口与通信
date: 2026-06-30
modified: 2026-07-11
description: 串行总线协议 并行 vs 串行 --- UART (通用异步收发) 基本概念 UART:异步全双工,发送方与接收方波特率必须一致 连接:TX 与 RX 交叉相连(全双工同时收发) MCUA MCUB TX → RX RX ← TX 帧格式:一帧数据的时序结构 空闲(高) Start=0 D0 D1 D2 D3 D4 D…
---

# 串行总线协议

## 并行 vs 串行

```
并行: 多根线同时传多位 (一次传 8/16/32 bit)
      快但线多、时钟偏移 (skew) 限制速度

串行: 一根线按顺序传 bit
      线少、能跑更高频率 → 现代高速接口全是串行
```

---

## UART (通用异步收发)

### 基本概念
<svg viewBox="0 0 720 300" xmlns="http://www.w3.org/2000/svg" font-family="-apple-system,'Source Han Sans CN','Microsoft YaHei',sans-serif" role="img" aria-label="UART连接与帧格式:异步全双工,TX与RX交叉相连">
  <defs>
    <marker id="uah" markerWidth="10" markerHeight="8" refX="8" refY="3" orient="auto"><path d="M0,0 L8,3 L0,6 Z" fill="#475569"/></marker>
  </defs>
  <rect width="720" height="300" fill="#ffffff"/>
  <text x="360" y="28" text-anchor="middle" font-size="17" font-weight="700" fill="#1f2933">UART:异步全双工,发送方与接收方波特率必须一致</text>
  <text x="40" y="54" font-size="12" font-weight="700" fill="#1f2933">连接:TX 与 RX 交叉相连(全双工同时收发)</text>
  <rect x="40" y="64" width="130" height="56" rx="8" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="105" y="97" text-anchor="middle" font-size="13" font-weight="700" fill="#3730a3">MCU_A</text>
  <rect x="550" y="64" width="130" height="56" rx="8" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="615" y="97" text-anchor="middle" font-size="13" font-weight="700" fill="#3730a3">MCU_B</text>
  <line x1="170" y1="80" x2="550" y2="80" stroke="#475569" stroke-width="1.6" marker-end="url(#uah)"/>
  <text x="360" y="74" text-anchor="middle" font-size="11" fill="#64748b">TX → RX</text>
  <line x1="550" y1="108" x2="170" y2="108" stroke="#475569" stroke-width="1.6" marker-end="url(#uah)"/>
  <text x="360" y="124" text-anchor="middle" font-size="11" fill="#64748b">RX ← TX</text>
  <text x="40" y="150" font-size="12" font-weight="700" fill="#1f2933">帧格式:一帧数据的时序结构</text>
  <rect x="40" y="160" width="85" height="30" rx="5" fill="#e2e8f0"/>
  <text x="82" y="180" text-anchor="middle" font-size="10" fill="#475569">空闲(高)</text>
  <rect x="133" y="160" width="65" height="30" rx="5" fill="#4f46e5"/>
  <text x="165" y="180" text-anchor="middle" font-size="10" font-weight="700" fill="#ffffff">Start=0</text>
  <rect x="206" y="160" width="230" height="30" rx="5" fill="#0d9488"/>
  <text x="321" y="180" text-anchor="middle" font-size="11" font-weight="700" fill="#ffffff">D0 D1 D2 D3 D4 D5 D6 D7</text>
  <rect x="444" y="160" width="90" height="30" rx="5" fill="#cbd5e1" stroke="#94a3b8" stroke-dasharray="3 2"/>
  <text x="489" y="180" text-anchor="middle" font-size="10" fill="#475569">Parity?(可选)</text>
  <rect x="542" y="160" width="75" height="30" rx="5" fill="#4f46e5"/>
  <text x="579" y="180" text-anchor="middle" font-size="10" font-weight="700" fill="#ffffff">Stop=1</text>
  <rect x="40" y="222" width="640" height="56" rx="8" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="54" y="244" font-size="12.5" fill="#3730a3">波特率(9600 / 115200 / 921600…)必须双方一致,误差需 &lt; ±2%;</text>
  <text x="54" y="264" font-size="12.5" fill="#3730a3">异步无独立时钟线,全靠双方按约定波特率对齐每一位。</text>
</svg>

### 参数
| 参数 | 常见值 |
|------|--------|
| 波特率 | 9600, 19200, 38400, 57600, 115200 |
| 数据位 | 8 (最常见), 7 |
| 停止位 | 1, 2 |
| 校验位 | None, Even, Odd |
| 流控 | None, RTS/CTS (硬件) |

### 应用
```
调试串口 (printf 输出到电脑)
GPS/蓝牙模块 (AT 指令)
旧式外设通信

优点: 简单、便宜、可靠
缺点: 只能一对一、低速、无标准应用层
```

---

## I2C (Inter-Integrated Circuit)

### 基本概念
```
同步、半双工、多主多从
2 根线: SCL (时钟) + SDA (数据)

    Vcc
     │
    Rp  Rp          ← 上拉电阻 (必须!)
     │   │
     ├───┼────────── SDA
     │   │
    ┌┴┐ ┌┴┐
    │主│ │从1│ │从2│ ...
    └─┘ └─┬┘ └─┬┘
          │    │
          ├────┼──── SCL
          │    │
         GND  GND

SCL: 主机提供时钟
SDA: 双向数据 (开漏, 线与)
每个从机有唯一 7bit (或 10bit) 地址
```

### 通信过程
```
起始条件: SDA↓ 而 SCL 高
  发送: 7bit地址 + R/W bit → 从机回应 ACK
  传输: 8bit数据 + ACK ...  (可连续)
停止条件: SDA↑ 而 SCL 高

ACK: 第9个时钟 SDA=0 (接收方确认)
NACK: 第9个时钟 SDA=1 (无应答)

速度模式:
  Standard: 100 kHz
  Fast:     400 kHz  (最常用)
  Fast+:    1 MHz
  High Speed: 3.4 MHz
```

### 应用
```
传感器 (温度/湿度/IMU/光线...)
EEPROM/FRAM 存储
RTC (时钟芯片)
PMIC (电源管理)
OLED 显示屏
几乎所有低速外设

优点: 仅需2线、多设备共享
缺点: 速度慢、上拉设计多(功耗)、地址冲突
```

### 上拉电阻选择
```
R 太小 → 功耗大 (灌电流大)
R 太大 → 上升沿太慢 → 速度受限

经验值:
  100kHz: 4.7kΩ
  400kHz: 2.2kΩ
  1MHz:   1kΩ 或更小

计算: Rmax = tr / (0.8473 × Cbus)
     tr = 300ns (100kHz 模式)
     Cbus = 总线电容 (芯片 + PCB走线)
```

---

## SPI (Serial Peripheral Interface)

### 基本概念
<svg viewBox="0 0 720 340" xmlns="http://www.w3.org/2000/svg" font-family="-apple-system,'Source Han Sans CN','Microsoft YaHei',sans-serif" role="img" aria-label="SPI连接图:同步全双工,主机驱动时钟与片选,四根信号线">
  <defs>
    <marker id="spah" markerWidth="10" markerHeight="8" refX="8" refY="3" orient="auto"><path d="M0,0 L8,3 L0,6 Z" fill="#475569"/></marker>
  </defs>
  <rect width="720" height="340" fill="#ffffff"/>
  <text x="360" y="28" text-anchor="middle" font-size="17" font-weight="700" fill="#1f2933">SPI:同步全双工,主机独占时钟与片选控制权(至少4根线)</text>
  <rect x="40" y="50" width="130" height="180" rx="8" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="105" y="134" text-anchor="middle" font-size="13" font-weight="700" fill="#3730a3">MCU</text>
  <text x="105" y="152" text-anchor="middle" font-size="11" fill="#4f46e5">(Master)</text>
  <rect x="550" y="50" width="130" height="180" rx="8" fill="#f0fdfa" stroke="#99f6e4"/>
  <text x="615" y="134" text-anchor="middle" font-size="13" font-weight="700" fill="#115e59">外设</text>
  <text x="615" y="152" text-anchor="middle" font-size="11" fill="#0f766e">(Slave)</text>
  <line x1="170" y1="85" x2="550" y2="85" stroke="#4f46e5" stroke-width="1.6" marker-end="url(#spah)"/>
  <text x="360" y="78" text-anchor="middle" font-size="11" font-weight="700" fill="#4f46e5">SCK · 时钟(主→从)</text>
  <line x1="170" y1="125" x2="550" y2="125" stroke="#4f46e5" stroke-width="1.6" marker-end="url(#spah)"/>
  <text x="360" y="118" text-anchor="middle" font-size="11" font-weight="700" fill="#4f46e5">MOSI · 主机发,从机收</text>
  <line x1="550" y1="165" x2="170" y2="165" stroke="#0d9488" stroke-width="1.6" marker-end="url(#spah)"/>
  <text x="360" y="158" text-anchor="middle" font-size="11" font-weight="700" fill="#0f766e">MISO · 从机发,主机收</text>
  <line x1="170" y1="205" x2="550" y2="205" stroke="#64748b" stroke-width="1.6" marker-end="url(#spah)"/>
  <text x="360" y="198" text-anchor="middle" font-size="11" font-weight="700" fill="#475569">CSn · 片选(每从机一根)</text>
  <text x="40" y="248" font-size="12" font-weight="700" fill="#1f2933">多从机怎么接:两种方式</text>
  <rect x="40" y="256" width="310" height="34" rx="6" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="54" y="277.5" font-size="11.5" fill="#3730a3">独立 CSn:每从机一根片选线,共享 SCK/MOSI/MISO</text>
  <rect x="370" y="256" width="310" height="34" rx="6" fill="#f0fdfa" stroke="#99f6e4"/>
  <text x="384" y="277.5" font-size="11.5" fill="#115e59">菊花链:从机 MISO → 下一从机 MOSI 串联,共用一根 CSn</text>
  <rect x="40" y="300" width="640" height="34" rx="8" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="54" y="321.5" font-size="12.5" fill="#3730a3">主机独占时钟与片选控制权;无地址机制,靠 CSn 区分从机。</text>
</svg>

### 时钟模式 (CPOL, CPHA)
```
CPOL=0: SCK 空闲低
CPOL=1: SCK 空闲高
CPHA=0: 第一个边沿采样
CPHA=1: 第二个边沿采样

最常用: Mode 0 (CPOL=0, CPHA=0) 和 Mode 3 (CPOL=1, CPHA=1)

双方必须匹配! 否则通信错乱
```

### 特点
```
优点:
  ✓ 全双工 (同时收发)
  ✓ 高速 (数十 MHz, 甚至 100MHz+)
  ✓ 无地址机制 → 无开销
  ✓ 灵活 (任意帧长度)

缺点:
  ✗ 线多 (n个从机=n+3根线)
  ✗ 无标准协议层 (仅定义物理/链路)
  ✗ 无从机流控 (ACK)
  ✗ 距离短 (PCB级)

应用: Flash (NOR/NAND), ADC/DAC, 显示屏, 以太网 PHY
```

---

## 三总线对比

| 特性 | UART | I2C | SPI |
|------|------|-----|-----|
| 线数 | 2 (TX+RX) | 2 (SCL+SDA) | 4 (SCK+MOSI+MISO+CS) |
| 通信方式 | 异步 | 同步 | 同步 |
| 双工 | 全双工 | 半双工 | 全双工 |
| 速度 | < 1Mbps | < 3.4Mbps | 数十 Mbps |
| 多从机 | 否 | 是 (地址) | 是 (CSn) |
| 距离 | 长 (rs-232) | PCB/短 | PCB |
| 复杂度 | 最简单 | 中等 | 较简单 |
| 流控 | 无/硬件 | ACK/NACK | 无 |
| 典型用途 | 调试/无线模块 | 传感器 | Flash/显示/高速 |

---

## 速查: 何时选什么

```
传感器读数 (低速, 多设备) → I2C
高速数据传输 (Flash/SD/显示) → SPI
调试信息、无线模块 → UART
多字节寄存器读写 → I2C (地址+数据) 或 SPI
电池供电 (最低功耗) → I2C (静态几乎零功耗)
抗干扰 (长距离差分) → RS-485 (UART 的物理层变体)
```

---
*关键词: UART, I2C, SPI, 波特率, SCL, SDA, MOSI, MISO, CS, 上拉电阻, ACK*
