基于51单片机的孵化环境温湿度监控系统设计方案

控制/MCU

1865人已加入

描述

实现功能:

1、对孵化环境的温度、湿度进行监控

2、可以警报提示、修改温度、湿度上下限值

3、led灯指示报警温湿度信息、LCD显示相关信息

电路原理图:

51单片机

部分程序:

#include

#include "intrins.h"

typedef unsigned char uint8;

typedef unsigned int uint16;

sbit rs=P2^6; // 数据命令选择

sbit rw=P2^5; //读写选择

sbit e=P2^7; //使能

sbit k1=P3^3; //模式

sbit k2=P2^1; //加

sbit k3=P2^2; //减

sbit DHT11_DQ_OUT=P3^2;

sbit led1=P3^6;

sbit led2=P3^7;

sbit dq=P2^0;

uint8 mode=0,xian;

char temph=50,templ=20;

char humih=80,humil=20;

uint8 temp,humi;

uint8 flag; //设定报警标志

uint8 a,c,tempvalue;

uint8 code num[10]="0123456789";

uint8 code str1[]="Temp:"; //温度

uint8 code str2[]="Humi:"; //湿度

uint8 code str3[]="Error";

uint8 code str4[]="Success ";

uint8 code str5[]="%RH";

uint8 code str6[]="TempH:"; //设定温度上限显示

uint8 code str7[]="TempL:"; //设定温度下限显示

uint8 code str8[]="HumiH:"; //设定湿度上限显示

uint8 code str9[]="HumiL:"; //设定湿度下限显示

//从DHT11读取一个位

//返回值:1/0

uint8 DHT11_Read_Bit(void)

{

uint8 retry=0;

while(DHT11_DQ_OUT&&retry<100)//等待变为低电平 12-14us 开始

{

retry++;

nop ();

}

retry=0;

while((!DHT11_DQ_OUT)&&retry<100)//等待变高电平 26-28us表示0,116-118us表示1

{

retry++;

nop ();

}

delay(1);//等待40us

if(DHT11_DQ_OUT)return 1;

else return 0;

}

//从DHT11读取一个字节

//返回值:读到的数据

uint8 DHT11_Read_Byte(void)

{

uint8 i,dat=0;

for (i=0;i<8;i++)

{

dat<<=1;

dat|=DHT11_Read_Bit();

}

return dat;

}

//从DHT11读取一次数据

//temp:温度值(范围:0~50°)

//humi:湿度值(范围:20%~90%)

//返回值:0,正常;1,读取失败

uint8 DHT11_Read_Data(uint8 *temp,uint8 *humi)

{

uint8 buf[5];

uint8 i;

DHT11_Rst();

if(DHT11_Check()==0)

{

for(i=0;i<5;i++)//读取40位数据

{

buf[i]=DHT11_Read_Byte();

}

if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])

{

*humi=buf[0];

*temp=buf[2];

}

}else return 1;

return 0;

}

void ds18b20init() //18b20的初始化

{

dq=1;

delay(1);

dq=0;

delay(80);

dq=1;

delay(5);

dq=0;

delay(20);

dq=1;

delay(35);

}

void ds18b20wr(uint8 dat) //18b20写数据

{

uint8 i;

for(i=0;i<8;i++)

{

dq=0;

dq=dat&0x01;

dat>>=1;

delay(8);//在时序上只有这一块对时序要求最准确,他的时间必须大于15us

dq=1;

delay(1);

}

}

uint8 ds18b20rd() //18b20读数据

{

uint8 value,i;

for(i=0;i<8;i++)

{

dq=0;

value>>=1;

dq=1;

if(dq==1)value|=0x80;

delay(8);//在这一块也对时间要求特别准确,整段程序必须大于60us

}

return value;

}

uint8 readtemp() //读取温度内需要复位的

{

uint8 b;

ds18b20init(); //初始化

ds18b20wr(0xcc); //发送忽略ROM指令

ds18b20wr(0x44); //发送温度转换指令

delay(100);

ds18b20init(); //初始化

ds18b20wr(0xcc); //发送忽略ROM指令

ds18b20wr(0xbe); //发读暂存器指令

a=ds18b20rd(); //温度的低八位

b=ds18b20rd(); //温度的高八位

b<<=4; //ssss s***;s为标志位s=0表示温度值为正数,s=1温度值为负数

c=b&0x80; //温度正负标志位确认

b+=(a&0xf0)>>4;

a=a&0x0f; //温度的小数部分

return b;

}

void key_pros() //按键处理函数

{

if(k1==0)

{

delay(1000);

if(k1==0)

{

mode++;

if(mode==5)mode=0;

wrc(0x01);

}

while(!k1);

}

if(mode==1) //对温度上限设定

{

if(k2==0) //加

{

delay(1000);

if(k2==0)

{

temph++;

if(temph>=80)temph=80;

}

while(!k2);

}

if(k3==0) //减

{

delay(1000);

if(k3==0)

{

temph--;

if(temph<=0)temph=0;

}

while(!k3);

}

}

if(mode==2) //对温度下限设定

{

if(k2==0) //加

{

delay(1000);

if(k2==0)

{

templ++;

if(templ>=80)templ=80;

}

while(!k2);

}

if(k3==0) //减

{

delay(1000);

if(k3==0)

{

templ--;

if(templ<=0)templ=0;

}

while(!k3);

}

}

if(mode==3) //对湿度上限设定

{

if(k2==0) //加

{

delay(1000);

if(k2==0)

{

humih++;

if(humih>=80)humih=80;

}

while(!k2);

}

if(k3==0) //减

{

delay(1000);

if(k3==0)

{

humih--;

if(humih<=0)humih=0;

}

while(!k3);

}

}

if(mode==4) //对湿度下限设定

{

if(k2==0) //加

{

delay(1000);

if(k2==0)

{

humil++;

if(humil>=80)humil=80;

}

while(!k2);

}

if(k3==0) //减

{

delay(1000);

if(k3==0)

{

humil--;

if(humil<=0)humil=0;

}

while(!k3);

}

}

}

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分