-
-
Save gacha/1d6d9d0f4a6ea6826e9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <wiringPi.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <stdbool.h> | |
#include "print_bits.c" | |
#define DHT_PIN 7 | |
#define DHT_TIMEOUT 200 | |
bool sleepUntil(int pin, uint8_t state){ | |
int counter = 0; | |
while(digitalRead(pin) != state){ | |
counter++; | |
delayMicroseconds(1); | |
if(counter > DHT_TIMEOUT) return false; | |
} | |
return true; | |
} | |
void readData(){ | |
uint8_t data[5] = {0,0,0,0,0}; | |
// MCU says hello | |
pinMode(DHT_PIN, OUTPUT); | |
digitalWrite(DHT_PIN, HIGH); | |
delay(10); | |
digitalWrite(DHT_PIN, LOW); | |
delay(18); | |
digitalWrite(DHT_PIN, HIGH); | |
delayMicroseconds(40); | |
// DHT responds | |
pinMode(DHT_PIN, INPUT); | |
if(!sleepUntil(DHT_PIN, HIGH)) return; | |
if(!sleepUntil(DHT_PIN, LOW)) return; | |
// Read data | |
int i; | |
for(i=0; i<40; i++){ | |
// ~50us | |
if(!sleepUntil(DHT_PIN, HIGH)) return; | |
uint32_t t = micros(); | |
if(!sleepUntil(DHT_PIN, LOW)) return; | |
// > ~40us then HIGH bit | |
data[i / 8] <<= 1; | |
if(micros() - t >= 68){ | |
data[i / 8] |= 1; // turn on current bit | |
} | |
//if(i > 0 && i % 8 == 0) printBits(&data[(i / 8)-1]); | |
} | |
//printBits(&data[0]); | |
//printBits(&data[1]); | |
//printBits(&data[2]); | |
//printBits(&data[3]); | |
//printBits(&data[4]); | |
if(data[4] == (data[0] + data[1] + data[2] + data[3]) & 0xFF){ | |
printf("0: %i\n", data[0]); | |
printf("1: %i\n", data[1]); | |
printf("2: %i\n", data[2]); | |
printf("3: %i\n", data[3]); | |
printf("4: %i\n", data[4]); | |
} | |
} | |
int main(int argc, const char* argv[]){ | |
wiringPiSetup(); | |
for(;;) { | |
delay(1000); | |
printf("Reading: \n"); | |
readData(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
00000001
11101001
00000000
11010101
10011110