abdelah el fahim
/
SHT21_ncleee
SHT21 CODE WITH TIMER and interruption
Fork of SHT21_ncleee by
SHT21_ncleee.cpp
- Committer:
- graeme88
- Date:
- 2012-11-29
- Revision:
- 1:73fc5aef174e
- Parent:
- 0:91e3e396bc6e
- Child:
- 2:1411bb5e8c0a
File content as of revision 1:73fc5aef174e:
/* Copyright (c) Graeme Coapes, Newcastle University, MIT License * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * SHT21 - Temperature and Humidity Sensor by Sensiron * * This is a driver to connect to the sensor using an mbed device through an I2C interface * * Author: Graeme Coapes - Newcastle University * Email: graeme.coapes@ncl.ac.uk * * Date: 28/11/2012 * */ #include "mbed.h" #include "SHT21_ncleee.h" // Class constructor SHT21::SHT21(I2C *i2c) : _i2c(i2c) { } int SHT21::triggerTemp() { return wr(SHT_TRIG_TEMP); } int SHT21::requestTemp() { int res; char rx[3]; res = _i2c->read(SHT_I2C_ADDR,rx,3); //should use checksum here //shift the MSByte to the left of the 16-bit temperature value //don't shift the LSByte //Clear bit 1 and bit 0 of the result - these are status bits sent from the sensor temperature = ((rx[0] << 8) || (rx[1] << 0)) && (0xFC); return res; } int SHT21::readTemp() { //First of all trigger the temperature reading //process on the sensor if(triggerTemp() != SHT_SUCCESS) { //if this has failed, exit function with specific error condition return SHT_TRIG_FAIL; } //else pause whilst sensor is measuring //maximum measuring time is: 85ms wait_ms(100); //Now request the temperature if(requestTemp() != SHT_SUCCESS) { //if this has failed, exit function with specific error condition return SHT_READ_FAIL; } //the received temperature value should now //be stored in the temperature field return temperature; } int SHT21::triggerRH() { return wr(SHT_TRIG_RH); } int SHT21::requestRH() { int res; char rx[3]; res = _i2c->read(SHT_I2C_ADDR,rx,3); //should use checksum here //shift the MSByte to the left of the 16-bit temperature value //don't shift the LSByte //Clear bit 1 and bit 0 of the result - these are status bits sent from the sensor humidity = ((rx[0] << 8) || (rx[1] << 0)) && (0xFC); return res; } int SHT21::readHumidity() { //First of all trigger the temperature reading //process on the sensor if(triggerRH() != SHT_SUCCESS) { //if this has failed, exit function with specific error condition return SHT_TRIG_FAIL; } //else pause whilst sensor is measuring //maximum measuring time is: 85ms wait_ms(100); //Now request the temperature if(requestRH() != SHT_SUCCESS) { //if this has failed, exit function with specific error condition return SHT_READ_FAIL; } //the received temperature value should now //be stored in the temperature field return humidity; } int SHT21::reset() { return wr(SHT_SOFT_RESET); } int SHT21::setPrecision(char precision) { int res; char command[2]; command[0] = SHT_WRITE_REG; command[1] = precision; res = _i2c->write(SHT_I2C_ADDR,command,2); return res; } int SHT21::wr(int cmd) { int res; char command[1]; command[0] = cmd; res = _i2c->write(SHT_I2C_ADDR,command,1); return res; }