version one

Fork of SHTx by Roy van Dam

Committer:
yongqiangwang
Date:
Mon Mar 04 19:11:37 2013 +0000
Revision:
2:cd8e210049b9
Parent:
1:8465801be23f
version one; ;

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 1:8465801be23f 64 c3[0] = -0.0000015955f; c3[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 1:8465801be23f 85 flag_otp_reload = 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
yongqiangwang 2:cd8e210049b9 152 /**
NegativeBlack 1:8465801be23f 153 * Function: setOTPReload
NegativeBlack 1:8465801be23f 154 * With this operation the calibration data is
yongqiangwang 2:cd8e210049b9 155 * uploaded to the register before each measurement. This
NegativeBlack 1:8465801be23f 156 * may be deactivated for reducing measurement time by
NegativeBlack 1:8465801be23f 157 * about 10ms
NegativeBlack 1:8465801be23f 158 *
NegativeBlack 1:8465801be23f 159 * Values:
NegativeBlack 1:8465801be23f 160 * value - true->enabled, false->disabled
NegativeBlack 1:8465801be23f 161 */
yongqiangwang 2:cd8e210049b9 162 bool setOTPReload(bool value);
NegativeBlack 1:8465801be23f 163
NegativeBlack 0:d55659b0c4a0 164 /**
NegativeBlack 0:d55659b0c4a0 165 * Function: setScale
NegativeBlack 0:d55659b0c4a0 166 * Sets output scale to fahrenheit or celcius.
NegativeBlack 0:d55659b0c4a0 167 *
NegativeBlack 0:d55659b0c4a0 168 * Values:
NegativeBlack 0:d55659b0c4a0 169 * value - true->fahrenheit, false->celcius
NegativeBlack 0:d55659b0c4a0 170 */
NegativeBlack 0:d55659b0c4a0 171 void setScale(bool value);
NegativeBlack 0:d55659b0c4a0 172
NegativeBlack 0:d55659b0c4a0 173 /**
NegativeBlack 0:d55659b0c4a0 174 * Function: update
NegativeBlack 0:d55659b0c4a0 175 * Performs humidity and temperature
NegativeBlack 0:d55659b0c4a0 176 * sensor readout of the chip.
NegativeBlack 0:d55659b0c4a0 177 *
NegativeBlack 0:d55659b0c4a0 178 * Values:
NegativeBlack 0:d55659b0c4a0 179 * return - operation result
NegativeBlack 0:d55659b0c4a0 180 */
NegativeBlack 0:d55659b0c4a0 181 bool update(void);
NegativeBlack 0:d55659b0c4a0 182
NegativeBlack 0:d55659b0c4a0 183 /**
NegativeBlack 0:d55659b0c4a0 184 * Function: reset
NegativeBlack 1:8465801be23f 185 * Resets the interface, clears the
yongqiangwang 2:cd8e210049b9 186 * status register to default values.
yongqiangwang 2:cd8e210049b9 187 * And waits for a minimum of 11ms.
NegativeBlack 0:d55659b0c4a0 188 *
NegativeBlack 0:d55659b0c4a0 189 * Values:
NegativeBlack 0:d55659b0c4a0 190 * return - operation result
NegativeBlack 0:d55659b0c4a0 191 */
NegativeBlack 0:d55659b0c4a0 192 bool reset(void);
NegativeBlack 1:8465801be23f 193
yongqiangwang 2:cd8e210049b9 194 /**
NegativeBlack 1:8465801be23f 195 * Function: connectionReset
NegativeBlack 1:8465801be23f 196 * If communication with the device is lost
yongqiangwang 2:cd8e210049b9 197 * the command will reset the serial interface
NegativeBlack 1:8465801be23f 198 */
yongqiangwang 2:cd8e210049b9 199 void connectionReset(void);
NegativeBlack 0:d55659b0c4a0 200
NegativeBlack 0:d55659b0c4a0 201 private:
NegativeBlack 0:d55659b0c4a0 202
NegativeBlack 0:d55659b0c4a0 203 /**
NegativeBlack 0:d55659b0c4a0 204 * Function: convertTemperature
NegativeBlack 0:d55659b0c4a0 205 * Convert sensor data to human readable value
NegativeBlack 0:d55659b0c4a0 206 * on the farenheit or celcius scale.
NegativeBlack 0:d55659b0c4a0 207 *
NegativeBlack 0:d55659b0c4a0 208 * Values:
NegativeBlack 0:d55659b0c4a0 209 * sot - raw temperatue sensor output
NegativeBlack 0:d55659b0c4a0 210 * res - true->low, false->high
NegativeBlack 0:d55659b0c4a0 211 * scale - true->fahrenheit, false->celcius
NegativeBlack 0:d55659b0c4a0 212 * return - temperature
NegativeBlack 0:d55659b0c4a0 213 */
NegativeBlack 0:d55659b0c4a0 214 float convertTemperature(uint16_t sot, bool res = false, bool scale = false);
NegativeBlack 0:d55659b0c4a0 215
NegativeBlack 0:d55659b0c4a0 216 /**
NegativeBlack 0:d55659b0c4a0 217 * Function: convertHumidity
NegativeBlack 0:d55659b0c4a0 218 * Convert sensor data to relative humidity
NegativeBlack 0:d55659b0c4a0 219 * in percentage.
NegativeBlack 0:d55659b0c4a0 220 *
NegativeBlack 0:d55659b0c4a0 221 * Values:
NegativeBlack 0:d55659b0c4a0 222 * sohr - raw humidity sensor output
NegativeBlack 0:d55659b0c4a0 223 * sot - raw temperatue sensor output
NegativeBlack 0:d55659b0c4a0 224 * res - true->low, false->high
NegativeBlack 0:d55659b0c4a0 225 * return - relative humidity
NegativeBlack 0:d55659b0c4a0 226 */
NegativeBlack 0:d55659b0c4a0 227 float convertHumidity(uint16_t sohr, uint16_t sot, bool res = false);
NegativeBlack 0:d55659b0c4a0 228
NegativeBlack 0:d55659b0c4a0 229 /**
NegativeBlack 0:d55659b0c4a0 230 * Function: writeRegister
NegativeBlack 0:d55659b0c4a0 231 * Write internal chip register.
NegativeBlack 0:d55659b0c4a0 232 *
NegativeBlack 0:d55659b0c4a0 233 * Values:
NegativeBlack 0:d55659b0c4a0 234 * return - operation result
NegativeBlack 0:d55659b0c4a0 235 */
NegativeBlack 0:d55659b0c4a0 236 bool writeRegister(void);
NegativeBlack 0:d55659b0c4a0 237
NegativeBlack 0:d55659b0c4a0 238 /**
NegativeBlack 0:d55659b0c4a0 239 * Function: readRegister
NegativeBlack 0:d55659b0c4a0 240 * Reads internal chip register
NegativeBlack 0:d55659b0c4a0 241 *
NegativeBlack 0:d55659b0c4a0 242 * Values:
NegativeBlack 0:d55659b0c4a0 243 * command - register to be accessed
NegativeBlack 0:d55659b0c4a0 244 * return - operation result
NegativeBlack 0:d55659b0c4a0 245 */
NegativeBlack 0:d55659b0c4a0 246 bool readRegister(cmd_list command);
NegativeBlack 0:d55659b0c4a0 247
NegativeBlack 0:d55659b0c4a0 248 /**
NegativeBlack 0:d55659b0c4a0 249 * Function: setFlag
NegativeBlack 0:d55659b0c4a0 250 * Modify local register flag.
NegativeBlack 0:d55659b0c4a0 251 *
NegativeBlack 0:d55659b0c4a0 252 * Values:
NegativeBlack 0:d55659b0c4a0 253 * flag - flag to be modified
NegativeBlack 0:d55659b0c4a0 254 * value - value to be assigned
NegativeBlack 0:d55659b0c4a0 255 */
NegativeBlack 0:d55659b0c4a0 256 void setFlag(flag_list flag, bool value);
NegativeBlack 0:d55659b0c4a0 257
NegativeBlack 0:d55659b0c4a0 258 /**
NegativeBlack 0:d55659b0c4a0 259 * Function: getFlag
NegativeBlack 0:d55659b0c4a0 260 * Get local register flag.
NegativeBlack 0:d55659b0c4a0 261 *
NegativeBlack 0:d55659b0c4a0 262 * Values:
NegativeBlack 0:d55659b0c4a0 263 * returns - flag value
NegativeBlack 0:d55659b0c4a0 264 */
NegativeBlack 0:d55659b0c4a0 265 bool getFlag(flag_list flag);
NegativeBlack 0:d55659b0c4a0 266 };
NegativeBlack 0:d55659b0c4a0 267 }
NegativeBlack 0:d55659b0c4a0 268
NegativeBlack 0:d55659b0c4a0 269 /* !_SHT15_HPP_ */
NegativeBlack 0:d55659b0c4a0 270 #endif