#include "DH11.h"
void DH11_Delay(unsigned int ns) //延时子函数
{
while(ns--);
}
unsigned char getData(unsigned char Data[5])
{
unsigned char i,j,k;
unsigned int overtime;
unsigned int t;
unsigned int threshold;
DH11 = 1;
//主机发送开始信号
DH11 = 0;
DH11_Delay(2047); // >= 18ms
DH11 = 1;//主机拉高数据线,>=20us
DH11_Delay(5);
overtime=65535;
while(DH11==1 && overtime>0) overtime--; //等待高电平结束,即等待 DHT 的低电平响应信号
if(overtime==0) return 1;
DH11_Delay(1); // >= 20us -- 40us
overtime=65535;
while(DH11==0 && overtime>0)overtime--;//等待低电平结束
if(overtime==0) return 2;
overtime=65535;
while(DH11==1 && overtime>0)overtime--; //等待高电平结束
if(overtime==0) return 3;
threshold=(65535-overtime);
threshold>>=1;
for(j=0;j<5;j++)
{
k=0;
for(i=0;i<8;i++)
{
t=0;
k=k<<1;
while(DH11==0 && t<0xffff) t++;//等待低电平结束
if(t==0xffff)
{
Data[0]=i;
return 4;
}
t=0;
while(DH11==1 && t<0xffff) t++; //等待高电平结束
if(t==0xffff)
{
Data[0]=i;
return 5;
}
if(t>=threshold)
k|=0x01;
}
Data[j]=k;
}
k=0;
for(i=0;i<4;i++)
{
k+=Data[i];
}
if(k!=Data[4])
{
return 6;
}
return 0;
}
因为protues没有DHT11,使用信号发生器pluse时用上面的程序,设置里面应该怎么设啊?

void DH11_Delay(unsigned int ns) //延时子函数
{
while(ns--);
}
unsigned char getData(unsigned char Data[5])
{
unsigned char i,j,k;
unsigned int overtime;
unsigned int t;
unsigned int threshold;
DH11 = 1;
//主机发送开始信号
DH11 = 0;
DH11_Delay(2047); // >= 18ms
DH11 = 1;//主机拉高数据线,>=20us
DH11_Delay(5);
overtime=65535;
while(DH11==1 && overtime>0) overtime--; //等待高电平结束,即等待 DHT 的低电平响应信号
if(overtime==0) return 1;
DH11_Delay(1); // >= 20us -- 40us
overtime=65535;
while(DH11==0 && overtime>0)overtime--;//等待低电平结束
if(overtime==0) return 2;
overtime=65535;
while(DH11==1 && overtime>0)overtime--; //等待高电平结束
if(overtime==0) return 3;
threshold=(65535-overtime);
threshold>>=1;
for(j=0;j<5;j++)
{
k=0;
for(i=0;i<8;i++)
{
t=0;
k=k<<1;
while(DH11==0 && t<0xffff) t++;//等待低电平结束
if(t==0xffff)
{
Data[0]=i;
return 4;
}
t=0;
while(DH11==1 && t<0xffff) t++; //等待高电平结束
if(t==0xffff)
{
Data[0]=i;
return 5;
}
if(t>=threshold)
k|=0x01;
}
Data[j]=k;
}
k=0;
for(i=0;i<4;i++)
{
k+=Data[i];
}
if(k!=Data[4])
{
return 6;
}
return 0;
}
因为protues没有DHT11,使用信号发生器pluse时用上面的程序,设置里面应该怎么设啊?

