一.原生串口通訊
原生的串口通信主要是控制器跟串口的設(shè)備或者傳感器通信,不需要經(jīng)過(guò)電平轉(zhuǎn)換芯片來(lái)轉(zhuǎn)換電平,直接就用TTL電平通信
比如: GPS模塊、GSM模塊、串口轉(zhuǎn)WIFI模塊、HC04藍(lán)牙模塊
二. 串口與PC通訊
USB轉(zhuǎn)串口主要用于設(shè)備跟電腦通信
電平轉(zhuǎn)換芯片一般有CH340、PL2303、CP2102、FT232
使用的時(shí)候電腦端需要安裝電平轉(zhuǎn)換芯片的驅(qū)動(dòng)
三. RS232標(biāo)準(zhǔn)串口通訊
RS232標(biāo)準(zhǔn)串口主要用于工業(yè)設(shè)備直接通信
電平轉(zhuǎn)換芯片一般有MAX3232,SP3232
四. STM32串口
1. 內(nèi)部結(jié)構(gòu)
寄存器 | 功能 |
---|---|
TX | 數(shù)據(jù)發(fā)送 |
RX | 數(shù)據(jù)接收 |
SCLK | 時(shí)鐘,僅同步通信時(shí)使用(不常用) |
nRTS | 發(fā)送請(qǐng)求(不常用) |
nCTS | 允許發(fā)送 (不常用) |
2. 串口引腳分布
注意:串口一是APB2總線, 其他是APB1總線
3. 串口引腳重定義
參見(jiàn)復(fù)用重映射和調(diào)試I/O配置寄存器(AFIO_MAPR)
4. 串口寄存器
(1). 串口數(shù)據(jù)寄存器 USART_DR
USART_DR, 9位有效
USART_DR一個(gè)地址對(duì)應(yīng)了兩個(gè)物理內(nèi)存。包含一個(gè)發(fā)送數(shù)據(jù)寄存器TDR和一個(gè)接收數(shù)據(jù)寄存器RDR。
(2). 串口數(shù)據(jù)寄存器 USART_CR1 USART_CR2 用來(lái)配置串口
(3). 串口波特率寄存器USART_BRR
五. 串口相關(guān)結(jié)構(gòu)體
1. 串口初始化結(jié)構(gòu)體 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: 設(shè)置波特率,如9600,115200等
USART_WordLength: 設(shè)置數(shù)據(jù)長(zhǎng)度。具體值:USART_WordLength_8b 或 USART_WordLength_9b
USART_StopBits: 設(shè)置停止位大小
USART_Parity: 奇偶校驗(yàn)
USART_Mode: 設(shè)置收發(fā)使能
發(fā)送接收都使能
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //發(fā)送接收都使能
2.同步時(shí)鐘初始化結(jié)構(gòu)體 (用的少)
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;
六. 串口相關(guān)庫(kù)函數(shù)
1. 串口初始化函數(shù) USART_Init()
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
示例:
USART_InitStructure.USART_BaudRate=115200; //設(shè)置波特率
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //設(shè)置數(shù)據(jù)長(zhǎng)度
USART_InitStructure.USART_StopBits=USART_StopBits_1; //設(shè)置停止位長(zhǎng)度
USART_InitStructure.USART_Parity=USART_Parity_No; //設(shè)置奇偶校驗(yàn)位
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //設(shè)置硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //設(shè)置收發(fā)使能
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
2. 中斷配置函數(shù) USART_ITConfig()
void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
例:使能接受完畢中斷USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
3. 串口使能函數(shù) USART_Cmd
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
4. 數(shù)據(jù)發(fā)送函數(shù) USART_SendData()
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
5. 數(shù)據(jù)接收函數(shù) USART_ReceiveData
uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
6.中斷狀態(tài)位獲取函數(shù) USART_GetITStatus
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
7. 編程方法
例子:
完整的串口發(fā)送, 可以printf, 可以發(fā)送 8位字 16位半字節(jié) 字符串等
中斷接收功能 , 當(dāng)有外部數(shù)據(jù)輸入時(shí)會(huì)產(chǎn)生中斷, 中斷后將得到的 字 再發(fā)送出去,同時(shí)點(diǎn)亮RGB
可以控制RGB燈的亮滅, PC端輸入:0,1,2可以點(diǎn)亮RGB~
psb_usart.h文件(這個(gè)文件將5個(gè)串口都定義好了, 通過(guò)選擇編譯來(lái)確定串口)
#ifndef __BSP_USART_H
#define __BSP_USART_H
#include 'stm32f10x.h'
#include //用哪個(gè)串口就把哪個(gè)置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中運(yùn)行的C程序執(zhí)行某些函數(shù)時(shí)死機(jī)?
下一篇:stm32固件庫(kù)UART操作
推薦閱讀最新更新時(shí)間:2025-04-29 11:04



