Subversion Repositories HomeAutomation

Rev

Rev 1895 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /************************************************************************/
  2. /*                                                                      */
  3. /*        Access DHT11 1-Wire Device with ATMEL AVRs                    */
  4. /*                                                                      */
  5. /*              Author: Peter Dannegger                                 */
  6. /*                      danni@specs.de                                  */
  7. /*                                                                      */
  8. /* modified by Martin Thomas <eversmith@heizung-thomas.de> 9/2004       */
  9. /* modified by Linus Lundin from Dallas 1-wire to DHT11 protocol        */
  10. /************************************************************************/
  11.  
  12. #include <avr/io.h>
  13. #include <avr/interrupt.h>
  14.  
  15. #include <drivers/sensor/ds18s20/delay.h>
  16. #include "DHT11.h"
  17.  
  18. #include <config.h>
  19.  
  20. #ifdef DHT11_ONE_BUS
  21.  
  22. #define DHT11_GET_IN()   gpio_get_state(DHT11_PIN)
  23. #define DHT11_OUT_LOW()  gpio_clr_pin(DHT11_PIN)
  24. #define DHT11_OUT_HIGH() gpio_set_pin(DHT11_PIN)
  25. #define DHT11_DIR_IN()   gpio_set_in(DHT11_PIN)
  26. #define DHT11_DIR_OUT()  gpio_set_out(DHT11_PIN)
  27.  
  28.  
  29. #else
  30.  
  31. /* set bus-config with dht11_set_bus() */
  32. uint8_t DHT11_PIN_MASK;
  33. volatile uint8_t* DHT11_IN;
  34. volatile uint8_t* DHT11_OUT;
  35. volatile uint8_t* DHT11_DDR;
  36.  
  37. #define DHT11_GET_IN()  ( *DHT11_IN & DHT11_PIN_MASK )
  38. #define DHT11_OUT_LOW() ( *DHT11_OUT &= (uint8_t) ~DHT11_PIN_MASK )
  39. #define DHT11OUT_HIGH() ( *DHT11_OUT |= (uint8_t)  DHT11_PIN_MASK )
  40. #define DHT11DIR_IN()   ( *DHT11_DDR &= (uint8_t) ~DHT11_PIN_MASK )
  41. #define DHT11DIR_OUT()  ( *DHT11_DDR |= (uint8_t)  DHT11_PIN_MASK )
  42.  
  43. void dht11_set_bus(volatile uint8_t* in,
  44.     volatile uint8_t* out,
  45.     volatile uint8_t* ddr,
  46.     uint8_t pin)
  47. {
  48.     DHT11_DDR=ddr;
  49.     DHT11_OUT=out;
  50.     DHT11_IN=in;
  51.     DHT11_PIN_MASK=(1<<pin);
  52.     dht11_reset();
  53. }
  54. #endif
  55.  
  56. uint8_t dht11_readData(uint8_t* word)
  57. {
  58.     // set Pin as input - wait for clients to pull low
  59.     DHT11_DIR_IN(); // input
  60.    
  61.     delay_us(80);
  62.     if (DHT11_GET_IN())     // no presence detect
  63.     {
  64.         //printf("Fel 1\n");
  65.         return 0;
  66.     }
  67.     // nobody pulled to low, still high
  68.    
  69.     // after a delay the clients should release the line
  70.     // and input-pin gets back to high due to pull-up-resistor
  71.     delay_us(80);
  72.     if (!DHT11_GET_IN())        // no presence detect
  73.     {
  74.         //printf("Fel 2\n");
  75.         return 0;
  76.     }
  77.     while (DHT11_GET_IN());
  78.     delay_us(30);
  79.     while (!DHT11_GET_IN());
  80.     uint8_t wordcounter = 0;
  81.     uint8_t bitcounter = 0;
  82.     delay_us(50);
  83.     for (wordcounter = 0; wordcounter < 5; wordcounter++) {
  84.         for (bitcounter = 0; bitcounter < 8; bitcounter++) {
  85.             word[wordcounter] = word[wordcounter] << 1;
  86.             if (DHT11_GET_IN()) {
  87.                 word[wordcounter] |= 1;
  88.                 while (DHT11_GET_IN());
  89.             } else {
  90.                 word[wordcounter] &= 0xfe;
  91.             }
  92.             while (!DHT11_GET_IN());
  93.             delay_us(50);
  94.         }
  95.     }
  96.     if (word[0]+word[1]+word[2]+word[3] != word[4]) {
  97.         return 0;
  98.     }
  99.     //printf("Data was %x %x %x %x %x\n", word[0], word[1], word[2], word[3], word[4]);
  100.     return 1;
  101. }
  102.  
  103.  
  104. uint8_t dht11_start(void)
  105. {
  106.     DHT11_OUT_LOW(); // disable internal pull-up (maybe on from parasite)
  107.     DHT11_DIR_OUT(); // pull OW-Pin low for 18ms
  108.     return 1;
  109. }
  110.