You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
5.3 KiB
128 lines
5.3 KiB
void dtu21()
|
|
{ //Set the address of the HTU21D. This detail is hidden at the bototm of page 10 of the datasheet.
|
|
const int I2C_address = 0x40; // I2C write address. Note that 'const' has been declared. If you try to write to this variable
|
|
// anywhere else in your code you'll get a compilation error. This prevents you from doing silly things.
|
|
|
|
const int TempCode = 0xE3; // A temperature measurement is executed with this command code. See page 11 of datasheet.
|
|
const int HumidCode = 0xE5; // A humidity measurement is executed with this command code. See page 11 of datasheet.
|
|
|
|
long Temperature; //Perform our calculation using LONG to prevent overflow at high measuremetn values when using 14bit mode.
|
|
long Humidity;
|
|
|
|
float TemperatureFL; //Variable to store our final calculated temperature measurement
|
|
float HumidityFL;
|
|
|
|
delay(100); // a short delay to let everything stabilise
|
|
|
|
|
|
delay(10); //add a small delay to slow things down
|
|
|
|
RequestMeas(I2C_address, TempCode); //Request the current temperature
|
|
Temperature = ReadMeas(I2C_address); //read the result
|
|
|
|
//We must now convert our temp measurement into an actual proper temperature value
|
|
//According to page 15, the conversion equation is: Temp = -46.85 + 175.72 x TeampMeas / 2^16
|
|
//Becasue the result will have decimal places in the result we must use a Float datatype.
|
|
// TemperatureFL = (Temperature/65536.0)*175.72-46.85 ;
|
|
TemperatureFL = (Temperature/65536.0)*175.72-46.85-1.1 ;
|
|
|
|
delay(100); //add a small delay to slow things down
|
|
|
|
RequestMeas(I2C_address, HumidCode); //Request the current humidity
|
|
Humidity = ReadMeas(I2C_address); //read the result
|
|
|
|
//We must now convert our humidity measurement into an actual proper humidity value
|
|
//According to page 15, the conversion equation is: Humidity = 6 + 125 x TeampMeas / 2^16
|
|
//Becasue the result will have decimal places in the result we must use a Float datatype.
|
|
//HumidityFL = -6 + 125*(Humidity/pow(2, 16));
|
|
HumidityFL =(Humidity/65536.0)*125.0-6.0;
|
|
|
|
|
|
tc=TemperatureFL;
|
|
td=abs(TemperatureFL*10-tc*10);
|
|
hc=HumidityFL;
|
|
hd=HumidityFL*10-hc*10;
|
|
if (tc<0){zn=0;}
|
|
//All the calculations are complete, lets now display our values for temperature and humidity on the serial port.
|
|
// Serial.print("Temperature:");
|
|
// Serial.print(TemperatureFL);
|
|
|
|
// Serial.print("\t");
|
|
// Serial.print(tc);
|
|
// Serial.print(".");
|
|
// Serial.print(td);
|
|
// Serial.print("\t");
|
|
// Serial.print("Humidity:");
|
|
//Serial.print(HumidityFL);
|
|
|
|
// Serial.print("\t");
|
|
// Serial.print(hc);
|
|
// Serial.print(".");
|
|
// Serial.println(hd);
|
|
|
|
|
|
}
|
|
|
|
void RequestMeas(int Address, int CommandCode)
|
|
{
|
|
Wire.beginTransmission(Address); // The HTU21D will respond to its address
|
|
Wire.write(CommandCode); // request the measurement, ie temperature or humidity
|
|
Wire.endTransmission();
|
|
return;
|
|
}
|
|
|
|
long ReadMeas(int Address)
|
|
{
|
|
//A measurement from the HTU21D is returned in 3 consecutive bytes. See page 11 of the datasheet
|
|
//the order of returend data is: [DATA (Most Sig Byte)], [DATA (Least Sig Byte)], [Checksum]
|
|
//We will ignore the checksum to save computation time and complexity. Page 14 of the datasheet details how to calculate the checksum
|
|
|
|
byte DataHIGH; //The location to store the high measurement byte
|
|
byte DataLOW; //The low measurement byte
|
|
byte CRC; //If you want to compute the CRC feel free to do so.
|
|
|
|
long Data; //The temporary memory location we are storing our data in as we receive it from the HTU21D
|
|
//Each packet of data is only 8bits in length, but if we do a bit shift we can store the 2 packets of
|
|
//measurement data in a variable. Remember you can only return 1 variable when a function finishes.
|
|
//Ideally, we'd use an int to store the result, however, at high measurement values in 14bit mode, an int overflows.
|
|
//To prevent overflow, we must use a single long (32bits).
|
|
|
|
Wire.requestFrom(Address, 3); //We are requesting data from the HTU21D and there will be 3x bytes that must be read.
|
|
while(Wire.available()) // Check for data from the HTU21D
|
|
{
|
|
DataHIGH = Wire.read(); // Read high byte
|
|
DataLOW = Wire.read(); // Read low byte
|
|
CRC = Wire.read(); // Read the CRC byte
|
|
}
|
|
|
|
//OK, so now we are going to 'pack' our 2x bytes of data measuremetns into a single int so only 1 variable is returned.
|
|
//To do this, we will use the left bitshift operator '&lt;&lt;'. What this does is push all the bits across to the left.
|
|
//FUN FACT: performing a left bit shift is equivalent to multiplication by 2 for each shift. The reverse is true for right
|
|
//bitshift which is equivalent to dividing by 2. This is a far more efficient method to perform multiply or divide by 2.
|
|
|
|
Data = DataHIGH; //The data sits in the low byte of 'int Data'.
|
|
//Ie, Data = [00000000][00000000][00000000][8 bits of DataHIGH]
|
|
//data[1] &lt;&lt; 8
|
|
//Data = Data &lt;&lt; 8; //Shift all the bits to the left for 8 bits. The old locations fill with zeros.
|
|
Data = Data<<8;
|
|
//Ie, Data = [00000000][00000000][8 bits of DataHIGH][00000000]
|
|
Data = Data + DataLOW; //Now simply add the low byte of data to the int.
|
|
//Ie, Data = [00000000][00000000][8 bits of DataHIGH][8 bits of DataLOW]
|
|
|
|
return Data; //return our measurement.
|
|
}
|
|
|
|
/*void dtu21u()
|
|
{
|
|
float temp = htu.readTemperature();
|
|
float rel_hum = htu.readHumidity();
|
|
Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
|
|
//tc=(temp*100)/100;
|
|
//td=int(temp*100)%100;
|
|
|
|
//hc=(rel_hum*100)/100;
|
|
//hd=int(rel_hum*100)%100;
|
|
|
|
}
|
|
*/
|
|
|
|
|