单片机温湿度检测.docx

上传人:b****1 文档编号:14669924 上传时间:2023-06-26 格式:DOCX 页数:17 大小:16.66KB
下载 相关 举报
单片机温湿度检测.docx_第1页
第1页 / 共17页
单片机温湿度检测.docx_第2页
第2页 / 共17页
单片机温湿度检测.docx_第3页
第3页 / 共17页
单片机温湿度检测.docx_第4页
第4页 / 共17页
单片机温湿度检测.docx_第5页
第5页 / 共17页
单片机温湿度检测.docx_第6页
第6页 / 共17页
单片机温湿度检测.docx_第7页
第7页 / 共17页
单片机温湿度检测.docx_第8页
第8页 / 共17页
单片机温湿度检测.docx_第9页
第9页 / 共17页
单片机温湿度检测.docx_第10页
第10页 / 共17页
单片机温湿度检测.docx_第11页
第11页 / 共17页
单片机温湿度检测.docx_第12页
第12页 / 共17页
单片机温湿度检测.docx_第13页
第13页 / 共17页
单片机温湿度检测.docx_第14页
第14页 / 共17页
单片机温湿度检测.docx_第15页
第15页 / 共17页
单片机温湿度检测.docx_第16页
第16页 / 共17页
单片机温湿度检测.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

单片机温湿度检测.docx

《单片机温湿度检测.docx》由会员分享,可在线阅读,更多相关《单片机温湿度检测.docx(17页珍藏版)》请在冰点文库上搜索。

单片机温湿度检测.docx

单片机温湿度检测

#include

#include

#include

#defineuintunsignedint

#defineucharunsignedchar

sbitlcden=P2^5;

sbitlcdrw=P2^6;

sbitlcdrs=P2^7;

sbitDSPORT=P3^2;

sbitDHT11_IO=P2^0;

sbitDHT11_IO2=P2^1;

sbitDHT11_IO3=P2^2;

//sbitD0=P1^0;

//sbitD1=P1^1;

ucharcodeBCD[]={'0','1','2','3','4','5','6','7','8','9','.'};//0,1,2,3,4,5,6,7,8,9,.,

ucharTX_Buffer[10]={"0"};

ucharTX_Buffer2[10]={"0"};

ucharTX_Buffer3[10]={"0"};

//ucharcodetable[]="%RH";

uchardata_byte;

voidDelayms(ucharms)

{

uchari;

while(ms--)

{

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

}

}

voidDelay1()

{

uchari;

i--;

i--;

i--;

i--;

i--;

i--;

}

voidwrite_com(ucharcom)

{

lcdrs=0;

lcdrw=0;

P0=com;

Delayms(5);

lcden=1;

Delayms(5);

lcden=0;

}

voidwrite_data(uchardat)

{

lcdrs=1;

lcdrw=0;

P0=dat;

Delayms(5);

lcden=1;

Delayms(5);

lcden=0;

}

voidinit()

{

write_com(0x38);

write_com(0x0f);

write_com(0x06);

write_com(0x01);

write_com(0x80);

}

voidstart(void)

{

DHT11_IO=1;

Delay1();

DHT11_IO=0;

Delayms(20);

DHT11_IO=1;

Delay1();//20-40us

Delay1();

Delay1();

Delay1();

Delay1();

}

ucharreceive_byte(void)

{

uchari,count;

uchartemp;

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

{

count=2;

while(!

DHT11_IO)

temp=0;

Delay1();

Delay1();

Delay1();

Delay1();

Delay1();

if(DHT11_IO==1)

temp=1;

count=2;

while(DHT11_IO);

if(count==1)break;

data_byte<<=1;

data_byte|=temp;

}

returndata_byte;

}

voidreceive()

{

ucharcheck,num_check,HZ,HX,WZ,WX;

ucharcount;

start();

DHT11_IO=1;

if(!

DHT11_IO)

{

count=2;

while((!

DHT11_IO)&&count++);

count=2;

while((DHT11_IO)&&count++);

HZ=receive_byte();

HX=receive_byte();

WZ=receive_byte();

WX=receive_byte();

check=receive_byte();

DHT11_IO=0;

DHT11_IO=1;

num_check=HZ+HX+WZ+WX;

if(num_check=check)

{

check=num_check;

}

}

TX_Buffer[0]=BCD[HZ/10];

TX_Buffer[1]=BCD[HZ%10];

TX_Buffer[2]=BCD[10];

TX_Buffer[3]=BCD[HX/10];

TX_Buffer[4]=BCD[HX%10];

}

voidstart2(void)

{

DHT11_IO2=1;

Delay1();

DHT11_IO2=0;

Delayms(20);

DHT11_IO2=1;

Delay1();//20-40us

Delay1();

Delay1();

Delay1();

Delay1();

}

ucharreceive_byte2(void)

{

uchari,count;

uchartemp;

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

{

count=2;

while(!

DHT11_IO2)

temp=0;

Delay1();

Delay1();

Delay1();

Delay1();

Delay1();

if(DHT11_IO2==1)

temp=1;

count=2;

while(DHT11_IO2);

if(count==1)break;

data_byte<<=1;

data_byte|=temp;

}

returndata_byte;

}

voidreceive2()

{

ucharcheck,num_check,HZ,HX,WZ,WX;

ucharcount;

start2();

DHT11_IO2=1;

if(!

DHT11_IO2)

{

count=2;

while((!

DHT11_IO2)&&count++);

count=2;

while((DHT11_IO2)&&count++);

HZ=receive_byte2();

HX=receive_byte2();

WZ=receive_byte2();

WX=receive_byte2();

check=receive_byte2();

DHT11_IO2=0;

DHT11_IO2=1;

num_check=HZ+HX+WZ+WX;

if(num_check=check)

{

check=num_check;

}

}

TX_Buffer2[0]=BCD[HZ/10];

TX_Buffer2[1]=BCD[HZ%10];

TX_Buffer2[2]=BCD[10];

TX_Buffer2[3]=BCD[HX/10];

TX_Buffer2[4]=BCD[HX%10];

}

