#include "led.h" #include "delay.h" #include "sys.h" #include "usart.h" #include "wifi.h" #include "dht11.h" #include "lcd1602.h" #include "adc.h" #include "timer.h" #include "string.h" #include "ds18b20.h" #include #include #include #include "motor.h" // 用户数据的结构体 struct UserInfo{ u8 temp_value; // 温度 u8 temp_threshold; float smog; // 烟雾 float PoisonousGas; // 有毒气体 int smog_threshold; int PoisonousGas_threshold; }; struct UserInfo userInfo; /** * @description: 报警 * @param {*} * @return {*} */ void Warning(){ BEEP = 0; delay_ms(100); BEEP = 1; } int getResult(){ int result = 0; if(userInfo.temp_value > userInfo.temp_threshold) result = 1; if(userInfo.smog > userInfo.smog_threshold) result = 1; if(userInfo.PoisonousGas > userInfo.PoisonousGas_threshold) result = 1; return result; } /** * @description: 数据处理 * @param {*} * @return {*} */ void Data_Deal() { u16 adcx; userInfo.temp_value = DS18B20_Get_Temp() / 10; adcx = Get_ADC1_Value(ADC_Channel_9,20); userInfo.smog = (float)adcx*(3.3/4096); adcx = Get_ADC_Value(ADC_Channel_1,20); userInfo.PoisonousGas = (float)adcx*(3.3/4096); if(getResult()) Warning(); else BEEP = 0; } /** * @description: 解析数据 * @param {*} * @return {*} */ int getValue() { int target1 = 0, target2 = 0,i = 0; int result = 0; target1 = strstr((const char*)USART_RX_BUF, "(") - USART_RX_BUF; target2 = strstr((const char*)USART_RX_BUF, ")") - USART_RX_BUF; result = 0; for(i = target1 + 1; i < target2; i++){ result = result * 10 + (*(USART_RX_BUF + i) - 0x30); } return result; } void Receive_data(void) { // 接受到数据 if(USART_RX_STA & 0x8000){ // 模式切换 if(strstr((const char*)USART_RX_BUF, "getInfo") != NULL){ printf("temp:%d, temp_threshold:%d,smog:%0.2f, PoisonousGas%0.2f, smog_threshold:%d, PoisonousGas_threshold:%d\r\n", \ userInfo.temp_value,userInfo.temp_threshold, userInfo.smog, userInfo.PoisonousGas, userInfo.smog_threshold, userInfo.PoisonousGas_threshold); } if(strstr((const char*)USART_RX_BUF, "setTemp") != NULL){ userInfo.temp_threshold = getValue(); } if(strstr((const char*)USART_RX_BUF, "setSmog") != NULL){ userInfo.smog_threshold = getValue(); } if(strstr((const char*)USART_RX_BUF, "setPoisonousGas") != NULL){ userInfo.PoisonousGas_threshold = getValue(); } memset(USART_RX_BUF, 0, sizeof(USART_RX_BUF)); USART_RX_STA = 0x00; } } void Sys_Init(void) { delay_init(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); uart_init(115200); LED_Init(); DS18B20_Init(); ADCx_Init(); userInfo.temp_threshold = 28; userInfo.smog_threshold = 28; userInfo.PoisonousGas_threshold = 28; } int main(void) { Sys_Init(); BEEP = 0; delay_ms(1000); BEEP = 1; printf("start"); while(1) { Data_Deal(); Receive_data(); } }