#include "sys.h" #include "usart.h" #include "string.h" #include #include "delay.h" #include "stdio.h" #include "stdlib.h" #include "stdio.h" #include "oled.h" #define Usart3ReadLen 120 //值 char Usart3ReadBuff[Usart3ReadLen]={0}; //接收数据缓存 u32 Usart3ReadCnt = 0;//串口1接收到的数据个数 u32 Usart3IdleCnt = 0;//空闲检测用 #define Usart1ReadLen 100 //值 char Usart1ReadBuff[Usart1ReadLen]={0}; //接收数据缓存 u32 Usart1ReadCnt = 0;//串口1接收到的数据个数 #define Usart2ReadLen 100 //值 char Usart2ReadBuff[Usart2ReadLen]={0}; //接收数据缓存 u32 Usart2ReadCnt = 0;//串口1接收到的数据个数 #if 1 #pragma import(__use_no_semihosting) //标准库需要的支持函数 struct __FILE { int handle; }; FILE __stdout; //定义_sys_exit()以避免使用半主机模式 _sys_exit(int x) { x = x; } //重定义fputc函数 int fputc(int ch, FILE *f) { while((USART1->SR&0X40)==0);//循环发送,直到发送完毕 USART1->DR = (u8) ch; return ch; } #endif void uart_init(u32 bound){ //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟 //USART1_TX GPIOA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9 //USART1_RX GPIOA.10初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10 //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 //USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;//串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART1, &USART_InitStructure); //初始化串口1 USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断 USART_Cmd(USART1, ENABLE); //使能串口1 } void uart3_init(u32 bound) { USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //使能USART3, RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);//设置端口部分重映射 功能 /* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; // 通道设置为串口3中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0 ; // 抢占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 中断响应优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 打开中断 NVIC_Init(&NVIC_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB10 //USART3_RX PB.11 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB11 /* 第4步:配置USART3参数 - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ 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_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART3, &USART_InitStructure); //USART_IT_IDLE USART_ITConfig(USART3, USART_IT_IDLE, ENABLE); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // 接收中断使能 USART_Cmd(USART3,ENABLE);// 使能U3 } void uart2_init(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART2,GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // USART_DeInit(USART2); //复位串口2 //USART2_TX PA.2 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA2 //USART2_RX PA.3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA3 //USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;//一般设置为9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART2, &USART_InitStructure); //初始化串口2 // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级2 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断 USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); USART_Cmd(USART2, ENABLE); //使能串口 } void USART2_SendByte(u8 data) { while((USART2->SR&0X40)==0);//循环发送,直到发送完毕 USART2->DR = (u8) data; } void USART2_String(char *pdat,int len) { TIM_Cmd(TIM4, DISABLE); //使能定时器2 while(len>0) { USART2_SendByte(*pdat++); len--; } TIM_Cmd(TIM4, ENABLE); } void USART3_SendByte(u8 data) { while((USART3->SR&0X40)==0);//循环发送,直到发送完毕 USART3->DR = (u8) data; } void USART3_String(char *pdat,int len) { TIM_Cmd(TIM4, DISABLE); //使能定时器2 while(len>0) { USART3_SendByte(*pdat++); len--; } TIM_Cmd(TIM4, ENABLE); } void USART1_SendByte(u8 data) { while((USART1->SR&0X40)==0);//循环发送,直到发送完毕 USART1->DR = (u8) data; } void USART1_String(char *pdat,int len) { TIM_Cmd(TIM4, DISABLE); //使能定时器2 while(len>0) { USART1_SendByte(*pdat++); len--; } TIM_Cmd(TIM4, ENABLE); } char String_Sub(char *pcBuf, char *pcRes,const char *pStart,const char *pEnd); void USART2_IRQHandler(void) //串口2中断服务程序 { u8 Res=0,rxes; if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断 { Res =USART_ReceiveData(USART2); //读取接收到的数据 if(Usart2ReadCnt < Usart2ReadLen-1) { Usart2ReadBuff[Usart2ReadCnt] = Res; } else { Usart2ReadCnt=0; } Usart2ReadCnt ++; //数据个数 } if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)//空闲中断 { //用读SR和DR的方法清除IDLE rxes = USART2->SR; rxes = USART2->DR; Usart2ReadCnt=0; memset(Usart2ReadBuff,0,Usart2ReadLen); } } /**************************************************** * 函数名称 :void String_Sub(char *pcBuf, char *pcRes,char *pStart,char *pEnd) * 函数功能 : 获取两个特殊字符串之间的字符串 NB-iot数据解析用 * 编写日期 :2019/7/26 * 编写人 :小浩电子科技 * 参数 ; *pcBuf 源字符串 *pcRes目标字符串 *pStart字符串1 *pEnd字符串2 * 版本记录 :V1.0 * : ****************************************************/ char String_Sub(char *pcBuf, char *pcRes,const char *pStart,const char *pEnd) { char *pcBegin = NULL; char *pcEnd = NULL; pcBegin = strstr((char*)pcBuf,pStart); pcEnd = strstr((char*)pcBegin+strlen(pStart)+1, pEnd); if(pcBegin == NULL || pcEnd == NULL || pcBegin > pcEnd) { return 0;//错误返回 } else { pcBegin += strlen(pStart); // memcpy(pcRes, pcBegin, pcEnd-pcBegin);//内存拷贝法 strncpy(pcRes, pcBegin, pcEnd-pcBegin);//开始拷贝数据 // pcRes[(pcEnd-pcBegin)+1]='\0'; } return 1; } void USART3_IRQHandler(void) //串口3中断服务程序 { u8 Res=0,rxes; if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断 { Res =USART_ReceiveData(USART3); //读取接收到的数据 if(Usart3ReadCnt < Usart3ReadLen-1) { Usart3ReadBuff[Usart3ReadCnt] = Res; } else { Usart3ReadCnt=0; } Usart3ReadCnt ++; //数据个数 Usart3IdleCnt = 0; } if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)//空闲中断 { //用读SR和DR的方法清除IDLE rxes = USART3->SR; rxes = USART3->DR; USART1_String(Usart3ReadBuff,Usart3ReadCnt); //memset(Usart3ReadBuff,0,Usart3ReadLen); Usart3ReadCnt=0; } } void USART1_IRQHandler(void) //串口1中断服务程序 { u8 Res,rxes; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾) { Res =USART_ReceiveData(USART1); //读取接收到的数据 if(Usart1ReadCnt < Usart1ReadLen-1) { Usart1ReadBuff[Usart1ReadCnt] = Res; } else { Usart1ReadCnt=0; } Usart1ReadCnt ++; //数据个数 } if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)//空闲中断 { //用读SR和DR的方法清除IDLE rxes = USART1->SR; rxes = USART1->DR; printf("%s",Usart1ReadBuff); Usart1ReadCnt=0; } }