图文概述: 

 

代码:

#include "stm32f10x.h" // Device header

#include

#include

/*对应的引脚号*/

#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---对串口通信的初始化配置(针对发送数据即USART1外设的TX引脚)

* @param 无

* @retval 无

*/

void Serial_Init(void)

{

//1.开启APB2外设的时钟---USART1是APB2的外设

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

//2.初始化GPIO的引脚配置(即USART1外设的TX引脚)

GPIO_InitTypeDef GPIO_InitStructure;

/* TX引脚是USART1外设控制的输出脚,需要选复用推挽输出模式 */

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外设控制的输出脚,需要选浮空输入或上拉输入 */

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;//硬件数据流控选择---不需要流控

USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//串口模式---同时开启发送模式和接收模式

USART_InitStructure.USART_Parity = USART_Parity_No;//数据帧的校验位---不需要校验

USART_InitStructure.USART_StopBits = USART_StopBits_1;//数据帧的停止位---选择1位的停止位

USART_InitStructure.USART_WordLength = USART_WordLength_8b;//数据帧字长---选择8位

USART_Init(USART1, &USART_InitStructure);

/* 当RXNE标志位一旦置1,就向NVIC申请中断,可以使用中断函数接收数据 */

//开启触发RXNE标志位到NVIC的输出

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

//配置NVIC

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //NVIC优先级分组

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //NVIC中断通道配置为USART1通道

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能通道

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级设为1

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级设为1

NVIC_Init(&NVIC_InitStructure);

//4.开启USART

USART_Cmd(USART1, ENABLE);

}

/**

* @brief Serial_SendByte---发送1位字节数据

* @param Byte---发送的字节数据

* @retval 无

*/

void Serial_SendByte(uint8_t Byte)

{

//1.发送数据

USART_SendData(USART1, Byte);

//2.判断发送数据寄存器为空的标志位(标志位为1则表示数据转移到移位寄存器,为0则还没有转移到移位寄存器)

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

}

/**

* @brief Serial_SendArray---发送数组元素是字节数据的数组

* @param *Array---发送的数组的首地址指针

* @param Length---发送的数组的长度

* @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---发送一串字符串

* @param *String---发送的字符串的地址指针

* @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---底数

* @param Y---指数

* @retval Result---计算的结果值

*/

uint32_t Serial_Pow(uint32_t X, uint32_t Y)

{

uint32_t Result = 1;

while(Y--)

{

Result *= X;

}

return Result;

}

/**

* @brief Serial_SendNumber---发送无符号整型数据(即数字)

* @param Number---要发送的数字数据

* @param Length---数字数据的长度

* @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的底层函数(移植printf函数,使printf输出到串口)

* @param ch---需要输出的字符

* @param *f---指定的某个指针地址

* @retval ch---输出的字符

*/

int fputc(int ch, FILE *f)

{

Serial_SendByte(ch);

return ch;

}

/**

* @brief Serial_Printf---对sprintf进行封装,便于数据输出到串口

* @param *format---接收的格式化字符串

* @param ...---接收可变参数列表

* @retval 无

*/

void Serial_Printf(char *format, ...)

{

char String[100];

va_list arg;//va_list---列表名

va_start(arg, format);//从format位置后开始接收参数表,放在arg

vsprintf(String, format, arg);/* vsprintf---使用参数列表发送格式化输出到字符串。 */

va_end(arg);//释放参数表

Serial_SendString(String);//串口发送字符串

}

uint8_t Serial_GetRxFlag(void)

{

if(Serial_RxFlag == 1)

{

Serial_RxFlag = 0;

return 1;

}

return 0; //返回状态值

}

uint8_t Serial_GetRxData(void)

{

return Serial_RxData; //返回接受数据寄存器的值

}

//USART1通道的中断函数

void USART1_IRQHandler(void)

{

//获取USART的接收数据寄存器不为空中断标志位

if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)

{

//获取接收数据寄存器中的值

Serial_RxData = USART_ReceiveData(USART1);

//置变量Serial_RxFlag为1(表示接收到数据)

Serial_RxFlag = 1;

//手动清除清除USART1的中断挂起位(如果获取接收寄存器则会自动清除标志位)

USART_ClearITPendingBit(USART1, USART_IT_RXNE);

}

}

关键步骤:

1. 开启APB2外设的时钟---USART1是APB2的外设(需要查看手册了解对应芯片上的USART1_TX和USART1_RX的引脚分布在哪个GPIO口上)

//1.开启APB2外设的时钟---USART1是APB2的外设

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

2. 初始化GPIO的引脚配置(即USART1外设的TX引脚) 

//2.初始化GPIO的引脚配置(即USART1外设的TX引脚)

GPIO_InitTypeDef GPIO_InitStructure;

/* TX引脚是USART1外设控制的输出脚,需要选复用推挽输出模式 */

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外设控制的输出脚,需要选浮空输入或上拉输入 */

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;//硬件数据流控选择---不需要流控

USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//串口模式---同时开启发送模式和接收模式

USART_InitStructure.USART_Parity = USART_Parity_No;//数据帧的校验位---不需要校验

USART_InitStructure.USART_StopBits = USART_StopBits_1;//数据帧的停止位---选择1位的停止位

USART_InitStructure.USART_WordLength = USART_WordLength_8b;//数据帧字长---选择8位

USART_Init(USART1, &USART_InitStructure);

补充:如果需要接收数据后进入中断,则还需要配置NVIC ,即如下代码:

/* 当RXNE标志位一旦置1,就向NVIC申请中断,可以使用中断函数接收数据 */

//开启触发RXNE标志位到NVIC的输出

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

//配置NVIC

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //NVIC优先级分组

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //NVIC中断通道配置为USART1通道

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能通道

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级设为1

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级设为1

NVIC_Init(&NVIC_InitStructure);

4. 开启USART

USART_Cmd(USART1, ENABLE);

5. 发送字节数据的基础函数:

/**

* @brief Serial_SendByte---发送1位字节数据

* @param Byte---发送的字节数据

* @retval 无

*/

void Serial_SendByte(uint8_t Byte)

{

//1.发送数据

USART_SendData(USART1, Byte);

//2.判断发送数据寄存器为空的标志位(标志位为1则表示数据转移到移位寄存器,为0则还没有转移到移位寄存器)

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

}

 6. 接收字节数据的基础函数(在触发的中断函数中接收数据):

//USART1通道的中断函数

void USART1_IRQHandler(void)

{

//获取USART的接收数据寄存器不为空中断标志位

if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)

{

//获取接收数据寄存器中的值

Serial_RxData = USART_ReceiveData(USART1);

//置变量Serial_RxFlag为1(表示接收到数据)

Serial_RxFlag = 1;

//手动清除清除USART1的中断挂起位(如果获取接收寄存器则会自动清除标志位)

USART_ClearITPendingBit(USART1, USART_IT_RXNE);

}

}

     

 

精彩文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。