The Arduino library for the DS3231 doesn't seem to support negative temperatures.
Can anyone help with this?
DS3231 - Reading Temperature
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- dcaroth552
- Posts: 4
- Joined: Mon Jan 15, 2018 9:36 am
DS3231 - Reading Temperature
Last edited by dcaroth552 on Mon Jan 15, 2018 11:05 am, edited 1 time in total.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: DS3231
Which library are you using? Adafruit's RTCLib does not support reading the temperature registers of the DS3231.
- dcaroth552
- Posts: 4
- Joined: Mon Jan 15, 2018 9:36 am
Re: DS3231
I'm using DS3231.h
The following line of code reads the temperature (Celsius) from the DS3231:
Temp = (rtc.getTemp());
But a negative temperature (say -5 degrees C) reads back 251, not -5.
The following line of code reads the temperature (Celsius) from the DS3231:
Temp = (rtc.getTemp());
But a negative temperature (say -5 degrees C) reads back 251, not -5.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: DS3231
That is not an Adafruit library. We support the DS3231 via our RTCLib library: https://github.com/adafruit/RTClibI'm using DS3231.h
There are many DS3231-specific libraries out there. You should contact the author of the one you are using.
- dcaroth552
- Posts: 4
- Joined: Mon Jan 15, 2018 9:36 am
Re: DS3231
Below is the actual code I’m using to retrieve the temperature from the DS3231:
This works fine so long as the temperature is a positive number.
I thought the library checked register 11h for a negative value (MSB) but it appears that it's not doing it.
The following is the code from the library:
Code: Select all
void Display_Temperature(){
int Deg = 0;
Display.clear();
Display.writeDisplay();
if(digitalRead(Temp_Switch) == HIGH){
Deg = 15; // display F
Temp = (rtc.getTemp() * 1.8 + 32);
}
else{
Deg = 12; //display C
Temp = (rtc.getTemp());
}
Temp = Temp * 10;
if(Temp == 0){
Display.writeDigitNum(3,0);
}
else{
Display.print(Temp);
}
Display.writeDigitNum(4,Deg);
Display.writeDisplay();
}
I thought the library checked register 11h for a negative value (MSB) but it appears that it's not doing it.
The following is the code from the library:
Code: Select all
float DS3231_get_treg()
{
float rv;
uint8_t temp_msb, temp_lsb;
int8_t nint;
Wire.beginTransmission(DS3231_I2C_ADDR);
Wire.write(DS3231_TEMPERATURE_ADDR);
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDR, 2);
temp_msb = Wire.read();
temp_lsb = Wire.read() >> 6;
if ((temp_msb & 0x80) != 0)
nint = temp_msb | ~((1 << 8) - 1); // if negative get two's complement
else
nint = temp_msb;
rv = 0.25 * temp_lsb + nint;
return rv;
}
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: DS3231 - Reading Temperature
It is not clear what the author was attempting with that operation. Looks to me like it would always evaluate to 0xFF.
- dcaroth552
- Posts: 4
- Joined: Mon Jan 15, 2018 9:36 am
Re: DS3231 - Reading Temperature
Well I guess I write some code when time permits to by-pass the library for reading the temperature register.
If I find a solution I'll post it here for knowledge base.
Thanks
If I find a solution I'll post it here for knowledge base.
Thanks
Please be positive and constructive with your questions and comments.