一.原生串口通訊
二. 串口與PC通訊
USB轉串口主要用于設備跟電腦通信
電平轉換芯片一般有CH340、PL2303、CP2102、FT232
使用的時候電腦端需要安裝電平轉換芯片的驅動
三. RS232標準串口通訊
RS232標準串口主要用于工業設備直接通信
電平轉換芯片一般有MAX3232,SP3232
四. STM32串口
1. 內部結構
寄存器 | 功能 |
---|---|
TX | 數據發送 |
RX | 數據接收 |
SCLK | 時鐘,僅同步通信時使用(不常用) |
nRTS | 發送請求(不常用) |
nCTS | 允許發送 (不常用) |
2. 串口引腳分布
注意:串口一是APB2總線, 其他是APB1總線
3. 串口引腳重定義
參見復用重映射和調試I/O配置寄存器(AFIO_MAPR)
4. 串口寄存器
(1). 串口數據寄存器 USART_DR
USART_DR, 9位有效
USART_DR一個地址對應了兩個物理內存。包含一個發送數據寄存器TDR和一個接收數據寄存器RDR。
(2). 串口數據寄存器 USART_CR1 USART_CR2 用來配置串口
(3). 串口波特率寄存器USART_BRR
五. 串口相關結構體
1. 串口初始化結構體 USART_InitTypeDef
typedef struct
{
uint32_t USART_BaudRate; /*!< This member configures the USART communication baud rate.
The baud rate is computed using the following formula:
- IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate)))
- FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
uint16_t USART_WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
This parameter can be a value of @ref USART_Word_Length */
uint16_t USART_StopBits; /*!< Specifies the number of stop bits transmitted.
This parameter can be a value of @ref USART_Stop_Bits */
uint16_t USART_Parity; /*!< Specifies the parity mode.
This parameter can be a value of @ref USART_Parity
@note When parity is enabled, the computed parity is inserted
at the MSB position of the transmitted data (9th bit when
the word length is set to 9 data bits; 8th bit when the
word length is set to 8 data bits). */
uint16_t USART_Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
This parameter can be a value of @ref USART_Mode */
uint16_t USART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled
or disabled.
This parameter can be a value of @ref USART_Hardware_Flow_Control */
} USART_InitTypeDef;
USART_BaudRate: 設置波特率,如9600,115200等
USART_WordLength: 設置數據長度。具體值:USART_WordLength_8b 或 USART_WordLength_9b
USART_StopBits: 設置停止位大小
USART_Parity: 奇偶校驗
USART_Mode: 設置收發使能
發送接收都使能
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //發送接收都使能
2.同步時鐘初始化結構體 (用的少)
typedef struct
{
uint16_t USART_Clock; /*!< Specifies whether the USART clock is enabled or disabled.
This parameter can be a value of @ref USART_Clock */
uint16_t USART_CPOL; /*!< Specifies the steady state value of the serial clock.
This parameter can be a value of @ref USART_Clock_Polarity */
uint16_t USART_CPHA; /*!< Specifies the clock transition on which the bit capture is made.
This parameter can be a value of @ref USART_Clock_Phase */
uint16_t USART_LastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
data bit (MSB) has to be output on the SCLK pin in synchronous mode.
This parameter can be a value of @ref USART_Last_Bit */
} USART_ClockInitTypeDef;
六. 串口相關庫函數
1. 串口初始化函數 USART_Init()
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
示例:
USART_InitStructure.USART_BaudRate=115200; //設置波特率
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //設置數據長度
USART_InitStructure.USART_StopBits=USART_StopBits_1; //設置停止位長度
USART_InitStructure.USART_Parity=USART_Parity_No; //設置奇偶校驗位
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //設置硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //設置收發使能
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
2. 中斷配置函數 USART_ITConfig()
void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
例:使能接受完畢中斷USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
3. 串口使能函數 USART_Cmd
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
4. 數據發送函數 USART_SendData()
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
5. 數據接收函數 USART_ReceiveData
uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
6.中斷狀態位獲取函數 USART_GetITStatus
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
7. 編程方法
例子:
完整的串口發送, 可以printf, 可以發送 8位字 16位半字節 字符串等
中斷接收功能 , 當有外部數據輸入時會產生中斷, 中斷后將得到的 字 再發送出去,同時點亮RGB
可以控制RGB燈的亮滅, PC端輸入:0,1,2可以點亮RGB~
psb_usart.h文件(這個文件將5個串口都定義好了, 通過選擇編譯來確定串口)
#ifndef __BSP_USART_H
#define __BSP_USART_H
#include 'stm32f10x.h'
#include //用哪個串口就把哪個置1 #define DEBUG_USART1 1 #define DEBUG_USART2 0 #define DEBUG_USART3 0 #define DEBUG_USART4 0 #define DEBUG_USART5 0 #if DEBUG_USART1 // 串口1-USART1 #define DEBUG_USARTx USART1 #define DEBUG_USART_CLK RCC_APB2Periph_USART1 #define DEBUG_USART_APBxClkCmd RCC_APB2PeriphClockCmd #define DEBUG_USART_BAUDRATE 115200 // USART GPIO 引腳宏定義 #define DEBUG_USART_GPIO_CLK (RCC_APB2Periph_GPIOA) #define DEBUG_USART_GPIO_APBxClkCmd RCC_APB2PeriphClockCmd #define DEBUG_USART_TX_GPIO_PORT GPIOA #define DEBUG_USART_TX_GPIO_PIN GPIO_Pin_9 #define DEBUG_USART_RX_GPIO_PORT GPIOA #define DEBUG_USART_RX_GPIO_PIN GPIO_Pin_10 #define DEBUG_USART_IRQ USART1_IRQn #define DEBUG_USART_IRQHandler USART1_IRQHandler #elif DEBUG_USART2 //串口2-USART2 #define DEBUG_USARTx USART2 #define DEBUG_USART_CLK RCC_APB1Periph_USART2 #define DEBUG_USART_APBxClkCmd RCC_APB1PeriphClockCmd #define DEBUG_USART_BAUDRATE 115200 // USART GPIO 引腳宏定義 #define DEBUG_USART_GPIO_CLK (RCC_APB2Periph_GPIOA) #define DEBUG_USART_GPIO_APBxClkCmd RCC_APB2PeriphClockCmd #define DEBUG_USART_TX_GPIO_PORT GPIOA #define DEBUG_USART_TX_GPIO_PIN GPIO_Pin_2 #define DEBUG_USART_RX_GPIO_PORT GPIOA #define DEBUG_USART_RX_GPIO_PIN GPIO_Pin_3 #define DEBUG_USART_IRQ USART2_IRQn #define DEBUG_USART_IRQHandler USART2_IRQHandler #elif DEBUG_USART3 //串口3-USART3 #define DEBUG_USARTx USART3 #define DEBUG_USART_CLK RCC_APB1Periph_USART3 #define DEBUG_USART_APBxClkCmd RCC_APB1PeriphClockCmd #define DEBUG_USART_BAUDRATE 115200
上一篇:為什么STM32中運行的C程序執行某些函數時死機?
下一篇:stm32固件庫UART操作
推薦閱讀最新更新時間:2025-04-29 11:04



