Advertisement
Guest User

dht11.c

a guest
Nov 23rd, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <wiringPi.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <stdint.h>  
  5. #define MAX_TIME 85  
  6. #define DHT11PIN 7  
  7. int dht11_val[5]={0,0,0,0,0};  
  8.  
  9. void dht11_read_val()  
  10. {  
  11.   uint8_t lststate=HIGH;  
  12.   uint8_t counter=0;  
  13.   uint8_t j=0,i;  
  14.   for(i=0;i<5;i++)  
  15.      dht11_val[i]=0;  
  16.   pinMode(DHT11PIN,OUTPUT);  
  17.   digitalWrite(DHT11PIN,LOW);  
  18.   delay(18);  
  19.   digitalWrite(DHT11PIN,HIGH);  
  20.   delayMicroseconds(40);  
  21.   pinMode(DHT11PIN,INPUT);  
  22.   for(i=0;i<MAX_TIME;i++)  
  23.   {  
  24.     counter=0;  
  25.     while(digitalRead(DHT11PIN)==lststate){  
  26.       counter++;  
  27.       delayMicroseconds(1);  
  28.       if(counter==255)  
  29.         break;  
  30.     }  
  31.     lststate=digitalRead(DHT11PIN);  
  32.     if(counter==255)  
  33.        break;  
  34.     // top 3 transistions are ignored  
  35.     if((i>=4)&&(i%2==0)){  
  36.       dht11_val[j/8]<<=1;  
  37.       if(counter>16)  
  38.         dht11_val[j/8]|=1;  
  39.       j++;  
  40.     }  
  41.   }  
  42.   // verify cheksum and print the verified data  
  43.   if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF)))  
  44.   {  
  45.     printf("Humidity = %d%d %% Temperature = %d%d °C\n",dht11_val[0],dht11_val[1],dht11_val[2],dht11_val[3]);  
  46.     fflush(stdout);
  47.   }  
  48.   else  
  49.     printf("Invalid Data!!\n");  
  50. }  
  51.  
  52. int main(void)  
  53. {  
  54.   printf("Interfacing Temperature and Humidity Sensor (DHT11) With Banana Pi\n");  
  55.   if(wiringPiSetup()==-1)  
  56.     exit(1);  
  57.   while(1)  
  58.   {  
  59.      dht11_read_val();  
  60.      delay(7500);  
  61.   }  
  62.   return 0;  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement