1、头文件
#ifndef _USART_H_
#define _USART_H_
#include "stm32f10x.h"
#include "led.h"
#define USART_DEBUG USART1 //调试打印所使用的串口组
void Usart1_Init(unsigned int baud);
void Usart2_Init(unsigned int baud);
void Usart3_Init(unsigned int baud);
void Usart4_Init(unsigned int baud);
void Usart_SendString(USART_TypeDef *USARTx, unsigned char *str, unsigned short len);
void UsartPrintf(USART_TypeDef *USARTx, char *fmt,...);
void Usart_SendByte(USART_TypeDef *USARTx, unsigned char str);
void USART_test(void);
#endif
2、具体实现
//硬件驱动
#include "usart.h"
#include "delay.h"
//C库
#include <stdarg.h>
#include <string.h>
/*
************************************************************
* 函数名称: Usart1_Init
*
* 函数功能: 串口1初始化
*
* 入口参数: baud:设定的波特率
*
* 返回参数: 无
*
* 说明: TX-PA9 RX-PA10
************************************************************
*/
void Usart1_Init(unsigned int baud)
{
GPIO_InitTypeDef gpioInitStruct;
USART_InitTypeDef usartInitStruct;
NVIC_InitTypeDef nvicInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //打开GPIOA的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //打开USART1的时钟
//PA9 TXD
gpioInitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //设置为复用模式
gpioInitStruct.GPIO_Pin = GPIO_Pin_9; //初始化Pin9
gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz; //承载的最大频率
GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
//PA10 RXD
gpioInitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置为浮空输入
gpioInitStruct.GPIO_Pin = GPIO_Pin_10; //初始化Pin10
gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz; //承载的最大频率
GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
usartInitStruct.USART_BaudRate = baud;
usartInitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控
usartInitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //接收和发送
usartInitStruct.USART_Parity = USART_Parity_No; //无校验
usartInitStruct.USART_StopBits = USART_StopBits_1; //1位停止位
usartInitStruct.USART_WordLength = USART_WordLength_8b; //8位数据位
USART_Init(USART1, &usartInitStruct);
USART_Cmd(USART1, ENABLE); //使能串口
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能接收中断
nvicInitStruct.NVIC_IRQChannel = USART1_IRQn; //usart1中断号
nvicInitStruct.NVIC_IRQChannelCmd = ENABLE; //中断通道使能
nvicInitStruct.NVIC_IRQChannelPreemptionPriority = 0; //抢占中断优先级(值越小优先级越高)
nvicInitStruct.NVIC_IRQChannelSubPriority = 2; //子中断优先级(值越小优先级越高)
NVIC_Init(&nvicInitStruct); //初始化NVIC
}
/*
************************************************************
* 函数名称: Usart2_Init
*
* 函数功能: 串口2初始化
*
* 入口参数: baud:设定的波特率
*
* 返回参数: 无
*
* 说明: TX-PA2 RX-PA3
************************************************************
*/
void Usart2_Init(unsigned int baud)
{
GPIO_InitTypeDef gpioInitStruct;
USART_InitTypeDef usartInitStruct;
NVIC_InitTypeDef nvicInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//PA2 TXD
gpioInitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
gpioInitStruct.GPIO_Pin = GPIO_Pin_2;
gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpioInitStruct);
//PA3 RXD
gpioInitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpioInitStruct.GPIO_Pin = GPIO_Pin_3;
gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpioInitStruct);
usartInitStruct.USART_BaudRate = baud;
usartInitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控
usartInitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //接收和发送
usartInitStruct.USART_Parity = USART_Parity_No; //无校验
usartInitStruct.USART_StopBits = USART_StopBits_1; //1位停止位
usartInitStruct.USART_WordLength = USART_WordLength_8b; //8位数据位
USART_Init(USART2, &usartInitStruct);
USART_Cmd(USART2, ENABLE); //使能串口
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //使能接收中断
nvicInitStruct.NVIC_IRQChannel = USART2_IRQn;
nvicInitStruct.NVIC_IRQChannelCmd = ENABLE;
nvicInitStruct.NVIC_IRQChannelPreemptionPriority = 0;
nvicInitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&nvicInitStruct);
}
/*
************************************************************
* 函数名称: Usart3_Init
*
* 函数功能: 串口3初始化
*
* 入口参数: baud:设定的波特率
*
* 返回参数: 无
*
* 说明: TX-PB10 RX-PB11
************************************************************
*/
void Usart3_Init(unsigned int baud)
{
GPIO_InitTypeDef gpioInitStruct;
USART_InitTypeDef usartInitStruct;
NVIC_InitTypeDef nvicInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
//PB10 TXD
gpioInitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
gpioInitStruct.GPIO_Pin = GPIO_Pin_10;
gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpioInitStruct);
//PB11 RXD
gpioInitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpioInitStruct.GPIO_Pin = GPIO_Pin_11;
gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpioInitStruct);
usartInitStruct.USART_BaudRate = baud;
usartInitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控
usartInitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //接收和发送
usartInitStruct.USART_Parity = USART_Parity_No; //无校验
usartInitStruct.USART_StopBits = USART_StopBits_1; //1位停止位
usartInitStruct.USART_WordLength = USART_WordLength_8b; //8位数据位
USART_Init(USART3, &usartInitStruct);
USART_Cmd(USART3, ENABLE); //使能串口
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //使能接收中断
nvicInitStruct.NVIC_IRQChannel = USART3_IRQn;
nvicInitStruct.NVIC_IRQChannelCmd = ENABLE;
nvicInitStruct.NVIC_IRQChannelPreemptionPriority = 0;
nvicInitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&nvicInitStruct);
}
/*
************************************************************
* 函数名称: Usart4_Init
*
* 函数功能: 串口4初始化
*
* 入口参数: baud:设定的波特率
*
* 返回参数: 无
*
* 说明: TX-PC10 RX-PC11
************************************************************
*/
void Usart4_Init(unsigned int baud)
{
GPIO_InitTypeDef gpioInitStruct;
USART_InitTypeDef usartInitStruct;
NVIC_InitTypeDef nvicInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
//PC10 TXD
gpioInitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
gpioInitStruct.GPIO_Pin = GPIO_Pin_10;
gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &gpioInitStruct);
//PC11 RXD
gpioInitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpioInitStruct.GPIO_Pin = GPIO_Pin_11;
gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &gpioInitStruct);
usartInitStruct.USART_BaudRate = baud;
usartInitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控
usartInitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //接收和发送
usartInitStruct.USART_Parity = USART_Parity_No; //无校验
usartInitStruct.USART_StopBits = USART_StopBits_1; //1位停止位
usartInitStruct.USART_WordLength = USART_WordLength_8b; //8位数据位
USART_Init(UART4, &usartInitStruct);
USART_Cmd(UART4, ENABLE); //使能串口
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //使能接收中断
nvicInitStruct.NVIC_IRQChannel = UART4_IRQn;
nvicInitStruct.NVIC_IRQChannelCmd = ENABLE;
nvicInitStruct.NVIC_IRQChannelPreemptionPriority = 0;
nvicInitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&nvicInitStruct);
}
/*
************************************************************
* 函数名称: Usart_SendString
*
* 函数功能: 串口数据发送
*
* 入口参数: USARTx:串口组
* str:要发送的数据
* len:数据长度
*
* 返回参数: 无
*
* 说明:
************************************************************
*/
void Usart_SendString(USART_TypeDef *USARTx, unsigned char *str, unsigned short len)
{
unsigned short count = 0;
for(; count < len; count++)
{
USART_SendData(USARTx, *str++); //发送数据
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET); //等待发送完成
}
}
/*
************************************************************
* 函数名称:Usart_SendByte
* 函数功能: 串口数据单字节发送
*
* 入口参数: USARTx:串口组
* str:要发送的数据
*
* 返回参数: 无
*
* 说明:
************************************************************
*/
void Usart_SendByte(USART_TypeDef *USARTx, unsigned char str)
{
USART_SendData(USARTx, str); //发送数据
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET); //等待发送完成
}
/*
************************************************************
* 函数名称: UsartPrintf
*
* 函数功能: 格式化打印
*
* 入口参数: USARTx:串口组
* fmt:不定长参
*
* 返回参数: 无
*
* 说明:
************************************************************
*/
void UsartPrintf(USART_TypeDef *USARTx, char *fmt,...)
{
unsigned char UsartPrintfBuf[296];
va_list ap;
unsigned char *pStr = UsartPrintfBuf;
va_start(ap, fmt);
vsprintf((char *)UsartPrintfBuf, fmt, ap); //格式化
va_end(ap);
while(*pStr != 0)
{
USART_SendData(USARTx, *pStr++);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);
}
}
extern char usart1Buf[512];
extern int usart1Len;
/*
************************************************************
* 函数名称: USART1_IRQHandler
*
* 函数功能: 串口1收发中断
*
* 入口参数: 无
*
* 返回参数: 无
*
* 说明:
************************************************************
*/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
{
if(usart1Len >= 512) //防止数据过多,导致内存溢出
usart1Len = 0;
usart1Buf[usart1Len++] = USART1->DR;
USART_ClearFlag(USART1, USART_FLAG_RXNE);
}
}
extern char usart2Buf[512];
extern int usart2Len;
//extern char check_men;
/*
************************************************************
* 函数名称: USART2_IRQHandler
*
* 函数功能: 串口2收发中断
*
* 入口参数: 无
*
* 返回参数: 无
*
* 说明:
************************************************************
*/
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断
{
//防止数据过多,导致内存溢出
if(usart2Len >= 512){
usart2Len = 0;
}
usart2Buf[usart2Len++] = USART2->DR;
// 用于验证特定信息
// if(usart2Buf[usart2Len] == '3' || usart2Buf[usart2Len-1] == '3'){
// check_men = 1;
// }
// else if(usart2Buf[usart2Len] == '4' || usart2Buf[usart2Len-1] == '4' ){
// check_men = 2;
// }
USART_ClearFlag(USART2, USART_FLAG_RXNE);
}
}
extern char usart3Buf[1024];
extern int usart3Len;
/*
************************************************************
* 函数名称: USART1_IRQHandler
*
* 函数功能: 串口1收发中断
*
* 入口参数: 无
*
* 返回参数: 无
*
* 说明:
************************************************************
*/
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断
{
if(usart3Len >= 1024) //防止数据过多,导致内存溢出
usart3Len = 0;
usart3Buf[usart3Len++] = USART3->DR;
USART_ClearFlag(USART3, USART_FLAG_RXNE);
}
}
extern unsigned char usart4Len ; //usart1接收的数据长度
extern unsigned char usart4Buf[64]; //usart1接收缓存
/*
************************************************************
* 函数名称: USART4_IRQHandler
*
* 函数功能: 串口4收发中断
*
* 入口参数: 无
*
* 返回参数: 无
*
* 说明:
************************************************************
*/
void UART4_IRQHandler(void)
{
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) //接收中断
{
if(usart4Len >= 64) //防止数据过多,导致内存溢出
usart4Len = 0;
usart4Buf[usart4Len++] = UART4->DR;
USART_ClearFlag(UART4, USART_FLAG_RXNE);
}
}
/*
************************************************************
* 函数名称: USART_test
*
* 函数功能: 串口测试函数
*
* 入口参数: 无
*
* 返回参数: 无
*
* 说明:
************************************************************
*/
void USART_test(void)
{
//**************************UART1***************************//
if(usart1Len > 0)
{
UsartPrintf(USART1, "Usart1 Get Data: \r\n%s\r\n", usart1Buf);
memset(usart1Buf, 0, sizeof(usart1Buf));
usart1Len = 0;
}
//**************************UART2***************************//
if(usart2Len > 0)
{
UsartPrintf(USART2, "Usart2 Get Data: \r\n%s\r\n", usart2Buf);
memset(usart2Buf, 0, sizeof(usart2Buf));
usart2Len = 0;
}
//**************************UART3***************************//
if(usart3Len > 0)
{
UsartPrintf(USART3, "Usart3 Get Data: \r\n%s\r\n", usart3Buf);
memset(usart3Buf, 0, sizeof(usart3Buf));
usart3Len = 0;
}
//**************************UART4***************************//
if(usart4Len > 0)
{
UsartPrintf(UART4, "Usart4 Get Data: \r\n%s\r\n", usart4Buf);
memset(usart4Buf, 0, sizeof(usart4Buf));
usart4Len = 0;
}
}
代码来自老师