![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Smart
HTU21D.cpp@0:69293b19ee19, 2018-02-28 (annotated)
- Committer:
- Team_Eta_MBED
- Date:
- Wed Feb 28 16:34:40 2018 +0000
- Revision:
- 0:69293b19ee19
Smart Baby Monitor Foundations
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Team_Eta_MBED | 0:69293b19ee19 | 1 | // EE 4391 Senior Design |
Team_Eta_MBED | 0:69293b19ee19 | 2 | // Professor Hinkle and Dr. Compeau |
Team_Eta_MBED | 0:69293b19ee19 | 3 | // @author Team Eta |
Team_Eta_MBED | 0:69293b19ee19 | 4 | // Texas State University |
Team_Eta_MBED | 0:69293b19ee19 | 5 | |
Team_Eta_MBED | 0:69293b19ee19 | 6 | // @section DESCRIPTION |
Team_Eta_MBED | 0:69293b19ee19 | 7 | // HTU21D Humidity and Temperature sensor. |
Team_Eta_MBED | 0:69293b19ee19 | 8 | // Datasheet, specs, and information: |
Team_Eta_MBED | 0:69293b19ee19 | 9 | // https://cdn.sparkfun.com/assets/6/a/8/e/f/525778d4757b7f50398b4567.pdf |
Team_Eta_MBED | 0:69293b19ee19 | 10 | |
Team_Eta_MBED | 0:69293b19ee19 | 11 | |
Team_Eta_MBED | 0:69293b19ee19 | 12 | #include "HTU21D.h" |
Team_Eta_MBED | 0:69293b19ee19 | 13 | |
Team_Eta_MBED | 0:69293b19ee19 | 14 | HTU21D::HTU21D(PinName sda, PinName scl) { |
Team_Eta_MBED | 0:69293b19ee19 | 15 | |
Team_Eta_MBED | 0:69293b19ee19 | 16 | i2c_ = new I2C(sda, scl); |
Team_Eta_MBED | 0:69293b19ee19 | 17 | //400KHz, as specified by the datasheet page 3. |
Team_Eta_MBED | 0:69293b19ee19 | 18 | i2c_->frequency(400000); |
Team_Eta_MBED | 0:69293b19ee19 | 19 | } |
Team_Eta_MBED | 0:69293b19ee19 | 20 | |
Team_Eta_MBED | 0:69293b19ee19 | 21 | int HTU21D::sample_ctemp(void) { |
Team_Eta_MBED | 0:69293b19ee19 | 22 | |
Team_Eta_MBED | 0:69293b19ee19 | 23 | char tx[1]; |
Team_Eta_MBED | 0:69293b19ee19 | 24 | char rx[2]; |
Team_Eta_MBED | 0:69293b19ee19 | 25 | |
Team_Eta_MBED | 0:69293b19ee19 | 26 | tx[0] = TRIGGER_TEMP_MEASURE; // Triggers a temperature measure by feeding correct opcode. |
Team_Eta_MBED | 0:69293b19ee19 | 27 | i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1); |
Team_Eta_MBED | 0:69293b19ee19 | 28 | wait_ms(50); // Per datasheet, wait long enough for device to sample temperature |
Team_Eta_MBED | 0:69293b19ee19 | 29 | |
Team_Eta_MBED | 0:69293b19ee19 | 30 | // Reads triggered measure |
Team_Eta_MBED | 0:69293b19ee19 | 31 | i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2); |
Team_Eta_MBED | 0:69293b19ee19 | 32 | wait_ms(1); |
Team_Eta_MBED | 0:69293b19ee19 | 33 | |
Team_Eta_MBED | 0:69293b19ee19 | 34 | // Algorithm from datasheet to compute temperature. |
Team_Eta_MBED | 0:69293b19ee19 | 35 | unsigned int rawTemperature = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1]; |
Team_Eta_MBED | 0:69293b19ee19 | 36 | rawTemperature &= 0xFFFC; |
Team_Eta_MBED | 0:69293b19ee19 | 37 | |
Team_Eta_MBED | 0:69293b19ee19 | 38 | float tempTemperature = rawTemperature / (float)65536; //2^16 = 65536 |
Team_Eta_MBED | 0:69293b19ee19 | 39 | float realTemperature = -46.85 + (175.72 * tempTemperature); //From page 14 |
Team_Eta_MBED | 0:69293b19ee19 | 40 | |
Team_Eta_MBED | 0:69293b19ee19 | 41 | return (int)realTemperature; |
Team_Eta_MBED | 0:69293b19ee19 | 42 | } |
Team_Eta_MBED | 0:69293b19ee19 | 43 | |
Team_Eta_MBED | 0:69293b19ee19 | 44 | int HTU21D::sample_ftemp(void){ |
Team_Eta_MBED | 0:69293b19ee19 | 45 | int temptemp = sample_ctemp(); |
Team_Eta_MBED | 0:69293b19ee19 | 46 | int ftemp = temptemp * 1.8 + 32; //Change this value to fit our Hexiwear or adjut temperature calibration -- "16" used to be "32" |
Team_Eta_MBED | 0:69293b19ee19 | 47 | |
Team_Eta_MBED | 0:69293b19ee19 | 48 | return ftemp; |
Team_Eta_MBED | 0:69293b19ee19 | 49 | } |
Team_Eta_MBED | 0:69293b19ee19 | 50 | |
Team_Eta_MBED | 0:69293b19ee19 | 51 | int HTU21D::sample_ktemp(void){ |
Team_Eta_MBED | 0:69293b19ee19 | 52 | int temptemp = sample_ctemp(); |
Team_Eta_MBED | 0:69293b19ee19 | 53 | int ktemp = temptemp + 274; |
Team_Eta_MBED | 0:69293b19ee19 | 54 | |
Team_Eta_MBED | 0:69293b19ee19 | 55 | return ktemp; |
Team_Eta_MBED | 0:69293b19ee19 | 56 | } |
Team_Eta_MBED | 0:69293b19ee19 | 57 | |
Team_Eta_MBED | 0:69293b19ee19 | 58 | int HTU21D::sample_humid(void) { |
Team_Eta_MBED | 0:69293b19ee19 | 59 | |
Team_Eta_MBED | 0:69293b19ee19 | 60 | char tx[1]; |
Team_Eta_MBED | 0:69293b19ee19 | 61 | char rx[2]; |
Team_Eta_MBED | 0:69293b19ee19 | 62 | |
Team_Eta_MBED | 0:69293b19ee19 | 63 | |
Team_Eta_MBED | 0:69293b19ee19 | 64 | tx[0] = TRIGGER_HUMD_MEASURE; // Triggers a humidity measure by feeding correct opcode. |
Team_Eta_MBED | 0:69293b19ee19 | 65 | i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1); |
Team_Eta_MBED | 0:69293b19ee19 | 66 | wait_ms(16); // Per datasheet, wait long enough for device to sample humidity |
Team_Eta_MBED | 0:69293b19ee19 | 67 | |
Team_Eta_MBED | 0:69293b19ee19 | 68 | // Reads triggered measure |
Team_Eta_MBED | 0:69293b19ee19 | 69 | i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2); |
Team_Eta_MBED | 0:69293b19ee19 | 70 | wait_ms(1); |
Team_Eta_MBED | 0:69293b19ee19 | 71 | |
Team_Eta_MBED | 0:69293b19ee19 | 72 | //Algorithm from datasheet. |
Team_Eta_MBED | 0:69293b19ee19 | 73 | unsigned int rawHumidity = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1]; |
Team_Eta_MBED | 0:69293b19ee19 | 74 | |
Team_Eta_MBED | 0:69293b19ee19 | 75 | rawHumidity &= 0xFFFC; //Zero out the status bits but keep them in place |
Team_Eta_MBED | 0:69293b19ee19 | 76 | |
Team_Eta_MBED | 0:69293b19ee19 | 77 | //Given the raw humidity data, calculate the actual relative humidity |
Team_Eta_MBED | 0:69293b19ee19 | 78 | float tempRH = rawHumidity / (float)65536; //2^16 = 65536 |
Team_Eta_MBED | 0:69293b19ee19 | 79 | float rh = 20 + (125 * tempRH); //From page 14 --- "20" used to be "-6" |
Team_Eta_MBED | 0:69293b19ee19 | 80 | |
Team_Eta_MBED | 0:69293b19ee19 | 81 | return (int)rh; |
Team_Eta_MBED | 0:69293b19ee19 | 82 | } |
Team_Eta_MBED | 0:69293b19ee19 | 83 |