You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

390 lines
8.0 KiB

#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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 num_th1;
int num_th2;
int num_th3;
int warn;
};
struct UserInfo userInfo;
struct timing nowTime;
u8 Settime[6]={0x22,0x05,0x18,0x18,0x51,0x00}; // 设置时间 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;
userInfo.num_th1 = AT24CXX_ReadOneByte(6);
userInfo.num_th2 = AT24CXX_ReadOneByte(7);
userInfo.num_th3 = AT24CXX_ReadOneByte(8);
}
/**
* @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 %2d,%2d,%2d num:%2d ", nowTime.mon, nowTime.day, nowTime.hour, nowTime.min, \
nowTime.sec, userInfo.light, userInfo.light_threshold, userInfo.warn, userInfo.num_th1,userInfo.num_th2,userInfo.num_th3 , userInfo.num);
else sprintf(str,"%3dLx %2d:%2d:%2d %2d,%2d,%2d %d ", userInfo.light_threshold,userInfo.SetTimer.hour, userInfo.SetTimer.min, \
userInfo.SetTimer.sec, userInfo.num_th1,userInfo.num_th2,userInfo.num_th3, 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 > 1000) userInfo.light_threshold = 1000;
}
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;
case 4:
if(userInfo.num_th1 < 10){
userInfo.num_th1 = userInfo.num_th1 * 10 + Value;
if(userInfo.num_th1 > 99) userInfo.num_th1 = 99;
}
break;
case 5:
if(userInfo.num_th2 < 10){
userInfo.num_th2 = userInfo.num_th2 * 10 + Value;
if(userInfo.num_th2 > 99) userInfo.num_th2 = 99;
}
break;
case 6:
if(userInfo.num_th3 < 10){
userInfo.num_th3 = userInfo.num_th3 * 10 + Value;
if(userInfo.num_th3 > 99) userInfo.num_th3 = 99;
}
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;
case 4:
userInfo.num_th1 /= 10;
break;
case 5:
userInfo.num_th2 /= 10;
break;
case 6:
userInfo.num_th3 /= 10;
break;
}
}
/**
* @description: 按键处理
* @param {*}
* @return {*}
*/
void keyHandling(){
static int flag = 0;
int key = 16;
int i = 0;
key = Key_Scan();
switch (key)
{
case 12:
userInfo.model++;
if(userInfo.model >= 2) userInfo.model = 0;
change = 0;
break;
case 13:
if(userInfo.model == 0) break;
change++;
if(change >= 7) 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();
}
if(IN == 0){
delay_ms(10);
if(IN == 0){
if(flag == 3) flag = 4;
else if(flag == 0) flag = 1;
else flag = 0;
}
while((i<50)&&(IN==0)){
delay_ms(10);
i++;
}
i=0;
}
if(OUT == 0){
delay_ms(10);
if(OUT == 0){
if(flag == 1) flag = 2;
else if(flag == 0) flag = 3;
else flag = 0;
}
while((i<50)&&(OUT==0)){
delay_ms(10);
i++;
}
i=0;
}
if(flag == 2){
userInfo.num++;
if(userInfo.num >= 99) userInfo.num = 99;
flag = 0;
}else if(flag == 4){
userInfo.num--;
if(userInfo.num < 0) userInfo.num = 0;
flag = 0;
}
}
/**
* @description: 任务处理
* @param {*}
* @return {*}
*/
void TaskProcessing()
{
static int flag = 0,min = 0, sec = 0;
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();
// 超过时间报警
if(getTimerResult()){
if(flag == 0) {
min = userInfo.SetTimer.min;
sec = userInfo.SetTimer.sec + 3;
if(sec >= 60){
sec = sec - 60;
min += 1;
if(min >= 60) min = min - 60;
}
flag = 1;
}else{
if(min == nowTime.min && sec == nowTime.sec){
if(flag == 1){
userInfo.warn = 0;
min = nowTime.min + 5;
sec = nowTime.sec;
if(min >= 60) min = min - 60;
flag = 2;
}else if(flag == 2){
flag = 3;
}
}
}
if(userInfo.warn) Warning();
}else{
flag = 0;
}
LED0 = 0;
LED1 = 0;
LED2 = 0;
if(userInfo.num >= userInfo.num_th1) LED0 = 1;
if(userInfo.num >= userInfo.num_th2) LED1 = 1;
if(userInfo.num >= userInfo.num_th3) LED2 = 1;
if(userInfo.light > userInfo.light_threshold) LED0 = 0,LED1 = 0,LED2 = 0;
if(flag == 3) LED0 = 0,LED1 = 0,LED2 = 0;
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);
AT24CXX_WriteOneByte(6, userInfo.num_th1);
AT24CXX_WriteOneByte(7, userInfo.num_th2);
AT24CXX_WriteOneByte(8, userInfo.num_th3);
}
/**
* @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;
}