欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
DHT11按鍵調(diào)節(jié)溫濕度+YL69土壤濕度報(bào)警
[打印本頁]
作者:
solanin
時(shí)間:
2017-7-28 10:39
標(biāo)題:
DHT11按鍵調(diào)節(jié)溫濕度+YL69土壤濕度報(bào)警
DHT11按鍵控制+土壤濕度報(bào)警
單片機(jī)源程序如下:
#include<reg52.h>
#include "intrins.h"
typedef unsigned char uint8;
typedef unsigned int uint16;
sbit rs=P2^6; // 數(shù)據(jù)命令選擇
sbit rw=P2^5; //讀寫選擇
sbit e=P2^7; //使能
sbit k1=P3^3; //模式
sbit k2=P2^1; //加
sbit k3=P2^2; //減
sbit bee=P1^5;//蜂鳴器
sbit DO=P3^1;
sbit DHT11_DQ_OUT=P3^2;
sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^2;
sbit dq=P2^0;
uint8 date;
uint8 mode=0,xian;
char temph=28,templ=20;
char humih=80,humil=20;
uint8 temp,humi;
uint8 flag; //設(shè)定報(bào)警標(biāo)志
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:"; //設(shè)定溫度上限顯示
uint8 code str7[]="TempL:"; //設(shè)定溫度下限顯示
uint8 code str8[]="HumiH:"; //設(shè)定濕度上限顯示
uint8 code str9[]="HumiL:"; //設(shè)定濕度下限顯示
void delay(uint16 i)
{
while(i--);
}
void delay_ms(uint16 i)
{
while(i--)
delay(90);
}
void delay1(uint16 z)
{
uint16 i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void wrc(uint8 c) //寫命令
{
delay(1000);
rs=0;
rw=0;
e=0;
P0=c;
e=1;
delay(10);
e=0;
}
void wrd(uint8 dat) //寫數(shù)據(jù)
{
delay(1000);
rs=1;
rw=0;
e=0;
P0=dat;
e=1;
delay(10);
e=0;
rs=0;
}
void lcd_init() // LCD1602初始化
{
delay(1000);
wrc(0x38);
wrc(0x38); //功能設(shè)置命令,選擇8位總線,雙行顯示 5*7點(diǎn)陣字符
wrc(0x38);
wrc(0x06); //光標(biāo)和顯示模式設(shè)置 光標(biāo)右移 整屏不移動
wrc(0x0c); //顯示開關(guān)控制 開顯示 無光標(biāo) 光標(biāo)不閃爍
wrc(0x01); //清零指令 固定的
}
//復(fù)位DHT11
void DHT11_Rst()
{
DHT11_DQ_OUT=0; //拉低DQ
delay_ms(20); //拉低至少18ms
DHT11_DQ_OUT=1; //DQ=1
delay(3); //主機(jī)拉高20~40us
}
//等待DHT11的回應(yīng)
//返回1:未檢測到DHT11的存在
//返回0:存在
uint8 DHT11_Check()
{
uint8 retry=0;
while (DHT11_DQ_OUT&&retry<100)//DHT11會拉低40~50us
{
retry++;
_nop_();
};
if(retry>=100)return 1;
else retry=0;
while (!DHT11_DQ_OUT&&retry<100)//DHT11拉低后會再次拉高40~50us
{
retry++;
_nop_();
};
if(retry>=100)return 1;
return 0;
}
//DHT11初始化
//返回0:初始化成功,1:失敗
uint8 DHT11_Init()
{
DHT11_Rst();
return DHT11_Check();
}
//從DHT11讀取一個(gè)位
//返回值:1/0
uint8 DHT11_Read_Bit(void)
{
uint8 retry=0;
while(DHT11_DQ_OUT&&retry<100)//等待變?yōu)榈碗娖?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讀取一個(gè)字節(jié)
//返回值:讀到的數(shù)據(jù)
uint8 DHT11_Read_Byte(void)
{
uint8 i,dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
//從DHT11讀取一次數(shù)據(jù)
//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位數(shù)據(jù)
{
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 key_pros() //按鍵處理函數(shù)
{
if(k1==0)
{
delay(1000);
if(k1==0)
{
mode++;
if(mode==5)mode=0;
wrc(0x01);
}
while(!k1);
}
if(mode==1) //對溫度上限設(shè)定
{
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) //對溫度下限設(shè)定
{
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) //對濕度上限設(shè)定
{
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) //對濕度下限設(shè)定
{
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);
}
}
}
void lcd_init_display() //LCD初始化顯示
{
uint8 i;
for(i=0;i<5;i++)
{
wrc(0x80+i);
wrd(str1[i]);
}
for(i=0;i<5;i++)
{
wrc(0xc0+i);
wrd(str2[i]);
}
}
void data_pros() //數(shù)據(jù)處理函數(shù)
{
uint8 i;
uint8 temp_buf[2],humi_buf[2];
uint8 temphbuf[2],templbuf[2],humihbuf[2],humilbuf[2];
DHT11_Read_Data(&temp,&humi);
temp_buf[0]=temp/10+0x30;
temp_buf[1]=temp%10+0x30;
humi_buf[0]=humi/10+0x30;
humi_buf[1]=humi%10+0x30;
temphbuf[0]=temph/10+0x30;
temphbuf[1]=temph%10+0x30;
templbuf[0]=templ/10+0x30;
templbuf[1]=templ%10+0x30;
humihbuf[0]=humih/10+0x30;
humihbuf[1]=humih%10+0x30;
humilbuf[0]=humil/10+0x30;
humilbuf[1]=humil%10+0x30;
if(mode==0)
{
lcd_init_display();
wrc(0x85);
wrd(num[temp/10]);
wrd(num[temp%10]);
wrd(0xdf);
wrd('C');
for(i=0;i<2;i++)
{
wrc(0Xc5+i);
wrd(humi_buf[i]);
}
for(i=0;i<3;i++)
{
wrc(0Xc7+i);
wrd(str5[i]);
}
}
if(mode==1) //溫度上限顯示
{
wrc(0x80);
for(i=0;i<6;i++)
{
wrd(str6[i]);
}
wrd(temphbuf[0]);
wrd(temphbuf[1]);
}
if(mode==2) //溫度下限顯示
{
wrc(0x80);
for(i=0;i<6;i++)
{
wrd(str7[i]);
}
wrd(templbuf[0]);
wrd(templbuf[1]);
}
if(mode==3) //濕度上限顯示
{
wrc(0x80);
for(i=0;i<6;i++)
{
wrd(str8[i]);
}
wrd(humihbuf[0]);
wrd(humihbuf[1]);
}
if(mode==4) //濕度下限顯示
{
wrc(0x80);
for(i=0;i<6;i++)
{
wrd(str9[i]);
}
wrd(humilbuf[0]);
wrd(humilbuf[1]);
}
}
void baojinpros() //報(bào)警處理
{
if(temp>=temph||humi>=humih) //檢測溫度或者濕度高于設(shè)定上限值 降溫濕
{
led1=1; //降溫濕指示燈
led2=0;
}
if(temp<=templ||humi<=humil) //檢測溫度或者濕度低于設(shè)定下限值 升溫濕
{
led1=0;
led2=1; //升高溫濕指示燈
}
if(DO==0)
{
led3=1;
}
if(DO==1)
{
led3=0;
}
if((temp>templ&&temp<temph)&&(humi>humil&&humi<humih)&&DO==1)
{
led1=0;
led2=0;
led3=0;
}
}
void Initial_com(void)
{
EA=1; //開總中斷
ES=1; //允許串口中斷
ET1=1; //允許定時(shí)器T1的中斷
TMOD=0x20; //定時(shí)器T1,在方式2中斷產(chǎn)生波特率
PCON=0x00; //SMOD=0
SCON=0x50; // 方式1 由定時(shí)器控制
TH1=0xfd; //波特率設(shè)置為9600
TL1=0xfd;
TR1=1; //開定時(shí)器T1運(yùn)行控制位
}
void main()
{
uint8 i=0;
led1=0;
led2=0;
led3=0;
Initial_com();
lcd_init();
if(DO==0)
{
delay1(1); //消抖動
if(DO==0) //確認(rèn)觸發(fā)
{
SBUF=0X01;
delay1(200);
}
if(RI)
{
date=SBUF; //單片機(jī)接受
SBUF=date; //單片機(jī)發(fā)送
RI=0;
}
}
while(DHT11_Init()) //檢測DHT11是否純在
{
for(i=0;i<5;i++)
{
wrc(0x80+i);
wrd(str3[i]);
}
}
wrc(0x01);
lcd_init_display(); //LCD初始化顯示
i=0;
while(1)
{
i++;
key_pros();
baojinpros(); //報(bào)警處理
if(i==15)
{
i=0;
data_pros(); //讀取一次DHT11數(shù)據(jù)最少要大于100ms
}
delay(1000);
}
}
復(fù)制代碼
作者:
66886666
時(shí)間:
2018-6-6 10:11
學(xué)習(xí)一下
作者:
威望值
時(shí)間:
2018-6-12 15:19
有仿真圖嗎
作者:
hhhhhh111
時(shí)間:
2018-12-19 10:34
有沒有用在STM32F103ZET6上的
作者:
caulucy2017
時(shí)間:
2020-3-30 17:56
請問有與51單片機(jī)的連接原理圖嗎
作者:
啦啦小呆瓜
時(shí)間:
2021-11-20 13:57
有仿真嘛??
作者:
wuhyy
時(shí)間:
2022-4-20 10:21
請教一下如何畫仿真圖
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1