Advertisement
Guest User

Untitled

a guest
Jul 13th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. while(GPIO_READ_PIN(pin)) //wait sensor pull down
  2. usleep(1);
  3.  
  4. #define GPIO_DIR_INPUT(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  5. #define GPIO_DIR_OUTPUT(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
  6. #define GPIO_READ_PIN(g) (*(gpio+13) & (1<<(g))) && 1
  7. #define GPIO_SET_PIN(g) *(gpio+7) = 1<<g;
  8. #define GPIO_CLEAR_PIN(g) *(gpio+10) = 1<<g;
  9.  
  10. uint64_t readDHT(int pin) {
  11.  
  12. // Set GPIO pin to output
  13. GPIO_DIR_OUTPUT(pin); // set pin output
  14. GPIO_CLEAR_PIN(pin); // Set low
  15. usleep(10000); // DHT22 needs min 1-10 ms to signal a startup
  16. GPIO_SET_PIN(pin); // Take pin high
  17. usleep(40);
  18.  
  19. /*------------------sensor response --------------------*/
  20. GPIO_DIR_INPUT(pin); // set pin input
  21. usleep(80); //sensor pull low 80 us
  22. usleep(80); // sensor pull up 80 us
  23. while(GPIO_READ_PIN(pin)); // wait till sensor pull down (ready to transmit data)
  24.  
  25. /*--------------------- begin transmit data ------------*/
  26.  
  27. uint64_t measure = 0; // store the 40 bits
  28. uint8_t i;
  29.  
  30. // start to read 40 bits
  31. for (i = 0; i < 40; i++) {
  32. while(GPIO_READ_PIN(pin) == 0) // wait till sensor pull up (ready to transmit bit)
  33. usleep(1);
  34.  
  35. usleep(40); //wait 40us if still high then bit = 1 else bit = 0 because output=1 still 70 us
  36. if (GPIO_READ_PIN(pin))
  37. {
  38. measure = measure | (1 << i);
  39. while(GPIO_READ_PIN(pin)) //wait sensor pull down
  40. usleep(1);
  41. }
  42. }
  43. return measure;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement