SHT21 CODE WITH TIMER and interruption

Dependencies:   mbed

Fork of SHT21_ncleee by Graeme Coapes

Committer:
graeme88
Date:
Thu Nov 29 10:48:06 2012 +0000
Revision:
1:73fc5aef174e
Parent:
0:91e3e396bc6e
Child:
2:1411bb5e8c0a
updated documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
graeme88 1:73fc5aef174e 1 /* Copyright (c) Graeme Coapes, Newcastle University, MIT License
graeme88 1:73fc5aef174e 2 *
graeme88 1:73fc5aef174e 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
graeme88 1:73fc5aef174e 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
graeme88 1:73fc5aef174e 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
graeme88 1:73fc5aef174e 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
graeme88 1:73fc5aef174e 7 * furnished to do so, subject to the following conditions:
graeme88 1:73fc5aef174e 8 *
graeme88 1:73fc5aef174e 9 * The above copyright notice and this permission notice shall be included in all copies or
graeme88 1:73fc5aef174e 10 * substantial portions of the Software.
graeme88 1:73fc5aef174e 11 *
graeme88 1:73fc5aef174e 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
graeme88 1:73fc5aef174e 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
graeme88 1:73fc5aef174e 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
graeme88 1:73fc5aef174e 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
graeme88 1:73fc5aef174e 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
graeme88 1:73fc5aef174e 17 */
graeme88 1:73fc5aef174e 18
graeme88 0:91e3e396bc6e 19 /**
graeme88 0:91e3e396bc6e 20 * SHT21 - Temperature and Humidity Sensor by Sensiron
graeme88 0:91e3e396bc6e 21 *
graeme88 0:91e3e396bc6e 22 * This is a driver to connect to the sensor using an mbed device through an I2C interface
graeme88 0:91e3e396bc6e 23 *
graeme88 0:91e3e396bc6e 24 * Author: Graeme Coapes - Newcastle University
graeme88 0:91e3e396bc6e 25 * Email: graeme.coapes@ncl.ac.uk
graeme88 0:91e3e396bc6e 26 *
graeme88 0:91e3e396bc6e 27 * Date: 28/11/2012
graeme88 0:91e3e396bc6e 28 *
graeme88 0:91e3e396bc6e 29 */
graeme88 0:91e3e396bc6e 30 #include "mbed.h"
graeme88 0:91e3e396bc6e 31 #include "SHT21_ncleee.h"
graeme88 0:91e3e396bc6e 32
graeme88 0:91e3e396bc6e 33
graeme88 0:91e3e396bc6e 34 // Class constructor
graeme88 0:91e3e396bc6e 35
graeme88 0:91e3e396bc6e 36 SHT21::SHT21(I2C *i2c) :
graeme88 0:91e3e396bc6e 37 _i2c(i2c)
graeme88 0:91e3e396bc6e 38 {
graeme88 0:91e3e396bc6e 39 }
graeme88 0:91e3e396bc6e 40
graeme88 0:91e3e396bc6e 41 int SHT21::triggerTemp()
graeme88 0:91e3e396bc6e 42 {
graeme88 0:91e3e396bc6e 43 return wr(SHT_TRIG_TEMP);
graeme88 0:91e3e396bc6e 44 }
graeme88 0:91e3e396bc6e 45
graeme88 0:91e3e396bc6e 46 int SHT21::requestTemp()
graeme88 0:91e3e396bc6e 47 {
graeme88 0:91e3e396bc6e 48 int res;
graeme88 0:91e3e396bc6e 49
graeme88 0:91e3e396bc6e 50 char rx[3];
graeme88 0:91e3e396bc6e 51
graeme88 0:91e3e396bc6e 52 res = _i2c->read(SHT_I2C_ADDR,rx,3);
graeme88 0:91e3e396bc6e 53
graeme88 0:91e3e396bc6e 54 //should use checksum here
graeme88 0:91e3e396bc6e 55
graeme88 0:91e3e396bc6e 56 //shift the MSByte to the left of the 16-bit temperature value
graeme88 0:91e3e396bc6e 57 //don't shift the LSByte
graeme88 0:91e3e396bc6e 58 //Clear bit 1 and bit 0 of the result - these are status bits sent from the sensor
graeme88 0:91e3e396bc6e 59 temperature = ((rx[0] << 8) || (rx[1] << 0)) && (0xFC);
graeme88 0:91e3e396bc6e 60
graeme88 0:91e3e396bc6e 61 return res;
graeme88 0:91e3e396bc6e 62 }
graeme88 0:91e3e396bc6e 63
graeme88 0:91e3e396bc6e 64 int SHT21::readTemp()
graeme88 0:91e3e396bc6e 65 {
graeme88 0:91e3e396bc6e 66 //First of all trigger the temperature reading
graeme88 0:91e3e396bc6e 67 //process on the sensor
graeme88 0:91e3e396bc6e 68 if(triggerTemp() != SHT_SUCCESS)
graeme88 0:91e3e396bc6e 69 {
graeme88 0:91e3e396bc6e 70 //if this has failed, exit function with specific error condition
graeme88 0:91e3e396bc6e 71 return SHT_TRIG_FAIL;
graeme88 0:91e3e396bc6e 72 }
graeme88 0:91e3e396bc6e 73
graeme88 0:91e3e396bc6e 74 //else pause whilst sensor is measuring
graeme88 0:91e3e396bc6e 75 //maximum measuring time is: 85ms
graeme88 0:91e3e396bc6e 76 wait_ms(100);
graeme88 0:91e3e396bc6e 77
graeme88 0:91e3e396bc6e 78 //Now request the temperature
graeme88 0:91e3e396bc6e 79 if(requestTemp() != SHT_SUCCESS)
graeme88 0:91e3e396bc6e 80 {
graeme88 0:91e3e396bc6e 81 //if this has failed, exit function with specific error condition
graeme88 0:91e3e396bc6e 82 return SHT_READ_FAIL;
graeme88 0:91e3e396bc6e 83 }
graeme88 0:91e3e396bc6e 84
graeme88 0:91e3e396bc6e 85 //the received temperature value should now
graeme88 0:91e3e396bc6e 86 //be stored in the temperature field
graeme88 0:91e3e396bc6e 87 return temperature;
graeme88 0:91e3e396bc6e 88 }
graeme88 0:91e3e396bc6e 89
graeme88 0:91e3e396bc6e 90 int SHT21::triggerRH()
graeme88 0:91e3e396bc6e 91 {
graeme88 0:91e3e396bc6e 92 return wr(SHT_TRIG_RH);
graeme88 0:91e3e396bc6e 93 }
graeme88 0:91e3e396bc6e 94
graeme88 0:91e3e396bc6e 95 int SHT21::requestRH()
graeme88 0:91e3e396bc6e 96 {
graeme88 0:91e3e396bc6e 97 int res;
graeme88 0:91e3e396bc6e 98
graeme88 0:91e3e396bc6e 99 char rx[3];
graeme88 0:91e3e396bc6e 100
graeme88 0:91e3e396bc6e 101 res = _i2c->read(SHT_I2C_ADDR,rx,3);
graeme88 0:91e3e396bc6e 102
graeme88 0:91e3e396bc6e 103 //should use checksum here
graeme88 0:91e3e396bc6e 104
graeme88 0:91e3e396bc6e 105 //shift the MSByte to the left of the 16-bit temperature value
graeme88 0:91e3e396bc6e 106 //don't shift the LSByte
graeme88 0:91e3e396bc6e 107 //Clear bit 1 and bit 0 of the result - these are status bits sent from the sensor
graeme88 0:91e3e396bc6e 108 humidity = ((rx[0] << 8) || (rx[1] << 0)) && (0xFC);
graeme88 0:91e3e396bc6e 109
graeme88 0:91e3e396bc6e 110 return res;
graeme88 0:91e3e396bc6e 111 }
graeme88 0:91e3e396bc6e 112
graeme88 0:91e3e396bc6e 113 int SHT21::readHumidity()
graeme88 0:91e3e396bc6e 114 {
graeme88 0:91e3e396bc6e 115 //First of all trigger the temperature reading
graeme88 0:91e3e396bc6e 116 //process on the sensor
graeme88 0:91e3e396bc6e 117 if(triggerRH() != SHT_SUCCESS)
graeme88 0:91e3e396bc6e 118 {
graeme88 0:91e3e396bc6e 119 //if this has failed, exit function with specific error condition
graeme88 0:91e3e396bc6e 120 return SHT_TRIG_FAIL;
graeme88 0:91e3e396bc6e 121 }
graeme88 0:91e3e396bc6e 122
graeme88 0:91e3e396bc6e 123 //else pause whilst sensor is measuring
graeme88 0:91e3e396bc6e 124 //maximum measuring time is: 85ms
graeme88 0:91e3e396bc6e 125 wait_ms(100);
graeme88 0:91e3e396bc6e 126
graeme88 0:91e3e396bc6e 127 //Now request the temperature
graeme88 0:91e3e396bc6e 128 if(requestRH() != SHT_SUCCESS)
graeme88 0:91e3e396bc6e 129 {
graeme88 0:91e3e396bc6e 130 //if this has failed, exit function with specific error condition
graeme88 0:91e3e396bc6e 131 return SHT_READ_FAIL;
graeme88 0:91e3e396bc6e 132 }
graeme88 0:91e3e396bc6e 133
graeme88 0:91e3e396bc6e 134 //the received temperature value should now
graeme88 0:91e3e396bc6e 135 //be stored in the temperature field
graeme88 0:91e3e396bc6e 136 return humidity;
graeme88 0:91e3e396bc6e 137 }
graeme88 0:91e3e396bc6e 138
graeme88 0:91e3e396bc6e 139 int SHT21::reset()
graeme88 0:91e3e396bc6e 140 {
graeme88 0:91e3e396bc6e 141 return wr(SHT_SOFT_RESET);
graeme88 0:91e3e396bc6e 142 }
graeme88 0:91e3e396bc6e 143
graeme88 0:91e3e396bc6e 144
graeme88 0:91e3e396bc6e 145 int SHT21::setPrecision(char precision)
graeme88 0:91e3e396bc6e 146 {
graeme88 0:91e3e396bc6e 147 int res;
graeme88 0:91e3e396bc6e 148
graeme88 0:91e3e396bc6e 149 char command[2];
graeme88 0:91e3e396bc6e 150 command[0] = SHT_WRITE_REG;
graeme88 0:91e3e396bc6e 151 command[1] = precision;
graeme88 0:91e3e396bc6e 152
graeme88 0:91e3e396bc6e 153 res = _i2c->write(SHT_I2C_ADDR,command,2);
graeme88 0:91e3e396bc6e 154
graeme88 0:91e3e396bc6e 155 return res;
graeme88 0:91e3e396bc6e 156 }
graeme88 0:91e3e396bc6e 157
graeme88 0:91e3e396bc6e 158 int SHT21::wr(int cmd)
graeme88 0:91e3e396bc6e 159 {
graeme88 0:91e3e396bc6e 160 int res;
graeme88 0:91e3e396bc6e 161
graeme88 0:91e3e396bc6e 162 char command[1];
graeme88 0:91e3e396bc6e 163 command[0] = cmd;
graeme88 0:91e3e396bc6e 164
graeme88 0:91e3e396bc6e 165 res = _i2c->write(SHT_I2C_ADDR,command,1);
graeme88 0:91e3e396bc6e 166
graeme88 0:91e3e396bc6e 167 return res;
graeme88 0:91e3e396bc6e 168 }
graeme88 0:91e3e396bc6e 169
graeme88 0:91e3e396bc6e 170
graeme88 0:91e3e396bc6e 171
graeme88 0:91e3e396bc6e 172
graeme88 0:91e3e396bc6e 173
graeme88 0:91e3e396bc6e 174