a library that provides a connection to a SHT21 temperature and humidity sensor Author: Graeme Coapes - Newcastle University, graeme.coapes@ncl.ac.uk Date: 29/11/12

Dependents:   test_ncleee WeatherStation Temp_hum PROJ ... more

Files at this revision

API Documentation at this revision

Comitter:
graeme88
Date:
Thu Dec 06 10:33:10 2012 +0000
Parent:
2:1411bb5e8c0a
Commit message:
tested on device, temperature and humidity read functions now return floats with real world values

Changed in this revision

SHT21_ncleee.cpp Show annotated file Show diff for this revision Revisions of this file
SHT21_ncleee.h Show annotated file Show diff for this revision Revisions of this file
diff -r 1411bb5e8c0a -r 03bbabb7b0b0 SHT21_ncleee.cpp
--- a/SHT21_ncleee.cpp	Thu Nov 29 10:51:56 2012 +0000
+++ b/SHT21_ncleee.cpp	Thu Dec 06 10:33:10 2012 +0000
@@ -23,6 +23,11 @@
  
  // Class constructor
 
+//SHT21::SHT21(I2C *i2c, Serial *pc) :
+//_i2c(i2c), _pc(pc)
+//{
+//}
+
 SHT21::SHT21(I2C *i2c) :
 _i2c(i2c)
 {
@@ -37,25 +42,37 @@
 {
     int res;
     
-    char rx[3];
+    int rx_bytes = 3;
+    
+    char rx[rx_bytes];
     
-    res = _i2c->read(SHT_I2C_ADDR,rx,3);
+    res = _i2c->read(SHT_I2C_ADDR,rx,rx_bytes);
+    
+//    _pc->printf("%x %x %x \n", rx[0], rx[1], rx[2]);
     
     //should use checksum here
     
     //shift the MSByte to the left of the 16-bit temperature value
     //don't shift the LSByte
     //Clear bit 1 and bit 0 of the result - these are status bits sent from the sensor
-    temperature = ((rx[0] << 8) || (rx[1] << 0)) && (0xFC);
+    //temperature = ((rx[0] << 8) || (rx[1] << 0)) ; //&& (0x00FC);
+    unsigned short msb = (rx[0] << 8);
+    unsigned short lsb = (rx[1] << 0);
+    
+    temperature = msb + lsb;
+    
+    //_pc->printf("msb: 0x%x, lsb: 0x%x, r: 0x%x, temp: 0x%x \n",msb, lsb,r,  temperature);
     
     return res;
 }
  
-int SHT21::readTemp()
+float SHT21::readTemp()
 {
     //First of all trigger the temperature reading
     //process on the sensor
-    if(triggerTemp() != SHT_SUCCESS)
+    int trig = triggerTemp();
+    
+    if(trig != SHT_SUCCESS)
     {
         //if this has failed, exit function with specific error condition
         return SHT_TRIG_FAIL;
@@ -70,11 +87,16 @@
     {
         //if this has failed, exit function with specific error condition
         return SHT_READ_FAIL;
-    }
+    }    
     
     //the received temperature value should now
     //be stored in the temperature field
-    return temperature;
+    
+    float realtemp;
+    
+    realtemp = -46.85 + 175.72 * ( ((float) temperature) / 65536 );
+    
+    return realtemp;
 } 
  
 int SHT21::triggerRH()
@@ -95,12 +117,14 @@
     //shift the MSByte to the left of the 16-bit temperature value
     //don't shift the LSByte
     //Clear bit 1 and bit 0 of the result - these are status bits sent from the sensor
-    humidity = ((rx[0] << 8) || (rx[1] << 0)) && (0xFC);
+    //humidity = ((rx[0] << 8) || (rx[1] << 0)) && (0xFC);
+    humidity = (rx[0]<<8) + rx[1];
+    
     
     return res;
 }
  
-int SHT21::readHumidity()
+float SHT21::readHumidity()
 {
     //First of all trigger the temperature reading
     //process on the sensor
@@ -121,9 +145,15 @@
         return SHT_READ_FAIL;
     }
     
+    float realhum;
+    realhum = -6 + 125 * ( ((float) humidity) / 65536 );
+    return realhum;
+    
     //the received temperature value should now
     //be stored in the temperature field
-    return humidity;
+//    return humidity;
+    
+    
 } 
  
 int SHT21::reset()
@@ -147,12 +177,14 @@
 
 int SHT21::wr(int cmd)
 {
+   
     int res;
     
     char command[1];
     command[0] = cmd;
     
-    res = _i2c->write(SHT_I2C_ADDR,command,1);
+    
+    res = _i2c->write(SHT_I2C_ADDR,command,1);    
     
     return res;
 }
diff -r 1411bb5e8c0a -r 03bbabb7b0b0 SHT21_ncleee.h
--- a/SHT21_ncleee.h	Thu Nov 29 10:51:56 2012 +0000
+++ b/SHT21_ncleee.h	Thu Dec 06 10:33:10 2012 +0000
@@ -113,6 +113,7 @@
     {
         private:
             I2C *_i2c;
+//            Serial *_pc;
             int triggerTemp();  
             int requestTemp();
             unsigned short temperature;
@@ -136,9 +137,9 @@
              * waiting for 100ms for the measuring to complete 
              * before reading the temperature             
              * 
-             * @param returns The temperature, a value of either 1,2 or 3 corresponds to an error
+             * @param returns a value representing the temperature in degrees centigrade
              */ 
-            int readTemp();
+            float readTemp();
             
             /** Read the humidity value from the sensor \n
              *
@@ -146,9 +147,9 @@
              * waiting for 100ms for the measuring to complete 
              * before reading the humidity             
              * 
-             * @param returns The humidity, a value of either 1,2 or 3 corresponds to an error
+             * @param returns the percentage humidity
              */             
-            int readHumidity();
+            float readHumidity();
             
             /**
              * Perform a soft-reset of the sensor unit.