#include "led.h" #include "delay.h" #include "sys.h" #include "usart.h" #include "wifi.h" #include "lcd1602.h" #include "adc.h" #include "timer.h" #include "string.h" #include "ds18b20.h" #include "Lcd_Driver.h" #include #include #include #include "DS1302.h" #include "oled.h" #include "key.h" #include "usart2.h" struct UserInfo userInfo; struct timing nowTime; int change = 0; // 函数声明 void Sys_Init(void); void DisplayTask(void); void TaskProcessing(void); void DataProcessing(void); void Init() { userInfo.water_temp_up = 30; userInfo.target_water_level = 1; userInfo.water_temp_down = 10; userInfo.timer = 1; userInfo.timerFlag = 1; userInfo.HotFlag = 1; userInfo.CoolFlag = 0; } /** * @description: main函数 * @param {*} * @return {*} */ int main(void) { char data[5] = {0xAA, 0x13, 0x01, 0x01, 0xD4}; Init(); Sys_Init(); BEEP = 0; delay_ms(1000); BEEP = 1; printf("start\r\n"); uart2_send(data, sizeof(data)); while(1) { TaskProcessing(); DataProcessing(); DisplayTask(); } } /** * @description: 系统初始化 * @param {*} * @return {*} */ void Sys_Init(void) { delay_init(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); uart_init(115200); uart2_Init(9600); TIM3_Int_Init(4999,7199); LED_Init(); DS18B20_Init(); Adc_Init(); OLED_Init(); Key4x4_GPIO_Config(); OLED_Clear(); } /** * @description: 显示 * @param {*} * @return {*} */ void DisplayTask(void) { char str[80]; if(!userInfo.setModel){ if(userInfo.control_mode == 0) sprintf(str,"%0.2f, %0.2fcm, %d %d Manual ", userInfo.water_temp, \ userInfo.water_level, (int)userInfo.water_quality, userInfo.target_water_level); else sprintf(str,"%0.2f, %0.2fcm, %d %d AUTO ", userInfo.water_temp, \ userInfo.water_level, (int)userInfo.water_quality, userInfo.target_water_level); OLED_ShowString(0, 0, str, 16); sprintf(str,"U:%d,D:%d, %d, %d ", userInfo.water_temp_up, userInfo.water_temp_down, userInfo.timer,userInfo.timerFlag); OLED_ShowString(0, 4, str, 16); }else{ sprintf(str,"U:%d,D:%d %d %d %d ",userInfo.water_temp_up, userInfo.water_temp_down, userInfo.timer, userInfo.target_water_level, change); OLED_ShowString(0, 0, str, 16); } }; /** * @description: 报警 * @param {*} * @return {*} */ void Warning(){ BEEP = 0; delay_ms(50); BEEP = 1; } /** * @description: 改变值 * @param {int} Value * @return {*} */ void ChangeValue(int Value){ if(userInfo.setModel != 1) return; switch(change){ case 0: if(userInfo.water_temp_up < 100) userInfo.water_temp_up = userInfo.water_temp_up * 10 + Value; break; case 1: if(userInfo.water_temp_down < 100) userInfo.water_temp_down = userInfo.water_temp_down * 10 + Value; break; case 2: if(userInfo.timer < 1000) userInfo.timer = userInfo.timer * 10 + Value; break; case 3: userInfo.target_water_level = Value; if(userInfo.target_water_level > 3) userInfo.target_water_level = 3; else if(userInfo.target_water_level <= 0) userInfo.target_water_level = 1; break; } } /** * @description: 删除键 * * @param {*} * @return {*} */ void DeleteValue() { if(userInfo.setModel != 1) return; switch(change){ case 0: userInfo.water_temp_up /= 10; break; case 1: userInfo.water_temp_down /= 10; break; case 2: userInfo.timer /= 10; break; case 3: userInfo.target_water_level /= 10; break; } } /** * @description: 按键处理 * @param {*} * @return {*} */ void keyHandling(){ int key = Key_Scan(); char data[6] = {0xAA, 0x07, 0x20, 0x00, 0x08, 0xbb}; switch (key) { case 12: if(userInfo.setModel == 0) userInfo.setModel = 1; else userInfo.setModel = 0; change = 0; break; case 13: change++; if(change >= 4) change = 0; break; case 14: userInfo.control_mode++; if(userInfo.control_mode >= 2) userInfo.control_mode = 0; break; case 15: if(userInfo.timerFlag == 0) userInfo.timerFlag = 1; else userInfo.timerFlag = 0; break; case 11: DeleteValue(); break; default: if(!userInfo.setModel){ if(key == 1){ if(userInfo.HotFlag == 0) userInfo.HotFlag =1; else userInfo.HotFlag = 0; }else if(key == 2){ if(userInfo.CoolFlag == 0) userInfo.CoolFlag =1; else userInfo.CoolFlag = 0; }else if(key == 3){ if(userInfo.cupLidFlag == 0) userInfo.cupLidFlag =1; else userInfo.cupLidFlag = 0; } } if(!userInfo.setModel || key == 16) break; if(key < 10) ChangeValue(key); break; } if(key != 16) { Warning(); OLED_Clear(); } if(key == 10) uart2_send(data, sizeof(data)); } /** * @description: 解析数据 * @param {*} * @return {*} */ int getValue(void) { int target1 = 0, target2 = 0,i = 0; int result = 0; target1 = (char *)strstr((const char *)USART_RX_BUF, "(") - (char *)USART_RX_BUF; target2 = (char *)strstr((const char *)USART_RX_BUF, ")") - (char *)USART_RX_BUF; result = 0; for(i = target1 + 1; i < target2; i++){ result = result * 10 + (*(USART_RX_BUF + i) - 0x30); } return result; } /** * @description: 接收数据 * @param {*} * @return {*} */ void DataProcessing(void) { // 接受到数据 if(USART_RX_STA&0x8000){ if(strstr((const char*)USART_RX_BUF, "getInfo") != NULL){ printf("Water_temp:%0.2f, TDS:%d, water_level:%0.2fcm, target_level:%d, Hot:%d, Cool:%d", userInfo.water_temp, (int)userInfo.water_quality, userInfo.water_level, \ userInfo.target_water_level, (int)userInfo.HotFlag, userInfo.CoolFlag); } // 模式切换 if(strstr((const char*)USART_RX_BUF, "Model") != NULL){ userInfo.control_mode++; if(userInfo.control_mode >= 2) userInfo.control_mode = 0; if(userInfo.control_mode == 0) printf("Manual\r\n"); else printf("AUTO\r\n"); } if(strstr((const char*)USART_RX_BUF, "Hot") != NULL){ if(userInfo.HotFlag == 0) userInfo.HotFlag =1; else userInfo.HotFlag = 0; if(userInfo.HotFlag == 0) printf("Close Hot!\r\n"); else printf("Open Hot!\r\n"); } if(strstr((const char*)USART_RX_BUF, "Cool") != NULL){ if(userInfo.CoolFlag == 0) userInfo.CoolFlag =1; else userInfo.CoolFlag = 0; if(userInfo.CoolFlag == 0) printf("Close Cool!\r\n"); else printf("Open Cool!\r\n"); } if(strstr((const char*)USART_RX_BUF, "Up") != NULL){ userInfo.water_temp_up = getValue(); printf("setUp OK!\r\n"); } if(strstr((const char*)USART_RX_BUF, "Down") != NULL){ userInfo.water_temp_down = getValue(); printf("setDown OK!\r\n"); } if(strstr((const char*)USART_RX_BUF, "cupLid") != NULL){ if(userInfo.cupLidFlag == 0) userInfo.cupLidFlag = 1; else userInfo.cupLidFlag = 0; if(userInfo.cupLidFlag == 0) printf("Close cupLid\r\n"); else printf("Open cupLid\r\n"); } if(strstr((const char*)USART_RX_BUF, "Timer") != NULL){ userInfo.timer = getValue(); printf("setTimer OK!\r\n"); } if(strstr((const char*)USART_RX_BUF, "timerFlag") != NULL){ if(userInfo.timerFlag == 0) userInfo.timerFlag = 1; else userInfo.timerFlag = 0; if(userInfo.timerFlag == 0) printf("Close Timer\r\n"); else printf("Open Timer\r\n"); } memset(USART_RX_BUF, 0, sizeof(USART_RX_BUF)); USART_RX_STA = 0x00; } } /** * @description: 任务处理 * @param {*} * @return {*} */ void TaskProcessing() { static int send_flag = 0; float TDS=0.0,TDS_voltage; float compensationCoefficient=1.0;//温度校准系数 float compensationVolatge; float kValue=1.0; float TDS_value=0.0,voltage_value; static int attr1 = 0; int water_up = 0, water_down = 0; int flag = 0, temp; char data[4] = {0xAA, 0x02, 0x00, 0xAC}; u16 num; userInfo.water_temp = DS18B20_Get_Temp() / 10; num = Get_Adc_Average(ADC_Channel_0,10); userInfo.water_level = (float)num * (4.0 / 4096); num = Get_Adc_Average(ADC_Channel_1,10); TDS_voltage = (float)num * (3.3/4096); compensationCoefficient=1.0+0.02*((userInfo.water_temp/10)-25.0); compensationVolatge=TDS_voltage/compensationCoefficient; TDS_value=(133.42*compensationVolatge*compensationVolatge*compensationVolatge - \ 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5*kValue; if((TDS_value<=0)){TDS_value=0;} if((TDS_value >1400)){TDS_value=1400;} userInfo.water_quality = TDS_value; keyHandling(); if(userInfo.timerFlag){ if(userInfo.timer == 0){ uart2_send(data, sizeof(data)); LED0 = 1, LED1 = 1, LED2 = 1; Warning(); delay_ms(100); LED0 = 0, LED1 = 0, LED2 = 0; userInfo.timerFlag = 0; } } LED0 = 0; LED1 = 0; LED2 = 0; if(userInfo.water_temp < userInfo.water_temp_down) LED1 = 1; if(userInfo.water_temp > userInfo.water_temp_up) LED0 = 1; if(userInfo.water_temp >= userInfo.water_temp_down && userInfo.water_temp <= userInfo.water_temp_up) LED2 = 1; if(userInfo.target_water_level == 1) water_up = 2, water_down = 1; else if (userInfo.target_water_level == 2) water_up = 3, water_down = 2; else if (userInfo.target_water_level == 3) water_up = 4, water_down = 3; if(userInfo.water_level < water_down || userInfo.water_level > water_up){ if(attr1 < 20) { Warning(); attr1++; } if(userInfo.water_level < water_down){ waterPump = 1; }else waterPump = 0; } if(userInfo.water_level >= water_down && userInfo.water_level <= water_up) { attr1 = 0; } if(userInfo.water_level >= water_down) flag = 1; // 手动模式 if(userInfo.control_mode == 0){ if(flag){ HOT = userInfo.HotFlag; COOL = userInfo.CoolFlag; cupLid = userInfo.cupLidFlag; }else HOT = 0, COOL = 0; }else{ if(userInfo.water_temp > userInfo.water_temp_up){ if(flag) COOL = 1; else COOL = 0; } else COOL = 0; if(userInfo.water_temp < userInfo.water_temp_down){ if(flag) HOT = 1; else HOT = 0; } else HOT = 0; } temp = userInfo.water_temp > userInfo.water_temp_up ? userInfo.water_temp - userInfo.water_temp_up : 0; if(temp > 5 && userInfo.water_level < water_down){ if(send_flag == 0) printf("temp:%0.2f, Automatic shutdown Heating failure, please manually shut down", userInfo.water_temp); send_flag = 1; }else{ send_flag = 0; } }