2019 电子设计大赛 激光炮题目 写的程序
主要是基于 FIFO 数据结构,实现的带缓冲的串口程序,当然有的微处理带有硬件的串口 FIFO,这里是软件定义的 FIFO
usart_all.h
:
#ifndef __USART_ALL_H
#define __USART_ALL_H
#include "sys.h"
#define myUSART1 1
#define myUSART2 1
#define myUSART3 1
#define myUART4 1
#define myUART5 0
#define myUSART6 0
#if(myUSART1)
#define USART1_TXBUF_SIZE 128
#define USART1_RXBUF_SIZE 128
void USART1_Init(u32 bound);
char USART1_GetChar(u8* byte);
char USART1_GetLastChar(u8* byte);
char USART1_GetStr(u8* string);
char USART1_GetDatas(u8* datas, u16 length);
char USART1_PutChar(u8 byte);
char USART1_PutStr(u8* string);
char USART1_PutDatas(u8* datas, u16 length);
void USART1_ClearRXBuf(void);
void USART1_Disable(void);
#endif
#if(myUSART2)
#define USART2_TXBUF_SIZE 128
#define USART2_RXBUF_SIZE 128
void USART2_Init(u32 bound);
char USART2_GetChar(u8* byte);
char USART2_PutChar(u8 byte);
void USART2_ClearRXBuf(void);
#endif
#if(myUSART3)
extern u16 USART3_RX_Len;
#define USART3_TXBUF_SIZE 128
#define USART3_RXBUF_SIZE 128
void USART3_Init(u32 bound);
char USART3_GetChar(u8* byte);
char USART3_GetDatas(u8* datas, u16 length);
char USART3_PutChar(u8 byte);
char USART3_PutStr(u8* string);
void USART3_ClearRXBuf(void);
#endif
#if(myUART4)
extern u16 UART4_RX_Len;
#define UART4_TXBUF_SIZE 128
#define UART4_RXBUF_SIZE 128
void UART4_Init(u32 bound);
char UART4_GetChar(u8* byte);
char UART4_GetDatas(u8* datas, u16 length);
char UART4_PutChar(u8 byte);
void UART4_ClearRXBuf(void);
#endif
#endif
usart_all.c
:
#include "usart_all.h"
#if(myUSART1)
u8 USART1_TX_BUFF[USART1_TXBUF_SIZE]; //发送FIFO缓冲区数组
u16 USART1_TX_Len = 0; //发送FIFO缓冲区内待发送的字符长度
u16 USART1_TX_RIndex = 0; //发送FIFO缓冲区内的读指针
u16 USART1_TX_WIndex = 0; //发送FIFO缓冲区内的写指针
u8 USART1_RX_BUFF[USART1_RXBUF_SIZE]; //接收FIFO缓冲区数组
u16 USART1_RX_Len = 0; //接收FIFO缓冲区内待接收的字符长度
u16 USART1_RX_RIndex = 0; //接收FIFO缓冲区内的读指针
u16 USART1_RX_WIndex = 0; //接收FIFO缓冲区内的写指针
u8 USART1_TX_READY = 1;
//初始化串口1
//USART1_TX:A9 USART1_RX:A10
//bound:串口波特率
void USART1_Init(u32 bound) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//使能时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能USART1的时钟
//串口1对应引脚复用映射
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);//GPIOA9复用为USART1
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);//GPIOA10复用为USART1
//串口端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);
//串口1初始化设置
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_Init(USART1, &USART_InitStructure);
//串口1使能
USART_Cmd(USART1, ENABLE);
//开启相关中断
USART_ITConfig(USART1, USART_IT_TC, ENABLE);//开启发送完成中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启接收数据中断
//串口1 NVIC配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
}
//串口1中断服务程序
void USART1_IRQHandler(void) {
if(USART_GetITStatus(USART1, USART_IT_TC) == SET) { //发送完成中断
USART_ClearITPendingBit(USART1, USART_IT_TC);
if(USART1_TX_Len > 0) { //发送中断前发送缓冲区有待发数据,在中断里继续发下一个数据
USART_SendData(USART1, USART1_TX_BUFF[USART1_TX_RIndex]);
USART1_TX_Len--; //发送缓冲区待发数据减一
USART1_TX_RIndex++; //发送缓冲区读指针加一
if(USART1_TX_RIndex >= USART1_TXBUF_SIZE) {
USART1_TX_RIndex = 0; //发送缓冲区读指针复位
}
return;
}
if(USART1_TX_Len == 0) {
USART1_TX_READY = 1; //发送缓冲区清空且发送空闲,下一次发送不用进缓存区,直接发送
}
}
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) { //接收中断
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
if(USART1_RX_Len < USART1_RXBUF_SIZE) { //接收缓冲区未满
USART1_RX_BUFF[USART1_RX_WIndex] = USART_ReceiveData(USART1);
USART1_RX_Len++; //接收缓冲区待读数据加一
USART1_RX_WIndex++; //接收写指针指向下一个单元
if(USART1_RX_WIndex >= USART1_RXBUF_SIZE) {
USART1_RX_WIndex = 0; //接收写指针复位
}
} else { //接收缓冲区已满,丢弃数据
return;
}
}
}
//串口1读单个数据函数
char USART1_GetChar(u8* byte) {
if(USART1_RX_Len <= 0) { //接收缓冲区没有待读数据
return 0;
} else {
INTX_DISABLE(); //关闭所有中断
*byte = USART1_RX_BUFF[USART1_RX_RIndex];
USART1_RX_Len--;
USART1_RX_RIndex++;
if(USART1_RX_RIndex >= USART1_RXBUF_SIZE) {
USART1_RX_RIndex = 0;
}
INTX_ENABLE();//开启总中断
return 1;
}
}
//串口1读 最近一个数据 函数
char USART1_GetLastChar(u8* byte) {
u16 i;
if(USART1_RX_Len <= 0) { //接收缓冲区没有待读数据
return 0;
} else {
INTX_DISABLE(); //关闭所有中断
//虚读USART1_RX_Len-1个数据
for(i = 0; i < USART1_RX_Len-1; i++) {
USART1_RX_RIndex++;
USART1_RX_Len--;
if(USART1_RX_RIndex >= USART1_RXBUF_SIZE) {
USART1_RX_RIndex = 0;
}
}
*byte = USART1_RX_BUFF[USART1_RX_RIndex];
USART1_RX_RIndex++;
USART1_RX_Len--;
if(USART1_RX_RIndex >= USART1_RXBUF_SIZE) {
USART1_RX_RIndex = 0;
}
INTX_ENABLE();//开启总中断
return 1;
}
}
//串口1读字符串函数
char USART1_GetStr(u8* string) {
do {
if(!USART1_GetChar(string)) { //读取失败
return 0;
}
string++;
} while(*(string - 1));
return 1;
}
//串口1读多个数据函数
char USART1_GetDatas(u8* datas, u16 length) {
if(length > USART1_RX_Len) { //缓冲区数据不足,读取失败
return 0;
}
while(length--) {
if(!USART1_GetChar(datas)) { //读取失败
return 0;
}
datas++;
}
return 1;
}
//串口1发单个数据函数
char USART1_PutChar(u8 byte){
if(USART1_TX_READY) { //串口发送空闲,直接发送
USART_SendData(USART1, byte);
USART1_TX_READY = 0; //串口忙碌
return 1;
} else { //串口发送不空闲,将数据加入到发送缓冲区
if(USART1_TX_Len >= USART1_TXBUF_SIZE) { //发送缓冲区已满,丢弃数据,返回错误信息
return 0;
} else {
INTX_DISABLE(); //关闭所有中断
USART1_TX_BUFF[USART1_TX_WIndex] = byte;
USART1_TX_WIndex ++;
if(USART1_TX_WIndex >= USART1_TXBUF_SIZE) {
USART1_TX_WIndex = 0;
}
USART1_TX_Len++;
INTX_ENABLE();//开启总中断
return 1;
}
}
}
//串口1发字符串函数
char USART1_PutStr(u8* string) {
while(*string) {
USART1_PutChar(*string);
string++;
}
return 1;
}
//串口1发多个数据函数
char USART1_PutDatas(u8* datas, u16 length) {
if(length > USART1_TXBUF_SIZE - USART1_TX_Len) { //缓冲区不足,有可能发送失败
return 0;
}
while(length--) {
if(!USART1_PutChar(*datas)) {
return 0;
}
datas++;
}
return 1;
}
//串口1清空接收缓冲区函数
void USART1_ClearRXBuf(void) {
USART1_RX_Len = 0; //接收FIFO缓冲区内待接收的字符长度
USART1_RX_RIndex = 0; //接收FIFO缓冲区内的读指针
USART1_RX_WIndex = 0; //接收FIFO缓冲区内的写指针
}
void USART1_Disable(void) {
USART_Cmd(USART1, DISABLE);
}
#endif
#if(myUSART2)
u8 USART2_TX_BUFF[USART2_TXBUF_SIZE]; //发送FIFO缓冲区数组
u16 USART2_TX_Len = 0; //发送FIFO缓冲区内待发送的字符长度
u16 USART2_TX_RIndex = 0; //发送FIFO缓冲区内的读指针
u16 USART2_TX_WIndex = 0; //发送FIFO缓冲区内的写指针
u8 USART2_RX_BUFF[USART2_RXBUF_SIZE]; //接收FIFO缓冲区数组
u16 USART2_RX_Len = 0; //接收FIFO缓冲区内待接收的字符长度
u16 USART2_RX_RIndex = 0; //接收FIFO缓冲区内的读指针
u16 USART2_RX_WIndex = 0; //接收FIFO缓冲区内的写指针
u8 USART2_TX_READY = 1;
//初始化串口2
//USART2_TX:A2 USART2_RX:A3
//bound:串口波特率
void USART2_Init(u32 bound) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//使能时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//使能USART2的时钟
//串口2对应引脚复用映射
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
//USART2端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART2初始化设置
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_Init(USART2, &USART_InitStructure);
//串口2使能
USART_Cmd(USART2, ENABLE);
//开启相关中断
USART_ITConfig(USART2, USART_IT_TC, ENABLE);//开启发送完成中断
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启接收数据中断
//串口2 NVIC配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
//串口2中断服务程序
void USART2_IRQHandler(void) {
if(USART_GetITStatus(USART2, USART_IT_TC) == SET) { //发送完成中断
USART_ClearITPendingBit(USART2, USART_IT_TC);
if(USART2_TX_Len > 0) { //发送中断前发送缓冲区有待发数据,在中断里继续发下一个数据
USART_SendData(USART2, USART2_TX_BUFF[USART2_TX_RIndex]);
USART2_TX_Len --; //发送缓冲区待发数据减一
USART2_TX_RIndex++; //发送缓冲区读指针加一
if(USART2_TX_RIndex >= USART2_TXBUF_SIZE) {
USART2_TX_RIndex = 0; //发送缓冲区读指针复位
}
return;
}
if(USART2_TX_Len == 0) {
USART2_TX_READY = 1; //发送缓冲区清空且发送空闲,下一次发送不用进缓存区,直接发送
}
}
if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET) { //接收中断
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
if(USART2_RX_Len < USART2_RXBUF_SIZE) { //接收缓冲区未满
USART2_RX_BUFF[USART2_RX_WIndex] = USART_ReceiveData(USART2);
USART2_RX_Len++; //接收缓冲区待读数据加一
USART2_RX_WIndex++; //接收写指针指向下一个单元
if(USART2_RX_WIndex >= USART2_RXBUF_SIZE) {
USART2_RX_WIndex = 0; //接收写指针复位
}
} else { //接收缓冲区已满,丢弃数据
return;
}
}
}
//串口2读单个数据函数
char USART2_GetChar(u8* byte) {
if(USART2_RX_Len <= 0) { //接收缓冲区没有待读数据
return 0;
} else {
INTX_DISABLE(); //关闭所有中断
*byte = USART2_RX_BUFF[USART2_RX_RIndex];
USART2_RX_Len--;
USART2_RX_RIndex++;
if(USART2_RX_RIndex >= USART2_RXBUF_SIZE) {
USART2_RX_RIndex = 0;
}
INTX_ENABLE();//开启总中断
return 1;
}
}
//串口2读多个数据函数
//串口2发单个数据函数
char USART2_PutChar(u8 byte){
if(USART2_TX_READY) { //串口发送空闲,直接发送
USART_SendData(USART2, byte);
USART2_TX_READY = 0; //串口忙碌
return 1;
} else { //串口发送不空闲,将数据加入到发送缓冲区
if(USART2_TX_Len >= USART2_TXBUF_SIZE) { //发送缓冲区已满,丢弃数据,返回错误信息
return 0;
} else {
INTX_DISABLE(); //关闭所有中断
USART2_TX_BUFF[USART2_TX_WIndex] = byte;
USART2_TX_WIndex ++;
if(USART2_TX_WIndex >= USART2_TXBUF_SIZE) {
USART2_TX_WIndex = 0;
}
USART2_TX_Len++;
INTX_ENABLE();//开启总中断
return 1;
}
}
}
//串口2发多个数据函数
//串口2清空接收缓冲区函数
void USART2_ClearRXBuf(void) {
USART2_RX_Len = 0; //接收FIFO缓冲区内待接收的字符长度
USART2_RX_RIndex = 0; //接收FIFO缓冲区内的读指针
USART2_RX_WIndex = 0; //接收FIFO缓冲区内的写指针
}
#endif
#if(myUSART3)
u8 USART3_TX_BUFF[USART3_TXBUF_SIZE]; //发送FIFO缓冲区数组
u16 USART3_TX_Len = 0; //发送FIFO缓冲区内待发送的字符长度
u16 USART3_TX_RIndex = 0; //发送FIFO缓冲区内的读指针
u16 USART3_TX_WIndex = 0; //发送FIFO缓冲区内的写指针
u8 USART3_RX_BUFF[USART3_RXBUF_SIZE]; //接收FIFO缓冲区数组
u16 USART3_RX_Len = 0; //接收FIFO缓冲区内待接收的字符长度
u16 USART3_RX_RIndex = 0; //接收FIFO缓冲区内的读指针
u16 USART3_RX_WIndex = 0; //接收FIFO缓冲区内的写指针
u8 USART3_TX_READY = 1;
//初始化串口3
//USART3_TX:A2 USART3_RX:A3
//bound:串口波特率
void USART3_Init(u32 bound) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//使能时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOB的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//使能USART3的时钟
//串口3对应引脚复用映射
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
//USART3端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3初始化设置
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_Init(USART3, &USART_InitStructure);
//串口3使能
USART_Cmd(USART3, ENABLE);
//开启相关中断
USART_ITConfig(USART3, USART_IT_TC, ENABLE);//开启发送完成中断
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启接收数据中断
//串口3 NVIC配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
//串口3中断服务程序
void USART3_IRQHandler(void) {
if(USART_GetITStatus(USART3, USART_IT_TC) == SET) { //发送完成中断
USART_ClearITPendingBit(USART3, USART_IT_TC);
if(USART3_TX_Len > 0) { //发送中断前发送缓冲区有待发数据,在中断里继续发下一个数据
USART_SendData(USART3, USART3_TX_BUFF[USART3_TX_RIndex]);
USART3_TX_Len --; //发送缓冲区待发数据减一
USART3_TX_RIndex++; //发送缓冲区读指针加一
if(USART3_TX_RIndex >= USART3_TXBUF_SIZE) {
USART3_TX_RIndex = 0; //发送缓冲区读指针复位
}
return;
}
if(USART3_TX_Len == 0) {
USART3_TX_READY = 1; //发送缓冲区清空且发送空闲,下一次发送不用进缓存区,直接发送
}
}
if(USART_GetITStatus(USART3, USART_IT_RXNE) == SET) { //接收中断
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
if(USART3_RX_Len < USART3_RXBUF_SIZE) { //接收缓冲区未满
USART3_RX_BUFF[USART3_RX_WIndex] = USART_ReceiveData(USART3);
USART3_RX_Len++; //接收缓冲区待读数据加一
USART3_RX_WIndex++; //接收写指针指向下一个单元
if(USART3_RX_WIndex >= USART3_RXBUF_SIZE) {
USART3_RX_WIndex = 0; //接收写指针复位
}
} else { //接收缓冲区已满,丢弃数据
return;
}
}
}
//串口3读单个数据函数
char USART3_GetChar(u8* byte) {
if(USART3_RX_Len <= 0) { //接收缓冲区没有待读数据
return 0;
} else {
INTX_DISABLE(); //关闭所有中断
*byte = USART3_RX_BUFF[USART3_RX_RIndex];
USART3_RX_Len--;
USART3_RX_RIndex++;
if(USART3_RX_RIndex >= USART3_RXBUF_SIZE) {
USART3_RX_RIndex = 0;
}
INTX_ENABLE();//开启总中断
return 1;
}
}
//串口3读多个数据函数
char USART3_GetDatas(u8* datas, u16 length) {
if(length > USART3_RX_Len) { //缓冲区数据不足,读取失败
return 0;
}
while(length--) {
if(!USART3_GetChar(datas)) { //读取失败
return 0;
}
datas++;
}
return 1;
}
//串口3发单个数据函数
char USART3_PutChar(u8 byte){
if(USART3_TX_READY) { //串口发送空闲,直接发送
USART_SendData(USART3, byte);
USART3_TX_READY = 0; //串口忙碌
return 1;
} else { //串口发送不空闲,将数据加入到发送缓冲区
if(USART3_TX_Len >= USART3_TXBUF_SIZE) { //发送缓冲区已满,丢弃数据,返回错误信息
return 0;
} else {
INTX_DISABLE(); //关闭所有中断
USART3_TX_BUFF[USART3_TX_WIndex] = byte;
USART3_TX_WIndex ++;
if(USART3_TX_WIndex >= USART3_TXBUF_SIZE) {
USART3_TX_WIndex = 0;
}
USART3_TX_Len++;
INTX_ENABLE();//开启总中断
return 1;
}
}
}
//串口3发多个数据函数
//串口3发字符串函数
char USART3_PutStr(u8* string) {
while(*string) {
USART3_PutChar(*string);
string++;
}
return 1;
}
//串口3清空接收缓冲区函数
void USART3_ClearRXBuf(void) {
USART3_RX_Len = 0; //接收FIFO缓冲区内待接收的字符长度
USART3_RX_RIndex = 0; //接收FIFO缓冲区内的读指针
USART3_RX_WIndex = 0; //接收FIFO缓冲区内的写指针
}
#endif
#if(myUART4)
u8 UART4_TX_BUFF[UART4_TXBUF_SIZE]; //发送FIFO缓冲区数组
u16 UART4_TX_Len = 0; //发送FIFO缓冲区内待发送的字符长度
u16 UART4_TX_RIndex = 0; //发送FIFO缓冲区内的读指针
u16 UART4_TX_WIndex = 0; //发送FIFO缓冲区内的写指针
u8 UART4_RX_BUFF[UART4_RXBUF_SIZE]; //接收FIFO缓冲区数组
u16 UART4_RX_Len = 0; //接收FIFO缓冲区内待接收的字符长度
u16 UART4_RX_RIndex = 0; //接收FIFO缓冲区内的读指针
u16 UART4_RX_WIndex = 0; //接收FIFO缓冲区内的写指针
u8 UART4_TX_READY = 1;
//初始化串口4
//UART4_TX:A2 UART4_RX:A3
//bound:串口波特率
void UART4_Init(u32 bound) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//使能时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);//使能GPIOC的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);//使能UART4的时钟
//串口4对应引脚复用映射
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);
//UART4端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOC, &GPIO_InitStructure);
//UART4初始化设置
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_Init(UART4, &USART_InitStructure);
//串口4使能
USART_Cmd(UART4, ENABLE);
//开启相关中断
USART_ITConfig(UART4, USART_IT_TC, ENABLE);//开启发送完成中断
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//开启接收数据中断
//串口4 NVIC配置
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
//串口4中断服务程序
void UART4_IRQHandler(void) {
if(USART_GetITStatus(UART4, USART_IT_TC) == SET) { //发送完成中断
USART_ClearITPendingBit(UART4, USART_IT_TC);
if(UART4_TX_Len > 0) { //发送中断前发送缓冲区有待发数据,在中断里继续发下一个数据
USART_SendData(UART4, UART4_TX_BUFF[UART4_TX_RIndex]);
UART4_TX_Len --; //发送缓冲区待发数据减一
UART4_TX_RIndex++; //发送缓冲区读指针加一
if(UART4_TX_RIndex >= UART4_TXBUF_SIZE) {
UART4_TX_RIndex = 0; //发送缓冲区读指针复位
}
return;
}
if(UART4_TX_Len == 0) {
UART4_TX_READY = 1; //发送缓冲区清空且发送空闲,下一次发送不用进缓存区,直接发送
}
}
if(USART_GetITStatus(UART4, USART_IT_RXNE) == SET) { //接收中断
USART_ClearITPendingBit(UART4, USART_IT_RXNE);
if(UART4_RX_Len < UART4_RXBUF_SIZE) { //接收缓冲区未满
UART4_RX_BUFF[UART4_RX_WIndex] = USART_ReceiveData(UART4);
UART4_RX_Len++; //接收缓冲区待读数据加一
UART4_RX_WIndex++; //接收写指针指向下一个单元
if(UART4_RX_WIndex >= UART4_RXBUF_SIZE) {
UART4_RX_WIndex = 0; //接收写指针复位
}
} else { //接收缓冲区已满,丢弃数据
return;
}
}
}
//串口4读单个数据函数
char UART4_GetChar(u8* byte) {
if(UART4_RX_Len <= 0) { //接收缓冲区没有待读数据
return 0;
} else {
INTX_DISABLE(); //关闭所有中断
*byte = UART4_RX_BUFF[UART4_RX_RIndex];
UART4_RX_Len--;
UART4_RX_RIndex++;
if(UART4_RX_RIndex >= UART4_RXBUF_SIZE) {
UART4_RX_RIndex = 0;
}
INTX_ENABLE();//开启总中断
return 1;
}
}
//串口4读多个数据函数
char UART4_GetDatas(u8* datas, u16 length) {
if(length > UART4_RX_Len) { //缓冲区数据不足,读取失败
return 0;
}
while(length--) {
if(!UART4_GetChar(datas)) { //读取失败
return 0;
}
datas++;
}
return 1;
}
//串口4发单个数据函数
char UART4_PutChar(u8 byte){
if(UART4_TX_READY) { //串口发送空闲,直接发送
USART_SendData(UART4, byte);
UART4_TX_READY = 0; //串口忙碌
return 1;
} else { //串口发送不空闲,将数据加入到发送缓冲区
if(UART4_TX_Len >= UART4_TXBUF_SIZE) { //发送缓冲区已满,丢弃数据,返回错误信息
return 0;
} else {
INTX_DISABLE(); //关闭所有中断
UART4_TX_BUFF[UART4_TX_WIndex] = byte;
UART4_TX_WIndex ++;
if(UART4_TX_WIndex >= UART4_TXBUF_SIZE) {
UART4_TX_WIndex = 0;
}
UART4_TX_Len++;
INTX_ENABLE();//开启总中断
return 1;
}
}
}
//串口4发多个数据函数
//串口4清空接收缓冲区函数
void UART4_ClearRXBuf(void) {
UART4_RX_Len = 0; //接收FIFO缓冲区内待接收的字符长度
UART4_RX_RIndex = 0; //接收FIFO缓冲区内的读指针
UART4_RX_WIndex = 0; //接收FIFO缓冲区内的写指针
}
#endif