51单片机烟雾报警器制作程序
时间:04-08 14:25 阅读:1529次
*温馨提示:点击图片可以放大观看高清大图
简介:51单片机烟雾报警器制作实物图及相关程序,经验证可行。
烟雾报警器制作过程及相关程序
//程序头函数
#include <reg52.h>
//宏定义
#define uint unsigned int
#define uchar unsigned char
#define Data_ADC0809 P1
//管脚声明
sbit Feng = P3^0;
//ADC0809
sbit ST=P3^3;
sbit EOC=P3^4;
sbit OE=P3^2;
//按键
sbit Key1=P3^5;
sbit Key2=P3^6;
sbit Key3=P3^7;
//显示数组 0-9+F
uchar Data_[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x71,0x3f};
sbit Wei1 = P2^7;
sbit Wei2 = P2^6;
sbit Wei3 = P2^5;
sbit Wei4 = P2^4;
//函数声明
extern uchar ADC0809();
void Display(uchar X,uchar Data);
void delay(uint t);
//酒精含量变量
uchar temp=0;
//蜂鸣器变量
uchar FF=10;
//显示模式
uchar Mode=0;
uchar p;
void main()
{
while(1)
{
//正常模式
if(Mode==0)
{
//读取AD值
temp=ADC0809();
for(p=0;p<30;p++)
Display(0,temp);
//判断是否报警
if(temp>FF)
{
Feng=0;
}
else
{
Feng=1;
}
}
//调整模式
else
{
Display(1,FF);
}
//功能键
if(Key3==0)
{
Feng=0;
delay(100);
while(Key3==0)
{
if(Mode==0)
Display(0,temp);
else
Display(1,FF);
}
if(Mode==0)
Mode=1;
else
Mode=0;
Feng=1;
}
//增加
if(Key2==0&&Mode==1)
{
Feng=0;
delay(100);
while(Key2==0)
{
Display(1,FF);
}
FF++;
if(FF==251)
FF=250;
Feng=1;
}
//减少
if(Key1==0&&Mode==1)
{
Feng=0;
delay(100);
while(Key1==0)
{
Display(1,FF);
}
FF--;
if(FF==0xff)
FF=0;
Feng=1;
}
}
}
//ADC0809读取信息
uchar ADC0809()
{
uchar temp_=0x00;
//初始化高阻太
OE=0;
//转化初始化
ST=0;
//开始转换
ST=1;
ST=0;
//外部中断等待AD转换结束
while(EOC==0)
//读取转换的AD值
OE=1;
temp_=Data_ADC0809;
OE=0;
return temp_;
}
//延时
void delay(uint t)
{
uint i,j;
for(i=0;i<t;i++)
for(j=0;j<10;j++);
}
//显示 X表示状态 Data表示数据
void Display(uchar X,uchar Data)
{
Wei1=1;
Wei2=1;
Wei3=1;
Wei4=1;
P0=0xff;
//正常模式
if(X==0)
{
P0=~Data_[11];
}
//非正常
else
{
P0=~Data_[10];
}
Wei1=0;
Wei2=1;
Wei3=1;
Wei4=1;
delay(10);
Wei1=1;
Wei2=1;
Wei3=1;
Wei4=1;
P0=~Data_[Data/100];
Wei1=1;
Wei2=0;
Wei3=1;
Wei4=1;
delay(10);
Wei1=1;
Wei2=1;
Wei3=1;
Wei4=1;
P0=~Data_[Data/10%10];
Wei1=1;
Wei2=1;
Wei3=0;
Wei4=1;
delay(10);
Wei1=1;
Wei2=1;
Wei3=1;
Wei4=1;
P0=~Data_[Data%10];
Wei1=1;
Wei2=1;
Wei3=1;
Wei4=0;
delay(10);
Wei1=1;
Wei2=1;
Wei3=1;
Wei4=1;
}