設計資源 培訓 開發板 精華推薦
- 具有 LTC2217 16 位 105Msps、高速和高動態范圍 ADC 的演示板
- 基于 LTC1480 超低待機功耗 RS-485 收發器的微功率轉發器參考設計
- ADR391A 2.5 Vout 微功率、低噪聲精密電壓基準的典型應用
- 2.5V 輸出 ADR391B 微功耗、低噪聲精密電壓基準的典型應用
- LT1170CT、5A 驅動高壓 NPN 的典型應用
- 具有無源平衡功能的 6 節 EV/HEV 集成電池監視器和保護器參考設計
- 具有低漂移滿量程微調的 LT1021DCS8-5 CMOS DAC 基準的典型應用
- NSVC2030JBT3G 用于基本交流應用的恒流 LED 驅動器的典型應用
- AM2G-1215SZ 15V 2 瓦 DC-DC 轉換器的典型應用
- 使用 Microchip Technology 的 MSL1064 的參考設計
- 防爆電機定期檢修的重要性_防爆電機日常檢修的內容
- Achronix和Signoff攜手為人工智能/機器學習提供FPGA和eFPGA IP服務
- 初始化片外RAM,讓程序有更大內存空間
- Digi-Key Electronics 將在 2021 ELEXCON 舉辦現場和線上活動
- ?平頭哥玄鐵910全球首次兼容安卓系統,可運行Chrome瀏覽器
- Pixelworks賦能iQOO Neo5S智能手機,讓游戲的視覺效果拉滿
- CFIUS不同意,智路收購Magnachip宣告失敗
- 又一云相冊關閉:可一鍵遷移至阿里云盤
- 【汽車創新三大驅動力】系列之一:解決電動化和電池測試挑戰的方法探討
- 一個超級實用的單片機調試組件!
- 解決方案 | 芯佰微助力電動車安全:通用型OPA、LDO、RS485芯片,提升報警器性能
- ASML 技術高級副總裁:已攜手蔡司啟動 5nm 分辨率 Hyper NA 光刻機開發
- 搭載Integrity Guard安全架構的芯片交付量突破100億, 充分彰顯英飛凌在安全領域的領導地位
- QNX與Vector簽署諒解備忘錄,共同打造基礎性車輛軟件平臺
- 瑞薩電子放緩增長預期,200 億美元營收目標延后五年至 2035 年
- 消息稱小米玄戒 O2 芯片有望“上車”,自研四合一域控制器已在鋪路
- 優化電動汽車車載充電器瞬態電壓保護和浪涌保護
- 新能源汽車電子水泵溫控系統及靜電浪涌
- 車載顯示接口測試趨勢分析
- Arm+AWS實現AI定義汽車 基于Arm KleidiAI優化并由AWS提供支持