功能:
0.本项目采用STC89C52作为单片机系统的控制MCU
1.LCD1602液晶实时显示温度阈值、当前温度和定时时间
2.支持按键和红外遥控设置温度阈值和定时时间
3.通过传感器检测,判定当前值是否超过设定的阈值,然后对相关继电器进行控制
4.支持声光报警
5.支持WIFI上报温度、温度上下限和继电器状态,支持WIFI设置温度阈值和控制继电器
命令:
*HTO#:打开加热控制继电器
*HTC#:关闭加热控制继电器
*WTO#:打开加水控制继电器
*WTC#:关闭加水控制继电器
*TH+#:温度上限值+1
*TH-#:温度上限值-1
*TL+#:温度下限值+1
*TL-#:温度下限值-1
6.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB :
主程序:
//程序头函数
#include <reg52.h>
#include "infrared.h"
#include "delay.h"
#include "18b20.h"
#include "esp8266.h"
#include "lcd1602.h"
//宏定义
#define OFF 1
#define ON 0
//管脚声明
sbit RELAY_HEATING = P2^1; //加热控制继电器
sbit RELAY_WATER_ADDING = P2^0; //进水控制继电器
sbit BUZZER = P2^2; //蜂鸣器
sbit KEY_SET = P3^3; //设置键
sbit KEY_ADD = P3^4; //加键
sbit KEY_SUB = P3^6; //减键
sbit KEY_ENTER = P3^5; //确认键
sbit WATER_UPPER_LIMIT_FLAG = P1^1; //水位上限传感器
sbit WATER_LOWER_LIMIT_FLAG = P1^2; //水位下限传感器
sbit LED_WATER_LOWER = P1^3; //水位下限指示灯
//全局变量
xdata unsigned char dis0[16]; //显示缓存
int tempDataBuf = 0; //温度缓存值
xdata float tempData = 0; //温度值
bit switchFlag = 0; //开关标志
bit alarmFlag; //报警标志
bit buzzerFlag; //蜂鸣器标志
bit sendFlag = 1; //发送标志
bit lackOfWaterFlag; //缺水标志
bit waterFlag1 = 0; //自动检测控制加水标志
bit waterFlag2 = 0; //WIFI控制加水标志
unsigned char tempLowerLimit = 29; //温度下限
unsigned char tempUpperLimit = 35; //温度上限
unsigned int setTime = 0; //倒计时
unsigned char setLocation = 0; //设定位置
bit irFlag = 0; //红外接收标志,收到一帧正确数据后置1
unsigned char irCode[4];
unsigned int timeCnt = 0; //计数
//函数声明
void CheckTemperature(void); //读取温度
void DispNormal(void); //正常显示
void DispSet(void); //设置显示
void SendData(void); //发送数据
void KeyProcess(void); //按键处理
void Alarm(void); //提醒
void InitTimer0(void); //初始化定时器0
void main()
{
IR_INPUT = 1;
LCD_Init(); //LCD初始化
Start18B20(); //启动18B20
CheckTemperature(); //检测温度
LCD_DispStr(4, 0, "Welcome!"); //显示
InitTimer0(); //初始化定时器0
IR_Init(); //初始化红外配置
DelayMs(250);
DelayMs(250);
ESP8266_Init(); //初始化WIFI模块
Start18B20(); //启动18B20
CheckTemperature(); //检测温度
DispNormal();//显示
while (1)
{
Start18B20(); //启动18B20
CheckTemperature(); //检测温度
if (sendFlag == 1) //WIFI上报数据
{
sendFlag = 0;
SendData();
}
if (setLocation == 0) //非设置状态
{
DispNormal();//正常显示
}
if ((WATER_LOWER_LIMIT_FLAG == 0) && (WATER_UPPER_LIMIT_FLAG == 0)) //低于下限
{
lackOfWaterFlag = 1; //缺水标志
waterFlag1 = 1; //加水
RELAY_HEATING = OFF; //关闭加热继电器
LED_WATER_LOWER = ON; //缺水指示灯
BUZZER = OFF; //蜂鸣器关闭
}
else if ((WATER_UPPER_LIMIT_FLAG == 1) && (WATER_LOWER_LIMIT_FLAG == 1)) //高于上限
{
lackOfWaterFlag = 0;
waterFlag1 = 0;
waterFlag2 = 0; //WIFI控制加水标志关
LED_WATER_LOWER = OFF; //关闭指示灯
if (alarmFlag == 0)
alarmFlag = 1;
BUZZER = OFF;
}
else if ((WATER_UPPER_LIMIT_FLAG == 1) && (WATER_LOWER_LIMIT_FLAG == 0)) //错误
{
waterFlag1 = 0;
waterFlag2 = 0; //WIFI控制加水标志关
RELAY_HEATING = OFF; //关闭加热继电器
LED_WATER_LOWER = OFF; //关闭指示灯
BUZZER = ON; //蜂鸣器响
lackOfWaterFlag = 1; //缺水标志
}
else
{
LED_WATER_LOWER = OFF;//关闭指示灯
lackOfWaterFlag = 0;
BUZZER = OFF;
}
if (waterFlag1 || waterFlag2)
{
RELAY_WATER_ADDING = ON; //打开加水继电器
}
else
{
RELAY_WATER_ADDING = OFF; //关闭加水继电器
}
if (switchFlag == 1) //加热开关打开
{
if (tempData >= tempUpperLimit) //高于温度上限
{
RELAY_HEATING = OFF; //关闭加热继电器
switchFlag = 0;
if (alarmFlag == 0)
alarmFlag = 1;
}
else if ((tempData < tempLowerLimit) && (lackOfWaterFlag == 0)) //低于温度下限且有水
{
RELAY_HEATING = ON; //打开加热继电器
if (alarmFlag == 0)
alarmFlag = 1;
}
else
{
alarmFlag = 0;
buzzerFlag = 0;
}
}
else
{
RELAY_HEATING = OFF; //关闭加热继电器
alarmFlag = 0;
buzzerFlag = 0;
}
Alarm(); //提醒处理
KeyProcess(); //按键处理
}
}
/************************* 温度检测 *************************/
void CheckTemperature(void)
{
bit ack;
ack = Get18B20Temp(&tempDataBuf);
DelayMs(20);
if (ack)
{
tempData = (float)tempDataBuf * 0.0625; //实际温度转换
}
if (tempData < 0)
{
tempData = 0;
}
if (tempData > 99)
{
tempData = 99;
}
}
/************************* 正常显示函数 *************************/
void DispNormal(void)
{
sprintf(dis0, "Tp:%4.1f", tempData); //显示温度
LCD_DispStr(0, 0, dis0);
LCD_DispOneChar(7, 0, 0xdf);
LCD_DispOneChar(8, 0, 'C');
sprintf(dis0, " Ti:%03d", (int)setTime); //显示定时
LCD_DispStr(9, 0, dis0);
sprintf(dis0, "TH:%02d TL:%02d ", (int)tempUpperLimit, (int)tempLowerLimit); //显示温度上下限
LCD_DispStr(0, 1, dis0);
//显示水位
LCD_DispOneChar(12, 1, 'H');
if (WATER_UPPER_LIMIT_FLAG == 0)
{
LCD_DispOneChar(13, 1, ' ');
}
else
{
LCD_DispOneChar(13, 1, '*');
}
LCD_DispOneChar(14, 1, 'L');
if (WATER_LOWER_LIMIT_FLAG == 0)
{
LCD_DispOneChar(15, 1, ' ');
}
else
{
LCD_DispOneChar(15, 1, '*');
}
}
/************************* 设置显示函数 *************************/
void DispSet(void)
{
LCD_DispStr(0, 0, "Set state");
sprintf(dis0, " Ti:%03d", (int)setTime);
LCD_DispStr(9, 0, dis0);
sprintf(dis0, "TH:%02d TL:%02d ", (int)tempUpperLimit, (int)tempLowerLimit);
LCD_DispStr(0, 1, dis0);
LCD_DispOneChar(12, 1, 'H');
if (WATER_UPPER_LIMIT_FLAG == 0)
{
LCD_DispOneChar(13, 1, ' ');
}
else
{
LCD_DispOneChar(13, 1, '*');
}
LCD_DispOneChar(14, 1, 'L');
if (WATER_LOWER_LIMIT_FLAG == 0)
{
LCD_DispOneChar(15, 1, ' ');
}
else
{
LCD_DispOneChar(15, 1, '*');
}
switch (setLocation)
{
case 1: LCD_SetCursor(15, 0, 1); break;
case 2: LCD_SetCursor(4, 1, 1); break;
case 3: LCD_SetCursor(10, 1, 1); break;
default:;
}
}
/************************* WIFI上报信息 *************************/
void SendData(void)
{
unsigned char dat[16];
UART_SendStr("AT+CIPSEND=0,38\r\n", 17); //发送AT命令
DelayMs(50);
sprintf(dat, "Tp:%4.1f'C ", tempData);
UART_SendStr(dat, 10); //发送内容
DelayMs(50);
sprintf(dat, "TH:%02d TL:%02d\r\n", (int)tempUpperLimit, (int)tempLowerLimit);
UART_SendStr(dat, 13); //发送内容
//发送继电器控制状态
if (RELAY_WATER_ADDING == OFF)
{
UART_SendStr("WATER:C ", 8);
}
else
{
UART_SendStr("WATER:O ", 8);
}
if (RELAY_HEATING == OFF)
{
UART_SendStr("HEAT:C\r\n", 8);
}
else
{
UART_SendStr("HEAT:O\r\n", 8);
}
}
/************************* 按键检测 *************************/
void KeyProcess(void)
{
if (KEY_SET == 0) //设置键按下
{
DelayMs(20); //延时去抖
if (KEY_SET == 0) //再次确认设置键按下
{
setLocation++; //设定位置切换
if (setLocation == 4)
{
setLocation = 1;
}
DispSet(); //设置界面显示
}
while (KEY_SET == 0);
}
if (KEY_ADD == 0 && setLocation != 0) //加键按下
{
DelayMs(180);
if (KEY_ADD == 0 && setLocation != 0)
{
switch (setLocation)
{
case 1:
{
if (setTime < 999)
{
setTime++;
}
break;
}
case 2:
{
if (tempUpperLimit < 99 - 1)
{
tempUpperLimit++;
}
break;
}
case 3:
{
if (tempLowerLimit < tempUpperLimit - 1)
{
tempLowerLimit++;
}
break;
}
}
DispSet();
}
}
if (KEY_SUB == 0 && setLocation != 0) //减键按下
{
DelayMs(180);
if (KEY_SUB == 0 && setLocation != 0)
{
switch (setLocation)
{
case 1:
{
if (setTime > 0)
{
setTime--;
}
break;
}
case 2:
{
if (tempUpperLimit > tempLowerLimit + 1)
{
tempUpperLimit--;
}
break;
}
case 3:
{
if (tempLowerLimit > 0)
{
tempLowerLimit--;
}
break;
}
}
DispSet();
}
}
if (KEY_ENTER == 0) //确认键按下
{
DelayMs(20);
if (KEY_ENTER == 0)
{
LCD_WriteCommand(0x0C, 0); //关闭光标闪烁
DispNormal();
if (setTime > 0)
{
timeCnt = 0;
switchFlag = 0;
RELAY_HEATING = OFF;
TR0 = 1;
}
else
{
switchFlag = ~switchFlag; //手动启停
}
setLocation = 0;
}
while (KEY_ENTER == 0);
}
//红外按键处理
if (irFlag == 1)
{
if (irCode[2] == IRCodeMap[8][0]) //遥控设置键
{
setLocation++;
if (setLocation == 4)
{
setLocation = 1;
}
DispSet();
}
else if (irCode[2] == IRCodeMap[7][0]) //+键
{
if (setLocation != 0)
{
switch (setLocation)
{
case 1:
{
if (setTime < 999)
{
setTime++;
}
break;
}
case 2:
{
if (tempUpperLimit < 99 - 1)
{
tempUpperLimit++;
}
break;
}
case 3:
{
if (tempLowerLimit < tempUpperLimit - 1)
{
tempLowerLimit++;
}
break;
}
}
}
DispSet();
}
else if (irCode[2] == IRCodeMap[6][0]) //-键
{
if (setLocation != 0)
{
switch (setLocation)
{
case 1:
{
if (setTime > 0)
{
setTime--;
}
break;
}
case 2:
{
if (tempUpperLimit > tempLowerLimit + 1)
{
tempUpperLimit--;
}
break;
}
case 3:
{
if (tempLowerLimit > 0)
{
tempLowerLimit--;
}
break;
}
}
}
DispSet();
}
else if (irCode[2] == IRCodeMap[5][0]) //确定键
{
LCD_WriteCommand(0x0C, 0); //关闭光标闪烁
DispNormal();
if (setTime > 0)
{
timeCnt = 0;
switchFlag = 0;
RELAY_HEATING = OFF;
TR0 = 1;
}
else
{
switchFlag = ~switchFlag; //手动启停
}
setLocation = 0;
}
irFlag = 0;
}
}
/************************* 提醒函数 *************************/
void Alarm(void)
{
if (alarmFlag == 1 && buzzerFlag == 0)
{
BUZZER = ON;
DelayMs(250);
DelayMs(250);
BUZZER = OFF;
buzzerFlag = 1;
}
}
/************************* 初始化定时器0 *************************/
void InitTimer0(void)
{
TMOD &= 0xF0;
TMOD |= 0x01; //设置工作方式 16位自动重载
TL0 = 0x00; //设置定时初始值
TH0 = 0x4C; //设置定时初始值 50ms
ET0 = 1; //打开允许开关
TR0 = 1; //t0开始计时
EA = 1; //打开中断总开关
}
/************************* 定时器0中断 *************************/
void Timer0_Interrupt() interrupt 1 //定时器函数
{
static unsigned char cnt;
TL0 = 0x00; //设置定时初始值
TH0 = 0x4C; //设置定时初始值 50ms
timeCnt++;
if (timeCnt >= 1200) //timeCnt=20为1s钟 1200为1分钟
{
timeCnt = 0;
if (setTime != 0)
{
setTime--;
if (setTime == 0) //定时时间到
{
switchFlag = 1; //开启加热
}
}
}
cnt++;
if (cnt >= 20) //1s发送一次数据
{
cnt = 0;
sendFlag = 1;
}
}
/************************* 红外解码定时器程序 *************************/
void Ext0_Interrupt(void) interrupt 0 //外部中断解码程序_外部中断0
{
unsigned char i, j;
unsigned char byt;
unsigned int time;
time = IR_GetLowTime();
if ((time < 7833) || (time > 8755))
{
IE0 = 0;
return;
} //找到启始码
time = IR_GetHighTime();
if ((time < 3686) || (time > 4608)) //时间判定范围为4.0~5.0ms,
{ //超过此范围则说明为误码,直接退出
IE0 = 0;
return;
}
//接收并判定后续的4 字节数据
for (i = 0; i < 4; i++) //循环接收4 个字节
{
for (j = 0; j < 8; j++) //循环接收判定每字节的8 个bit
{
//接收判定每bit 的560us 低电平
time = IR_GetLowTime();
if ((time < 313) || (time > 718)) //时间判定范围为340~780us,
{ //超过此范围则说明为误码,直接退出
IE1 = 0;
return;
}
//接收每bit 高电平时间,判定该bit 的值
time = IR_GetHighTime();
if ((time > 313) && (time < 718)) //时间判定范围为340~780us,
{ //在此范围内说明该bit 值为0
byt >>= 1; //因低位在先,所以数据右移,高位为0
}
else if ((time > 1345) && (time < 1751)) //时间判定范围为1460~1900us,
{ //在此范围内说明该bit 值为1
byt >>= 1; //因低位在先,所以数据右移,
byt |= 0x80; //高位置1
}
else //不在上述范围内则说明为误码,直接退出
{
IE0 = 0;
return;
}
}
irCode[i] = byt; //接收完一个字节后保存到缓冲区
}
irFlag = 1; //接收完毕后设置标志
IE0 = 0; //退出前清零INT0 中断标志
}
仿真演示视频:
https://www.bilibili.com/video/BV1dP411c7k3/
实物演示视频:
https://www.bilibili.com/video/BV1id4y1b7mL/