stm32串口應(yīng)用及使用注意事項小白必備

串口是我們常用的一個數(shù)據(jù)傳輸接口,STM32F103系列單片機(jī)共有5個串口。

其中1-3是通用同步/異步串行接口USART(Universal Synchronous/Asynchronous Receiver/Transmitter)。

4,、5是通用異步串行接口UART(Universal Asynchronous Receiver/Transmitter)。

看完文章總結(jié)可以看下邊的資料了解詳細(xì)情況

(stm32 USART串口應(yīng)用)

http://www.makeru.com.cn/live/1392_1164.html?s=45051

通過Z-stack協(xié)議棧實現(xiàn)串口透傳

http://www.makeru.com.cn/live/1758_330.html?s=45051

配置串口包括三部分內(nèi)容:

1. I/O口配置:TXD配置為復(fù)用推挽輸出(GPIO_Mode_AF_PP),RXD配置為浮空輸入(GPIO_Mode_IN_FLOATING);

2. 串口配置:波特率等;

3. 中斷向量配置:一般用中斷方式接收數(shù)據(jù)瘪匿。

注意事項:

1. USART1是掛在APB2,使能時鐘命令為:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );

其他幾個則掛在APB1上寻馏,如2口:

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );

2. 配置4口和5口的時候棋弥,中斷名為UART4、UART5诚欠,中斷入口分別為

UART4_IRQn顽染、UART5_IRQn

對應(yīng)的中斷服務(wù)函數(shù)為

void UART4_IRQHandler(void)

void UART5_IRQHandler(void)。

下面是5個串口的配置函數(shù)和收發(fā)數(shù)據(jù)函數(shù)代碼:

#include “stm32f10x.h”

#include “misc.h”

#include “stm32f10x_gpio.h”

#include “stm32f10x_usart.h”

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; //復(fù)用推挽輸出;

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; //數(shù)據(jù)位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;

//收發(fā)模式;

USART_Init(USART1, &USART_InitStructure);//配置串口參數(shù);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置中斷組轰绵,4位搶占優(yōu)先級粉寞,4位響應(yīng)優(yōu)先級;

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中斷號;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶占優(yōu)先級;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應(yīng)優(yōu)先級;

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) //發(fā)送一個字節(jié);

{

USART_SendData(USART1,Data);

while( USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET );

}

void USART1_Send_String(u8 *Data) //發(fā)送字符串;

{

while(*Data)

USART1_Send_Byte(*Data++);

}

void USART1_IRQHandler(void) //中斷處理函數(shù);

{

u8 res;

if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //判斷是否發(fā)生中斷;

{

USART_ClearFlag(USART1, USART_IT_RXNE); //清除標(biāo)志位;

res=USART_ReceiveData(USART1); //接收數(shù)據(jù);

USART1_Send_Byte(res); //用戶自定義;

}

}

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; //復(fù)用推挽輸出;

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; //數(shù)據(jù)位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;

//收發(fā)模式;

USART_Init(USART2, &USART_InitStructure);//配置串口參數(shù);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置中斷組,4位搶占優(yōu)先級左腔,4位響應(yīng)優(yōu)先級;

NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //中斷號;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶占優(yōu)先級;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應(yīng)優(yōu)先級;

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) //發(fā)送一個字節(jié);

{

USART_SendData(USART2,Data);

while( USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET );

}

void USART2_Send_String(u8 *Data) //發(fā)送字符串;

{

while(*Data)

USART2_Send_Byte(*Data++);

}

void USART2_IRQHandler(void) //中斷處理函數(shù);

{

u8 res;

if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET) //判斷是否發(fā)生中斷;

{

USART_ClearFlag(USART2, USART_IT_RXNE); //清除標(biāo)志位;

res=USART_ReceiveData(USART2); //接收數(shù)據(jù);

USART2_Send_Byte(res); //用戶自定義;

}

}

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; //復(fù)用推挽輸出;

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; //數(shù)據(jù)位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;

//收發(fā)模式;

USART_Init(USART3, &USART_InitStructure);//配置串口參數(shù);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置中斷組唧垦,4位搶占優(yōu)先級,4位響應(yīng)優(yōu)先級;

NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //中斷號;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶占優(yōu)先級;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應(yīng)優(yōu)先級;

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) //發(fā)送一個字節(jié);

{

USART_SendData(USART3,Data);

while( USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET );

}

void USART3_Send_String(u8 *Data) //發(fā)送字符串;

{

while(*Data)

USART3_Send_Byte(*Data++);

}

void USART3_IRQHandler(void) //中斷處理函數(shù);