voidstart3(void)

{

DHT11_IO3=1;

Delay1();

DHT11_IO3=0;

Delayms(20);

DHT11_IO3=1;

Delay1();//20-40us

Delay1();

Delay1();

Delay1();

Delay1();

}

ucharreceive_byte3(void)

{

uchari,count;

uchartemp;

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

{

count=2;

while(!

DHT11_IO3)

temp=0;

Delay1();

Delay1();

Delay1();

Delay1();

Delay1();

if(DHT11_IO3==1)

temp=1;

count=2;

while(DHT11_IO3);

if(count==1)break;

data_byte<<=1;

data_byte|=temp;

}

returndata_byte;

}

voidreceive3()

{

ucharcheck,num_check,HZ,HX,WZ,WX;

ucharcount;

start3();

DHT11_IO3=1;

if(!

DHT11_IO3)

{

count=2;

while((!

DHT11_IO3)&&count++);

count=2;

while((DHT11_IO3)&&count++);

HZ=receive_byte3();

HX=receive_byte3();

WZ=receive_byte3();

WX=receive_byte3();

check=receive_byte3();

DHT11_IO3=0;

DHT11_IO3=1;

num_check=HZ+HX+WZ+WX;

if(num_check=check)

{

check=num_check;

}

}

TX_Buffer3[0]=BCD[HZ/10];

TX_Buffer3[1]=BCD[HZ%10];

TX_Buffer3[2]=BCD[10];

TX_Buffer3[3]=BCD[HX/10];

TX_Buffer3[4]=BCD[HX%10];

}

voidDelay1ms(uinty)

{

uintx;

for(;y>0;y--)

{

for(x=110;x>0;x--);

}

}

ucharDs18b20Init()

{

uchari;

DSPORT=0;//½«×ÜÏßÀµÍ480us~960us

i=70;

while(i--);//ÑÓʱ642us

DSPORT=1;//È»ºóÀ¸ß×ÜÏߣ¬Èç¹ûDS18B20×ö³ö·´Ó¦»á½«ÔÚ15us~60usºó×ÜÏßÀµÍ

i=0;

while(DSPORT)//µÈ´ýDS18B20ÀµÍ×ÜÏß

{

i++;

if(i>5)//µÈ´ý>5MS

{

return0;//³õʼ»¯Ê§°Ü

}

Delay1ms

(1);

}

return1;//³õʼ»¯³É¹¦

}

voidDs18b20WriteByte(uchardat)

{

uinti,j;

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

{

DSPORT=0;

i++;

DSPORT=dat&0x01;

i=6;

while(i--);

DSPORT=1;

dat>>=1;

}

}

ucharDs18b20ReadByte()

{

ucharbyte,bi;

uinti,j;

for(j=8;j>0;j--)

{

DSPORT=0;

i++;

DSPORT=1;

i++;

i++;

bi=DSPORT;

byte=(byte>>1)|(bi<<7);

i=4;

while(i--);

}

returnbyte;

}

voidDs18b20ChangTemp()

{

Ds18b20Init();

Delay1ms

(1);

Ds18b20WriteByte(0xcc);

Ds18b20WriteByte(0x44);

//Delay1ms(100);

}

voidDs18b20ReadTempCom()

{

Ds18b20Init();

Delay1ms

(1);

Ds18b20WriteByte(0xcc);

Ds18b20WriteByte(0xbe);

}

intDs18b20ReadTemp()

{

inttemp=0;

uchartmh,tml;

Ds18b20ChangTemp();

Ds18b20ReadTempCom();

tml=Ds18b20ReadByte();

tmh=Ds18b20ReadByte();

temp=tmh;

temp<<=8;

temp|=tml;

returntemp;

}

voidLcdDisplay(inttemp)

unsignedchardatas[]={0,0,0,0,0};

unsignedcharalarm[]={0,0,0};

floattp;

if(temp<0)

{

write_com(0x80+0x4a);

write_data('-');

temp=temp-1;

temp=~temp;

tp=temp;

temp=tp*0.0625*100+0.5;

}

else

{

write_com(0x80+0x4a);

//write_data('+');

tp=temp;

temp=tp*0.0625*100+0.5;

}

datas[0]=temp/10000;

datas[1]=temp%10000/1000;

datas[2]=temp%1000/100;

datas[3]=temp%100/10;

datas[4]=temp%10;

write_com(0x80+0x0c);

write_data('0'+datas[1]);

write_data('0'+datas[2]);

write_data('.');

write_data('0'+datas[3]);

//write_data(0xdf);

//write_data('C');

}

voidmain()

{

uintt;

init();

while

(1)

{

write_com(0x80);

receive();

for(t=0;t<2;t++)

{

write_data(TX_Buffer[t]);

Delayms(5);

}

for(t=0;t<3;t++)

//write_data(table[t]);

Delayms(5);

write_com(0x80+0x04);

receive2();

for(t=0;t<2;t++)

{

write_data(TX_Buffer2[t]);

Delayms(5);

}

for(t=0;t<3;t++)

//write_data(table[t]);

Delayms(5);

write_com(0x80+0x08);

receive3();

for(t=0;t<2;t++)

{

write_data(TX_Buffer3[t]);

Delayms(5);

}

for(t=0;t<3;t++)

//write_data(table[t]);

Delayms(5);

LcdDisplay(Ds18b20ReadTemp());

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2