DHT22 --- Temperature & Humidity Sensor
Fork of DHT22 by
Revision 1:8b95fd076a28, committed 2016-05-23
- Comitter:
- chiang404
- Date:
- Mon May 23 11:12:15 2016 +0000
- Parent:
- 0:7fd3ff04ae95
- Commit message:
- DHT22 --- Temperature & Humidity Sensor
Changed in this revision
DHT22.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/DHT22.cpp Thu Oct 27 15:11:06 2011 +0000 +++ b/DHT22.cpp Mon May 23 11:12:15 2016 +0000 @@ -1,8 +1,12 @@ #include "DHT22.h" +#define timeout 200 DHT22::DHT22(PinName pin) { _data_pin = pin; + DigitalInOut dht(_data_pin); + dht.output(); + dht.write(1); } int DHT22::getTemperature() { @@ -14,36 +18,48 @@ } bool DHT22::sample() { - DigitalInOut DHT22(_data_pin); + __disable_irq(); // Disable Interrupts + Timer t; + DigitalInOut dht(_data_pin); int dht22_dat [5]; - DHT22.output(); - DHT22.write(0); - wait_ms(18); - DHT22.write(1); - DHT22.input(); + dht.output(); + dht.write(0); + wait_ms(1); + dht.write(1); + dht.input(); wait_us(40); wait_us(80); int i,j,result=0; for (i=0; i<5; i++) { - result=0; + result=0; for (j=0; j<8; j++) { - while (DHT22); - while (!DHT22); - wait_us(50); + while(dht.read()); + while(!dht.read()); + t.reset(); + t.start(); + while(dht.read()); + t.stop(); int p; - p=DHT22; + if(t.read_us()>50) + p = 1; + else + p = 0; p=p <<(7-j); result=result|p; } dht22_dat[i] = result; } + dht.output(); + dht.write(1); int dht22_check_sum; dht22_check_sum=dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3]; dht22_check_sum= dht22_check_sum%256; if (dht22_check_sum==dht22_dat[4]) { _humidity=dht22_dat[0]*256+dht22_dat[1]; _temperature=dht22_dat[2]*256+dht22_dat[3]; + __enable_irq(); return true; } + __enable_irq(); return false; }