a library that provides a connection to a SHT21 temperature and humidity sensor Author: Graeme Coapes - Newcastle University, graeme.coapes@ncl.ac.uk Date: 29/11/12

Dependents:   test_ncleee WeatherStation Temp_hum PROJ ... more

Committer:
graeme88
Date:
Thu Nov 29 10:51:56 2012 +0000
Revision:
2:1411bb5e8c0a
Parent:
1:73fc5aef174e
Child:
3:03bbabb7b0b0
more documentation updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
graeme88 2:1411bb5e8c0a 1 /* Copyright (c) 2012 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 2:1411bb5e8c0a 19
graeme88 0:91e3e396bc6e 20 #include "mbed.h"
graeme88 0:91e3e396bc6e 21 #include "SHT21_ncleee.h"
graeme88 0:91e3e396bc6e 22
graeme88 0:91e3e396bc6e 23
graeme88 0:91e3e396bc6e 24 // Class constructor
graeme88 0:91e3e396bc6e 25
graeme88 0:91e3e396bc6e 26 SHT21::SHT21(I2C *i2c) :
graeme88 0:91e3e396bc6e 27 _i2c(i2c)
graeme88 0:91e3e396bc6e 28 {
graeme88 0:91e3e396bc6e 29 }
graeme88 0:91e3e396bc6e 30
graeme88 0:91e3e396bc6e 31 int SHT21::triggerTemp()
graeme88 0:91e3e396bc6e 32 {
graeme88 0:91e3e396bc6e 33 return wr(SHT_TRIG_TEMP);
graeme88 0:91e3e396bc6e 34 }
graeme88 0:91e3e396bc6e 35
graeme88 0:91e3e396bc6e 36 int SHT21::requestTemp()
graeme88 0:91e3e396bc6e 37 {
graeme88 0:91e3e396bc6e 38 int res;
graeme88 0:91e3e396bc6e 39
graeme88 0:91e3e396bc6e 40 char rx[3];
graeme88 0:91e3e396bc6e 41
graeme88 0:91e3e396bc6e 42 res = _i2c->read(SHT_I2C_ADDR,rx,3);
graeme88 0:91e3e396bc6e 43
graeme88 0:91e3e396bc6e 44 //should use checksum here
graeme88 0:91e3e396bc6e 45
graeme88 0:91e3e396bc6e 46 //shift the MSByte to the left of the 16-bit temperature value
graeme88 0:91e3e396bc6e 47 //don't shift the LSByte
graeme88 0:91e3e396bc6e 48 //Clear bit 1 and bit 0 of the result - these are status bits sent from the sensor
graeme88 0:91e3e396bc6e 49 temperature = ((rx[0] << 8) || (rx[1] << 0)) && (0xFC);
graeme88 0:91e3e396bc6e 50
graeme88 0:91e3e396bc6e 51 return res;
graeme88 0:91e3e396bc6e 52 }
graeme88 0:91e3e396bc6e 53
graeme88 0:91e3e396bc6e 54 int SHT21::readTemp()
graeme88 0:91e3e396bc6e 55 {
graeme88 0:91e3e396bc6e 56 //First of all trigger the temperature reading
graeme88 0:91e3e396bc6e 57 //process on the sensor
graeme88 0:91e3e396bc6e 58 if(triggerTemp() != SHT_SUCCESS)
graeme88 0:91e3e396bc6e 59 {
graeme88 0:91e3e396bc6e 60 //if this has failed, exit function with specific error condition
graeme88 0:91e3e396bc6e 61 return SHT_TRIG_FAIL;
graeme88 0:91e3e396bc6e 62 }
graeme88 0:91e3e396bc6e 63
graeme88 0:91e3e396bc6e 64 //else pause whilst sensor is measuring
graeme88 0:91e3e396bc6e 65 //maximum measuring time is: 85ms
graeme88 0:91e3e396bc6e 66 wait_ms(100);
graeme88 0:91e3e396bc6e 67
graeme88 0:91e3e396bc6e 68 //Now request the temperature
graeme88 0:91e3e396bc6e 69 if(requestTemp() != SHT_SUCCESS)
graeme88 0:91e3e396bc6e 70 {
graeme88 0:91e3e396bc6e 71 //if this has failed, exit function with specific error condition
graeme88 0:91e3e396bc6e 72 return SHT_READ_FAIL;
graeme88 0:91e3e396bc6e 73 }
graeme88 0:91e3e396bc6e 74
graeme88 0:91e3e396bc6e 75 //the received temperature value should now
graeme88 0:91e3e396bc6e 76 //be stored in the temperature field
graeme88 0:91e3e396bc6e 77 return temperature;
graeme88 0:91e3e396bc6e 78 }
graeme88 0:91e3e396bc6e 79
graeme88 0:91e3e396bc6e 80 int SHT21::triggerRH()
graeme88 0:91e3e396bc6e 81 {
graeme88 0:91e3e396bc6e 82 return wr(SHT_TRIG_RH);
graeme88 0:91e3e396bc6e 83 }
graeme88 0:91e3e396bc6e 84
graeme88 0:91e3e396bc6e 85 int SHT21::requestRH()
graeme88 0:91e3e396bc6e 86 {
graeme88 0:91e3e396bc6e 87 int res;
graeme88 0:91e3e396bc6e 88
graeme88 0:91e3e396bc6e 89 char rx[3];
graeme88 0:91e3e396bc6e 90
graeme88 0:91e3e396bc6e 91 res = _i2c->read(SHT_I2C_ADDR,rx,3);
graeme88 0:91e3e396bc6e 92
graeme88 0:91e3e396bc6e 93 //should use checksum here
graeme88 0:91e3e396bc6e 94
graeme88 0:91e3e396bc6e 95 //shift the MSByte to the left of the 16-bit temperature value
graeme88 0:91e3e396bc6e 96 //don't shift the LSByte
graeme88 0:91e3e396bc6e 97 //Clear bit 1 and bit 0 of the result - these are status bits sent from the sensor
graeme88 0:91e3e396bc6e 98 humidity = ((rx[0] << 8) || (rx[1] << 0)) && (0xFC);
graeme88 0:91e3e396bc6e 99
graeme88 0:91e3e396bc6e 100 return res;
graeme88 0:91e3e396bc6e 101 }
graeme88 0:91e3e396bc6e 102
graeme88 0:91e3e396bc6e 103 int SHT21::readHumidity()
graeme88 0:91e3e396bc6e 104 {
graeme88 0:91e3e396bc6e 105 //First of all trigger the temperature reading
graeme88 0:91e3e396bc6e 106 //process on the sensor
graeme88 0:91e3e396bc6e 107 if(triggerRH() != SHT_SUCCESS)
graeme88 0:91e3e396bc6e 108 {
graeme88 0:91e3e396bc6e 109 //if this has failed, exit function with specific error condition
graeme88 0:91e3e396bc6e 110 return SHT_TRIG_FAIL;
graeme88 0:91e3e396bc6e 111 }
graeme88 0:91e3e396bc6e 112
graeme88 0:91e3e396bc6e 113 //else pause whilst sensor is measuring
graeme88 0:91e3e396bc6e 114 //maximum measuring time is: 85ms
graeme88 0:91e3e396bc6e 115 wait_ms(100);
graeme88 0:91e3e396bc6e 116
graeme88 0:91e3e396bc6e 117 //Now request the temperature
graeme88 0:91e3e396bc6e 118 if(requestRH() != SHT_SUCCESS)
graeme88 0:91e3e396bc6e 119 {
graeme88 0:91e3e396bc6e 120 //if this has failed, exit function with specific error condition
graeme88 0:91e3e396bc6e 121 return SHT_READ_FAIL;
graeme88 0:91e3e396bc6e 122 }
graeme88 0:91e3e396bc6e 123
graeme88 0:91e3e396bc6e 124 //the received temperature value should now
graeme88 0:91e3e396bc6e 125 //be stored in the temperature field
graeme88 0:91e3e396bc6e 126 return humidity;
graeme88 0:91e3e396bc6e 127 }
graeme88 0:91e3e396bc6e 128
graeme88 0:91e3e396bc6e 129 int SHT21::reset()
graeme88 0:91e3e396bc6e 130 {
graeme88 0:91e3e396bc6e 131 return wr(SHT_SOFT_RESET);
graeme88 0:91e3e396bc6e 132 }
graeme88 0:91e3e396bc6e 133
graeme88 0:91e3e396bc6e 134
graeme88 0:91e3e396bc6e 135 int SHT21::setPrecision(char precision)
graeme88 0:91e3e396bc6e 136 {
graeme88 0:91e3e396bc6e 137 int res;
graeme88 0:91e3e396bc6e 138
graeme88 0:91e3e396bc6e 139 char command[2];
graeme88 0:91e3e396bc6e 140 command[0] = SHT_WRITE_REG;
graeme88 0:91e3e396bc6e 141 command[1] = precision;
graeme88 0:91e3e396bc6e 142
graeme88 0:91e3e396bc6e 143 res = _i2c->write(SHT_I2C_ADDR,command,2);
graeme88 0:91e3e396bc6e 144
graeme88 0:91e3e396bc6e 145 return res;
graeme88 0:91e3e396bc6e 146 }
graeme88 0:91e3e396bc6e 147
graeme88 0:91e3e396bc6e 148 int SHT21::wr(int cmd)
graeme88 0:91e3e396bc6e 149 {
graeme88 0:91e3e396bc6e 150 int res;
graeme88 0:91e3e396bc6e 151
graeme88 0:91e3e396bc6e 152 char command[1];
graeme88 0:91e3e396bc6e 153 command[0] = cmd;
graeme88 0:91e3e396bc6e 154
graeme88 0:91e3e396bc6e 155 res = _i2c->write(SHT_I2C_ADDR,command,1);
graeme88 0:91e3e396bc6e 156
graeme88 0:91e3e396bc6e 157 return res;
graeme88 0:91e3e396bc6e 158 }
graeme88 0:91e3e396bc6e 159
graeme88 0:91e3e396bc6e 160
graeme88 0:91e3e396bc6e 161
graeme88 0:91e3e396bc6e 162
graeme88 0:91e3e396bc6e 163
graeme88 0:91e3e396bc6e 164