1、基于51单片机的DHT11温湿度传感器基于51单片机的DHT11温湿度传感器#include#include#define uchar unsigned char#define uint unsigned intsbit dth=P10;sbit dula=P26;sbit wela=P27;uchar s8h,s8h_temp,s8l,s8l_temp,w8h,w8h_temp,w8l,w8l_temp,check8,check8_temp,flag=0;float sd,wd;uchar code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
2、0x7f,0x6f,0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef;void delayms(uint z)uint x,y;for(x=z;x0;x-)for(y=110;y0;y-);void Delay_10us() /进入函数3us uchar i; /每条语句1us i-; i-; i-; i-; i-; i-;void display(uchar th,uchar tl)uchar ih=0,il=0,jh=0,jl=0,kh=0,kl=0;ih=th/100;jh=th%100/10;kh=th%10;il=tl/100;jl=
3、tl%100/10;kl=tl%10;P0=0xfe;wela=1;wela=0;P0=tableih;dula=1;dula=0; delayms(2);P0=0xfd;wela=1;wela=0;P0=tablejh;dula=1;dula=0;delayms(2);P0=0xfb;wela=1;wela=0;P0=tablekh+10;dula=1;dula=0;delayms(2);P0=0xf7;wela=1;wela=0;P0=tableil;dula=1;dula=0; delayms(2);P0=0xef;wela=1;wela=0;P0=tablejl;dula=1;dula
4、=0;delayms(2);P0=0xdf;wela=1;wela=0;P0=tablekl;dula=1;dula=0;delayms(2); char com()uchar i,j,temp,cdata=0;for(i=0;i8;i+)flag=2;while(!dth)&(flag+); /等待拉高/Delay_10us();/Delay_10us();/Delay_10us();j=3;while(j-);/延时30ustemp=0;if(dth=1)temp=1;flag=2;while(dth&(flag+);/等待拉低/if(flag=1)break;cdata=1;cdata=
5、cdata|temp;return cdata;void getdth()uchar i;dth=0;delayms(18); /18msdth=1;/Delay_10us();/Delay_10us();/Delay_10us();/Delay_10us();i=4;while(i-); /延时40usdth=1;if(dth!=1) /dth11响应flag=2;while(!dth)&(flag+); /等待拉高flag=2;while(dth&(flag+);/等待读入数据s8h_temp=com();s8l_temp=com();w8h_temp=com();w8l_temp=com();check8_temp=com();dth=1;if(s8h_temp+s8l_temp+w8h_temp+w8l_temp)=check8_temp) /校验 s8h=s8h_temp;s8l=s8l_temp;w8h=w8h_temp;w8l=w8l_temp;check8=check8_temp;sd=s8h+(float)s8l/256;wd=w8h+(float)w8l/256;void main()while(1)getdth();display(s8h,w8h);