基于51单片机的报警器(燃气,温度,2模拟)原理图、流程图、物料清单、仿真图、源代码
基于51单片机的报警器(燃气,温度,2模拟)
燃气报警系统
1、检测温度、甲醛、一氧化碳(也可以改成别的名字、别的传感器)
2、按键设定报警值,测量值过限报警
3、使用LCD1602显示测量值和报警参数
#include "reg51.h"
#include "ADC0832.h"
#include "Ds18b20.h"
#include "lcd1602.h"
#define uchar unsigned char
#define uint unsigned int
sbit beep=P3^4;//蜂鸣器
sbit k1=P2^3;//按钮
sbit k2=P2^4;
sbit k3=P2^5;
sbit k4=P2^6;
sbit k5=P2^7;
sbit led1=P3^5;
sbit led2=P3^6;
sbit led3=P3^7;
uchar time=0,sec=0;//系统定时
uchar co=0;//一氧化碳
uchar hcho=0;//甲醛
uchar down=10;//温度下限
uchar up[]={40,20,15};//上限
uchar disp1[]="00 ";
uchar disp2[]="00% ";
uchar mode=0;//模式
//主函数
void main()
{
disp1[2]=0xdf;
init_1602();
TMOD|=0X01;
TH0=0X3C;
TL0=0XB0;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
while(1)
{
if(!k1) //设置
{
if(mode<3)
mode++;
else
mode=0;
write_string(1,0," ");
write_string(2,0," ");
while(!k1);
}
if(mode>0)//上下限设置
{
if(!k2)
{
if(up[mode-1]<99)
up[mode-1]++;
while(!k2);
}
if(!k3)
{
if(up[mode-1]>0)
up[mode-1]--;
while(!k3);
}
if(!k4)
{
if(down<up[0])
down++;
while(!k4);
}
if(!k5)
{
if(down>0)
down--;
while(!k5);
}
}
//判断
if(co>up[2])//一氧化碳
led3=0;
else
led3=1;
if(hcho>up[1])//甲醛
led2=0;
else
led2=1;
if((ds18b20_temp<down)||(ds18b20_temp>up[0]))//温度
led1=0;
else
led1=1;
//报警
if(!led1 || !led2 || !led3)
sec=10;
}
}
//定时器中断
void Timer0() interrupt 1
{
uint i;
if(sec>0) //蜂鸣器驱动
{
sec--;
beep=0;
}
else
beep=1;
if(time<19)//0.5s
time++;
else
{
time=0;
co=ADC(2);//测量一氧化碳
i=co;
co=i*99/127;
Ds18b20ReadTemp();
hcho=ADC(1);//测量甲醛
i=hcho;
hcho=i*99/127;
//显示
if(mode==0)
{
write_string(1,0,"tem: ");//温度
i=ds18b20_temp;
disp1[0]=i/10+0x30;
disp1[1]=i%10+0x30;
write_string(1,4,disp1);
write_string(2,0,"hcho:");//甲醛
disp2[0]=hcho/10+0x30;
disp2[1]=hcho%10+0x30;
write_string(2,5,disp2);
write_string(2,9,"CO:");//一氧化碳
disp2[0]=co/10+0x30;
disp2[1]=co%10+0x30;
write_string(2,12,disp2);
}
if(mode==3)//一氧化碳
{
write_string(1,0,"CO: ");
disp2[0]=up[2]/10+0x30;
disp2[1]=up[2]%10+0x30;
write_string(2,4,disp2);
}
if(mode==1)//温度
{
write_string(1,0,"temp: ");
write_string(2,0," H:");
disp1[0]=up[0]/10+0x30;
disp1[1]=up[0]%10+0x30;
write_string(2,4,disp1);
write_string(2,8," L:");
disp1[0]=down/10+0x30;
disp1[1]=down%10+0x30;
write_string(2,12,disp1);
}
if(mode==2)//甲醛
{
write_string(1,0,"hcho: ");
disp2[0]=up[1]/10+0x30;
disp2[1]=up[1]%10+0x30;
write_string(2,4,disp2);
}
}
TH0=0X3C;
TL0=0XB0;
}