Benoit Marchand / iGreenhouse_Node2_gcc_arm_nucleo_l073rz

Dependencies:   mbed Sht31

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SHT1x.cpp Source File

SHT1x.cpp

00001 /**
00002  * SHT1x Library
00003  *
00004  * Copyright 2009 Jonathan Oxer <jon@oxer.com.au> / <www.practicalarduino.com>
00005  * Based on previous work by:
00006  *    Maurice Ribble: <www.glacialwanderer.com/hobbyrobotics/?p=5>
00007  *    Wayne ?: <ragingreality.blogspot.com/2008/01/ardunio-and-sht15.html>
00008  *
00009  * Manages communication with SHT1x series (SHT10, SHT11, SHT15)
00010  * temperature / humidity sensors from Sensirion (www.sensirion.com).
00011  */
00012 
00013 #include "SHT1x.h"
00014 
00015 SHT1x::SHT1x(int dataPin, int clockPin)
00016 {
00017   _dataPin = dataPin;
00018   _clockPin = clockPin;
00019 }
00020 
00021 
00022 /* ================  Public methods ================ */
00023 
00024 /**
00025  * Reads the current temperature in degrees Celsius
00026  */
00027 float SHT1x::readTemperatureC()
00028 {
00029   int _val;                // Raw value returned from sensor
00030   float _temperature;      // Temperature derived from raw value
00031 
00032   // Conversion coefficients from SHT15 datasheet
00033   const float D1 = -40.0;  // for 14 Bit @ 5V
00034   const float D2 =   0.01; // for 14 Bit DEGC
00035 
00036   // Fetch raw value
00037   _val = readTemperatureRaw();
00038 
00039 
00040   // Convert raw value to degrees Celsius
00041   _temperature = (_val * D2) + D1;
00042 
00043   return (_temperature);
00044 }
00045 
00046 /**
00047  * Reads the current temperature in degrees Fahrenheit
00048  */
00049 float SHT1x::readTemperatureF()
00050 {
00051   int _val;                 // Raw value returned from sensor
00052   float _temperature;       // Temperature derived from raw value
00053 
00054   // Conversion coefficients from SHT15 datasheet
00055   const float D1 = -40.0;   // for 14 Bit @ 5V
00056   const float D2 =   0.018; // for 14 Bit DEGF
00057 
00058   // Fetch raw value
00059   _val = readTemperatureRaw();
00060 
00061   // Convert raw value to degrees Fahrenheit
00062   _temperature = (_val * D2) + D1;
00063 
00064   return (_temperature);
00065 }
00066 
00067 /**
00068  * Reads current temperature-corrected relative humidity
00069  */
00070 float SHT1x::readHumidity()
00071 {
00072   int _val;                    // Raw humidity value returned from sensor
00073   float _linearHumidity;       // Humidity with linear correction applied
00074   float _correctedHumidity;    // Temperature-corrected humidity
00075   float _temperature;          // Raw temperature value
00076 
00077   // Conversion coefficients from SHT15 datasheet
00078   const float C1 = -4.0;       // for 12 Bit
00079   const float C2 =  0.0405;    // for 12 Bit
00080   const float C3 = -0.0000028; // for 12 Bit
00081   const float T1 =  0.01;      // for 14 Bit @ 5V
00082   const float T2 =  0.00008;   // for 14 Bit @ 5V
00083 
00084   // Command to send to the SHT1x to request humidity
00085   int _gHumidCmd = 0b00000101;
00086 
00087   // Fetch the value from the sensor
00088   sendCommandSHT(_gHumidCmd, _dataPin, _clockPin);
00089   waitForResultSHT(_dataPin);
00090   _val = getData16SHT(_dataPin, _clockPin);
00091   skipCrcSHT(_dataPin, _clockPin);
00092 
00093   // Apply linear conversion to raw value
00094   _linearHumidity = C1 + C2 * _val + C3 * _val * _val;
00095 
00096   // Get current temperature for humidity correction
00097   _temperature = readTemperatureC();
00098 
00099   // Correct humidity value for current temperature
00100   _correctedHumidity = _temperature * (T1 + T2 * _val) + _linearHumidity;
00101 
00102   return (_correctedHumidity);
00103 }
00104 
00105 
00106 /* ================  Private methods ================ */
00107 
00108 /**
00109  * Reads the current raw temperature value
00110  */
00111 float SHT1x::readTemperatureRaw()
00112 {
00113   int _val;
00114 
00115   // Command to send to the SHT1x to request Temperature
00116   int _gTempCmd  = 0b00000011;
00117 
00118   sendCommandSHT(_gTempCmd, _dataPin, _clockPin);
00119   waitForResultSHT(_dataPin);
00120   _val = getData16SHT(_dataPin, _clockPin);
00121   skipCrcSHT(_dataPin, _clockPin);
00122 
00123   return (_val);
00124 }
00125 
00126 /**
00127  */
00128 int SHT1x::shiftIn(int _dataPin, int _clockPin, int _numBits)
00129 {
00130   int ret = 0;
00131   int i;
00132 
00133   for (i=0; i<_numBits; ++i)
00134   {
00135      _clockPin = 1;
00136 //     delay(10);  // I don't know why I need this, but without it I don't get my 8 lsb of temp
00137      //ret = ret*2 + digitalRead(_dataPin);
00138      ret = ret*2 + _dataPin;
00139      _clockPin = 0;
00140   }
00141 
00142   return(ret);
00143 }
00144 
00145 /**
00146  */
00147 void SHT1x::sendCommandSHT(int _command, int _dataPin, int _clockPin)
00148 {
00149   int ack;
00150 
00151   // Transmission Start
00152   /*pinMode(_dataPin, OUTPUT);
00153   pinMode(_clockPin, OUTPUT);
00154   digitalWrite(_dataPin, HIGH);
00155   digitalWrite(_clockPin, HIGH);
00156   digitalWrite(_dataPin, LOW);
00157   digitalWrite(_clockPin, LOW);
00158   digitalWrite(_clockPin, HIGH);
00159   digitalWrite(_dataPin, HIGH);
00160   digitalWrite(_clockPin, LOW);*/
00161 
00162   _dataPin = 1;
00163   _clockPin = 1;
00164   _dataPin = 0;
00165   _clockPin = 0;
00166   _clockPin = 1;
00167   _dataPin = 1;
00168   _clockPin = 0;
00169   
00170   // The command (3 msb are address and must be 000, and last 5 bits are command)
00171   //shiftOut(_dataPin, _clockPin, MSBFIRST, _command);
00172   ShiftReg (D14, D13, D15);
00173 
00174   // Verify we get the correct ack
00175   _clockPin = 1;
00176   //pinMode(_dataPin, INPUT_PULLUP);
00177   ack = _dataPin;
00178   if (ack != 0) {
00179     //Serial.println("Ack Error 0");
00180   }
00181   _clockPin = 0;
00182   ack = _dataPin;
00183   if (ack != 1) {
00184     //Serial.println("Ack Error 1");
00185   }
00186 }
00187 
00188 /**
00189  */
00190 void SHT1x::waitForResultSHT(int _dataPin)
00191 {
00192   int i;
00193   int ack;
00194 
00195   //pinMode(_dataPin, INPUT_PULLUP);
00196 
00197   for(i= 0; i < 1000; ++i)
00198   {
00199     wait(1);
00200     ack = _dataPin;
00201 
00202     if (ack == 0) {
00203       break;
00204     }
00205   }
00206 
00207   if (ack == 1) {
00208     //Serial.println("Ack Error 2"); // Can't do serial stuff here, need another way of reporting errors
00209   }
00210 }
00211 
00212 /**
00213  */
00214 int SHT1x::getData16SHT(int _dataPin, int _clockPin)
00215 {
00216   int val;
00217   DigitalIn dataPin(D14);
00218   dataPin.mode(PullUp);
00219 
00220   // Get the most significant bits
00221   
00222   //pinMode(clockPin, OUTPUT);
00223   val = shiftIn(_dataPin, _clockPin, 8);
00224   val *= 256;
00225 
00226   // Send the required ack
00227   //pinMode(_dataPin, OUTPUT);
00228   /*digitalWrite(_dataPin, HIGH);
00229   digitalWrite(_dataPin, LOW);
00230   digitalWrite(_clockPin, HIGH);
00231   digitalWrite(_clockPin, LOW);*/
00232   
00233   _dataPin = 1;
00234   _dataPin = 0;
00235   _clockPin = 1;
00236   _clockPin = 0;
00237 
00238   // Get the least significant bits
00239   //pinMode(_dataPin, INPUT_PULLUP);
00240   val |= shiftIn(_dataPin, _clockPin, 8);
00241 
00242   return val;
00243 }
00244 
00245 /**
00246  */
00247 void SHT1x::skipCrcSHT(int _dataPin, int _clockPin)
00248 {
00249   // Skip acknowledge to end trans (no CRC)
00250   /*pinMode(_dataPin, OUTPUT);
00251   pinMode(_clockPin, OUTPUT);
00252 
00253   digitalWrite(_dataPin, HIGH);
00254   digitalWrite(_clockPin, HIGH);
00255   digitalWrite(_clockPin, LOW);*/
00256   
00257   _dataPin = 1;
00258   _clockPin = 1;
00259   _clockPin = 0;
00260 }