串口功能講解
串口框圖
對應的板載引腳,我的是STM32F103VET6
引腳 | APB2總線 | APB1總線 | APB1總線 | APB1總線 | APB1總線 |
---|---|---|---|---|---|
串口 | USART1 | USART2 | USART3 | USART4 | USART5 |
TXD | PA9 | PA2 | PB10 | PC10 | PC12 |
RXD | PA10 | PA3 | PB11 | PC11 | PD2 |
SCLK | PA8 | PA4 | PB12 | ||
NCTS | PA11 | PA0 | PB13 | ||
NRTS | PA12 | PA1 | PB14 |
TXD:數據發送
RXD:數據接收
SCLK:時鍾,僅同步通信時使用
nRTS:請求發送(Request to send)
nCTS:允許發送(CLear to send)
注意:STM32F103VET6 系統控制器有三個 USART 和兩個 UART,其中 USART1 和時鍾來源 於 APB2 總線時鍾,其最大頻率為 72MHz,其他四個的時鍾來源於 APB1 總線時鍾,其最 大頻率為 36MHz。UART 只是異步傳輸功能,所以沒有 SCLK、nCTS 和 nRTS 功能引腳。
數據寄存器——USART_DR:9位有效,包含一個發送數據寄存器IDR和一個接收數據寄存器RDR,一個地址對應了兩個物理內存。
數據寄存器
數據寄存器——USART_DR:只有底9位有效,包含一個發送數據寄存器TDR和一個接收數據寄存器RDR。一個地址對應了兩個物理內存。
數據格式
(啟動位)USART_CR1:M,0:8bit 1:9bit
(停止位)USART_CR2:STOP
(校驗位)USATR_CR1:PCE(使能校驗),PS(選擇校驗),PEIE
(校驗檢測)USART_SR:PE(校驗錯誤)
具體流程
數據發送
UE:USART使能(USART enable) 0:時鍾與輸出被禁止 1:模塊使能
當該位被清零 .
TE:發送使能(使能后 串口可以發送數據)
內存→(讀取)CPU或DMA→發送數據寄存器(TDR)→發送移位寄存器→TX
TXE(發送數據寄存器空)→TXC(發送移位寄存器完成)
數據接收
UE:USART使能(USART enable) 0:時鍾與輸出被禁止 1:模塊使能
當該位被清零 .
RE:接收使能(使能后 串口可以接收數據).
RX→接收移位寄存器→接收數據寄存器(RXNE)
波特率
波特率:每秒鍾要發送多少數據
USART_BRR:波特率寄存器
分數波特率的產生
Tx / Rx 波特率 =fck/16*USARTDIV
fck:外設的時鍾(PCLK1用於USART2、3、4、5,PCLK2用於USART1)
注意需要區分APB1 APB2 兩條總線的時鍾
USARTDIV:無符號的定點數 這12位的值設置在USART_BRR寄存器
列如:串口1
USART:USART1 時鍾為72M
波特率:115200
115200 = 72000000/16*USARTDIV
USARTDIV = 39.0625
DIV_Fraction = 0.0625*16 = 1 = 0x01
DIV_Mantissa = 39 = 0x17
則:USART_BRR = 0X171
基本結構體
串口初始化結構體
typedef struct
{
//串口波特率 115200
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 */
//字長 控制寄存器M位
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 whenthe 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 enabledor disabled.
This parameter can be a value of @ref USART_Hardware_Flow_Control */
} USART_InitTypeDef;
串口時鍾配置(同步還是異步通訊)
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;