moon
Dependencies: Hexi_OLED_SSD1351
MOON_HUMTEMP.cpp
00001 #include "Moon_HUMTEMP.h" 00002 00003 HTU21D::HTU21D(PinName sda, PinName scl) { 00004 00005 i2c_ = new I2C(sda, scl); 00006 //400KHz, as specified by the datasheet. 00007 i2c_->frequency(400000); 00008 00009 00010 00011 } 00012 00013 int HTU21D::sampleTemp_cel(void) { 00014 00015 char tx[1]; 00016 char rx[2]; 00017 00018 tx[0] = TRIGGER_TEMP_MEASURE; // Triggers a temperature measure by feeding correct opcode. 00019 i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1); 00020 wait_ms(50); // Per datasheet, wait long enough for device to sample temperature 00021 00022 // Reads triggered measure 00023 i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2); 00024 wait_ms(1); 00025 00026 // Algorithm from datasheet to compute temperature. 00027 unsigned int rawTemperature = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1]; 00028 rawTemperature &= 0xFFFC; 00029 00030 float tempTemperature = rawTemperature / (float)65536; //2^16 = 65536 00031 float realTemperature = -46.85 + (175.72 * tempTemperature); //From page 14 00032 00033 return (int)realTemperature; 00034 00035 } 00036 00037 int HTU21D::sampleTemp_fah(void){ 00038 int Temp_Original = sampleTemp_cel(); 00039 int fah_temp = Temp_Original * 1.8 + 32; 00040 00041 return fah_temp; 00042 } 00043 00044 int HTU21D::sampleTemp_kel(void){ 00045 int Temp_Original = sampleTemp_cel(); 00046 int kel_temp = Temp_Original + 274; 00047 00048 return kel_temp; 00049 00050 00051 } 00052 00053 int HTU21D:: sample_Humid(void) { 00054 00055 char tx[1]; 00056 char rx[2]; 00057 00058 00059 tx[0] = TRIGGER_HUMD_MEASURE; // Triggers a humidity measure by feeding correct opcode. 00060 i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1); 00061 wait_ms(16); // Per datasheet, wait long enough for device to sample humidity 00062 00063 // Reads triggered measure 00064 i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2); 00065 wait_ms(1); 00066 00067 //Algorithm from datasheet. 00068 unsigned int rawHumidity = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1]; 00069 00070 rawHumidity &= 0xFFFC; //Zero out the status bits but keep them in place 00071 00072 //Given the raw humidity data, calculate the actual relative humidity 00073 float tempRH = rawHumidity / (float)65536; //2^16 = 65536 00074 float rh = -6 + (125 * tempRH); //From page 14 00075 00076 00077 return (int)rh; 00078 00079 } 00080 00081
Generated on Tue Jul 12 2022 18:19:43 by
1.7.2