Sensiron SHT 7x Temperature and humidity device library

Dependents:   temp xj-Nucleo-F303K8-SHT75-TEST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sht7X.h Source File

sht7X.h

00001 /* mbed Sensiron SHT7x temperature and humidity sensor library
00002 *
00003 * Sensiron data at http://www.sensirion.com/en/01_humidity_sensors/06_humidity_sensor_sht75.htm
00004 *
00005 * Copyright (c) Ian Molesworth October 2010
00006 *
00007 * Permission is hereby granted, free of charge, to any person obtaining a copy
00008 * of this software and associated documentation files (the "Software"), to use
00009 * copy or modify the software for private or non-commercial purposes only. 
00010 *
00011 * The above copyright notice and this permission notice shall be included in
00012 * all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 * THE SOFTWARE.
00021 */
00022 
00023 
00024 #ifndef SHT75_H
00025 #define SHT75_H
00026 
00027 #include "mbed.h"
00028 
00029 const float C1= -4.0;              // for 12 Bit humi
00030 const float C2= +0.0405;           // for 12 Bit humi
00031 const float C3= -0.0000028;        // for 12 Bit hum
00032 const float TL= -39.61;             // 3v 14 bit 
00033 const float T1= +0.01;             // for 14 Bit @ 5V
00034 const float T2= +0.00008;          // for 14 Bit @ 5V
00035 
00036 /** SHT7X driver class
00037 *
00038 * Example:
00039 * @code
00040 * //initialise the device read temperature ticks, read humidity ticks and then
00041 * // calculate the liniarised humidity value.
00042 * #include "mbed.h"
00043 * #include "sht7X.h"
00044 * 
00045 * SHT75 sht(p12, p11); 
00046 * Serial pc(USBTX, USBRX); 
00047 * 
00048 * int main() 
00049 *     {
00050 *     float temperature;                    // temperature -40 to 120 deg C
00051 *     float humidity;                       // relative humidity 1% to 100%
00052 *     float humi_f,rh_lin,rh_true;           // working registers for Illustration purposes
00053 *     int t;                                // temporary store for the temp ticks
00054 *     int h;                                // temp store for the humidity ticks
00055 *
00056 *     pc.baud(115200);
00057 *
00058 *     while(1)
00059 *         {
00060 *         sht.readTempTicks(&t); 
00061 *         temperature = ((float)(t) * 0.01) - 39.61;
00062 *
00063 *         sht.readHumidityTicks(&h); 
00064 *         humi_f = (float)(h);
00065 *         rh_lin = C3 * humi_f * humi_f + C2 * humi_f + C1;
00066 *         rh_true=(((temperature/100)-25)*(T1+T2*humi_f)+rh_lin);
00067 *         if(rh_true>100)rh_true=100;                             //cut if the value is outside
00068 *         if(rh_true<1)rh_true=1;                                 //the physical possible range
00069 *         humidity = rh_true;                   
00070 *         pc.printf("Temp: %2.2f RH %2.2f\n\r",temperature, humidity);         
00071 *         }
00072 *     }
00073 * @endcode
00074 */
00075 
00076 class SHT75 
00077     {
00078     public:
00079      /**
00080       * @var temperature float to hold the temp
00081       */
00082         float temperature;
00083       /**
00084       * @var humidity float to hold the calculated humidity
00085       */
00086 
00087         float humidity;
00088     
00089      /** Create an SHT object connected to the specified Digital pins
00090       *
00091       * @param pclock digital pin to use as clock
00092       * @param pdata digital pin to use as data bus ( bidirectional ) 
00093       */
00094         SHT75(PinName pclock, PinName pdata): _clock(pclock), _data(pdata) {};
00095      /** read the temperature ticks value 14 bit resolution
00096       *
00097       * @param int *temp pointer to an integer to hold the tick value
00098       * @returns boolean true if read acknowledges
00099       */
00100         bool readTempTicks(int* temp);
00101      /** read the humidity ticks value 12 bit resolution
00102       *
00103       * @param int *temp pointer to an integer to hold the tick value
00104       * @returns boolean true if read acknowledges
00105       */
00106         bool readHumidityTicks(int* temp);
00107      /** start up reset
00108       *
00109       * call to resync or abort current operation.
00110       * worth calling every now and then to make sure your system is not hung.
00111       */
00112         void reset(void);
00113         void softReset(void);
00114         int  readStatus(void);
00115   
00116     private:  
00117         DigitalInOut _data;
00118         DigitalOut _clock;
00119         
00120         void start(void);
00121         int read(char);
00122         bool write(char);
00123     };
00124 
00125 #endif