I have replaced a DS1307 with a more accurate DS3231 RTC.
Since i was already familiar with the libraries for the DS1307 I didnt want to re-write all my code just to get a temperature reading. Instead I kept the code the same and added a function that returns the temperature. I believe this might be used with minor moddifications with other dalas I2C temperature sensors.
Since I lost a bit of time finding what I needed, I decided to share.
Example code below
#include <Wire.h>
#define DS3231_I2C_ADDR 0x68
#define DS3231_TEMPERATURE_ADDR 0x11
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop()
{
int tempC = DS3231_get_treg(); // Reads the temperature as an int, to save memory
// float tempC = DS3231_get_treg();
delay(500);
Serial.print(tempC);
}
float DS3231_get_treg()
{
int rv; // Reads the temperature as an int, to save memory
// 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;
}
You aren't doing the conversion properly. The Arduino uses twos complement arithmetic so there's no need to convert. Also, the fraction is part of the twos complement number but you are treating it separately.
I use this code:
JimboZA:
I'm curious why you guys want to access this temperature. It's the chip internal temp isn't it, for timing compensation?
Yes, for adjustment of the internal TCXO, but its quite accurate, unlike the one on the atmega chip.
Since I already have the I2C clock, it saves me an interface with another sensor for the indoor temperature
Foo. Anecdotal evidence. A sample size of one means nothing. The guy got lucky. Maxim pays one or more engineers six figures to arrive at the ±3°C spec; ignore the datasheet at your own peril. That blog post should at least have had a prominent YMMV.
Jack, I am not mentioning tolerances, I am just saying on my particular case it is "accurate enough" to avoid using a DS18B20 (which is accurate to 0.5C) to measure ambient temperature. It is certainly more accurate that the internal atmel on-chip temperature sensor.
Im by no means saying that they are factory calibrated or can achieve xC accuracy.
My gripe was with the blog item. The author ( I assume that wasn't you? :D) seems to conclude that the two temperature sensors have comparable accuracy, which is what I take exception with. OTOH, if the DS3231 sensor is a good match for a particular application, then by all means use it!
Here is some quick code to display the temperature with no floats. I leave it to you to figure out how to handle the below zero values of temperature. If you do see them, I'm not sure I would be sitting at your desk.
cattledog:
Here is some quick code to display the temperature with no floats. I leave it to you to figure out how to handle the below zero values of temperature. If you do see them, I'm not sure I would be sitting at your desk.
switch(temp_lsb){
case 0:
Serial.println(".00");
break;
case 1 :
Serial.println(".25");
break;
case 2:
Serial.println(".50");
break;
case 3:
Serial.println(".75");
break;
}
You used two functions as I was thinking, thanks for clarifying!
About negative values, what exactly happens? I know I cant read them, but havent found out the reason why, just a specification "wrong values displayed".