Add function to get temperature to DS3231

Read temperature from register 0x11 of DS3231
Change the example accordingly
This commit is contained in:
toannds
2016-03-17 01:30:57 +08:00
parent c763eded43
commit c3d2d11fd4
3 changed files with 25 additions and 0 deletions
+19
View File
@@ -504,3 +504,22 @@ void RTC_DS3231::writeSqwPinMode(Ds3231SqwPinMode mode) {
//Serial.println( read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL), HEX);
}
float RTC_DS3231::getTemperature()
{
uint8_t msb, lsb;
Wire.beginTransmission(DS3231_ADDRESS);
Wire._I2C_WRITE(DS3231_TEMPERATUREREG);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 2);
msb = Wire._I2C_READ();
lsb = Wire._I2C_READ();
// Serial.print("msb=");
// Serial.print(msb,HEX);
// Serial.print(", lsb=");
// Serial.println(lsb,HEX);
return (float) msb + (lsb >> 6) * 0.25f;
}
+2
View File
@@ -19,6 +19,7 @@ class TimeSpan;
#define DS3231_ADDRESS 0x68
#define DS3231_CONTROL 0x0E
#define DS3231_STATUSREG 0x0F
#define DS3231_TEMPERATUREREG 0x11
#define SECONDS_PER_DAY 86400L
@@ -103,6 +104,7 @@ public:
static DateTime now();
static Ds3231SqwPinMode readSqwPinMode();
static void writeSqwPinMode(Ds3231SqwPinMode mode);
static float getTemperature(); // in Celcius degree
};
+4
View File
@@ -72,6 +72,10 @@ void loop () {
Serial.print(future.second(), DEC);
Serial.println();
Serial.print("Temperature: ");
Serial.print(rtc.getTemperature());
Serial.println(" C");
Serial.println();
delay(3000);
}