按照时序图改了很多次,但是总是输出为0,有没有大神能够帮忙解答一下
dht11.c
- #include "STC8A.h"
- #include "time111.h"
- #include <string.h>
- #include "stdio.h"
- #include "dht111.h"
- sbit dht11=P0^3;
- sbit led=P2^0;
- sbit led1=P2^1;
- unsigned char s[5]=0;
- unsigned char rec[5]=0;
- void start()
- {
-
- dht11=1;
- Delay1us();
- Delay1us();
- dht11=0;
- Delay18ms();
- dht11=1;
- Delay30us();
- //dht11=1;
- while(!dht11)
- {
- led=0;
- }
- }
- unsigned char readbyte()
- {
- unsigned char i,dat=0;
- for(i=0;i<8;i++) //从高到低依次接收8位数据
- {
- while(!dht11); //等待50us低电平过去
- Delay50us(); //延时60us,如果还为高则数据为1,否则为0
- dat<<=1; //移位使正确接收8位数据,数据为0时直接移位
- if(dht11==1) //数据为1时,使dat加1来接收数据1
- dat+=1;
- while(dht11); //等待数据线拉低
- }
- return dat;
- }
- void readall(unsigned char *rec)
- {
- unsigned char R_H,R_L,T_H,T_L,RH,RL,TH,TL,revise;
- start();
- if(start==1)led1=0;
- if(dht11==0)
- {
- while(dht11==0);//等待拉高
- Delay80us(); //拉高后延时80us
- R_H=readbyte(); //接收湿度高八位
- R_L=readbyte(); //接收湿度低八位
- T_H=readbyte(); //接收温度高八位
- T_L=readbyte(); //接收温度低八位
- revise=readbyte(); //接收校正位
- Delay25us(); //结束
- if((R_H+R_L+T_H+T_L)==revise) //校正
- {
-
- RH=R_H;
- RL=R_L;
- TH=T_H;
- TL=T_L;
- //led1=0;
- }
- rec[0]=(RH)/10;
- rec[1]=(RH)%10;
- rec[2]=(TH)/10;
- rec[3]=(TH)%10;
-
- }
-
- }
复制代码 main.c
- #include "STC8A.h"
- #include <intrins.h>
- #include <string.h>
- #include "stdio.h"
- #include "Uart.h"
- #include "time111.h"
- #include "dht111.h"
- extern unsigned char s[5];
- sbit led1=P2^0;
- void main()
- {
-
- Uart1Init();
- EA=1;//开启总中断
-
- while(1)
- {
- char send[2]={'O','k'};
- char dht11_data[4]=0;
- start();
-
- send_string(send);
-
- readall(dht11_data);
- sprintf(s,"%d",dht11_data);
- send_string(s);
- Delay2000ms();
- }
- }
复制代码 dht11时序图
|