library HTU21D results in float

Dependents:   HTU21D_HELLOWORLD Major_dHome

Fork of HTU21D by Alex Lipford

Committer:
hwing91
Date:
Fri Mar 28 14:55:03 2014 +0000
Revision:
0:db86ad1b4459
Child:
1:d7568a103ef5
INITIAL COMMIT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hwing91 0:db86ad1b4459 1 /**
hwing91 0:db86ad1b4459 2 * @author Alan Lai & Nelson Diaz
hwing91 0:db86ad1b4459 3 * The Georgia Institute of Technology
hwing91 0:db86ad1b4459 4 * ECE 4180 Embeded Systems
hwing91 0:db86ad1b4459 5 * Professor Hamblen
hwing91 0:db86ad1b4459 6 * 03/28/2014
hwing91 0:db86ad1b4459 7 *
hwing91 0:db86ad1b4459 8 * @section LICENSE
hwing91 0:db86ad1b4459 9 * ----------------------------------------------------------------------------
hwing91 0:db86ad1b4459 10 * "THE BEER-WARE LICENSE" (Revision 42):
hwing91 0:db86ad1b4459 11 * <alanhlai91@gmail.com> wrote this file. As long as you retain this notice you
hwing91 0:db86ad1b4459 12 * can do whatever you want with this stuff. If we meet some day, and you think
hwing91 0:db86ad1b4459 13 * this stuff is worth it, you can buy me a beer in return.
hwing91 0:db86ad1b4459 14 * ----------------------------------------------------------------------------
hwing91 0:db86ad1b4459 15 *
hwing91 0:db86ad1b4459 16 *
hwing91 0:db86ad1b4459 17 * @section DESCRIPTION
hwing91 0:db86ad1b4459 18 *
hwing91 0:db86ad1b4459 19 * HTU21D Humidity and Temperature sensor.
hwing91 0:db86ad1b4459 20 *
hwing91 0:db86ad1b4459 21 * Datasheet, specs, and information:
hwing91 0:db86ad1b4459 22 *
hwing91 0:db86ad1b4459 23 * https://www.sparkfun.com/products/12064
hwing91 0:db86ad1b4459 24 */
hwing91 0:db86ad1b4459 25
hwing91 0:db86ad1b4459 26 /**
hwing91 0:db86ad1b4459 27 * Includes
hwing91 0:db86ad1b4459 28 */
hwing91 0:db86ad1b4459 29 #include "HTU21D.h"
hwing91 0:db86ad1b4459 30
hwing91 0:db86ad1b4459 31 HTU21D::HTU21D(PinName sda, PinName scl) {
hwing91 0:db86ad1b4459 32
hwing91 0:db86ad1b4459 33 i2c_ = new I2C(sda, scl);
hwing91 0:db86ad1b4459 34 //400KHz, as specified by the datasheet.
hwing91 0:db86ad1b4459 35 i2c_->frequency(400000);
hwing91 0:db86ad1b4459 36
hwing91 0:db86ad1b4459 37
hwing91 0:db86ad1b4459 38
hwing91 0:db86ad1b4459 39 }
hwing91 0:db86ad1b4459 40
hwing91 0:db86ad1b4459 41 int HTU21D::sample_ctemp(void) {
hwing91 0:db86ad1b4459 42
hwing91 0:db86ad1b4459 43 char tx[1];
hwing91 0:db86ad1b4459 44 char rx[2];
hwing91 0:db86ad1b4459 45
hwing91 0:db86ad1b4459 46 tx[0] = TRIGGER_TEMP_MEASURE; // Triggers a temperature measure by feeding correct opcode.
hwing91 0:db86ad1b4459 47 i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1);
hwing91 0:db86ad1b4459 48 wait_ms(1);
hwing91 0:db86ad1b4459 49
hwing91 0:db86ad1b4459 50 // Reads triggered measure
hwing91 0:db86ad1b4459 51 i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2);
hwing91 0:db86ad1b4459 52 wait_ms(1);
hwing91 0:db86ad1b4459 53
hwing91 0:db86ad1b4459 54 // Algorithm from datasheet to compute temperature.
hwing91 0:db86ad1b4459 55 unsigned int rawTemperature = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1];
hwing91 0:db86ad1b4459 56 rawTemperature &= 0xFFFC;
hwing91 0:db86ad1b4459 57
hwing91 0:db86ad1b4459 58 float tempTemperature = rawTemperature / (float)65536; //2^16 = 65536
hwing91 0:db86ad1b4459 59 float realTemperature = -46.85 + (175.72 * tempTemperature); //From page 14
hwing91 0:db86ad1b4459 60
hwing91 0:db86ad1b4459 61 return (int)realTemperature;
hwing91 0:db86ad1b4459 62
hwing91 0:db86ad1b4459 63 }
hwing91 0:db86ad1b4459 64
hwing91 0:db86ad1b4459 65 int HTU21D::sample_ftemp(void){
hwing91 0:db86ad1b4459 66 int temptemp = sample_ctemp();
hwing91 0:db86ad1b4459 67 int ftemp = temptemp * 1.8 + 32;
hwing91 0:db86ad1b4459 68
hwing91 0:db86ad1b4459 69 return ftemp;
hwing91 0:db86ad1b4459 70 }
hwing91 0:db86ad1b4459 71
hwing91 0:db86ad1b4459 72 int HTU21D::sample_ktemp(void){
hwing91 0:db86ad1b4459 73 int temptemp = sample_ctemp();
hwing91 0:db86ad1b4459 74 int ktemp = temptemp + 274;
hwing91 0:db86ad1b4459 75
hwing91 0:db86ad1b4459 76 return ktemp;
hwing91 0:db86ad1b4459 77
hwing91 0:db86ad1b4459 78
hwing91 0:db86ad1b4459 79 }
hwing91 0:db86ad1b4459 80
hwing91 0:db86ad1b4459 81 int HTU21D::sample_humid(void) {
hwing91 0:db86ad1b4459 82
hwing91 0:db86ad1b4459 83 char tx[1];
hwing91 0:db86ad1b4459 84 char rx[2];
hwing91 0:db86ad1b4459 85
hwing91 0:db86ad1b4459 86
hwing91 0:db86ad1b4459 87 tx[0] = TRIGGER_HUMD_MEASURE; // Triggers a humidity measure by feeding correct opcode.
hwing91 0:db86ad1b4459 88 i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1);
hwing91 0:db86ad1b4459 89 wait_ms(1);
hwing91 0:db86ad1b4459 90
hwing91 0:db86ad1b4459 91 // Reads triggered measure
hwing91 0:db86ad1b4459 92 i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2);
hwing91 0:db86ad1b4459 93 wait_ms(1);
hwing91 0:db86ad1b4459 94
hwing91 0:db86ad1b4459 95 //Algorithm from datasheet.
hwing91 0:db86ad1b4459 96 unsigned int rawHumidity = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1];
hwing91 0:db86ad1b4459 97
hwing91 0:db86ad1b4459 98 rawHumidity &= 0xFFFC; //Zero out the status bits but keep them in place
hwing91 0:db86ad1b4459 99
hwing91 0:db86ad1b4459 100 //Given the raw humidity data, calculate the actual relative humidity
hwing91 0:db86ad1b4459 101 float tempRH = rawHumidity / (float)65536; //2^16 = 65536
hwing91 0:db86ad1b4459 102 float rh = -6 + (125 * tempRH); //From page 14
hwing91 0:db86ad1b4459 103
hwing91 0:db86ad1b4459 104
hwing91 0:db86ad1b4459 105 return (int)rh;
hwing91 0:db86ad1b4459 106
hwing91 0:db86ad1b4459 107 }
hwing91 0:db86ad1b4459 108