Go Down

Topic: 1-wire temperature reading (Read 776 times) previous topic - next topic

ludo

Based on the OneWire lib, I've wrote the following code to convert the DS18S20 reading into Celcius Degres. Please note my code display the High Resolution Mode.
I would like to create a library with some other component (DS2438 for voltage then Barometer, Humidity, etc...). But the first step is to validate with you my code and my syntax. So your comments are welcomed.

Code: [Select]
void DisplayTempHR(byte Temperature_LSB, byte Temperature_MSB, byte Count_Remain, byte Count_per_C)
{
 boolean SignBit = false;
 int Temperature;
 byte Whole;
 byte Fract;
 
 // Truncate the 0.5°C bit (the LSB) from the read value.  
 Temperature = Temperature_LSB>>1;

 // The MSB is the sign extension
 if (Temperature_MSB != 0)
 {
   SignBit = true;
   // set the sign bit
   Temperature |= B10000000;
 }

 // Temp = TempRead - 0.25 + (CountPerC-CountRemain)/CountPerC
 Temperature = Temperature<<8;

 // Ox40 is Ox100/4, the 0,25
 Temperature = Temperature - 0x40 + (((Count_per_C - Count_Remain)<<8)/Count_per_C);
 
 // Compute the complement if signbit set to 1
 if (Temperature >> 15)
 {
   SignBit = true;
   Temperature = ~Temperature;
 }
 Whole = Temperature>>8;
 Fract = ((Temperature % 0x100) * 0x64 ) >> 8;

 Serial.print(" Temp=");
 if (SignBit) Serial.print("-");
 Serial.print(Whole, DEC);
 Serial.print(".");
 if (Fract <10) Serial.print("0");
 Serial.print(Fract, DEC);
}


Go Up