adafruit / RTClib

A fork of Jeelab's fantastic RTC Arduino library
MIT License
795 stars 704 forks source link

How to read the Digital Temp Sensor Output: ±3°C Accuracy [DS3231] #72

Closed rtek1000 closed 4 years ago

rtek1000 commented 7 years ago

Please see: https://github.com/JChristensen/DS3232RTC

https://github.com/rodan/ds3231

rtek1000 commented 7 years ago

//Read the temperature value from the register and convert it into float (deg C) float Sodaq_DS3231::getTemperature() { float fTemperatureCelsius; uint8_t tUBYTE = readRegister(DS3231_TMP_UP_REG); //Two's complement form uint8_t tLRBYTE = readRegister(DS3231_TMP_LOW_REG); //Fractional part

if(tUBYTE & 0b10000000) //check if -ve number
{
   tUBYTE  ^= 0b11111111;  
   tUBYTE  += 0x1;
   fTemperatureCelsius = tUBYTE + ((tLRBYTE >> 6) * 0.25);
   fTemperatureCelsius = fTemperatureCelsius * -1;
}
else
{
    fTemperatureCelsius = tUBYTE + ((tLRBYTE >> 6) * 0.25); 
}

return (fTemperatureCelsius);

}

https://github.com/SodaqMoja/Sodaq_DS3231/blob/master/src/Sodaq_DS3231.cpp