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.
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 DHT22.output(); 00020 DHT22.write(0); 00021 wait_ms(18); 00022 DHT22.write(1); 00023 DHT22.input(); 00024 wait_us(40); 00025 wait_us(80); 00026 int i,j,result=0; 00027 for (i=0; i<5; i++) { 00028 result=0; 00029 for (j=0; j<8; j++) { 00030 while (DHT22); 00031 while (!DHT22); 00032 wait_us(50); 00033 int p; 00034 p=DHT22; 00035 p=p <<(7-j); 00036 result=result|p; 00037 } 00038 dht22_dat[i] = result; 00039 } 00040 int dht22_check_sum; 00041 dht22_check_sum=dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3]; 00042 dht22_check_sum= dht22_check_sum%256; 00043 if (dht22_check_sum==dht22_dat[4]) { 00044 _humidity=dht22_dat[0]*256+dht22_dat[1]; 00045 _temperature=dht22_dat[2]*256+dht22_dat[3]; 00046 return true; 00047 } 00048 return false; 00049 }
Generated on Sun Aug 7 2022 04:19:43 by
1.7.2