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