资源描述:
《DHT11温湿度传感器驱动程序-C51单片机.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、/**************DHT11简单应用显示在数码管上(我的数码管是两个74HC595不懂的可以问我)*********************************建议显示在液晶上******************/#include#includecharT_H1,T_L1,RH_H1,RH_L1,checkdata1;charT_H2,T_L2,RH_H2,RH_L2,checkdata2;charF16T,F16RH,tshi,tge,rhshi,rhge;sbitDHT11=P2^1;
2、ucharbdataoutput;ucharlocation,j,i,ge,shi;uintnum=0,num1,num2;ucharcodesegment[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,/*共阴7段LED段码表*/0x7c,0x39,0x5e,0x79,0x71,0x76,0x73,0x3e,0x00};sbitsrclk=P4^2;sbitrclk=P4^1;sbitser=P4^4;sbitout_put=output^7;/*传感器接受数据的响应命令*
3、/voiddelay(uintx);voidDelay32us();voidDelay22ms();voidDelay500ms();voidshumaguan(ucharwei,ucharduan){output=wei;for(j=0;j<8;j++)//位码{ser=out_put;srclk=0;srclk=1;srclk=0;output=_crol_(output,1);}output=duan;//段码for(j=0;j<8;j++){ser=out_put;srclk=0;srclk=1;srclk=0;output=outpu
4、t<<1;}rclk=0;rclk=1;rclk=0;}voidshuru(){num=0;while(num<1000){shumaguan(0x01,~segment[tshi]);shumaguan(0x02,~segment[tge]);shumaguan(0x10,~segment[rhshi]);shumaguan(0x20,~segment[rhge]);shumaguan(0,~segment[19]);}}charCOM(void){chari,temp,comdata;/**判断信号时0还是1**/for(i=0;i<8;i
5、++){while(!DHT11);Delay32us();temp=0;if(DHT11){temp=1;}//当DHT11变为低电平时,开始下bit的传送comdata=comdata<<1;comdata=comdata
6、temp;while(DHT11);}returncomdata;}/***主机的开始信号和从机的响应信号***//***********DHT11的主函数*********************/voidgetDHT11(){DHT11=0;Delay22ms();//主机拉低22msDHT11=1;Delay32u
7、s();//总线由上拉电阻拉高主机延时20-40us//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行if(!DHT11)//T!{//判断从机是否发出80us的低电平响应信号是否结束while(!DHT11);//判断从机是否发出80us的高电平,如发出则进入数据接收状态while(DHT11);/*****接收数据的命令每次传送八位******/RH_H1=COM();RH_L1=COM();T_H1=COM();T_L1=COM();checkdata1=COM();if(T_H1+T_L1+RH_H1+RH_L1==chec
8、kdata1){RH_H2=RH_H1;RH_L2=RH_L1;T_H2=T_H1;T_L2=T_L1;F16RH=RH_H2+RH_L2/1000;F16T=T_H2+T_L2/1000;tshi=F16T/10;tge=F16T%10;rhshi=F16RH/10;rhge=F16RH%10;}}}/************主函数*************/voidmain(){EA=1;ET0=1;TR0=1;TMOD=0X01;TH0=(65536-1000)/256;TL0=(65536-1000)%256;while(1){DHT1
9、1=0;getDHT11();shuru();}}/**********延时子程序************************/voidtime1