系统供电3.3V,USART2串口2中断接收初始化(PB4,PB5),115200波特率,采用中断接收,主动发送,接收完成的处理函数为4G指令解析,不用在意,移植时候可以根据项目需求,替换成自己的应用代码即可。同样,前面是头文件代码,后面是应用代码,仅做记录,方便以后移植应用,代码已实测。
#ifndef __USART2_H
#define __USART2_H
#ifdef __cplusplus
extern "C" {
#endif
#include "main.h"
#define USART2_RX_LEN 1000*8 //接受数据缓存区长度
#define USART2_TX_LEN 1024 //接受数据缓存区长度
#define USART2_RX_TIMEOUT 320 //接受数据缓存区长度
struct __USART2_ //串口数据结构体
{
char rxBuf[USART2_RX_LEN]; //接收缓存区
// char txBuf[USART2_TX_LEN]; //发送缓存区
u16 rxLen; //串口1接收到的数据长度
u8 notRx;
u16 txLen; //串口1接收到的数据长度
u8 rxOkFlag; //RX 数据接收完成标志
u8 txOkFlag; //tx数据发送ready flag
u32 rxTimeOut;
u8 debug;
};
extern struct __USART2_ usart2; //串口数据结构体
void usart2Init(u32 baudRate); //usart initial
void usart2Deal(void); //usart data deal
#ifdef __cplusplus
}
#endif
#endif
#include "usart2.h"
struct __USART2_ usart2; //串口数据结构体
void usart2DmaInit(u32 baudRate ) //usart initial
{
}
void usart2IntInit(u32 baudRate) //usart initial,interupt rx mode
{
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE); //Enable GPIO clock
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USART2, ENABLE); // Enable USART Clock
NVIC_InitType NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitType GPIO_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure); //初始化结构体
/* Configure USART2 Tx as alternate function push-pull */
GPIO_InitStructure.Pin = GPIO_PIN_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_USART2;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
/* Configure USART2 Rx as alternate function push-pull and pull-up */
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF6_USART2;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
USART_InitType USART_InitStructure;
USART_StructInit(&USART_InitStructure);
USART_InitStructure.BaudRate = baudRate;
USART_InitStructure.WordLength = USART_WL_8B;
USART_InitStructure.StopBits = USART_STPB_1;
USART_InitStructure.Parity = USART_PE_NO;
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
USART_Init(USART2, &USART_InitStructure);
USART_ConfigInt(USART2, USART_INT_RXDNE, ENABLE);
// USART_ConfigInt(USART2, USART_INT_TXDE, ENABLE);
USART_Enable(USART2, ENABLE);
}
void usart2Init(u32 baudRate )
{
#ifdef USART2_DMA
usart2DmaInit(baudRate); //usart initial,DMA rx mode
#else
usart2IntInit(baudRate); //usart initial,interupt rx mode
// printf("-------------------11111-----------------------------------");
#endif
}
void usart2RxBuf(u8 rxData)
{
if(usart2.notRx != TRUE)
{
if(usart2.rxLen<USART2_RX_LEN-10)
{
usart2.rxBuf[usart2.rxLen++] = rxData;
usart2.rxTimeOut = 0;
}
else
{
usart2.rxLen = 0;
}
}
}
#ifdef USART2_DMA
#else
void USART2_IRQHandler(void)
{
u8 rec = 0;
if (USART_GetIntStatus(USART2, USART_INT_RXDNE) != RESET)
{
rec = USART_ReceiveData(USART2);
usart2RxBuf(rec);
}
if (USART_GetIntStatus(USART2, USART_INT_TXDE) != RESET)
{
}
if(USART_GetIntStatus(USART2, USART_INT_OREF) != RESET)
{
/*Read the STS register first,and the read the DAT
register to clear the overflow interrupt*/
(void)USART2->STS;
(void)USART2->DAT;
}
}
#endif
#ifdef USART2_DMA
#else
void usart2Deal(void) //This code just for testing.
{
// char buf[8192]; //1024*8 = 8192
if((usart2.rxLen>1)&&(usart2.rxTimeOut>USART2_RX_TIMEOUT))
{
usart2.rxTimeOut = 0;
// memcpy(ec800.rxBuf,usart2.rxBuf,usart2.rxLen); //copy the data to buf
// ec800.rxLen = usart2.rxLen; //
ec800.rxOkFlag = OK;
if(usart2.rxLen>1024)
{
usart2.notRx = TRUE;//禁止接受新数据。
}
// memcpy(buf,usart2.rxBuf,usart2.rxLen); //copy the data to buf
ec800AtCmdParser(usart2.rxBuf ,usart2.rxLen); //EC800数据解析。
// usartSendBuf(USART1,(u8*)usart2.rxBuf,usart2.rxLen); //
memset(usart2.rxBuf,0,usart2.rxLen); //清零串口数据缓存
usart2.rxLen = 0;
usart2.notRx = FALSE;//允许串口缓存放入新数据。
}
}
#endif