unsigned char h_i = 0 , h_f = 0; unsigned char t_i = 0 , t_f = 0;
unsigned char check_sum = 0;
DHT11_OUT;
DHT11_L;
mdelay(30); //>18ms;
DHT11_H;
udelay(30);
DHT11_IN;
while(DHT11_STA == 1)
{
udelay(1); t_count ;
if(t_count > 50)
{
printk("device error: dht11!\n");
return 0;
}
}
t_count = 0;
while(!DHT11_STA)
{
udelay(1); t_count ;
if(t_count > 250)
{
printk("read_dht11 error1\n");
return 0;
}
}
t_count = 0;
udelay(50);
while(DHT11_STA)
{
udelay(1);
t_count ; if(t_count > 250)
{
printk("read_dht11 error2\n");
return 0;
}
}
h_i = read_byte();
h_f = read_byte();
t_i = read_byte();
t_f = read_byte(); check_sum = read_byte();
if(check_sum == (h_i h_f t_i t_f) || (h_i!=100 && t_i != 100))
{
dht11 = t_i;
dht11 <<= 8; //高 8 位用来放温度值;
dht11 = h_i; //低 8 位存放湿度值;
}
else
{
dht11 = 0;
printk("read_dht11 error3\n");
}
return dht11;
}
3. 应用程序
int main(int argc, char **argv)
{
int fd;
unsigned int dht11 = 0; unsigned int humi,temp;
//打开温度传感器驱动模块
fd = open("/dev/dht11", O_RDWR | O_NONBLOCK);
if (fd < 0)
{
printf("can't open /dev/dht11\n");
return -1;
}
read(fd, &dht11, sizeof(dht11));
temp = dht11>>8; //取出温度值;
humi = dht11 &0x000000ff; //取出湿度值; printf("the current temperature is: %d\n",temp); printf("the current humidity is: %d\n",humi);
close(fd);
return 0;
}
4. 实验结果