![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SHT21 CODE WITH TIMER and interruption
Fork of SHT21_ncleee by
SHT21_ncleee.cpp
- Committer:
- graeme88
- Date:
- 2012-11-29
- Revision:
- 0:91e3e396bc6e
- Child:
- 1:73fc5aef174e
File content as of revision 0:91e3e396bc6e:
/** * 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; }