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 #include "sht15.hpp"
NegativeBlack 0:d55659b0c4a0 28
NegativeBlack 0:d55659b0c4a0 29 namespace SHTx {
NegativeBlack 0:d55659b0c4a0 30 SHT15::SHT15(PinName sda, PinName scl): i2c(sda, scl) {
NegativeBlack 0:d55659b0c4a0 31 this->ready = true;
NegativeBlack 0:d55659b0c4a0 32 wait_ms(11);
NegativeBlack 0:d55659b0c4a0 33 }
NegativeBlack 0:d55659b0c4a0 34
NegativeBlack 0:d55659b0c4a0 35 float
NegativeBlack 0:d55659b0c4a0 36 SHT15::getTemperature(void) {
NegativeBlack 0:d55659b0c4a0 37 return this->convertTemperature(
NegativeBlack 0:d55659b0c4a0 38 this->temperature,
NegativeBlack 0:d55659b0c4a0 39 this->getFlag(flag_resolution),
NegativeBlack 0:d55659b0c4a0 40 this->scale
NegativeBlack 0:d55659b0c4a0 41 );
NegativeBlack 0:d55659b0c4a0 42 }
NegativeBlack 0:d55659b0c4a0 43
NegativeBlack 0:d55659b0c4a0 44 float
NegativeBlack 0:d55659b0c4a0 45 SHT15::getHumidity(void) {
NegativeBlack 0:d55659b0c4a0 46 return this->convertHumidity(
NegativeBlack 0:d55659b0c4a0 47 this->humidity,
NegativeBlack 0:d55659b0c4a0 48 this->temperature,
NegativeBlack 0:d55659b0c4a0 49 this->getFlag(flag_resolution)
NegativeBlack 0:d55659b0c4a0 50 );
NegativeBlack 0:d55659b0c4a0 51 }
NegativeBlack 0:d55659b0c4a0 52
NegativeBlack 0:d55659b0c4a0 53 bool
NegativeBlack 0:d55659b0c4a0 54 SHT15::checkBattery(void) {
NegativeBlack 0:d55659b0c4a0 55 this->readRegister(cmd_read_register);
NegativeBlack 0:d55659b0c4a0 56 return this->getFlag(flag_battery);
NegativeBlack 0:d55659b0c4a0 57 }
NegativeBlack 0:d55659b0c4a0 58
NegativeBlack 0:d55659b0c4a0 59 bool
NegativeBlack 0:d55659b0c4a0 60 SHT15::setHeater(bool value) {
NegativeBlack 0:d55659b0c4a0 61 this->setFlag(flag_heater, value);
NegativeBlack 0:d55659b0c4a0 62 return this->writeRegister();
NegativeBlack 0:d55659b0c4a0 63 }
NegativeBlack 0:d55659b0c4a0 64
NegativeBlack 0:d55659b0c4a0 65 bool
NegativeBlack 0:d55659b0c4a0 66 SHT15::setResolution(bool value) {
NegativeBlack 0:d55659b0c4a0 67 this->setFlag(flag_resolution, value);
NegativeBlack 0:d55659b0c4a0 68 return this->writeRegister();
NegativeBlack 0:d55659b0c4a0 69 }
NegativeBlack 0:d55659b0c4a0 70
NegativeBlack 0:d55659b0c4a0 71 void
NegativeBlack 0:d55659b0c4a0 72 SHT15::setScale(bool value) {
NegativeBlack 0:d55659b0c4a0 73 this->scale = value;
NegativeBlack 0:d55659b0c4a0 74 }
NegativeBlack 0:d55659b0c4a0 75
NegativeBlack 0:d55659b0c4a0 76 bool
NegativeBlack 0:d55659b0c4a0 77 SHT15::update(void) {
NegativeBlack 0:d55659b0c4a0 78 if ((this->ready == false) ||
NegativeBlack 0:d55659b0c4a0 79 !this->readRegister(cmd_read_temperature) ||
NegativeBlack 0:d55659b0c4a0 80 !this->readRegister(cmd_read_humidity)) {
NegativeBlack 0:d55659b0c4a0 81 return false;
NegativeBlack 0:d55659b0c4a0 82 }
NegativeBlack 0:d55659b0c4a0 83
NegativeBlack 0:d55659b0c4a0 84 return true;
NegativeBlack 0:d55659b0c4a0 85 }
NegativeBlack 0:d55659b0c4a0 86
NegativeBlack 0:d55659b0c4a0 87 bool
NegativeBlack 0:d55659b0c4a0 88 SHT15::reset(void) {
NegativeBlack 0:d55659b0c4a0 89 while (this->ready == false) {
NegativeBlack 0:d55659b0c4a0 90 continue;
NegativeBlack 0:d55659b0c4a0 91 }
NegativeBlack 0:d55659b0c4a0 92
NegativeBlack 0:d55659b0c4a0 93 this->ready = false;
NegativeBlack 0:d55659b0c4a0 94 this->i2c.start();
NegativeBlack 0:d55659b0c4a0 95 bool ack = this->i2c.write(cmd_reset_device);
NegativeBlack 0:d55659b0c4a0 96 this->i2c.stop();
NegativeBlack 0:d55659b0c4a0 97 this->ready = true;
NegativeBlack 0:d55659b0c4a0 98
NegativeBlack 0:d55659b0c4a0 99 if (ack) {
NegativeBlack 0:d55659b0c4a0 100 this->status_register = 0;
NegativeBlack 0:d55659b0c4a0 101 this->humidity = 0;
NegativeBlack 0:d55659b0c4a0 102 this->temperature = 0;
NegativeBlack 0:d55659b0c4a0 103 wait_ms(11);
NegativeBlack 0:d55659b0c4a0 104 return true;
NegativeBlack 0:d55659b0c4a0 105 }
NegativeBlack 0:d55659b0c4a0 106
NegativeBlack 0:d55659b0c4a0 107 return false;
NegativeBlack 0:d55659b0c4a0 108 }
NegativeBlack 0:d55659b0c4a0 109
NegativeBlack 0:d55659b0c4a0 110 float
NegativeBlack 0:d55659b0c4a0 111 SHT15::convertTemperature(uint16_t sot, bool res, bool scale) {
NegativeBlack 0:d55659b0c4a0 112 // Temperature conversion coefficients
NegativeBlack 0:d55659b0c4a0 113 float d1 = this->coefficient.dv[scale];
NegativeBlack 0:d55659b0c4a0 114 float d2 = ((scale) ? this->coefficient.df[res]
NegativeBlack 0:d55659b0c4a0 115 : this->coefficient.dc[res]);
NegativeBlack 0:d55659b0c4a0 116
NegativeBlack 0:d55659b0c4a0 117 // Temperature data conversion
NegativeBlack 0:d55659b0c4a0 118 return d1 + (d2 * (float)(sot));
NegativeBlack 0:d55659b0c4a0 119 }
NegativeBlack 0:d55659b0c4a0 120
NegativeBlack 0:d55659b0c4a0 121 float
NegativeBlack 0:d55659b0c4a0 122 SHT15::convertHumidity(uint16_t sohr, uint16_t sot, bool res) {
NegativeBlack 0:d55659b0c4a0 123 // Humidity conversion coefficients
NegativeBlack 0:d55659b0c4a0 124 float c1 = this->coefficient.c1[res];
NegativeBlack 0:d55659b0c4a0 125 float c2 = this->coefficient.c2[res];
NegativeBlack 0:d55659b0c4a0 126 float c3 = this->coefficient.c3[res];
NegativeBlack 0:d55659b0c4a0 127
NegativeBlack 0:d55659b0c4a0 128 // Temperature compensation coefficients
NegativeBlack 0:d55659b0c4a0 129 float t1 = this->coefficient.t1[res];
NegativeBlack 0:d55659b0c4a0 130 float t2 = this->coefficient.t2[res];
NegativeBlack 0:d55659b0c4a0 131
NegativeBlack 0:d55659b0c4a0 132 // Temperature data conversion to celcius
NegativeBlack 0:d55659b0c4a0 133 float temp = this->convertTemperature(sot, res, false);
NegativeBlack 0:d55659b0c4a0 134
NegativeBlack 0:d55659b0c4a0 135 // Humidity data conversion to relative humidity
NegativeBlack 0:d55659b0c4a0 136 float humid = c1 + (c2 * (float)(sohr)) + (c3 * (float)(sohr * sohr));
NegativeBlack 0:d55659b0c4a0 137
NegativeBlack 0:d55659b0c4a0 138 // Calculate temperature compensation
NegativeBlack 0:d55659b0c4a0 139 return (temp - 25) + (t1 + (t2 * (float)(sohr)) + humid);
NegativeBlack 0:d55659b0c4a0 140 }
NegativeBlack 0:d55659b0c4a0 141
NegativeBlack 0:d55659b0c4a0 142 bool
NegativeBlack 0:d55659b0c4a0 143 SHT15::writeRegister(void) {
NegativeBlack 0:d55659b0c4a0 144 while (this->ready == false) {
NegativeBlack 0:d55659b0c4a0 145 continue;
NegativeBlack 0:d55659b0c4a0 146 }
NegativeBlack 0:d55659b0c4a0 147
NegativeBlack 0:d55659b0c4a0 148 this->ready = false;
NegativeBlack 0:d55659b0c4a0 149 this->i2c.start();
NegativeBlack 0:d55659b0c4a0 150
NegativeBlack 0:d55659b0c4a0 151 if (this->i2c.write(cmd_write_register)) {
NegativeBlack 0:d55659b0c4a0 152 this->i2c.write(this->status_register);
NegativeBlack 0:d55659b0c4a0 153 }
NegativeBlack 0:d55659b0c4a0 154
NegativeBlack 0:d55659b0c4a0 155 this->i2c.stop();
NegativeBlack 0:d55659b0c4a0 156 this->ready = true;
NegativeBlack 0:d55659b0c4a0 157
NegativeBlack 0:d55659b0c4a0 158 return true;
NegativeBlack 0:d55659b0c4a0 159 }
NegativeBlack 0:d55659b0c4a0 160
NegativeBlack 0:d55659b0c4a0 161 bool
NegativeBlack 0:d55659b0c4a0 162 SHT15::readRegister(cmd_list command) {
NegativeBlack 0:d55659b0c4a0 163 while (this->ready == false) {
NegativeBlack 0:d55659b0c4a0 164 continue;
NegativeBlack 0:d55659b0c4a0 165 }
NegativeBlack 0:d55659b0c4a0 166
NegativeBlack 0:d55659b0c4a0 167 this->ready = false;
NegativeBlack 0:d55659b0c4a0 168 this->i2c.start();
NegativeBlack 0:d55659b0c4a0 169
NegativeBlack 0:d55659b0c4a0 170 if (!this->i2c.write(command)) {
NegativeBlack 0:d55659b0c4a0 171 this->i2c.stop();
NegativeBlack 0:d55659b0c4a0 172 return false;
NegativeBlack 0:d55659b0c4a0 173 }
NegativeBlack 0:d55659b0c4a0 174
NegativeBlack 0:d55659b0c4a0 175 switch (command) {
NegativeBlack 0:d55659b0c4a0 176 case cmd_read_temperature: {
NegativeBlack 0:d55659b0c4a0 177 if (!this->i2c.wait()) {
NegativeBlack 0:d55659b0c4a0 178 this->i2c.stop();
NegativeBlack 0:d55659b0c4a0 179 return false;
NegativeBlack 0:d55659b0c4a0 180 }
NegativeBlack 0:d55659b0c4a0 181
NegativeBlack 0:d55659b0c4a0 182 this->temperature = this->i2c.read(1) << 8;
NegativeBlack 0:d55659b0c4a0 183 this->temperature |= this->i2c.read(0);
NegativeBlack 0:d55659b0c4a0 184 } break;
NegativeBlack 0:d55659b0c4a0 185
NegativeBlack 0:d55659b0c4a0 186 case cmd_read_humidity: {
NegativeBlack 0:d55659b0c4a0 187 if (!this->i2c.wait()) {
NegativeBlack 0:d55659b0c4a0 188 this->i2c.stop();
NegativeBlack 0:d55659b0c4a0 189 return false;
NegativeBlack 0:d55659b0c4a0 190 }
NegativeBlack 0:d55659b0c4a0 191
NegativeBlack 0:d55659b0c4a0 192 this->humidity = this->i2c.read(1) << 8;
NegativeBlack 0:d55659b0c4a0 193 this->humidity |= this->i2c.read(0);
NegativeBlack 0:d55659b0c4a0 194 } break;
NegativeBlack 0:d55659b0c4a0 195
NegativeBlack 0:d55659b0c4a0 196 case cmd_read_register: {
NegativeBlack 0:d55659b0c4a0 197 this->status_register = this->i2c.read(0);
NegativeBlack 0:d55659b0c4a0 198 } break;
NegativeBlack 0:d55659b0c4a0 199 }
NegativeBlack 0:d55659b0c4a0 200
NegativeBlack 0:d55659b0c4a0 201 this->i2c.stop();
NegativeBlack 0:d55659b0c4a0 202 this->ready = true;
NegativeBlack 0:d55659b0c4a0 203
NegativeBlack 0:d55659b0c4a0 204 return true;
NegativeBlack 0:d55659b0c4a0 205 }
NegativeBlack 0:d55659b0c4a0 206
NegativeBlack 0:d55659b0c4a0 207 bool
NegativeBlack 0:d55659b0c4a0 208 SHT15::getFlag(SHT15::flag_list flag) {
NegativeBlack 0:d55659b0c4a0 209 return (this->status_register & flag) ? true : false;
NegativeBlack 0:d55659b0c4a0 210 }
NegativeBlack 0:d55659b0c4a0 211
NegativeBlack 0:d55659b0c4a0 212 void
NegativeBlack 0:d55659b0c4a0 213 SHT15::setFlag(SHT15::flag_list flag, bool value) {
NegativeBlack 0:d55659b0c4a0 214 if (value) {
NegativeBlack 0:d55659b0c4a0 215 this->status_register |= flag;
NegativeBlack 0:d55659b0c4a0 216 } else {
NegativeBlack 0:d55659b0c4a0 217 this->status_register &= ~flag;
NegativeBlack 0:d55659b0c4a0 218 }
NegativeBlack 0:d55659b0c4a0 219 }
NegativeBlack 0:d55659b0c4a0 220 }