STM32F1xx的5個串列埠使用方法
STM32F1系列晶片的5個串列埠初始化方法(有的晶片引腳少,只有3個串列埠)
串列埠是我們常用的一個資料傳輸介面,STM32F103系列微控制器共有5個串列埠
- 1-3是通用同步/非同步序列介面USART(Universal Synchronous/ Asynchronous Receiver/ Transmitter)
- 4、5是通用非同步序列介面UART(Universal Asynchronous Receiver/Transmitter)
配置串列埠包括三部分內容:
- I/O口配置:TXD配置為複用推輓輸出(GPIO_Mode_AF_PP),RXD配置為浮空輸入(GPIO_Mode_IN_FLOATING);
- 串列埠配置:波特率,校驗位,停止位等;
- 中斷向量配置:一般用中斷方式接收資料。
注意事項
- USART1是掛在APB2,使能時鐘命令為:
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_USART1, ENABLE );
其他幾個則掛在APB1上,如串列埠2:RCC_APB1PeriphClockCmd ( RCC_APB1Periph_USART2, ENABLE );- 配置4口和5口的時候,中斷名為UART4、UART5,中斷入口分別為UART4_IRQn、UART5_IRQn
對應的中斷服務函式為void UART4_IRQHandler(void) 和 void UART5_IRQHandler(void)。
下面是5個串列埠的配置函式和收發資料函式程式碼:
串列埠1
void USART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //複用推輓輸出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //埠A;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入;
GPIO_Init(GPIOA, &GPIO_InitStructure); //埠A;
USART_InitStructure.USART_BaudRate = 9600; //波特率;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //資料位8位;
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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(USART1, &USART_InitStructure);//配置串列埠引數;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設定中斷組,4位搶佔優先順序,4位響應優先順序;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中斷號;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶佔優先順序;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應優先順序;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); //使能串列埠;
}
void USART1_Send_Byte(u8 Data) //傳送一個位元組;
{
USART_SendData(USART1,Data);
while( USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET );
}
void USART1_Send_String(u8 *Data) //傳送字串;
{
while(*Data)
USART1_Send_Byte(*Data++);
}
void USART1_IRQHandler(void) //中斷處理函式;
{
u8 res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //判斷是否發生中斷;
{
USART_ClearFlag(USART1, USART_IT_RXNE); //清除標誌位;
res=USART_ReceiveData(USART1); //接收資料;
USART1_Send_Byte(res); //使用者自定義;
}
}
串列埠2
void USART2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //USART2 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //複用推輓輸出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //埠A;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //USART2 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入;
GPIO_Init(GPIOA, &GPIO_InitStructure); //埠A;
USART_InitStructure.USART_BaudRate = 9600; //波特率;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //資料位8位;
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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);//配置串列埠引數;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設定中斷組,4位搶佔優先順序,4位響應優先順序;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //中斷號;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶佔優先順序;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應優先順序;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE); //使能串列埠;
}
void USART2_Send_Byte(u8 Data) //傳送一個位元組;
{
USART_SendData(USART2,Data);
while( USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET );
}
void USART2_Send_String(u8 *Data) //傳送字串;
{
while(*Data)
USART2_Send_Byte(*Data++);
}
void USART2_IRQHandler(void) //中斷處理函式;
{
u8 res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET) //判斷是否發生中斷;
{
USART_ClearFlag(USART2, USART_IT_RXNE); //清除標誌位;
res=USART_ReceiveData(USART2); //接收資料;
USART2_Send_Byte(res); //使用者自定義;
}
}
串列埠3
void USART3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART3 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //複用推輓輸出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //埠B;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //USART3 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入;
GPIO_Init(GPIOB, &GPIO_InitStructure); //埠B;
USART_InitStructure.USART_BaudRate = 9600; //波特率;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //資料位8位;
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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);//配置串列埠引數;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設定中斷組,4位搶佔優先順序,4位響應優先順序;
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //中斷號;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶佔優先順序;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應優先順序;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE); //使能串列埠;
}
void USART3_Send_Byte(u8 Data) //傳送一個位元組;
{
USART_SendData(USART3,Data);
while( USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET );
}
void USART3_Send_String(u8 *Data) //傳送字串;
{
while(*Data)
USART3_Send_Byte(*Data++);
}
void USART3_IRQHandler(void) //中斷處理函式;
{
u8 res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) == SET) //判斷是否發生中斷;
{
USART_ClearFlag(USART3, USART_IT_RXNE); //清除標誌位;
res=USART_ReceiveData(USART3); //接收資料;
USART3_Send_Byte(res); //使用者自定義;
}
}
64腳以上的晶片才有
串列埠4
void UART4_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //UART4 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //複用推輓輸出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //埠C;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //UART4 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入;
GPIO_Init(GPIOC, &GPIO_InitStructure); //埠C;
USART_InitStructure.USART_BaudRate = 9600; //波特率;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //資料位8位;
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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(UART4, &USART_InitStructure);//配置串列埠引數;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設定中斷組,4位搶佔優先順序,4位響應優先順序;
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //中斷號;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶佔優先順序;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應優先順序;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
USART_Cmd(UART4, ENABLE); //使能串列埠;
}
void UART4_Send_Byte(u8 Data) //傳送一個位元組;
{
USART_SendData(UART4,Data);
while( USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET );
}
void UART4_Send_String(u8 *Data) //傳送字串;
{
while(*Data)
UART4_Send_Byte(*Data++);
}
void UART4_IRQHandler(void) //中斷處理函式;
{
u8 res;
if(USART_GetITStatus(UART4, USART_IT_RXNE) == SET) //判斷是否發生中斷;
{
USART_ClearFlag(UART4, USART_IT_RXNE); //清除標誌位;
res=USART_ReceiveData(UART4); //接收資料;
UART4_Send_Byte(res); //使用者自定義;
}
}
64腳以上的晶片才有
串列埠5
void UART5_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //UART5 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //複用推輓輸出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //埠C;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //UART5 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入;
GPIO_Init(GPIOD, &GPIO_InitStructure); //埠D;
USART_InitStructure.USART_BaudRate = 9600; //波特率;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //資料位8位;
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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(UART5, &USART_InitStructure); //配置串列埠引數;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設定中斷組,4位搶佔優先順序,4位響應優先順序;
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; //中斷號;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶佔優先順序;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應優先順序;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
USART_Cmd(UART5, ENABLE); //使能串列埠;
}
void UART5_Send_Byte(u8 Data) //傳送一個位元組;
{
USART_SendData(UART5,Data);
while( USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET );
}
void UART5_Send_String(u8 *Data) //傳送字串;
{
while(*Data)
UART5_Send_Byte(*Data++);
}
void UART5_IRQHandler(void) //中斷處理函式;
{
u8 res;
if(USART_GetITStatus(UART5, USART_IT_RXNE) == SET) //判斷是否發生中斷;
{
USART_ClearFlag(UART5, USART_IT_RXNE); //清除標誌位;
res=USART_ReceiveData(UART5); //接收資料;
UART5_Send_Byte(res); //使用者自定義;
}
}
相關文章
- IBM串列埠線序以及串列埠線的做法(轉)IBM串列埠
- 串列埠UART串列埠
- QT實現串列埠助手中串列埠名的實時更新QT串列埠
- 帶內串列埠 在串列埠中輸入命令串列埠
- Linux下串列埠通訊詳解(下)讀寫串列埠及關閉串列埠Linux串列埠
- 沒有真實串列埠裝置時使用"虛擬串列埠驅動"除錯你的串列埠程式碼串列埠除錯
- 串列埠資料抓取及串列埠通訊模擬串列埠
- 你真的瞭解串列埠嗎(示波器串列埠波形分析)串列埠
- 串列埠流控串列埠
- 串列埠通訊串列埠
- 串列埠blog串列埠
- Linux下PCI轉串列埠卡及USB轉串列埠Linux串列埠
- 安卓下的串列埠測試安卓串列埠
- Linux 串列埠程式設計 串列埠裝置程式開發Linux串列埠程式設計
- 【STM32】串列埠串列埠
- 串列埠小工具串列埠
- mina serial 串列埠串列埠
- android串列埠程式Android串列埠
- putty能使用串列埠串列埠
- 串列埠通訊 (轉)串列埠
- Uart進行的串列埠收發串列埠
- VC++ 的串列埠通訊 (轉)C++串列埠
- 串列埠、IIC、SPI的優缺點串列埠
- 9針串列埠引腳定義 25針串列埠引腳定義串列埠
- 串列埠,COM口,UART,USART串列埠
- linux 串列埠通訊Linux串列埠
- 串列埠通訊協議串列埠協議
- Android 串列埠通訊Android串列埠
- ubuntu繫結串列埠號Ubuntu串列埠
- 9針串列埠除錯串列埠除錯
- 初學串列埠問題串列埠
- C# 串列埠通訊C#串列埠
- 11. 串列埠通訊串列埠
- 串列埠通訊型別串列埠型別
- (10)uart串列埠通訊串列埠
- C#串列埠通訊遇到的坑C#串列埠
- qt---串列埠共享庫的製作QT串列埠
- Linux串列埠程式設計Linux串列埠程式設計