<pre name="code" class="html"><span style="font-size:24px;color:#ff0000;"><strong>这个程序同时初始化了五个串口,前四个串口具有dma,所以均采用DMA接收和发送,并提供了格式化输出的类似printf的接口,可以接收未知长度的数据,但是收发字节数</strong></span><pre name="code" class="html">UART_RECV_BUF_MAX_LEN 和<pre name="code" class="html">UART_SEND_BUF_MAX_LEN有关
,和串口5没有用于485通信。
<strong><span style="font-size:24px;color:#ff0000;">//usrt.h</span></strong>
#ifndef _USART_H
#define _USART_H
#include "stm32f10x.h"
typedef enum
{
UART1_3520D = 0,
UART2_RS232 = 1,
UART3_GPS = 2,
UART4_GPRS = 3,
UART5_RS485 = 4
} COM_TypeDef;
#define UART_RECV_BUF_MAX_LEN 1024
#define UART_SEND_BUF_MAX_LEN 1024
__align(8) typedef struct
{
u8 RecvLen;
u8 RecvBuf[UART_RECV_BUF_MAX_LEN];
u8 SendLen;
u8 SendBuf[UART_SEND_BUF_MAX_LEN];
u8 Status; //状态位的第一个是忙状态位
//COM_TypeDef Com;
} SERIAL_TypeDef;
#define USART1_Port GPIOA
#define USART1_Tx GPIO_Pin_9
#define USART1_Rx GPIO_Pin_10
#define USART2_Port GPIOA
#define USART2_Tx GPIO_Pin_2
#define USART2_Rx GPIO_Pin_3
#define USART3_Port GPIOB
#define USART3_Tx GPIO_Pin_10
#define USART3_Rx GPIO_Pin_11
#define USART4_Port GPIOC
#define USART4_Tx GPIO_Pin_10
#define USART4_Rx GPIO_Pin_11
#define USART5_Port GPIOD
#define USART5_Tx GPIO_Pin_5
#define USART5_Rx GPIO_Pin_6
#define UART_485_TX_EN GPIOA->BSS = GPIO_Pin_1 //485 控制引脚
#define UART_485_RX_EN GPIOA->BRSS = GPIO_Pin_1
extern u8 UART_STATE_FLAG;
extern SERIAL_TypeDef serial1, serial2, serial3,serial4,serial5;
void print(char* fmt,...);
void Serial_Init(void);
void UART_Send(COM_TypeDef COM, u8 *buf, u16 len);
void UART_Read(COM_TypeDef COM, u8 *buf);
void USART_Config(COM_TypeDef COM, u32 COM_BaudRate, u8 *recvbuf, u8 *sendbuf);
#endif
<pre name="code" class="html"><strong><span style="font-size:24px;color:#ff0000;">//usrt.c</span></strong>
/***************************************************串口初始化程序2014年12月李乾坤*************************************************/#include "usart.h"#include "stdarg.h"#include "common.h"USART_TypeDef* USART[5] = {USART1, USART2, USART3, UART4, UART5}; DMA_Channel_TypeDef *DMA_Channel[8] = {DMA1_Channel4, DMA1_Channel5, DMA1_Channel7, \DMA1_Channel6, DMA1_Channel2,DMA1_Channel3, DMA2_Channel5, DMA2_Channel3};SERIAL_TypeDef serial1, serial2, serial3,serial4,serial5;SERIAL_TypeDef *serial[5] = {&serial1,&serial2,&serial3,&serial4,&serial5};void Serial_Init(void){mymemset(&serial1, 0, sizeof(SERIAL_TypeDef));mymemset(&serial2, 0, sizeof(SERIAL_TypeDef));mymemset(&serial3, 0, sizeof(SERIAL_TypeDef));mymemset(&serial4, 0, sizeof(SERIAL_TypeDef));mymemset(&serial5, 0, sizeof(SERIAL_TypeDef));USART_Config(UART1_3520D, 115200, serial1.RecvBuf, serial1.SendBuf);//与3520D通信USART_Config(UART2_RS232, 115200, serial2.RecvBuf, serial2.SendBuf);//接RS232USART_Config(UART3_GPS, 9600, serial3.RecvBuf, serial3.SendBuf);//GPSUSART_Config(UART4_GPRS, 115200, serial4.RecvBuf, serial4.SendBuf);//3G// USART_Config(UART5_RS485, 9600, serial1.RecvBuf, serial1.SendBuf);//485云台}/**************************************************************************************@Decr: 初始化串口(利用串口接受完数据会进入空闲中断,在空闲中断里对DMA重新初始化,设置下次传输的参数,\关闭DMA发送完成中断,直接判断CNDTR的值来判断上次传输是否完成@Para1:COM口@Para2:波特率@Para3:发送缓冲区@Para4: