Advertisement
saper_2

dht11.c

Nov 27th, 2012
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.17 KB | None | 0 0
  1. /* ****************************************************************************
  2.    *                                                                          *
  3.    * Name    : DHT11 Temperature and humidity digital sensor                  *
  4.    *            (cheap version of Sensirion SHT11)                            *
  5.    * Author  : saper_2 / nolin11                                              *
  6.    * Contact : nolinno11@gmail.com                                            *
  7.    * Date    : 2012.08.28                                                     *
  8.    * Version : 0.1                                                            *
  9.    * License : You are free to use this library in any kind project, but at   *
  10.    *           least send me a info that YOU are using it. I'll be happy      *
  11.    *           for memory :)                                                  *
  12.    * About   :                                                                *
  13.    *           What to write? Name gives what is this :)                      *
  14.    *                                                                          *
  15.    * Bugs    : nothing so far...                                              *
  16.    *                                                                          *
  17.    **************************************************************************** */
  18.    
  19. #include <avr/io.h>
  20. #include <inttypes.h>
  21. #include <avr/pgmspace.h>
  22. #include <util/delay.h>
  23. #include <avr/interrupt.h>
  24. //#include <avr/power.h>
  25. //#include <avr/eeprom.h>
  26. //#include <math.h>
  27.  
  28. #include "dht11.h"
  29. #include "delay.h"
  30.  
  31.  
  32. #define DHT_DEBUG 0
  33.  
  34. #if (DHT_DEBUG == 1)
  35. #include "usart.h"
  36. #endif
  37.  
  38. void dht11_init(void) {
  39.     // pull-up, input
  40.     dht_data_in;
  41.     // configure interrupt
  42.     #if (DHT_DATA_INT_SOURCE == DHT_DATA_INT_SOURCE_NOT_USED)
  43.         // nothing to do :)
  44.     #elif (DHT_DATA_INT_SOURCE == DHT_DATA_INT_SOURCE_INT0)
  45.    
  46.     #elif (DHT_DATA_INT_SOURCE == DHT_DATA_INT_SOURCE_INT1)
  47.    
  48.     #else
  49.         #error "Interrupt souce not supported!"
  50.     #endif
  51. }
  52.  
  53.  
  54.  
  55. uint8_t dht11_read(uint8_t* data) {
  56. //uint8_t dht11_read(void) {
  57.     uint8_t tout=0, tmp=0, csm=0;
  58.     // reset structure
  59.     for (uint8_t i=0;i<4;i++) *(data+i)=0;
  60.     *(data+4)=0xaa;
  61.    
  62.     // ---- start ----
  63.         // pull down data for at least 18ms
  64.         dht_data_lo;
  65.         delay1ms(25);
  66.         // pull up and wait about 40us
  67.         dht_data_hi;
  68.         tout=0;
  69.         while (dht_get_data > 0) {
  70.             tout++;
  71.             delay1us(1);
  72.             if (tout>100) return DHT11_READ_ERROR_START;  // return error-start
  73.         }
  74.         tout=0;
  75.         // now dht11 pulls down data for at least 80us
  76.         while (dht_get_data < 1) {
  77.             tout++;
  78.             delay1us(1);
  79.             if (tout>120) return DHT11_READ_ERROR_ACK;  // return error-ack start
  80.         }
  81.         // now dht release data line for at least 80us...
  82.         tout=0;
  83.         while (dht_get_data > 0) {
  84.             tout++;
  85.             delay1us(1);
  86.             if (tout>120) return DHT11_READ_ERROR_ACK2;  // return error-ack start
  87.         }
  88.     // ---- data , 8bit from MSB to LSB: RH integral, RH decimal, TEMP integral, TEMP decimal, CSM (sum of all 4 bytes)
  89.     // ---- each data bit starts with 50us data pulled down and then released, release time determinates 0 or 1 in rule:
  90.     // ----   26-28us is 0 , ~70us is "1"
  91.     #if (DHT_DEBUG != 0)
  92.         uint8_t dta[5] = {0,0,0,0,0};
  93.         uint8_t touts1[40] = {0,0,0,0,0};
  94.         uint8_t touts2[40] = {0,0,0,0,0};
  95.         uint8_t touts_cnt=0;
  96.     #endif
  97.     for (uint8_t j=0;j<5;j++) {
  98.         tmp=0;
  99.         for (uint8_t i=0;i<8;i++) {
  100.             tmp <<= 1;
  101.             // pull down - 50us
  102.             tout=0;
  103.             while (dht_get_data < 1) {
  104.                 tout++;
  105.                 delay1us(1);
  106.                 if (tout>120) return DHT11_READ_ERROR_TIME;  // return error-ack start
  107.             }
  108.             #if (DHT_DEBUG != 0)
  109.                 touts1[touts_cnt] = tout;
  110.             #endif
  111.             // pull up: ~26-28us=0 , ~70us=1
  112.             tout=0;
  113.             while (dht_get_data > 0) {
  114.                 tout++;
  115.                 delay1us(1);
  116.                 if (tout>150) return DHT11_READ_ERROR_TIME2;  // return error-ack start
  117.             }
  118.             #if (DHT_DEBUG != 0)
  119.                 touts2[touts_cnt] = tout;
  120.             #endif
  121.             // 0
  122.             if ( (tout > 3) && (tout < 10) ) {
  123.                
  124.             }
  125.             // 1
  126.             if ( tout > 12 ) {
  127.                 tmp |= 0x01;
  128.             }
  129.             #if (DHT_DEBUG != 0)
  130.                 touts_cnt++;
  131.             #endif
  132.         }
  133.         #if (DHT_DEBUG != 0)
  134.             dta[j]=tmp;
  135.         #endif
  136.         *(data+j) = tmp;
  137.         // do not include into checksum checksum :)
  138.         if (j < 4) csm = csm + tmp;
  139.     }
  140.     #if (DHT_DEBUG != 0)
  141.         usartSendStrP((prog_char*)PSTR("\r\n\r\n"));
  142.        
  143.         usartSendStrP((prog_char*)PSTR("DATA[0..4]="));
  144.         for (uint8_t j=0;j<5;j++) {
  145.             usartSendHexByte(dta[j]);
  146.             usartSendChar(' ');
  147.            
  148.         }
  149.        
  150.         usartSendStrP((prog_char*)PSTR(" = DEC{ "));
  151.         for (uint8_t j=0;j<5;j++) {
  152.             usartSendDec(dta[j]);
  153.             usartSendChar(',');
  154.             usartSendChar(' ');
  155.            
  156.         }
  157.         usartSendStrP((prog_char*)PSTR("}"));
  158.        
  159.         usartSendStrP((prog_char*)PSTR("\r\n" "TOUT_LOW_BIT counters:  "));
  160.         for (uint8_t j=0;j<40;j++) {
  161.             if (touts1[j] < 10) usartSendChar('0');
  162.             usartSendDec(touts1[j]);
  163.             usartSendChar(' ');
  164.         }
  165.        
  166.         usartSendStrP((prog_char*)PSTR("\r\n" "TOUT_HIGH_BIT counters: "));
  167.         for (uint8_t j=0;j<40;j++) {
  168.         if (touts2[j] < 10) usartSendChar('0');
  169.             usartSendDec(touts2[j]);
  170.             usartSendChar(' ');
  171.         }
  172.         usartSendStrP((prog_char*)PSTR("\r\n"));
  173.     #endif
  174.     if (*(data+4) == csm) return DHT11_READ_OK;
  175.     return DHT11_READ_ERROR_CSM;
  176.     //return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement