单片机温湿度检测.docx
《单片机温湿度检测.docx》由会员分享,可在线阅读,更多相关《单片机温湿度检测.docx(17页珍藏版)》请在冰点文库上搜索。
![单片机温湿度检测.docx](https://file1.bingdoc.com/fileroot1/2023-6/26/eee7c9d6-e4de-4314-b000-e6d15dde1239/eee7c9d6-e4de-4314-b000-e6d15dde12391.gif)
单片机温湿度检测
#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());
}
}