发布时间:2023-12-04 12:00
以51单片机为核心,设计和制作一个温度报警器,能在LCD上显示环境的温度与希望温度上下限阀值,并能设置希望温度上下限阀值,系统上电的时候显示的是当前环境温度和设定的温度阀值,通过按键来修改温度上下限阀值,再次上电时保持上一次的温度设置。根据温度传感器测得的温度做如下处理:温度当温度低于设定下限温度时,蜂鸣器发出报警声和绿光报警,并且通过继电器控制加热设备提升温度至正常温度,然后加热设备停止工作;当温度高于设定上限温度时,蜂鸣器发出报警声和红光报警,并且通过继电器控制散热设备降低温度至正常温度,然后散热设备停止工作。
(1)确定系统设计方案;
(2)进行系统的硬件设计;
(3)完成应用程序设计;
(4)应用系统的硬件和软件的调试;
main.c
#include "public.h"
#include "lcd.h"
#include "temp.h"
#include "i2c.h"
sbit k3=P3^2; //设置温度上下限
sbit k1=P3^4; //加
sbit k2=P3^5; //减
sbit ledL=P1^0;
sbit ledH=P1^1;
sbit led=P2^4; //电源指示灯
sbit beep=P1^5; //蜂鸣器报警
sbit relay=P1^4; //加热或散热设备
char set_templ=22,set_temph=40; //设定温度上下限默认值
u16 temp_val; //检测的实际温度
u8 mode; //温度模式
void Temp_DataPros()
{
short temp;
u8 temp_buf[5];
temp=Ds18b20ReadTemp();
temp_val=temp;
if(temp<0)
{
temp=-temp;
LCD_Dispstring(2+5,0,"-");
}
else
{
LCD_Dispstring(2+5,0," ");
}
temp_buf[0]=temp/100+0x30;
temp_buf[1]=temp%100/10+0x30;
temp_buf[2]='.';
temp_buf[3]=temp%100%10+0x30;
temp_buf[4]='\0';
LCD_Dispstring(2+6,0,temp_buf); //显示检测的温度xx.x
temp_buf[0]=set_temph/10+0x30;
temp_buf[1]=set_temph%10+0x30;
temp_buf[2]='\0';
LCD_Dispstring(5,1,temp_buf); //显示设定的温度上限值xx
temp_buf[0]=set_templ/10+0x30;
temp_buf[1]=set_templ%10+0x30;
temp_buf[2]='\0';
LCD_Dispstring(14,1,temp_buf); //显示设定的温度下限值xx
}
#define K1_MODE 1
#define K2_ADD 2
#define K3_DEC 3
//mode: 0-单次扫描 1-连续扫描
u8 KEY_Scan(u8 mode)
{
static u8 key=1;
if(key&&(k1==0||k2==0||k3==0))
{
delay(1000); //消抖
key=0;
if(k3==0)
{
return K1_MODE;
}
else if(k1==0)
{
return K2_ADD;
}
else if(k2==0)
{
return K3_DEC;
}
}
else if(k1==1&&k2==1&&k3==1)
{
key=1;
}
if(mode)
{
key=1;
}
return 0;
}
void KEY_Pros()
{
u8 key;
u8 temph_buf[3];
key=KEY_Scan(0);
if(key==K1_MODE) //模式选择
{
mode++;
LCD_Clear();
if(mode==1)
{
LCD_Dispstring(0,0,"SETH: C");
}
else if(mode==2)
{
LCD_Dispstring(0,1,"SETL: C");
}
else
{
mode=0;
LCD_Dispstring(2,0,"Temp: C");
LCD_Dispstring(0,1,"SETH: ");
LCD_Dispstring(9,1,"SETL: ");
}
}
if(mode==1) //温度上限设置
{
switch(key)
{
case K2_ADD: //加
set_temph++;
if(set_temph>=80)set_temph=80;
break;
case K3_DEC: //减
set_temph--;
if(set_temph<=0)set_temph=0;
break;
}
temph_buf[0]=set_temph/10+0x30;
temph_buf[1]=set_temph%10+0x30;
temph_buf[2]='\0';
LCD_Dispstring(6,0,temph_buf);
At24c02Write(0,set_temph);
}
else if(mode==2) //温度下限设置
{
switch(key)
{
case K2_ADD: //加
set_templ++;
if(set_templ>=80)set_templ=80;
break;
case K3_DEC: //减
set_templ--;
if(set_templ<=0)set_templ=0;
break;
}
temph_buf[0]=set_templ/10+0x30;
temph_buf[1]=set_templ%10+0x30;
temph_buf[2]='\0';
LCD_Dispstring(6,1,temph_buf);
At24c02Write(2,set_templ);
}
}
void sound()
{
u8 i=50;
while(i--)
{
beep=!beep;
delay(10);
}
}
void TempData_Compare()
{
if(temp_val>set_temph*10) //实际温度高于上限值 报警
{
relay=0;
ledH=0;
ledL=1;
sound();
}
else if(temp_val<set_templ*10) //实际温度低于下限值 报警
{
relay=0;
ledH=1;
ledL=0;
sound();
}
else //实际温度在下限值和上限值之间 取消报警 取消加热 取消散热
{
ledH=1;
ledL=1;
relay=1;
}
}
void kai_display()
{
if(At24c02Read(255)!=18)
{
At24c02Write(0,set_temph);
At24c02Write(2,set_templ);
At24c02Write(255,18);
}
else
{
set_temph=At24c02Read(0);
set_templ=At24c02Read(2);
}
LCD_Dispstring(2,0,"Temp: C");
LCD_Dispstring(0,1,"SETH: ");
LCD_Dispstring(9,1,"SETL: ");
}
void main()
{
ledH=1;
ledL=1;
relay=1;
led=0;
LCD_Init();
kai_display();
while(1)
{
if(mode==0)
Temp_DataPros();
KEY_Pros();
TempData_Compare();
}
}
如需具体工程代码可评论区留言
Python基础知识——程序流程的三大结构(顺序、选择、循环)
论文介绍Two-StreamConvolutional Networks for Action Recognition in Videos
姿态估算03-01:mmpose(Associative embedding)-资源下载(前奏准备)
【Rust日报】 2019-07-31:Debian Buster 将会预装Rust编译器
抖音短视频数据抓取实战系列(七)——python连接MySQL数据库
GitHub原生AI代码生成工具Copilot,官方支持Visual Studio 2022