#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 "motor.h" #include "24cxx.h" #include "bh1750.h" #include "wdg.h" // 用户数据的结构体 struct UserInfo{ int model; // 模块 0 - 正常模块 1 - 设置模式 struct timing SetTimer; // 设定时间 int num; float light; int light_threshold; int warn; }; struct UserInfo userInfo; struct timing nowTime; u8 Settime[6]={0x22,0x04,0x27,0x19,0x17,0x20}; // 设置时间 22年 03月 27日 22:34:00 int setModel = 0; int change = 0; // 函数声明 u8 getTimerResult(void); void Sys_Init(void); void DisplayTask(void); void TaskProcessing(void); void UserInfo_Init() { userInfo.SetTimer.hour = AT24CXX_ReadOneByte(0); userInfo.SetTimer.min = AT24CXX_ReadOneByte(1); userInfo.SetTimer.sec = AT24CXX_ReadOneByte(2); userInfo.model = 0; userInfo.light_threshold = AT24CXX_ReadOneByte(3) * 255 + AT24CXX_ReadOneByte(4); userInfo.num = AT24CXX_ReadOneByte(5); userInfo.warn = 1; } /** * @description: main函数 * @param {*} * @return {*} */ int main(void) { Sys_Init(); BEEP = 1; delay_ms(1000); BEEP = 0; UserInfo_Init(); while(AT24CXX_Check()){ delay_ms(200); } IWDG_Init(4,1875); //与分频数为64,重载值为625,溢出时间为1s while(1) { TaskProcessing(); DS1302_Readtime(); DisplayTask(); IWDG_Feed();//如果WK_UP按下,则喂狗 } } /** * @description: 系统初始化 * @param {*} * @return {*} */ void Sys_Init(void) { delay_init(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); uart_init(115200); LED_Init(); DS18B20_Init(); AT24CXX_Init(); //IIC初始化 Init_BH1750(); PWM_Init(899,0); OLED_Init(); Key4x4_GPIO_Config(); DS1302_init(Settime); DS1302_SetTime(Settime); OLED_Clear(); bh_data_send(BHPowOn); } /** * @description: 显示 * @param {*} * @return {*} */ void DisplayTask(void) { char str[60]; if(userInfo.model == 0) sprintf(str,"%2d-%2d %2d:%2d:%2d %0.1fLx,%dLx %d num:%d", nowTime.mon, nowTime.day, nowTime.hour, nowTime.min, \ nowTime.sec, userInfo.light, userInfo.light_threshold, userInfo.warn, userInfo.num); else sprintf(str,"%3dLx %2d:%2d:%2d %d", userInfo.light_threshold,userInfo.SetTimer.hour, userInfo.SetTimer.min, \ userInfo.SetTimer.sec, change); OLED_ShowString(0, 0, str, 16); }; /** * @description: 报警 * @param {*} * @return {*} */ void Warning(){ BEEP = 1; delay_ms(50); BEEP = 0; } /** * @description: 改变值 * @param {int} Value * @return {*} */ void ChangeValue(int Value){ switch(change){ case 0: if(userInfo.light_threshold < 1000){ userInfo.light_threshold = userInfo.light_threshold * 10 + Value; if(userInfo.light_threshold > 390) userInfo.light_threshold = 390; } break; case 1: if(userInfo.SetTimer.hour < 10){ userInfo.SetTimer.hour = userInfo.SetTimer.hour * 10 + Value; if(userInfo.SetTimer.hour > 23) userInfo.SetTimer.hour = 23; } break; case 2: if(userInfo.SetTimer.min < 10){ userInfo.SetTimer.min = userInfo.SetTimer.min * 10 + Value; if(userInfo.SetTimer.min > 59) userInfo.SetTimer.min = 59; } break; case 3: if(userInfo.SetTimer.sec < 10){ userInfo.SetTimer.sec = userInfo.SetTimer.sec * 10 + Value; if(userInfo.SetTimer.sec > 59) userInfo.SetTimer.sec = 59; } break; } } /** * @description: 删除键 * * @param {*} * @return {*} */ void DeleteValue() { if(userInfo.model != 1) return; switch(change){ case 0: userInfo.light_threshold /= 10; break; case 1: userInfo.SetTimer.hour /= 10; break; case 2: userInfo.SetTimer.min /= 10; break; case 3: userInfo.SetTimer.sec /= 10; break; } } /** * @description: 按键处理 * @param {*} * @return {*} */ void keyHandling(){ int key = 16; key = Key_Scan(); switch (key) { case 12: userInfo.model++; if(userInfo.model >= 2) userInfo.model = 0; break; case 13: if(userInfo.model == 0) break; change++; if(change >= 4) change = 0; break; case 14: if(userInfo.warn == 0) userInfo.warn = 1; else userInfo.warn = 0; break; case 11: DeleteValue(); break; default: if(!userInfo.model || key == 16) break; if(key < 10) ChangeValue(key); break; } if(key != 16){ OLED_Clear(); Warning(); } key = KEY_Scan(0); if(key == IN_PRES){ userInfo.num++; if(userInfo.num >= 99) userInfo.num = 99; } else if(key == OUT_PRES){ userInfo.num--; if(userInfo.num < 0) userInfo.num = 0; } } /** * @description: 任务处理 * @param {*} * @return {*} */ void TaskProcessing() { int x=0, pwm; //bh1750模块获取到的数据 bh_data_send(BHReset); bh_data_send(BHModeH1); delay_ms(180); x = bh_data_read(); userInfo.light = x/1.2; keyHandling(); PWMA = userInfo.light_threshold * 1.3; // 超过时间报警 if(getTimerResult()){ if(userInfo.warn) Warning(); } AT24CXX_WriteOneByte(0, userInfo.SetTimer.hour); AT24CXX_WriteOneByte(1, userInfo.SetTimer.min); AT24CXX_WriteOneByte(2, userInfo.SetTimer.sec); AT24CXX_WriteOneByte(3, userInfo.light_threshold >> 8); AT24CXX_WriteOneByte(4, userInfo.light_threshold & 0x00FF); AT24CXX_WriteOneByte(5, userInfo.num); } /** * @description: 判断时间是否到达 * @param {*} * @return {*} */ u8 getTimerResult(void) { u8 result = 1; if(nowTime.hour > userInfo.SetTimer.hour) return result; else if(userInfo.SetTimer.hour == nowTime.hour && nowTime.min > userInfo.SetTimer.min) return result; else if(userInfo.SetTimer.hour == nowTime.hour && userInfo.SetTimer.min == nowTime.min && nowTime.sec > userInfo.SetTimer.sec) return result; result = 0; return result; }