Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DHT22 by
DHT22.cpp
00001 #include "DHT22.h" 00002 00003 00004 DHT22::DHT22(PinName pin) { 00005 _data_pin = pin; 00006 } 00007 00008 int DHT22::getTemperature() { 00009 return _temperature; 00010 } 00011 00012 int DHT22::getHumidity() { 00013 return _humidity; 00014 } 00015 00016 bool DHT22::sample() { 00017 DigitalInOut DHT22(_data_pin); 00018 int dht22_dat [5]; 00019 00020 DHT22.output(); 00021 DHT22.write(0); 00022 wait_ms(18); 00023 00024 DHT22.write(1); 00025 DHT22.input(); 00026 wait_us(40); 00027 wait_us(80); 00028 int i,j,result=0; 00029 00030 for (i=0; i<5; i++) { 00031 result=0; 00032 for (j=0; j<8; j++) { 00033 while (DHT22); 00034 while (!DHT22); 00035 wait_us(50); 00036 int p; 00037 p=DHT22; 00038 p=p <<(7-j); 00039 result=result|p; 00040 } 00041 dht22_dat[i] = result; 00042 } 00043 int dht22_check_sum; 00044 dht22_check_sum=dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3]; 00045 dht22_check_sum= dht22_check_sum%256; 00046 if (dht22_check_sum==dht22_dat[4]) { 00047 _humidity=dht22_dat[0]*256+dht22_dat[1]; 00048 _temperature=dht22_dat[2]*256+dht22_dat[3]; 00049 return true; 00050 } 00051 return false; 00052 }
Generated on Mon Jul 18 2022 10:02:02 by
1.7.2
