基于51单片机的智能热水器红外遥控控制系统proteus仿真原理图PCB

功能:
0.本系统采用STC89C52作为单片机
1.LCD1602液晶实时显示温度阈值、当前温度和定时时间
2.支持按键和红外遥控设置温度阈值和定时时间
3.通过传感器检测,判定当前值是否超过设定的阈值,然后对相关继电器进行控制
4.支持声光报警
5.采用DC002作为电源接口可直接输入5V给整个系统供电

原理图:在这里插入图片描述

PCB :
在这里插入图片描述

主程序:

//程序头函数
#include <reg52.h>
#include <stdio.h>
#include "infrared.h"
#include "delay.h"
#include "18b20.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 lackOfWaterFlag; //缺水标志
bit waterFlag1 = 0; //自动检测控制加水标志
bit waterFlag2 = 0; //WIFI控制加水标志
unsigned char tempLowerLimit = 29; //温度下限
unsigned char tempUpperLimit = 35; //温度上限
unsigned int setTime = 0; //倒计时
unsigned char setLocation = 0; //设定位置
unsigned int timeCnt = 0; //计数
bit irFlag = 0; //红外接收标志,收到一帧正确数据后置1
unsigned char irCode[4];

//函数声明
void CheckTemperature(void); //读取温度
void DispNormal(void); //正常显示
void DispSet(void); //设置显示
void KeyProcess(void); //按键处理
void Alarm(void); //提醒
void InitTimer0(void); //初始化定时器0

void main()
{

    LCD_Init(); //LCD初始化
    Start18B20(); //启动18B20
    CheckTemperature(); //检测温度
    LCD_DispStr(4, 0, "Welcome!"); //显示

    InitTimer0();  //初始化定时器0
    IR_Init(); //初始化红外配置
    DelayMs(250);
    DelayMs(250);

    Start18B20(); //启动18B20
    CheckTemperature(); //检测温度
    DispNormal();//显示

    while (1)
    {
        Start18B20(); //启动18B20
        CheckTemperature(); //检测温度

        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:;
    }
}

/************************* 按键检测 *************************/
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;
    }
    
}


/************************* 红外解码定时器程序 *************************/
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/BV1vd4y1b7kJ/

实物演示视频:
https://www.bilibili.com/video/BV1nG4y1o77F/

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值