C8T6串口概述
STM32F103C8T6微控制器包含3個串口模塊:
-
USART1 (高級串口)
-
USART2
-
USART3 (部分型號可能標記為UART3)
-
引腳分布圖
-
USART1 (串口1)
基本特性
-
類型:全功能USART(通用同步異步收發器)
-
通信模式:
-
全雙工異步通信
-
單線半雙工通信
-
同步通信(需要外部時鐘)
-
支持LIN總線協議
-
支持IrDA SIR ENDEC規范
-
支持智能卡協議(ISO7816)
-
引腳分配
-
TX:PA9 (主用), PB6 (重映射)
-
RX:PA10 (主用), PB7 (重映射)
-
CK:PA8 (同步模式時鐘, 主用), PB6 (重映射)
-
CTS:PA11 (主用), PB13 (重映射)
-
RTS:PA12 (主用), PB14 (重映射)
性能參數
-
最高支持4.5Mbps傳輸速率(在72MHz系統時鐘下)
-
可編程數據字長度(8或9位)
-
可配置的停止位(1或2位)
-
可選的奇偶校驗位(偶校驗/奇校驗)
USART2 (串口2)
基本特性
-
類型:基本USART
-
通信模式:
-
全雙工異步通信
-
單線半雙工通信
-
不支持同步模式和硬件流控制
-
引腳分配
-
TX:PA2 (主用), PD5 (重映射)
-
RX:PA3 (主用), PD6 (重映射)
性能參數
-
最高支持2.25Mbps傳輸速率
-
可編程數據字長度(8或9位)
-
可配置的停止位(1位)
-
可選的奇偶校驗位(偶校驗/奇校驗)
USART3 (串口3)
基本特性
-
類型:基本UART(在STM32F103C8T6上功能較USART1簡化)
-
通信模式:
-
全雙工異步通信
-
單線半雙工通信
-
不支持同步模式
-
引腳分配
-
TX:PB10 (主用), PC10 (部分重映射), PD8 (完全重映射)
-
RX:PB11 (主用), PC11 (部分重映射), PD9 (完全重映射)
性能參數
-
最高支持2.25Mbps傳輸速率
-
可編程數據字長度(8位)
-
可配置的停止位(1位)
-
可選的奇偶校驗位(偶校驗/奇校驗)
主要區別對比
特性 | USART1 | USART2 | USART3 |
---|---|---|---|
類型 | 全功能USART | 基本USART | 基本UART |
同步模式 | 支持 | 不支持 | 不支持 |
硬件流控制 | 支持(CTS/RTS) | 不支持 | 不支持 |
最高速率 | 4.5Mbps | 2.25Mbps | 2.25Mbps |
DMA支持 | 發送和接收 | 發送和接收 | 發送和接收 |
中斷源 | 豐富 | 基本 | 基本 |
時鐘源 | PCLK2(APB2) | PCLK1(APB1) | PCLK1(APB1) |
時鐘配置
-
USART1掛載在APB2總線上(最高72MHz)
-
USART2和USART3掛載在APB1總線上(最高36MHz)
應用建議
-
USART1:推薦用于高速通信或需要硬件流控制的場景
-
USART2/USART3:適合普通速率通信,節省硬件資源
-
多串口應用時,可根據外設連接方便性選擇不同的串口
STM32 代碼實現
注意:本次采用串口1? tx :pa9 rx:pa10實現。
proteus 仿真電路圖
虛擬串口配置:虛擬串口配置教程
串口3實現代碼
? ? ? ? usart.c
#include "usart3.h"
#include "stm32f10x.h"
#include "sys.h"static int Send_buf[10] = {0} ; u8 USART3_RX_STA = 0; //接收狀態標記
u8 USART3_RX_CMD;void USART3_Init(u32 bound)
{//GPIO端口設置GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //使能USART3時鐘RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能GPIOB時鐘,端口復用時鐘//USART3_TX GPIOB.10GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //萬勿省略此條,一經刪除則發送不了數據GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOB.10//USART3_RX GPIOB.11GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //PB11GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOB.11 //USART 初始化設置USART_InitStructure.USART_BaudRate = bound; //串口波特率USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字長為8位數據格式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(USART3, &USART_InitStructure); //初始化串口3 USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //開啟串口接受中斷//Usart3 NVIC 配置NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; //搶占優先級3NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子優先級3NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能NVIC_Init(&NVIC_InitStructure); //根據指定的參數初始化VIC寄存器USART_Cmd(USART3, ENABLE);}void USART3_SendByte(uint8_t Data) //串口發送一個字節;字節 (byte) 1byte=8bit
{while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); //USART_FLAG_TXE發送寄存器空USART_SendData(USART3, Data); //從串口2發送傳入的數據while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); //USART_FLAG_TC發送完成標志
}void USART3_IRQHandler(void) //串口2中斷服務程序
{u8 Res=0;if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中斷{Res=USART_ReceiveData(USART3); //讀取接收到的數據USART3_RX_CMD = Res; //把接收到的數據傳遞給USART_RX_CMDUSART3_RX_STA=1;USART3_SendByte(Res); //去進入主函數WHILE語句 }
}void USART3_SendCmd(int len)
{ int i = 0 ;USART3_SendByte(0x7E); //起始for(i=0; i<len; i++)//數據{ USART3_SendByte(Send_buf[i]); //len 為8 ;依次將Send_buf[0]、Send_buf[1] !!!Send_buf[8] 發送出來 }USART3_SendByte(0xEF); //結束}void Uart3_SendCMD(int CMD ,int dat1 , int dat2 ,int dat3)
{Send_buf[0] = 0xff; //保留字節 Send_buf[1] = 0x06; //長度Send_buf[2] = CMD; //控制指令Send_buf[3] = (int)(dat1); //Send_buf[4] = (int)(dat2); //datah2Send_buf[5] = (int)(dat3); //datal3USART3_SendCmd(6); //發送此幀數據
}void USART3_SendString(char *str)
{while (*str){while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);USART_SendData(USART3, *str++);}
}
usart.c
#ifndef __USART3_H
#define __USART3_H#include "stm32f10x.h"
#include <stdio.h>
#include "sys.h" #define EN_USART3_RX 1 //使能(1)/禁止(0)串口1接收extern u8 USART3_RX_STA; //接收狀態標記
extern u8 USART3_RX_CMD; //void Init_hardware_usart2_(u32 bound);
void USART3_Init(u32 bound);void USART3_IRQHandler(void);void USART3_SendByte(uint8_t Data); void Uart3_SendCMD(int CMD ,int dat1 , int dat2 ,int dat3);
void USART3_SendString(char *str);#endif
?
主函數
#include "stm32f10x.h"
#include "delay.h"
#include "oled.h"#include "usart3.h"//蜂鳴器初始化---------------------------------------------------------------------------------------
#define BEEP_GPIO_PIN_PROT GPIOB
#define BEEP_GPIO_PIN GPIO_Pin_5
void Beep_Init()
{GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);GPIO_InitStructure.GPIO_Pin =BEEP_GPIO_PIN ;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(BEEP_GPIO_PIN_PROT, &GPIO_InitStructure);//GPIO_ResetBits(BEEP_GPIO_PIN_PROT,BEEP_GPIO_PIN);//默認給0,關閉GPIO_ResetBits(BEEP_GPIO_PIN_PROT,BEEP_GPIO_PIN);
}
//蜂鳴器驅動
void Beep_Run(unsigned char x)
{if(x==1){GPIO_SetBits(BEEP_GPIO_PIN_PROT,BEEP_GPIO_PIN);}else{GPIO_ResetBits(BEEP_GPIO_PIN_PROT,BEEP_GPIO_PIN);}}int main()
{SystemInit();//配置系統時鐘為72M delay_init(72);//延時初始化OLED_Init();//OLED 屏幕USART3_Init(9600);USART3_SendString("hello");while (1){}//while line}