Humidity and Temperature Sensor - Sensirion SHT1x driver

Dependents:   ProgrammePrincipal Wetterstation project1 4180_Project ... more

Committer:
NegativeBlack
Date:
Thu Nov 18 10:23:43 2010 +0000
Revision:
0:d55659b0c4a0
Child:
1:8465801be23f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NegativeBlack 0:d55659b0c4a0 1 /**
NegativeBlack 0:d55659b0c4a0 2 * Copyright (c) 2010 Roy van Dam <roy@negative-black.org>
NegativeBlack 0:d55659b0c4a0 3 * All rights reserved.
NegativeBlack 0:d55659b0c4a0 4 *
NegativeBlack 0:d55659b0c4a0 5 * Redistribution and use in source and binary forms, with or without
NegativeBlack 0:d55659b0c4a0 6 * modification, are permitted provided that the following conditions
NegativeBlack 0:d55659b0c4a0 7 * are met:
NegativeBlack 0:d55659b0c4a0 8 * 1. Redistributions of source code must retain the above copyright
NegativeBlack 0:d55659b0c4a0 9 * notice, this list of conditions and the following disclaimer.
NegativeBlack 0:d55659b0c4a0 10 * 2. Redistributions in binary form must reproduce the above copyright
NegativeBlack 0:d55659b0c4a0 11 * notice, this list of conditions and the following disclaimer in the
NegativeBlack 0:d55659b0c4a0 12 * documentation and/or other materials provided with the distribution.
NegativeBlack 0:d55659b0c4a0 13 *
NegativeBlack 0:d55659b0c4a0 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
NegativeBlack 0:d55659b0c4a0 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
NegativeBlack 0:d55659b0c4a0 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
NegativeBlack 0:d55659b0c4a0 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
NegativeBlack 0:d55659b0c4a0 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
NegativeBlack 0:d55659b0c4a0 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
NegativeBlack 0:d55659b0c4a0 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
NegativeBlack 0:d55659b0c4a0 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
NegativeBlack 0:d55659b0c4a0 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
NegativeBlack 0:d55659b0c4a0 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
NegativeBlack 0:d55659b0c4a0 24 * SUCH DAMAGE.
NegativeBlack 0:d55659b0c4a0 25 */
NegativeBlack 0:d55659b0c4a0 26
NegativeBlack 0:d55659b0c4a0 27 #ifndef _SHT15_HPP_
NegativeBlack 0:d55659b0c4a0 28 #define _SHT15_HPP_
NegativeBlack 0:d55659b0c4a0 29
NegativeBlack 0:d55659b0c4a0 30 #include "mbed.h"
NegativeBlack 0:d55659b0c4a0 31 #include "i2c.hpp"
NegativeBlack 0:d55659b0c4a0 32
NegativeBlack 0:d55659b0c4a0 33 namespace SHTx {
NegativeBlack 0:d55659b0c4a0 34 /**
NegativeBlack 0:d55659b0c4a0 35 * Class: SHT15
NegativeBlack 0:d55659b0c4a0 36 * Humidity and Temperature Sensor - SHT15
NegativeBlack 0:d55659b0c4a0 37 * High level software interface.
NegativeBlack 0:d55659b0c4a0 38 */
NegativeBlack 0:d55659b0c4a0 39 class SHT15 {
NegativeBlack 0:d55659b0c4a0 40 private:
NegativeBlack 0:d55659b0c4a0 41 I2C i2c;
NegativeBlack 0:d55659b0c4a0 42
NegativeBlack 0:d55659b0c4a0 43 bool ready;
NegativeBlack 0:d55659b0c4a0 44 bool scale;
NegativeBlack 0:d55659b0c4a0 45
NegativeBlack 0:d55659b0c4a0 46 uint8_t status_register;
NegativeBlack 0:d55659b0c4a0 47 uint16_t humidity;
NegativeBlack 0:d55659b0c4a0 48 uint16_t temperature;
NegativeBlack 0:d55659b0c4a0 49
NegativeBlack 0:d55659b0c4a0 50 struct coefficients {
NegativeBlack 0:d55659b0c4a0 51 // Humidity conversion coefficients
NegativeBlack 0:d55659b0c4a0 52 float c1[2], c2[2], c3[2];
NegativeBlack 0:d55659b0c4a0 53
NegativeBlack 0:d55659b0c4a0 54 // Temperature conversion coefficients
NegativeBlack 0:d55659b0c4a0 55 float dc[2], df[2], dv[2];
NegativeBlack 0:d55659b0c4a0 56
NegativeBlack 0:d55659b0c4a0 57 // Temperature compensation coefficients
NegativeBlack 0:d55659b0c4a0 58 float t1[2], t2[2];
NegativeBlack 0:d55659b0c4a0 59
NegativeBlack 0:d55659b0c4a0 60 // Initialize table
NegativeBlack 0:d55659b0c4a0 61 coefficients(void) {
NegativeBlack 0:d55659b0c4a0 62 c1[0] = -2.046f; c2[1] = -2.046f;
NegativeBlack 0:d55659b0c4a0 63 c2[0] = 0.036f; c2[1] = 0.587f;
NegativeBlack 0:d55659b0c4a0 64 c3[0] = -0.0000015955f; c2[1] = -0.00040845f;
NegativeBlack 0:d55659b0c4a0 65
NegativeBlack 0:d55659b0c4a0 66 dc[0] = 0.01f; dc[1] = 0.04f;
NegativeBlack 0:d55659b0c4a0 67 df[0] = 0.018f; df[1] = 0.072f;
NegativeBlack 0:d55659b0c4a0 68 dv[0] = -39.7f; dv[1] = -39.5f;
NegativeBlack 0:d55659b0c4a0 69
NegativeBlack 0:d55659b0c4a0 70 t1[0] = 0.01f; t1[1] = 0.01f;
NegativeBlack 0:d55659b0c4a0 71 t2[0] = 0.00008f; t2[1] = 0.00128f;
NegativeBlack 0:d55659b0c4a0 72 };
NegativeBlack 0:d55659b0c4a0 73 } coefficient;
NegativeBlack 0:d55659b0c4a0 74
NegativeBlack 0:d55659b0c4a0 75 enum cmd_list {
NegativeBlack 0:d55659b0c4a0 76 cmd_read_temperature = 0x03,
NegativeBlack 0:d55659b0c4a0 77 cmd_read_humidity = 0x05,
NegativeBlack 0:d55659b0c4a0 78 cmd_read_register = 0x07,
NegativeBlack 0:d55659b0c4a0 79 cmd_write_register = 0x06,
NegativeBlack 0:d55659b0c4a0 80 cmd_reset_device = 0x1E
NegativeBlack 0:d55659b0c4a0 81 };
NegativeBlack 0:d55659b0c4a0 82
NegativeBlack 0:d55659b0c4a0 83 enum flag_list {
NegativeBlack 0:d55659b0c4a0 84 flag_resolution = 0x01,
NegativeBlack 0:d55659b0c4a0 85 flag_reset = 0x02,
NegativeBlack 0:d55659b0c4a0 86 flag_heater = 0x04,
NegativeBlack 0:d55659b0c4a0 87 flag_battery = 0x40
NegativeBlack 0:d55659b0c4a0 88 };
NegativeBlack 0:d55659b0c4a0 89
NegativeBlack 0:d55659b0c4a0 90 public:
NegativeBlack 0:d55659b0c4a0 91 /*
NegativeBlack 0:d55659b0c4a0 92 * Initializes I2C interface and waits
NegativeBlack 0:d55659b0c4a0 93 * the 11ms device initialization time.
NegativeBlack 0:d55659b0c4a0 94 */
NegativeBlack 0:d55659b0c4a0 95 SHT15(PinName sda, PinName scl);
NegativeBlack 0:d55659b0c4a0 96
NegativeBlack 0:d55659b0c4a0 97 /**
NegativeBlack 0:d55659b0c4a0 98 * Function: getTemperature
NegativeBlack 0:d55659b0c4a0 99 * Returns the current temperature.
NegativeBlack 0:d55659b0c4a0 100 *
NegativeBlack 0:d55659b0c4a0 101 * Values:
NegativeBlack 0:d55659b0c4a0 102 * returns - current temperature.
NegativeBlack 0:d55659b0c4a0 103 */
NegativeBlack 0:d55659b0c4a0 104 float getTemperature(void);
NegativeBlack 0:d55659b0c4a0 105
NegativeBlack 0:d55659b0c4a0 106 /**
NegativeBlack 0:d55659b0c4a0 107 * Function: getHumidity
NegativeBlack 0:d55659b0c4a0 108 * Returns the current relative humidity.
NegativeBlack 0:d55659b0c4a0 109 *
NegativeBlack 0:d55659b0c4a0 110 * Values:
NegativeBlack 0:d55659b0c4a0 111 * returns - relative humidity.
NegativeBlack 0:d55659b0c4a0 112 */
NegativeBlack 0:d55659b0c4a0 113 float getHumidity(void);
NegativeBlack 0:d55659b0c4a0 114
NegativeBlack 0:d55659b0c4a0 115 /**
NegativeBlack 0:d55659b0c4a0 116 * Function: checkBattery
NegativeBlack 0:d55659b0c4a0 117 * Returns true if battery voltage drops
NegativeBlack 0:d55659b0c4a0 118 * below 2.4 volt.
NegativeBlack 0:d55659b0c4a0 119 *
NegativeBlack 0:d55659b0c4a0 120 * Values:
NegativeBlack 0:d55659b0c4a0 121 * returns - true on low battery, false otherwise
NegativeBlack 0:d55659b0c4a0 122 */
NegativeBlack 0:d55659b0c4a0 123 bool checkBattery(void);
NegativeBlack 0:d55659b0c4a0 124
NegativeBlack 0:d55659b0c4a0 125 /**
NegativeBlack 0:d55659b0c4a0 126 * Function: setHeater
NegativeBlack 0:d55659b0c4a0 127 * Enable on chip heating element. The heater may
NegativeBlack 0:d55659b0c4a0 128 * increase the temperature of the sensor by 5C to
NegativeBlack 0:d55659b0c4a0 129 * 10C beyond ambient temperature.
NegativeBlack 0:d55659b0c4a0 130 *
NegativeBlack 0:d55659b0c4a0 131 * Values:
NegativeBlack 0:d55659b0c4a0 132 * value - true->on, false->off
NegativeBlack 0:d55659b0c4a0 133 * return - operation result
NegativeBlack 0:d55659b0c4a0 134 */
NegativeBlack 0:d55659b0c4a0 135 bool setHeater(bool value);
NegativeBlack 0:d55659b0c4a0 136
NegativeBlack 0:d55659b0c4a0 137 /**
NegativeBlack 0:d55659b0c4a0 138 * Function: setResolution
NegativeBlack 0:d55659b0c4a0 139 * Set lower measurement resolution to allow
NegativeBlack 0:d55659b0c4a0 140 * faster update frequencies.
NegativeBlack 0:d55659b0c4a0 141 *
NegativeBlack 0:d55659b0c4a0 142 * Resolutions
NegativeBlack 0:d55659b0c4a0 143 * low: 8bit humid. - 12bit temp.
NegativeBlack 0:d55659b0c4a0 144 * high: 12bit humid. - 14bit temp.
NegativeBlack 0:d55659b0c4a0 145 *
NegativeBlack 0:d55659b0c4a0 146 * Values:
NegativeBlack 0:d55659b0c4a0 147 * value - true->low, false->high
NegativeBlack 0:d55659b0c4a0 148 * return - operation result
NegativeBlack 0:d55659b0c4a0 149 */
NegativeBlack 0:d55659b0c4a0 150 bool setResolution(bool value);
NegativeBlack 0:d55659b0c4a0 151
NegativeBlack 0:d55659b0c4a0 152 /**
NegativeBlack 0:d55659b0c4a0 153 * Function: setScale
NegativeBlack 0:d55659b0c4a0 154 * Sets output scale to fahrenheit or celcius.
NegativeBlack 0:d55659b0c4a0 155 *
NegativeBlack 0:d55659b0c4a0 156 * Values:
NegativeBlack 0:d55659b0c4a0 157 * value - true->fahrenheit, false->celcius
NegativeBlack 0:d55659b0c4a0 158 */
NegativeBlack 0:d55659b0c4a0 159 void setScale(bool value);
NegativeBlack 0:d55659b0c4a0 160
NegativeBlack 0:d55659b0c4a0 161 /**
NegativeBlack 0:d55659b0c4a0 162 * Function: update
NegativeBlack 0:d55659b0c4a0 163 * Performs humidity and temperature
NegativeBlack 0:d55659b0c4a0 164 * sensor readout of the chip.
NegativeBlack 0:d55659b0c4a0 165 *
NegativeBlack 0:d55659b0c4a0 166 * Values:
NegativeBlack 0:d55659b0c4a0 167 * return - operation result
NegativeBlack 0:d55659b0c4a0 168 */
NegativeBlack 0:d55659b0c4a0 169 bool update(void);
NegativeBlack 0:d55659b0c4a0 170
NegativeBlack 0:d55659b0c4a0 171 /**
NegativeBlack 0:d55659b0c4a0 172 * Function: reset
NegativeBlack 0:d55659b0c4a0 173 * Performs full chip reset.
NegativeBlack 0:d55659b0c4a0 174 *
NegativeBlack 0:d55659b0c4a0 175 * Values:
NegativeBlack 0:d55659b0c4a0 176 * return - operation result
NegativeBlack 0:d55659b0c4a0 177 */
NegativeBlack 0:d55659b0c4a0 178 bool reset(void);
NegativeBlack 0:d55659b0c4a0 179
NegativeBlack 0:d55659b0c4a0 180 private:
NegativeBlack 0:d55659b0c4a0 181
NegativeBlack 0:d55659b0c4a0 182 /**
NegativeBlack 0:d55659b0c4a0 183 * Function: convertTemperature
NegativeBlack 0:d55659b0c4a0 184 * Convert sensor data to human readable value
NegativeBlack 0:d55659b0c4a0 185 * on the farenheit or celcius scale.
NegativeBlack 0:d55659b0c4a0 186 *
NegativeBlack 0:d55659b0c4a0 187 * Values:
NegativeBlack 0:d55659b0c4a0 188 * sot - raw temperatue sensor output
NegativeBlack 0:d55659b0c4a0 189 * res - true->low, false->high
NegativeBlack 0:d55659b0c4a0 190 * scale - true->fahrenheit, false->celcius
NegativeBlack 0:d55659b0c4a0 191 * return - temperature
NegativeBlack 0:d55659b0c4a0 192 */
NegativeBlack 0:d55659b0c4a0 193 float convertTemperature(uint16_t sot, bool res = false, bool scale = false);
NegativeBlack 0:d55659b0c4a0 194
NegativeBlack 0:d55659b0c4a0 195 /**
NegativeBlack 0:d55659b0c4a0 196 * Function: convertHumidity
NegativeBlack 0:d55659b0c4a0 197 * Convert sensor data to relative humidity
NegativeBlack 0:d55659b0c4a0 198 * in percentage.
NegativeBlack 0:d55659b0c4a0 199 *
NegativeBlack 0:d55659b0c4a0 200 * Values:
NegativeBlack 0:d55659b0c4a0 201 * sohr - raw humidity sensor output
NegativeBlack 0:d55659b0c4a0 202 * sot - raw temperatue sensor output
NegativeBlack 0:d55659b0c4a0 203 * res - true->low, false->high
NegativeBlack 0:d55659b0c4a0 204 * return - relative humidity
NegativeBlack 0:d55659b0c4a0 205 */
NegativeBlack 0:d55659b0c4a0 206 float convertHumidity(uint16_t sohr, uint16_t sot, bool res = false);
NegativeBlack 0:d55659b0c4a0 207
NegativeBlack 0:d55659b0c4a0 208 /**
NegativeBlack 0:d55659b0c4a0 209 * Function: writeRegister
NegativeBlack 0:d55659b0c4a0 210 * Write internal chip register.
NegativeBlack 0:d55659b0c4a0 211 *
NegativeBlack 0:d55659b0c4a0 212 * Values:
NegativeBlack 0:d55659b0c4a0 213 * return - operation result
NegativeBlack 0:d55659b0c4a0 214 */
NegativeBlack 0:d55659b0c4a0 215 bool writeRegister(void);
NegativeBlack 0:d55659b0c4a0 216
NegativeBlack 0:d55659b0c4a0 217 /**
NegativeBlack 0:d55659b0c4a0 218 * Function: readRegister
NegativeBlack 0:d55659b0c4a0 219 * Reads internal chip register
NegativeBlack 0:d55659b0c4a0 220 *
NegativeBlack 0:d55659b0c4a0 221 * Values:
NegativeBlack 0:d55659b0c4a0 222 * command - register to be accessed
NegativeBlack 0:d55659b0c4a0 223 * return - operation result
NegativeBlack 0:d55659b0c4a0 224 */
NegativeBlack 0:d55659b0c4a0 225 bool readRegister(cmd_list command);
NegativeBlack 0:d55659b0c4a0 226
NegativeBlack 0:d55659b0c4a0 227 /**
NegativeBlack 0:d55659b0c4a0 228 * Function: setFlag
NegativeBlack 0:d55659b0c4a0 229 * Modify local register flag.
NegativeBlack 0:d55659b0c4a0 230 *
NegativeBlack 0:d55659b0c4a0 231 * Values:
NegativeBlack 0:d55659b0c4a0 232 * flag - flag to be modified
NegativeBlack 0:d55659b0c4a0 233 * value - value to be assigned
NegativeBlack 0:d55659b0c4a0 234 */
NegativeBlack 0:d55659b0c4a0 235 void setFlag(flag_list flag, bool value);
NegativeBlack 0:d55659b0c4a0 236
NegativeBlack 0:d55659b0c4a0 237 /**
NegativeBlack 0:d55659b0c4a0 238 * Function: getFlag
NegativeBlack 0:d55659b0c4a0 239 * Get local register flag.
NegativeBlack 0:d55659b0c4a0 240 *
NegativeBlack 0:d55659b0c4a0 241 * Values:
NegativeBlack 0:d55659b0c4a0 242 * returns - flag value
NegativeBlack 0:d55659b0c4a0 243 */
NegativeBlack 0:d55659b0c4a0 244 bool getFlag(flag_list flag);
NegativeBlack 0:d55659b0c4a0 245 };
NegativeBlack 0:d55659b0c4a0 246 }
NegativeBlack 0:d55659b0c4a0 247
NegativeBlack 0:d55659b0c4a0 248 /* !_SHT15_HPP_ */
NegativeBlack 0:d55659b0c4a0 249 #endif