{

u8 res;

if(USART_GetITStatus(USART3, USART_IT_RXNE) == SET) //判斷是否發(fā)生中斷;

{

USART_ClearFlag(USART3, USART_IT_RXNE); //清除標(biāo)志位;

res=USART_ReceiveData(USART3); //接收數(shù)據(jù);

USART3_Send_Byte(res); //用戶自定義;

}

}

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; //復(fù)用推挽輸出;

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; //數(shù)據(jù)位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;

//收發(fā)模式;

USART_Init(UART4, &USART_InitStructure);//配置串口參數(shù);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置中斷組液样,4位搶占優(yōu)先級振亮,4位響應(yīng)優(yōu)先級;

NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //中斷號;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶占優(yōu)先級;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應(yīng)優(yōu)先級;

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) //發(fā)送一個字節(jié);

{

USART_SendData(UART4,Data);

while( USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET );

}

void UART4_Send_String(u8 *Data) //發(fā)送字符串;

{

while(*Data)

UART4_Send_Byte(*Data++);

}

void UART4_IRQHandler(void) //中斷處理函數(shù);

{

u8 res;

if(USART_GetITStatus(UART4, USART_IT_RXNE) == SET) //判斷是否發(fā)生中斷;

{

USART_ClearFlag(UART4, USART_IT_RXNE); //清除標(biāo)志位;

res=USART_ReceiveData(UART4); //接收數(shù)據(jù);

UART4_Send_Byte(res); //用戶自定義;

}

}

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; //復(fù)用推挽輸出;

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; //數(shù)據(jù)位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;

//收發(fā)模式;

USART_Init(UART5, &USART_InitStructure);//配置串口參數(shù);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置中斷組巧还,4位搶占優(yōu)先級,4位響應(yīng)優(yōu)先級;

NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; //中斷號;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶占優(yōu)先級;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應(yīng)優(yōu)先級;

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) //發(fā)送一個字節(jié);

{

USART_SendData(UART5,Data);

while( USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET );

}

void UART5_Send_String(u8 *Data) //發(fā)送字符串;

{

while(*Data)

UART5_Send_Byte(*Data++);

}

void UART5_IRQHandler(void) //中斷處理函數(shù);

{

u8 res;

if(USART_GetITStatus(UART5, USART_IT_RXNE) == SET) //判斷是否發(fā)生中斷;

{

USART_ClearFlag(UART5, USART_IT_RXNE); //清除標(biāo)志位;

res=USART_ReceiveData(UART5); //接收數(shù)據(jù);

UART5_Send_Byte(res); //用戶自定義;

}

}

祝大家學(xué)習(xí)愉快!咱們也可以一起來學(xué)習(xí)交流點擊鏈接加入群聊【嵌入式單片機(jī)Linux C交流群】【715272998】:https://jq.qq.com/?_wv=1027&k=Fk0u8pUw

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末坊秸,一起剝皮案震驚了整個濱河市麸祷,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌褒搔,老刑警劉巖阶牍,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異站超,居然都是意外死亡荸恕,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進(jìn)店門死相,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人咬像,你說我怎么就攤上這事算撮。” “怎么了县昂?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵肮柜,是天一觀的道長。 經(jīng)常有香客問我倒彰,道長审洞,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任待讳,我火速辦了婚禮芒澜,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘创淡。我一直安慰自己痴晦,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布琳彩。 她就那樣靜靜地躺著誊酌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪露乏。 梳的紋絲不亂的頭發(fā)上碧浊,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天,我揣著相機(jī)與錄音瘟仿,去河邊找鬼箱锐。 笑死,一個胖子當(dāng)著我的面吹牛猾骡,可吹牛的內(nèi)容都是我干的瑞躺。 我是一名探鬼主播敷搪,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼幢哨!你這毒婦竟也來了赡勘?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤捞镰,失蹤者是張志新(化名)和其女友劉穎闸与,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體岸售,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡践樱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了凸丸。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拷邢。...
    茶點故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖屎慢,靈堂內(nèi)的尸體忽然破棺而出瞭稼,到底是詐尸還是另有隱情,我是刑警寧澤腻惠,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布环肘,位于F島的核電站,受9級特大地震影響集灌,放射性物質(zhì)發(fā)生泄漏悔雹。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一欣喧、第九天 我趴在偏房一處隱蔽的房頂上張望腌零。 院中可真熱鬧,春花似錦续誉、人聲如沸莱没。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽饰躲。三九已至,卻和暖如春臼隔,著一層夾襖步出監(jiān)牢的瞬間嘹裂,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工摔握, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留寄狼,地道東北人。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像泊愧,于是被迫代替她去往敵國和親伊磺。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,612評論 2 350