当前位置:网站首页>(十三)基于51单片机的简单温度报警装置
(十三)基于51单片机的简单温度报警装置
2022-07-28 06:45:00 【Meursault639】
目录
下午刚刚做了一个测温装置,我们晚上继续整活。既然已经可以测温了,那我们结合之前的知识点,来做一个温度报警装置!
需求分析
温度报警装置在许多的场所都有着极其重要的作用,比如实验室仪器或者药品的存放,提醒火灾的发生等等,如相对湿度经常大于95%、无烟火、有大量粉尘、在正常情况下有烟和粉尘滞留、厨房、锅炉房、发电机房、烘干车间、吸烟室、其它不宜安装感烟探测器的厅堂和公共场所。同时,我们希望温度的范围是我们可以调控的,最好还能有记忆功能,这样就能防止断电之后需要重新调整安全的温度范围。
功能实现
获取温度我们使用DS18B20来实现,具体可以看一下之前介绍的文章(1条消息) (十二)51单片机----用DS18B20浅测一下工(江)西的室外温度_Meursault639的博客-CSDN博客;
显示温度我们用LCD1602来实现;
调安全温度范围我们用按键来实现,但是得用定时器来扫描,不然会影响获取温度;
报警功能我们用蜂鸣器来实现;
记忆功能我们用AT24C02来实现,也可以参考一下之前写的,关于定时器扫描也在其中(1条消息) (十一)51单片机——用AT24C02实现存储秒表数据(附成果展示)_Meursault639的博客-CSDN博客;
代码实现
#include <REGX52.H>
#include "LCD1602.h"
#include "DS18B20.h"
#include "Delay.h"
#include "AT24C02.h"
#include "Key.h"
#include "Time0.h"
#include "Buzzer.h"
float T,TShow;// 温度,显示温度
char TL,TH;
unsigned char KeyNum;
void main()
{
DS18B20_ConvertT();//上电先转换一次温度,防止第一次读数据错误
Delay(1000); //等待转换完成
TH=AT24C02_ReadByte(0); //读取温度阈值数据
TL=AT24C02_ReadByte(1);
if(TH>125 || TL<-55 || TH<=TL)
{
TH=20; //如果阈值非法,则设为默认值
TL=15;
}
LCD_Init();
LCD_ShowString(1,1,"T:");
LCD_ShowString(2,1,"TH:");
LCD_ShowString(2,9,"TL:");
LCD_ShowSignedNum(2,4,TH,3);
LCD_ShowSignedNum(2,12,TL,3);
Timer0_Init();
while(1)
{
KeyNum=Key();
// 温度读取以及显示
DS18B20_ConvertT();//转换温度
T = DS18B20_ReadT();//读取温度
if(T < 0){
LCD_ShowChar(1,3,'-');//显示负号
TShow = -T;//将温度变为正数
}else{
LCD_ShowChar(1,3,'+');//显示正号
TShow = T;
}
LCD_ShowNum(1,4,TShow,3);//显示温度整数部分
LCD_ShowChar(1,7,'.');
LCD_ShowNum(1,8,(unsigned long)(TShow*100)%100,2);//显示温度小数部分,保留两位小数点
// 阈值判断及显示
if(KeyNum)
{
if(KeyNum==1) //K1按键,TH自增
{
TH++;
if(TH>125){TH=125;}
}
if(KeyNum==2) //K2按键,TH自减
{
TH--;
if(TH<=TL){TH++;}
}
if(KeyNum==3) //K3按键,TL自增
{
TL++;
if(TL>=TH){TL--;}
}
if(KeyNum==4) //K4按键,TL自减
{
TL--;
if(TL<-55){TL=-55;}
}
LCD_ShowSignedNum(2,4,TH,3); //显示阈值数据
LCD_ShowSignedNum(2,12,TL,3);
AT24C02_WriteByte(0,TH); //写入到At24C02中保存
Delay(5);
AT24C02_WriteByte(1,TL);
Delay(5);
}
if(T>TH) //越界判断
{
LCD_ShowString(1,13,"OV:H");
Buzzer_Time(100);
}
else if(T<TL)
{
LCD_ShowString(1,13,"OV:L");
Buzzer_Time(100);
}
else
{
LCD_ShowString(1,13," ");
}
}
}
void Timer0_Routine() interrupt 1
{
static unsigned int T0Count;
TL0 = 0x66; //设置定时初始值
TH0 = 0xFC;;
T0Count++;
if(T0Count>=20)
{
T0Count=0;
Key_Loop(); //每20ms调用一次按键驱动函数
}
}各个模块都在之前的文章中有所介绍,如果有感兴趣的同学,找我要完整的代码玩一下也是可以的,这个我还会不断改进,也希望大佬可以指点一下。
运行结果
温度报警器
边栏推荐
- [pyqt] pyqt development experience_ How to find events and methods of controls
- What if the computer file cannot be deleted?
- 金属质感登录框样式
- (Reprinted) plantuml Quick Guide
- Get the clicked line number in qtablewidget
- Talk about synchronous, asynchronous, blocking and non blocking
- 二维数组及操作
- Use of namespaces
- opencv+paddle orc 识别图片提取表格信息
- ASP. Net core foundation VIII
猜你喜欢

Common solutions for distributed ID - take one

Can the variable modified by final be modified

JS thoroughly understand this point

How to build the protection system of class protection technology of 2022 series of ISO compliance (Part I)
![[event registration] cloud native technology exchange meetup, see you in Guangzhou on August 6](/img/08/b892bd6c14d5ba3691f9b7def29c35.png)
[event registration] cloud native technology exchange meetup, see you in Guangzhou on August 6

Meituan Er Mian: why does redis have sentinels?

Es6: template string

二维数组及操作

Melt cloud x chat, create a "stress free social" habitat with sound

Find out whether the number exists from the matrix
随机推荐
leetcode/单词长度的最大乘积
Exception handling in SQL Server
uniapp的swiper动态设置current值不生效解决办法
opencv+paddle orc 识别图片提取表格信息
SQL function
See how Google uses pre training weights in target detection tasks | CVPR 2022
In the task manager, the CPU speed displayed is greater than its maximum speed [main frequency]
Chairman tree review
CarSim simulation quick start (10) - Modeling of braking system
Meituan Er Mian: why does redis have sentinels?
Talk about row storage and column storage of database
pyflink连接iceberg 实践
Understand the propagation process of EMI electromagnetic interference through five diagrams - the influence of square wave steepness on high-frequency components, the spectrum graph from time sequenc
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(八)
How to build the protection system of class protection technology of 2022 series of ISO compliance (Part I)
解决CNN固有缺陷!通用 CNN 架构CCNN来了| ICML2022
解决EMC、EMI传导干扰的八大方法
Plantuml Usage Summary
单片机IO口控制12V电压通断,MOS和三极管电路
Redis of non relational database [detailed setup of redis cluster]