Humidity and Temperature Sensor - Sensirion SHT1x driver

Dependents:   ProgrammePrincipal Wetterstation project1 4180_Project ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sht15.hpp Source File

sht15.hpp

00001 /**
00002  * Copyright (c) 2010 Roy van Dam <roy@negative-black.org>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  *
00014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00015  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00016  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00017  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00018  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00019  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00020  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00021  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00022  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00023  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00024  * SUCH DAMAGE.
00025  */
00026  
00027 #ifndef _SHT15_HPP_
00028 #define _SHT15_HPP_
00029 
00030 #include "mbed.h"
00031 #include "i2c.hpp"
00032 
00033 namespace SHTx {    
00034     /**
00035      * Class: SHT15
00036      *  Humidity and Temperature Sensor - SHT15
00037      *  High level software interface.
00038      */
00039     class SHT15 {
00040     private:
00041         I2C i2c;
00042         
00043         bool ready;
00044         bool scale;
00045     
00046         uint8_t  status_register;
00047         uint16_t humidity;
00048         uint16_t temperature;
00049 
00050         struct coefficients {
00051             // Humidity conversion coefficients
00052             float c1[2], c2[2], c3[2];
00053 
00054             // Temperature conversion coefficients
00055             float dc[2], df[2], dv[2];
00056 
00057             // Temperature compensation coefficients
00058             float t1[2], t2[2];
00059 
00060             // Initialize table
00061             coefficients(void) {
00062                 c1[0] = -2.046f;        c2[1] = -2.046f;
00063                 c2[0] =  0.036f;        c2[1] =  0.587f;
00064                 c3[0] = -0.0000015955f; c3[1] = -0.00040845f;
00065 
00066                 dc[0] =  0.01f;         dc[1] =  0.04f;
00067                 df[0] =  0.018f;        df[1] =  0.072f;
00068                 dv[0] = -39.7f;         dv[1] = -39.5f;
00069 
00070                 t1[0] =  0.01f;         t1[1] =  0.01f;
00071                 t2[0] =  0.00008f;      t2[1] =  0.00128f;
00072             };
00073         } coefficient;
00074     
00075         enum cmd_list {
00076             cmd_read_temperature = 0x03,
00077             cmd_read_humidity    = 0x05,
00078             cmd_read_register    = 0x07,
00079             cmd_write_register   = 0x06,
00080             cmd_reset_device     = 0x1E
00081         };
00082 
00083         enum flag_list {
00084             flag_resolution = 0x01,
00085             flag_otp_reload = 0x02,
00086             flag_heater     = 0x04,
00087             flag_battery    = 0x40
00088         };
00089     
00090     public:
00091         /*
00092          * Initializes I2C interface and waits
00093          * the 11ms device initialization time.
00094          */
00095         SHT15(PinName sda, PinName scl);
00096 
00097         /**
00098          * Function: getTemperature
00099          *  Returns the current temperature.
00100          *
00101          * Values:
00102          *  returns - current temperature.
00103          */
00104         float getTemperature(void);
00105     
00106         /**
00107          * Function: getHumidity
00108          *  Returns the current relative humidity.
00109          *
00110          * Values:
00111          *  returns - relative humidity.
00112          */
00113         float getHumidity(void);
00114 
00115         /**
00116          * Function: checkBattery
00117          *  Returns true if battery voltage drops
00118          *    below 2.4 volt.
00119          *
00120          * Values:
00121          *  returns - true on low battery, false otherwise
00122          */
00123         bool checkBattery(void);
00124     
00125         /**
00126          * Function: setHeater
00127          *  Enable on chip heating element. The heater may 
00128          *  increase the temperature of the sensor by 5C to
00129          *  10C beyond ambient temperature.
00130          *
00131          * Values:
00132          *  value - true->on, false->off
00133          *  return - operation result
00134          */
00135         bool setHeater(bool value);
00136 
00137         /**
00138          * Function: setResolution
00139          *  Set lower measurement resolution to allow
00140          *  faster update frequencies.
00141          *
00142          * Resolutions
00143          *  low:   8bit humid. - 12bit temp.
00144          *  high: 12bit humid. - 14bit temp.
00145          *
00146          * Values:
00147          *  value - true->low, false->high
00148          *  return - operation result
00149          */
00150         bool setResolution(bool value);
00151 
00152         /**
00153          * Function: setOTPReload
00154          *  With this operation the calibration data is 
00155          *  uploaded to the register before each measurement. This 
00156          *  may be deactivated for reducing measurement time by 
00157          *  about 10ms
00158          *
00159          * Values:
00160          *  value - true->enabled, false->disabled
00161          */
00162         bool setOTPReload(bool value);
00163 
00164         /**
00165          * Function: setScale
00166          *  Sets output scale to fahrenheit or celcius.
00167          *
00168          * Values:
00169          *  value - true->fahrenheit, false->celcius
00170          */
00171         void setScale(bool value);
00172     
00173         /**
00174          * Function: update
00175          *  Performs humidity and temperature
00176          *  sensor readout of the chip.
00177          *
00178          * Values:
00179          *  return - operation result
00180          */
00181         bool update(void);
00182     
00183         /**
00184          * Function: reset
00185          *  Resets the interface, clears the 
00186          *  status register to default values.
00187          *  And waits for a minimum of 11ms.
00188          *
00189          * Values:
00190          *  return - operation result
00191          */
00192         bool reset(void);
00193 
00194         /**
00195          * Function: connectionReset
00196          *  If communication with the device is lost
00197          *  the command will reset the serial interface
00198          */
00199         void connectionReset(void);
00200     
00201     private:
00202     
00203         /**
00204          * Function: convertTemperature
00205          *  Convert sensor data to human readable value
00206          *  on the farenheit or celcius scale.
00207          *
00208          * Values:
00209          *  sot    - raw temperatue sensor output
00210          *  res    - true->low, false->high 
00211          *  scale  - true->fahrenheit, false->celcius 
00212          *  return - temperature
00213          */
00214         float convertTemperature(uint16_t sot, bool res = false, bool scale = false);
00215     
00216         /**
00217          * Function: convertHumidity
00218          *  Convert sensor data to relative humidity
00219          *  in percentage.
00220          *
00221          * Values:
00222          *  sohr   - raw humidity sensor output
00223          *  sot    - raw temperatue sensor output
00224          *  res    - true->low, false->high
00225          *  return - relative humidity
00226          */
00227         float convertHumidity(uint16_t sohr, uint16_t sot, bool res = false);
00228       
00229         /**
00230          * Function: writeRegister
00231          *  Write internal chip register.
00232          *
00233          * Values:
00234          *  return - operation result
00235          */
00236         bool writeRegister(void);
00237     
00238         /**
00239          * Function: readRegister
00240          *  Reads internal chip register
00241          *
00242          * Values:
00243          *  command - register to be accessed
00244          *  return  - operation result
00245          */
00246         bool readRegister(cmd_list command);
00247     
00248         /**
00249          * Function: setFlag
00250          *  Modify local register flag.
00251          *
00252          * Values:
00253          *  flag  - flag to be modified
00254          *  value - value to be assigned
00255          */
00256         void setFlag(flag_list flag, bool value);
00257     
00258         /**
00259          * Function: getFlag
00260          *  Get local register flag.
00261          *
00262          * Values:
00263          *  returns - flag value
00264          */
00265         bool getFlag(flag_list flag);
00266     };
00267 }
00268 
00269 /* !_SHT15_HPP_ */
00270 #endif