Y SI / lib_SHT25

Dependents:   lib_SHT25_example

Files at this revision

API Documentation at this revision

Comitter:
YSI
Date:
Wed Sep 22 13:26:04 2021 +0000
Parent:
7:b55223269e6b
Child:
9:382955a266cb
Commit message:
fix maximum I2C frequency to 400KHz; reduce I2C read request at one by data request

Changed in this revision

lib_SHT25.cpp Show annotated file Show diff for this revision Revisions of this file
lib_SHT25.h Show annotated file Show diff for this revision Revisions of this file
--- a/lib_SHT25.cpp	Thu Sep 16 11:17:17 2021 +0000
+++ b/lib_SHT25.cpp	Wed Sep 22 13:26:04 2021 +0000
@@ -34,10 +34,10 @@
 */
 #include "lib_SHT25.h"
 
-SHT25::SHT25(PinName sda, PinName scl, enum_sht_prec prec, int frequency) : _i2c(sda, scl)
+SHT25::SHT25(PinName sda, PinName scl, enum_sht_prec precision, int frequency) : _i2c(sda, scl)
 {
-    _i2c.frequency(frequency);
-    setPrecision(prec);
+    _i2c.frequency((frequency<=SHT_I2C_FREQUENCY)?frequency:SHT_I2C_FREQUENCY);
+    setPrecision(precision);
     _temperature = _humidity = NAN;
     _selfHeatTemperature = _selfHeatHumidity = false;
     _t.attach(callback(this, &SHT25::keepSafeTemperature), SHT_SELF_HEATING);
@@ -65,12 +65,13 @@
     return _temperature;
 }
 
-float SHT25::readTemperature(void)
+float SHT25::readTemperature(void) // if I2C Freezing go down PullUp resistor to 1K or slow frequency
 {
     char cmd[1] = {SHT_TRIG_TEMP_NHOLD}, rx[3] = {0xFF, 0xFF, 0xFF};
     if(!_i2c.write(SHT_I2C_ADDR_WRITE, cmd, 1, false))
     {
-        for(int i = 0; _i2c.read(SHT_I2C_ADDR_READ, rx, 3, false) && (i < 1889); i++);  //1889*45ms(time to read at 400KHz) = 85ms(time max to measure T on 14bit)
+        wait_us(SHT_TEMP_MEASURE);
+        _i2c.read(SHT_I2C_ADDR_READ, rx, 3, false);
         _selfHeatTemperature = false;
         _t.attach(callback(this, &SHT25::keepSafeTemperature), SHT_SELF_HEATING);
         return -46.85f + 175.72f * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0f);
@@ -85,12 +86,13 @@
     return _humidity;
 }
 
-float SHT25::readHumidity(void)
+float SHT25::readHumidity(void) // if I2C Freezing go down PullUp resistor to 1K or slow frequency
 {
     char cmd[1] = {SHT_TRIG_RH_NHOLD}, rx[3] = {0xFF, 0xFF, 0xFF};
     if(!_i2c.write(SHT_I2C_ADDR_WRITE, cmd, 1, false))
     {
-        for(int i = 0; _i2c.read(SHT_I2C_ADDR_READ, rx, 3, false) && (i < 667); i++);   //667*45ms(time to read at 400KHz) = 30ms(time max to measure RH on 12bit)
+        wait_us(SHT_HUM_MEASURE);
+        _i2c.read(SHT_I2C_ADDR_READ, rx, 3, false);
         _selfHeatHumidity = false;
         _h.attach(callback(this, &SHT25::keepSafeHumidity), SHT_SELF_HEATING);
         return -6.0f + 125.0f * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0f);
--- a/lib_SHT25.h	Thu Sep 16 11:17:17 2021 +0000
+++ b/lib_SHT25.h	Wed Sep 22 13:26:04 2021 +0000
@@ -47,6 +47,8 @@
 #define SHT_WRITE_REG_USER  0xE6    //Write to user register
 #define SHT_READ_REG_USER   0xE7    //Read from user register
 #define SHT_SOFT_RESET      0xFE    //Soft reset the sensor
+#define SHT_TEMP_MEASURE    85e3    //Waiting to measure T on 14bit
+#define SHT_HUM_MEASURE     29e3    //Waiting to measure H on 12bit
 #if MBED_MAJOR_VERSION > 5
 #define SHT_SELF_HEATING    1s      //Keep self heating
 #else
@@ -66,10 +68,12 @@
         /** make new SHT25 instance
         * connected to sda, scl I2C pins
         *
-        * @param sda I2C pin, default I2C_SDA
-        * @param scl I2C pin, default I2C_SCL
+        * @param sda I2C pin
+        * @param scl I2C pin
+        * @param precision SHT25 precision for humidity(default 12 bits) and temperature(default 14 bits)
+        * @param frequency I2C frequency, default and maximum 400KHz
         */
-        SHT25(PinName sda = I2C_SDA, PinName scl = I2C_SCL, enum_sht_prec prec = SHT_PREC_RH12T14, int frequency = SHT_I2C_FREQUENCY);
+        SHT25(PinName sda, PinName scl, enum_sht_prec precision = SHT_PREC_RH12T14, int frequency = SHT_I2C_FREQUENCY);
         
         /** return Temperature(°C) and Humidity
         *
@@ -124,6 +128,7 @@
         void  keepSafeHumidity(void);
         float _temperature, _humidity;
         bool  _selfHeatTemperature, _selfHeatHumidity;
+        char _rxT[3];
 };
 
 #endif
\ No newline at end of file