Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- while(GPIO_READ_PIN(pin)) //wait sensor pull down
- usleep(1);
- #define GPIO_DIR_INPUT(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
- #define GPIO_DIR_OUTPUT(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
- #define GPIO_READ_PIN(g) (*(gpio+13) & (1<<(g))) && 1
- #define GPIO_SET_PIN(g) *(gpio+7) = 1<<g;
- #define GPIO_CLEAR_PIN(g) *(gpio+10) = 1<<g;
- uint64_t readDHT(int pin) {
- // Set GPIO pin to output
- GPIO_DIR_OUTPUT(pin); // set pin output
- GPIO_CLEAR_PIN(pin); // Set low
- usleep(10000); // DHT22 needs min 1-10 ms to signal a startup
- GPIO_SET_PIN(pin); // Take pin high
- usleep(40);
- /*------------------sensor response --------------------*/
- GPIO_DIR_INPUT(pin); // set pin input
- usleep(80); //sensor pull low 80 us
- usleep(80); // sensor pull up 80 us
- while(GPIO_READ_PIN(pin)); // wait till sensor pull down (ready to transmit data)
- /*--------------------- begin transmit data ------------*/
- uint64_t measure = 0; // store the 40 bits
- uint8_t i;
- // start to read 40 bits
- for (i = 0; i < 40; i++) {
- while(GPIO_READ_PIN(pin) == 0) // wait till sensor pull up (ready to transmit bit)
- usleep(1);
- usleep(40); //wait 40us if still high then bit = 1 else bit = 0 because output=1 still 70 us
- if (GPIO_READ_PIN(pin))
- {
- measure = measure | (1 << i);
- while(GPIO_READ_PIN(pin)) //wait sensor pull down
- usleep(1);
- }
- }
- return measure;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement