/* ***************************************************************** 头文件 ***************************************************************** */ #include "usart4.h" /* ************************************************************************************ 变量定义 ************************************************************************************ */ char temp_data[100]; //串口接收缓存区 u8 USART4_TX_BUF[USART4_MAX_SEND_LEN]; //发送缓冲 /* ********************************************************************************************************* * 函 数 名: USART4_Configuration() * 功能说明: 串口4配置 * 输 入 :波特率baund * 输 出 :无 ********************************************************************************************************* */ void USART4_Configuration(u32 baund) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); //使能USART4 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); //GPIOc时钟 USART_DeInit(UART4); //复位串口1 //USART1_TX PA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOC, &GPIO_InitStructure); //USART1_RX PA.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4); GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4); USART_InitStructure.USART_BaudRate = baund; 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(UART4,&USART_InitStructure); USART_Cmd(UART4,ENABLE); //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;//串口1中断通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=5;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、 USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);//开启USART1的接收中断和空闲中断 USART_ClearFlag(UART4,USART_FLAG_TC); //清除发送完成标志位 USART_ITConfig(UART4,USART_IT_IDLE,ENABLE);//开启USART1的接收中断和空闲中断 } /* ********************************************************************************************************* * 函 数 名: Usart4_ReceiveBuff_Clear() * 功能说明: 串口4清空缓冲区 * 输 入 :无 * 输 出 :无 ********************************************************************************************************* */ void Usart4_ReceiveBuff_Clear(void) { ESP8266_Fram_Record_Struct .InfBit .FramFinishFlag=0; ESP8266_Fram_Record_Struct .InfBit .FramLength=0; memset(ESP8266_Fram_Record_Struct.Data_RX_BUF,0,RX_BUF_MAX_LEN); //memset(USART4_TX_BUF,0,USART4_MAX_SEND_LEN); } /* ********************************************************************************************************* * 函 数 名: UART4_IRQHandler() * 功能说明: 串口4中断函数 * 输 入 :无 * 输 出 :无 ********************************************************************************************************* */ void UART4_IRQHandler(void) { u8 ucCh; if(USART_GetITStatus( UART4, USART_IT_RXNE ) != RESET ) { ucCh = USART_ReceiveData( UART4 ); if(ESP8266_Fram_Record_Struct .InfBit .FramLength < ( RX_BUF_MAX_LEN - 1 ) ) { //留最后一位做结束位 ESP8266_Fram_Record_Struct .Data_RX_BUF[ ESP8266_Fram_Record_Struct .InfBit .FramLength ++ ] = ucCh; } else { ESP8266_Fram_Record_Struct .InfBit .FramLength = 0; ESP8266_Fram_Record_Struct .InfBit .FramFinishFlag = 1; } } if(USART_GetITStatus( UART4, USART_FLAG_ORE ) != RESET ) { USART_ReceiveData(UART4); USART_ClearFlag(UART4, USART_FLAG_ORE); } if( USART_GetITStatus( UART4, USART_IT_IDLE ) != RESET ) //如果总线空闲 { ESP8266_Fram_Record_Struct .InfBit .FramFinishFlag = 1; ucCh = USART_ReceiveData( UART4 ); //由软件序列清除中断标志位(先读USART_SR,然后读USART_DR) } } /* ********************************************************************************************************* * 函 数 名: u4_printf() * 功能说明: 串口4输出 * 输 入 : * 输 出 : ********************************************************************************************************* */ void u4_printf(char* fmt,...) { u16 i,j; va_list ap; va_start(ap,fmt); vsprintf((char*)USART4_TX_BUF,fmt,ap); va_end(ap); i=strlen((const char*)USART4_TX_BUF); //此次发送数据的长度 for(j=0;j12| ((temp_data[6]-'0')*10+(temp_data[7]-'0')*1)>31| ((temp_data[8]-'0')*10+(temp_data[9]-'0')*1)>23| ((temp_data[10]-'0')*10+(temp_data[11]-'0')*1)>59| ((temp_data[12]-'0')*10+(temp_data[13]-'0')*1)>59) { ESP8266_SendString(DISABLE,"parw-4,fail,*",13,0); }else { SetRTC( ((temp_data[2]-'0')*10+(temp_data[3]-'0')*1), ((temp_data[4]-'0')*10+(temp_data[5]-'0')*1), ((temp_data[6]-'0')*10+(temp_data[7]-'0')*1), ((temp_data[8]-'0')*10+(temp_data[9]-'0')*1), ((temp_data[10]-'0')*10+(temp_data[11]-'0')*1), ((temp_data[12]-'0')*10+(temp_data[13]-'0')*1) ); DS3231_Set( ((temp_data[2]-'0')*10+(temp_data[3]-'0')*1), ((temp_data[4]-'0')*10+(temp_data[5]-'0')*1), ((temp_data[6]-'0')*10+(temp_data[7]-'0')*1), ((temp_data[8]-'0')*10+(temp_data[9]-'0')*1), ((temp_data[10]-'0')*10+(temp_data[11]-'0')*1), ((temp_data[12]-'0')*10+(temp_data[13]-'0')*1) ); EEPROM_WriteBytes(170,(u8*)temp_data,2); if((RTC->CR)&0x100)//已经开启了远传闹钟 { RTC_AlarmAConfig(get_pubidv()); } if((RTC->CR)&0x200)//已经开启间隔闹钟 { RtcWakeUpConfig(get_meaidv() - 1); } ESP8266_SendString(DISABLE,"parw-4,OK,*",11,0); Init_Data_Equipment.par_change=1; EEPROM_WriteBytes(185, 0,1); } } /*写入******传感器地址修改*********/ if((recString=strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parw-5")) !=NULL) //parw-aAb,0A1 { recString=strstr(recString, ","); recString++; recStringNext=recString+2; Sdi_chanegadd(recString[0],recStringNext[0]); adda=Sdi_Readone(recStringNext[0]); if(adda==0) { ESP8266_SendString(DISABLE,"parw-5,OK,*",11,0); //数值相同 }else ESP8266_SendString(DISABLE,"parw-5,fail,*",13,0); } /*读取******设备ID、测量间隔、预约时间、测量模式、远传时间*********/ if((recString=strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parr-1")) !=NULL) { //&&(strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "\r\n"))!=NULL) memset(temp_data,0,100); Init_Data_Equipment=Read_Equipment_Par(); sprintf(temp_data,"parr-1,%s,%d,%s,%s,%d,*", Init_Data_Equipment.Equipment_ID, hexToDec(Init_Data_Equipment.Measure_Interval), (Init_Data_Equipment.appointment_time), (Init_Data_Equipment.measure_mode), hexToDec(Init_Data_Equipment.Pub_Time) ); ESP8266_SendString(DISABLE,temp_data,strlen(temp_data),0); } /*读取******远传IP、远传端口、wifi名称、wifi密码、wifi端口*********/ if(strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parr-2")) { memset(temp_data,0,100); GetRTC(hhmmss,yymmdd); sprintf(temp_data,"%s,%s,%s,%s,%s,%s,*", "parr-2", Init_Data_Equipment.Bc25_Ip, Init_Data_Equipment.Bc25_Port, Init_Data_Equipment.ESP8266_AP_SSIDs, Init_Data_Equipment.ESP8266_AP_PWDs, Init_Data_Equipment.ESP8266_AP_Ports ); ESP8266_SendString(DISABLE,temp_data,strlen(temp_data),0); } /*读取******系统时间 日期*********/ if(strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parr-4")) { memset(temp_data,0,100); GetRTC(hhmmss,yymmdd); EEPROM_ReadBytes(170, (u8*)year_head,2); sprintf(temp_data,"%s,%s%s,%s,*","parr-4",year_head,yymmdd,hhmmss); ESP8266_SendString(DISABLE,temp_data,strlen(temp_data),0); } /*读取******传感器地址查询*********/ if((recString=strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parr-s?")) !=NULL) { memset(temp_data,0,100); adda=Sdi_readaddress(); if(adda!=0x7f) { sprintf(temp_data,"Address:%c,*",adda); ESP8266_SendString(DISABLE,temp_data,strlen(temp_data),0); } else ESP8266_SendString(DISABLE,"Address,fail,*",14,0); } Usart4_ReceiveBuff_Clear(); } return 0; } /***************************** END OF FILE *********************************/