設(shè)計(jì)資源 培訓(xùn) 開(kāi)發(fā)板 精華推薦
- Microchip 升級(jí)數(shù)字信號(hào)控制器(DSC)產(chǎn)品線 推出PWM 分辨率和 ADC 速度業(yè)界領(lǐng)先的新器件
- 意法半導(dǎo)體STM32MP23x:突破成本限制的工業(yè)AI應(yīng)用核心
- 意法半導(dǎo)體推出用于匹配遠(yuǎn)距離無(wú)線微控制器STM32WL33的集成的匹配濾波芯片
- ESP32開(kāi)發(fā)板連接TFT顯示屏ST7789跳坑記
- 如何讓ESP32支持analogWrite函數(shù)
- LGVL配合FreeType為可變字體設(shè)置字重-ESP32篇
- 使用樹(shù)莓派進(jìn)行 ESP32 Jtag 調(diào)試
- ESP32怎么在SPIFFS里面存儲(chǔ)html,css,js文件,以及網(wǎng)頁(yè)和arduino的通訊
- ESP32 freeRTOS使用測(cè)試
- 具有 LTC2217 16 位 105Msps、高速和高動(dòng)態(tài)范圍 ADC 的演示板
- 基于 LTC1480 超低待機(jī)功耗 RS-485 收發(fā)器的微功率轉(zhuǎn)發(fā)器參考設(shè)計(jì)
- ADR391A 2.5 Vout 微功率、低噪聲精密電壓基準(zhǔn)的典型應(yīng)用
- 2.5V 輸出 ADR391B 微功耗、低噪聲精密電壓基準(zhǔn)的典型應(yīng)用
- LT1170CT、5A 驅(qū)動(dòng)高壓 NPN 的典型應(yīng)用
- 具有無(wú)源平衡功能的 6 節(jié) EV/HEV 集成電池監(jiān)視器和保護(hù)器參考設(shè)計(jì)
- 具有低漂移滿量程微調(diào)的 LT1021DCS8-5 CMOS DAC 基準(zhǔn)的典型應(yīng)用
- NSVC2030JBT3G 用于基本交流應(yīng)用的恒流 LED 驅(qū)動(dòng)器的典型應(yīng)用
- AM2G-1215SZ 15V 2 瓦 DC-DC 轉(zhuǎn)換器的典型應(yīng)用
- 使用 Microchip Technology 的 MSL1064 的參考設(shè)計(jì)
- 防爆電機(jī)定期檢修的重要性_防爆電機(jī)日常檢修的內(nèi)容
- Achronix和Signoff攜手為人工智能/機(jī)器學(xué)習(xí)提供FPGA和eFPGA IP服務(wù)
- 初始化片外RAM,讓程序有更大內(nèi)存空間
- Digi-Key Electronics 將在 2021 ELEXCON 舉辦現(xiàn)場(chǎng)和線上活動(dòng)
- ?平頭哥玄鐵910全球首次兼容安卓系統(tǒng),可運(yùn)行Chrome瀏覽器
- Pixelworks賦能iQOO Neo5S智能手機(jī),讓游戲的視覺(jué)效果拉滿
- CFIUS不同意,智路收購(gòu)Magnachip宣告失敗
- 又一云相冊(cè)關(guān)閉:可一鍵遷移至阿里云盤(pán)
- 【汽車(chē)創(chuàng)新三大驅(qū)動(dòng)力】系列之一:解決電動(dòng)化和電池測(cè)試挑戰(zhàn)的方法探討
- 一個(gè)超級(jí)實(shí)用的單片機(jī)調(diào)試組件!
- 臺(tái)積電計(jì)劃兩年后停止氮化鎵晶圓生產(chǎn),納微轉(zhuǎn)投力積電
- 研華新品搭載最新 AMD Ryzen 嵌入式 8000 系列處理器,驅(qū)動(dòng)AI 智能新篇章
- 美國(guó)EDA恢復(fù)供應(yīng)?
- 從追趕到超越:的盧深視的“AI +三維機(jī)器視覺(jué)“破局之戰(zhàn)
- 常用的音頻處理器有哪些應(yīng)用場(chǎng)景
- 研究顯示2035年全球酒店服務(wù)機(jī)器人市場(chǎng)規(guī)模有望達(dá)125億美元
- i9-14900HX對(duì)比一款換湯不換藥的馬甲老U R9-8945HX
- 納祥科技2W 24位數(shù)字功放NX4920,可用于AI語(yǔ)音播報(bào)、WIFI播放器
- 常用解調(diào)器的定義和工作原理
- 從性能與網(wǎng)絡(luò)傳輸出發(fā),講講鐵威馬MAX系列為什么一騎絕塵
- 系列二:TI模擬芯片選型指南(參與過(guò)系列一活動(dòng)的也可以參加這期喲)
- 預(yù)約有禮:以光代電,硅光芯片了解一下~ 走進(jìn)工程師網(wǎng)絡(luò)學(xué)堂直播,贏好禮
- 直播已結(jié)束【基于邁來(lái)芯第二代位置傳感器優(yōu)化設(shè)計(jì)的新一代產(chǎn)品】
- 有獎(jiǎng)直播|TI Sitara™ 產(chǎn)品在智能電網(wǎng)中的應(yīng)用
- 答題贏京東卡|PI InnoSwitch產(chǎn)品系列學(xué)習(xí)中心
- TI“無(wú)線”風(fēng)光可穿戴現(xiàn)場(chǎng)培訓(xùn)直播,誠(chéng)邀參與,預(yù)注冊(cè)贏好禮
- 有獎(jiǎng)直播|普源精電(RIGOL)MIPI D-PHY測(cè)試技術(shù)主題研討會(huì)
- 2020Q1智能手機(jī)顯示面板市場(chǎng)份額排行,第一名毫無(wú)懸念
- 英特爾助推物聯(lián)網(wǎng)創(chuàng)新應(yīng)用落地,2020 EdgeX中國(guó)挑戰(zhàn)賽打響
- Nordic低功耗藍(lán)牙SoC無(wú)線跟蹤器產(chǎn)品讓你不在丟三落四
- 推動(dòng)SOC全生命周期管理設(shè)計(jì),西門(mén)子收購(gòu)UltraSoC
- BionicFlower仿生花:仿生機(jī)器花朵
- 電容式電壓互感器接線原理
- 電磁式與電容式電壓互感器的主要區(qū)別是什么
- CVT電容式電壓互感器內(nèi)部結(jié)構(gòu)分析
- 節(jié)卡機(jī)器人完成6000萬(wàn)元A+輪融資
- 選購(gòu)掃地機(jī)器人 認(rèn)準(zhǔn)這四大標(biāo)準(zhǔn)就夠了
- 在EVC安裝要結(jié)束時(shí) 注冊(cè)過(guò)程中無(wú)反應(yīng)是什么原因?
- 跪求:請(qǐng)問(wèn)使用VC如何獲取到USB移動(dòng)硬盤(pán)的空間使用情況
- DM642復(fù)位問(wèn)題解決 經(jīng)驗(yàn)分享
- 大家有在熟悉captivate的電容觸摸MSP430FR2633嗎?
- FPGA設(shè)計(jì)經(jīng)驗(yàn)分享
- 請(qǐng)教下PCB上這種焊點(diǎn)是什么作用的?
- 動(dòng)力電池組特性分析與均衡管理
- 論壇能修改昵稱(chēng)嗎
- wince 5.0 WinCEPB50-091231-Product-Update-Rollup-Armv4I.msi
- 曬曬板子