圖文概述:?
?
代碼:
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
/*對應(yīng)的引腳號*/
#define USART1_TX GPIO_Pin_9
#define USART1_RX GPIO_Pin_10
/*模塊需要使用到的端口:GPIOA或GPIOB*/
#define BUS GPIOA
uint8_t Serial_RxData;
uint8_t Serial_RxFlag;
/**
* @brief Serial_Init---對串口通信的初始化配置(針對發(fā)送數(shù)據(jù)即USART1外設(shè)的TX引腳)
* @param 無
* @retval 無
*/
void Serial_Init(void)
{
//1.開啟APB2外設(shè)的時鐘---USART1是APB2的外設(shè)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//2.初始化GPIO的引腳配置(即USART1外設(shè)的TX引腳)
GPIO_InitTypeDef GPIO_InitStructure;
/* TX引腳是USART1外設(shè)控制的輸出腳,需要選復(fù)用推挽輸出模式 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = USART1_TX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(BUS, &GPIO_InitStructure);
/* RX引腳是USART1外設(shè)控制的輸出腳,需要選浮空輸入或上拉輸入 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入模式
GPIO_InitStructure.GPIO_Pin = USART1_RX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(BUS, &GPIO_InitStructure);
//3.初始化USART的各項配置
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600; //波特率---9600
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//硬件數(shù)據(jù)流控選擇---不需要流控
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//串口模式---同時開啟發(fā)送模式和接收模式
USART_InitStructure.USART_Parity = USART_Parity_No;//數(shù)據(jù)幀的校驗位---不需要校驗
USART_InitStructure.USART_StopBits = USART_StopBits_1;//數(shù)據(jù)幀的停止位---選擇1位的停止位
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//數(shù)據(jù)幀字長---選擇8位
USART_Init(USART1, &USART_InitStructure);
/* 當(dāng)RXNE標(biāo)志位一旦置1,就向NVIC申請中斷,可以使用中斷函數(shù)接收數(shù)據(jù) */
//開啟觸發(fā)RXNE標(biāo)志位到NVIC的輸出
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//配置NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //NVIC優(yōu)先級分組
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //NVIC中斷通道配置為USART1通道
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //搶占優(yōu)先級設(shè)為1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //響應(yīng)優(yōu)先級設(shè)為1
NVIC_Init(&NVIC_InitStructure);
//4.開啟USART
USART_Cmd(USART1, ENABLE);
}
/**
* @brief Serial_SendByte---發(fā)送1位字節(jié)數(shù)據(jù)
* @param Byte---發(fā)送的字節(jié)數(shù)據(jù)
* @retval 無
*/
void Serial_SendByte(uint8_t Byte)
{
//1.發(fā)送數(shù)據(jù)
USART_SendData(USART1, Byte);
//2.判斷發(fā)送數(shù)據(jù)寄存器為空的標(biāo)志位(標(biāo)志位為1則表示數(shù)據(jù)轉(zhuǎn)移到移位寄存器,為0則還沒有轉(zhuǎn)移到移位寄存器)
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
/**
* @brief Serial_SendArray---發(fā)送數(shù)組元素是字節(jié)數(shù)據(jù)的數(shù)組
* @param *Array---發(fā)送的數(shù)組的首地址指針
* @param Length---發(fā)送的數(shù)組的長度
* @retval 無
*/
void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
uint16_t i;
for(i = 0; i < Length; i++)
{
Serial_SendByte(Array[i]);
}
}
/**
* @brief Serial_SendString---發(fā)送一串字符串
* @param *String---發(fā)送的字符串的地址指針
* @retval 無
*/
void Serial_SendString(char *String)
{
uint8_t i;
for(i = 0; String[i] != '\0'; i++)
{
Serial_SendByte(String[i]);
}
}
/**
* @brief Serial_Pow---計算X的Y次方值
* @param X---底數(shù)
* @param Y---指數(shù)
* @retval Result---計算的結(jié)果值
*/
uint32_t Serial_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1;
while(Y--)
{
Result *= X;
}
return Result;
}
/**
* @brief Serial_SendNumber---發(fā)送無符號整型數(shù)據(jù)(即數(shù)字)
* @param Number---要發(fā)送的數(shù)字?jǐn)?shù)據(jù)
* @param Length---數(shù)字?jǐn)?shù)據(jù)的長度
* @retval 無
*/
void Serial_SendNumber(uint32_t Number, uint8_t Length)
{
uint8_t i;
for(i = Length; i > 0; i--)
{
Serial_SendByte((Number / Serial_Pow(10, i-1)) % 10 + '0');
}
}
/**
* @brief fputc---printf的底層函數(shù)(移植printf函數(shù),使printf輸出到串口)
* @param ch---需要輸出的字符
* @param *f---指定的某個指針地址
* @retval ch---輸出的字符
*/
int fputc(int ch, FILE *f)
{
Serial_SendByte(ch);
return ch;
}
/**
* @brief Serial_Printf---對sprintf進(jìn)行封裝,便于數(shù)據(jù)輸出到串口
* @param *format---接收的格式化字符串
* @param ...---接收可變參數(shù)列表
* @retval 無
*/
void Serial_Printf(char *format, ...)
{
char String[100];
va_list arg;//va_list---列表名
va_start(arg, format);//從format位置后開始接收參數(shù)表,放在arg
vsprintf(String, format, arg);/* vsprintf---使用參數(shù)列表發(fā)送格式化輸出到字符串。 */
va_end(arg);//釋放參數(shù)表
Serial_SendString(String);//串口發(fā)送字符串
}
uint8_t Serial_GetRxFlag(void)
{
if(Serial_RxFlag == 1)
{
Serial_RxFlag = 0;
return 1;
}
return 0; //返回狀態(tài)值
}
uint8_t Serial_GetRxData(void)
{
return Serial_RxData; //返回接受數(shù)據(jù)寄存器的值
}
//USART1通道的中斷函數(shù)
void USART1_IRQHandler(void)
{
//獲取USART的接收數(shù)據(jù)寄存器不為空中斷標(biāo)志位
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
//獲取接收數(shù)據(jù)寄存器中的值
Serial_RxData = USART_ReceiveData(USART1);
//置變量Serial_RxFlag為1(表示接收到數(shù)據(jù))
Serial_RxFlag = 1;
//手動清除清除USART1的中斷掛起位(如果獲取接收寄存器則會自動清除標(biāo)志位)
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
關(guān)鍵步驟:
1.?開啟APB2外設(shè)的時鐘---USART1是APB2的外設(shè)(需要查看手冊了解對應(yīng)芯片上的USART1_TX和USART1_RX的引腳分布在哪個GPIO口上)
//1.開啟APB2外設(shè)的時鐘---USART1是APB2的外設(shè)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
2. 初始化GPIO的引腳配置(即USART1外設(shè)的TX引腳)?
//2.初始化GPIO的引腳配置(即USART1外設(shè)的TX引腳)
GPIO_InitTypeDef GPIO_InitStructure;
/* TX引腳是USART1外設(shè)控制的輸出腳,需要選復(fù)用推挽輸出模式 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = USART1_TX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(BUS, &GPIO_InitStructure);
/* RX引腳是USART1外設(shè)控制的輸出腳,需要選浮空輸入或上拉輸入 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入模式
GPIO_InitStructure.GPIO_Pin = USART1_RX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(BUS, &GPIO_InitStructure);
3.?初始化USART的各項配置
//3.初始化USART的各項配置
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600; //波特率---9600
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//硬件數(shù)據(jù)流控選擇---不需要流控
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//串口模式---同時開啟發(fā)送模式和接收模式
USART_InitStructure.USART_Parity = USART_Parity_No;//數(shù)據(jù)幀的校驗位---不需要校驗
USART_InitStructure.USART_StopBits = USART_StopBits_1;//數(shù)據(jù)幀的停止位---選擇1位的停止位
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//數(shù)據(jù)幀字長---選擇8位
USART_Init(USART1, &USART_InitStructure);
補(bǔ)充:如果需要接收數(shù)據(jù)后進(jìn)入中斷,則還需要配置NVIC?,即如下代碼:
/* 當(dāng)RXNE標(biāo)志位一旦置1,就向NVIC申請中斷,可以使用中斷函數(shù)接收數(shù)據(jù) */
//開啟觸發(fā)RXNE標(biāo)志位到NVIC的輸出
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//配置NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //NVIC優(yōu)先級分組
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //NVIC中斷通道配置為USART1通道
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //搶占優(yōu)先級設(shè)為1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //響應(yīng)優(yōu)先級設(shè)為1
NVIC_Init(&NVIC_InitStructure);
4.?開啟USART
USART_Cmd(USART1, ENABLE);
5. 發(fā)送字節(jié)數(shù)據(jù)的基礎(chǔ)函數(shù):
/**
* @brief Serial_SendByte---發(fā)送1位字節(jié)數(shù)據(jù)
* @param Byte---發(fā)送的字節(jié)數(shù)據(jù)
* @retval 無
*/
void Serial_SendByte(uint8_t Byte)
{
//1.發(fā)送數(shù)據(jù)
USART_SendData(USART1, Byte);
//2.判斷發(fā)送數(shù)據(jù)寄存器為空的標(biāo)志位(標(biāo)志位為1則表示數(shù)據(jù)轉(zhuǎn)移到移位寄存器,為0則還沒有轉(zhuǎn)移到移位寄存器)
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
?6. 接收字節(jié)數(shù)據(jù)的基礎(chǔ)函數(shù)(在觸發(fā)的中斷函數(shù)中接收數(shù)據(jù)):
//USART1通道的中斷函數(shù)
void USART1_IRQHandler(void)
{
//獲取USART的接收數(shù)據(jù)寄存器不為空中斷標(biāo)志位
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
//獲取接收數(shù)據(jù)寄存器中的值
Serial_RxData = USART_ReceiveData(USART1);
//置變量Serial_RxFlag為1(表示接收到數(shù)據(jù))
Serial_RxFlag = 1;
//手動清除清除USART1的中斷掛起位(如果獲取接收寄存器則會自動清除標(biāo)志位)
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
? ???文章來源:http://www.zghlxwxcb.cn/news/detail-760441.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-760441.html
到了這里,關(guān)于stm32---串口接收與發(fā)送(針對USART1的Rx和Tx引腳)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!