使用wiringpi编写C程序:
如下程序借鉴了网上一老外的程序,忘了原帖地址在哪。
#include
#include
#include
#include
#define MAX_TIME 85
#define DHT11PIN 7
#define ATTEMPTS 5 //retry 5 times when no response
int dht11_val[5]={0,0,0,0,0};
int dht11_read_val(){
uint8_t lststate=HIGH; //last state
uint8_t counter=0;
uint8_t j=0,i;
for(i=0;i<5;i++)
dht11_val[i]=0;
//host send start signal
pinMode(DHT11PIN,OUTPUT); //set pin to output
digitalWrite(DHT11PIN,LOW); //set to low at least 18ms
delay(18);
digitalWrite(DHT11PIN,HIGH); //set to high 20-40us
delayMicroseconds(40);
//start recieve dht response
pinMode(DHT11PIN,INPUT); //set pin to input
for(i=0;i
{
counter=0;
while(digitalRead(DHT11PIN)==lststate){ //read pin state to see if dht responsed. if dht always high for 255 + 1 times, break this while circle
counter++;
delayMicroseconds(1);
if(counter==255)
break;
}
lststate=digitalRead(DHT11PIN); //read current state and store as last state.
if(counter==255) //if dht always high for 255 + 1 times, break this for circle
break;
// top 3 transistions are ignored, maybe aim to wait for dht finish response signal
if((i>=4)&&(i%2==0)){
dht11_val[j/8]<<=1; //write 1 bit to 0 by moving left (auto add 0)
if(counter>16) //long mean 1
dht11_val[j/8]|=1; //write 1 bit to 1
j++;
}
}
// verify checksum and print the verified data
if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF))){
printf("RH:%d,TEMP:%d\n",dht11_val[0],dht11_val[2]);
return 1;
}
else
return 0;
}
int main(void){
int attempts=ATTEMPTS;
if(wiringPiSetup()==-1)
exit(1);
while(attempts){ //you have 5 times to retry
int success = dht11_read_val(); //get result including printing out
if (success) { //if get result, quit program; if not, retry 5 times then quit
break;
}
attempts--;
delay(2500);
}
return 0;
} 上述程序保存为.c文件后编译成可执行文件,运行后会在屏幕打印温度和湿度。
程序中的数据接收处理部分细节如下:
if((i>=4)&&(i%2==0)){ //前3次分别是:1低电平,2高电平(即响应信号),3低电平(即数据第一个低电平)
//i%2==0 是因为每次都是循环读取低电平和高电平,每次要循环2次才读出一个bit处理
dht11_val[j/8]<<=1; //读到后,j/8可以限制一个数的8个位,左移1位自动补0,相当于读出0
if(counter>16) //counter计数如果超过16,则高电平长,应读1.
dht11_val[j/8]|=1; //故再将上面数与1位或,使最后一位变成1
j++; //j++8个换成下一个数据
}
if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF))){
//这其中(dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF)是将5个数相加,和1与。
//目的是防止读出数据都为0,和为0,0和1与后得0,所以if判断条件不成立,返回读取失败码。
//如果读出数据是不为0的正常数据,和1与后还得原数。