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.
lib_SHT25.cpp
00001 /** SHT25 class 00002 * 00003 * @purpose library for SHT25 humidity and temperature sensor 00004 * 00005 * Use to get temperature and humidity 00006 * 00007 * https://www.sensirion.com/products/humidity-sensor/ 00008 * 00009 * Example: 00010 * @code 00011 * #include "lib_SHT25.h" 00012 * 00013 * SHT25 sensor(I2C_SDA, I2C_SCL); 00014 * 00015 * int main() 00016 * { 00017 * while(1) 00018 * { 00019 * sensor.waitSafeHeat(); 00020 * float temperature = sensor.getTemperature(), humidity = sensor.getHumidity(); 00021 * printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity); 00022 * sensor.waitSafeHeat(); 00023 * sensor.getData(&temperature, &humidity); 00024 * printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity); 00025 * } 00026 * } 00027 * @endcode 00028 * @file lib_SHT25.cpp 00029 * @date Jun 2018 00030 * @author Yannic Simon 00031 */ 00032 #include "lib_SHT25.h" 00033 00034 SHT25::SHT25(PinName sda, PinName scl, enum_sht_prec precision, int frequency) : _i2c(sda, scl) 00035 { 00036 _i2c.frequency((frequency<=400e3)?frequency:400e3); 00037 setPrecision(precision); 00038 _temperature = _humidity = NAN; 00039 _selfHeatTemperature = _selfHeatHumidity = false; 00040 _t.attach(callback(this, &SHT25::keepSafeTemperature), SHT_SELF_HEATING); 00041 _h.attach(callback(this, &SHT25::keepSafeHumidity), SHT_SELF_HEATING); 00042 } 00043 00044 void SHT25::getData(float *tempC, float *relHumidity) 00045 { 00046 if(_selfHeatTemperature && _selfHeatHumidity) readData(); 00047 *tempC = _temperature; 00048 *relHumidity = _humidity; 00049 } 00050 00051 void SHT25::readData(void) 00052 { 00053 _temperature = readTemperature(); 00054 _humidity = readHumidity(); 00055 } 00056 00057 float SHT25::getTemperature(void) 00058 { 00059 if(_selfHeatTemperature) _temperature = readTemperature(); 00060 return _temperature; 00061 } 00062 00063 float SHT25::readTemperature(void) // if I2C Freezing go down PullUp resistor to 2K or slow frequency 00064 { 00065 char cmd[] = {SHT_TRIG_TEMP_NHOLD}, rx[] = {0xFF, 0xFF, 0xFF}; 00066 _selfHeatTemperature = false; 00067 _t.attach(callback(this, &SHT25::keepSafeTemperature), SHT_SELF_HEATING); 00068 if(!_i2c.write(SHT_I2C_ADDR, cmd, 1)) 00069 { 00070 SHT_WAIT(66); 00071 int ack = _i2c.read(SHT_I2C_ADDR, rx, 3); 00072 if(ack) 00073 { 00074 SHT_WAIT(19); 00075 ack = _i2c.read(SHT_I2C_ADDR, rx, 3); 00076 } 00077 return ack?NAN:-46.85f + 175.72f * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0f); 00078 } 00079 return NAN; 00080 } 00081 00082 float SHT25::getHumidity(void) 00083 { 00084 if(_selfHeatHumidity) _humidity = readHumidity(); 00085 return _humidity; 00086 } 00087 00088 float SHT25::readHumidity(void) // if I2C Freezing go down PullUp resistor to 2K or slow frequency 00089 { 00090 char cmd[] = {SHT_TRIG_RH_NHOLD}, rx[] = {0xFF, 0xFF, 0xFF}; 00091 _selfHeatHumidity = false; 00092 _h.attach(callback(this, &SHT25::keepSafeHumidity), SHT_SELF_HEATING); 00093 if(!_i2c.write(SHT_I2C_ADDR, cmd, 1)) 00094 { 00095 SHT_WAIT(29); 00096 int ack = _i2c.read(SHT_I2C_ADDR, rx, 3); 00097 return ack?NAN:-6.0f + 125.0f * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0f); 00098 } 00099 return NAN; 00100 } 00101 00102 bool SHT25::setPrecision(const enum_sht_prec precision) 00103 { 00104 char cmd[] = {SHT_WRITE_REG_USER, precision}; 00105 return !_i2c.write(SHT_I2C_ADDR, cmd, 2, false); 00106 } 00107 00108 bool SHT25::softReset() 00109 { 00110 char cmd[] = {SHT_SOFT_RESET}; 00111 return !_i2c.write(SHT_I2C_ADDR, cmd, 1, false); 00112 } 00113 00114 void SHT25::waitSafeHeat(void) 00115 { 00116 while(!_selfHeatTemperature || !_selfHeatHumidity) __NOP(); 00117 } 00118 00119 void SHT25::keepSafeTemperature(void) 00120 { 00121 _selfHeatTemperature = true; 00122 } 00123 00124 void SHT25::keepSafeHumidity(void) 00125 { 00126 _selfHeatHumidity = true; 00127 }
Generated on Thu Jul 14 2022 22:23:55 by
1.7.2