Subversion Repositories HomeAutomation

Rev

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