mirror of
https://github.com/adafruit/RTClib.git
synced 2026-09-15 19:12:40 +00:00
Add function to get temperature to DS3231
Read temperature from register 0x11 of DS3231 Change the example accordingly
This commit is contained in:
+19
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user