From d26dcdc74050c79365c92a09f26b60c17b9338f2 Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Mon, 28 May 2012 17:41:14 +1000 Subject: [PATCH] Reading the temperature from a DS3232 chip --- libraries/RTC/DS3232RTC.cpp | 10 +++ libraries/RTC/DS3232RTC.h | 2 + libraries/RTC/RTC.cpp | 18 +++++ libraries/RTC/RTC.h | 4 ++ libraries/RTC/examples/DumpRTC/DumpRTC.pde | 78 ++++++++++++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 libraries/RTC/examples/DumpRTC/DumpRTC.pde diff --git a/libraries/RTC/DS3232RTC.cpp b/libraries/RTC/DS3232RTC.cpp index 4f928395..ef31a767 100644 --- a/libraries/RTC/DS3232RTC.cpp +++ b/libraries/RTC/DS3232RTC.cpp @@ -353,6 +353,16 @@ void DS3232RTC::writeByte(uint8_t offset, uint8_t value) RTC::writeByte(offset, value); } +int DS3232RTC::readTemperature() +{ + if (_isRealTime) { + return (((int)(signed char)readRegister(DS3232_TEMP_MSB)) << 2) | + (readRegister(DS3232_TEMP_LSB) >> 6); + } else { + return NO_TEMPERATURE; + } +} + /** * \brief Enables the generation of interrupts for alarms 0 and 1. * diff --git a/libraries/RTC/DS3232RTC.h b/libraries/RTC/DS3232RTC.h index 961307df..40fcf246 100644 --- a/libraries/RTC/DS3232RTC.h +++ b/libraries/RTC/DS3232RTC.h @@ -48,6 +48,8 @@ public: uint8_t readByte(uint8_t offset); void writeByte(uint8_t offset, uint8_t value); + int readTemperature(); + void enableAlarmInterrupts(); void disableAlarmInterrupts(); int firedAlarm(); diff --git a/libraries/RTC/RTC.cpp b/libraries/RTC/RTC.cpp index fd0681dc..83c6e906 100644 --- a/libraries/RTC/RTC.cpp +++ b/libraries/RTC/RTC.cpp @@ -257,6 +257,24 @@ void RTC::writeByte(uint8_t offset, uint8_t value) } } +/** + * \var RTC::NO_TEMPERATURE + * \brief Value that is returned from readTemperature() if the realtime + * clock chip cannot determine the temperature. + */ + +/** + * \brief Reads the value of the temperature sensor and returns the + * temperature in quarters of a degree celcius. + * + * Returns the value NO_TEMPERATURE if the realtime clock chip cannot + * determine the temperature. + */ +int RTC::readTemperature() +{ + return NO_TEMPERATURE; +} + /** * \var RTC::INCREMENT * \brief Increment the day, month, or year in a call to adjustDays(), adjustMonths(), or adjustYears(). diff --git a/libraries/RTC/RTC.h b/libraries/RTC/RTC.h index 055480e6..923287d7 100644 --- a/libraries/RTC/RTC.h +++ b/libraries/RTC/RTC.h @@ -69,6 +69,10 @@ public: virtual uint8_t readByte(uint8_t offset); virtual void writeByte(uint8_t offset, uint8_t value); + static const int NO_TEMPERATURE = 32767; + + virtual int readTemperature(); + // Flags for adjustDays(), adjustMonths(), and adjustYears(). static const uint8_t INCREMENT = 0x0000; static const uint8_t DECREMENT = 0x0001; diff --git a/libraries/RTC/examples/DumpRTC/DumpRTC.pde b/libraries/RTC/examples/DumpRTC/DumpRTC.pde new file mode 100644 index 00000000..d5d2d821 --- /dev/null +++ b/libraries/RTC/examples/DumpRTC/DumpRTC.pde @@ -0,0 +1,78 @@ +/* +This example demonstrates how to read time, date, and other information +from the DS3232 realtime clock chip. + +This example is placed into the public domain. +*/ + +#include +#include + +SoftI2C i2c(A4, A5); +DS3232RTC rtc(i2c); + +const char *months[] = { + " Jan ", " Feb ", " Mar ", " Apr ", " May ", " Jun ", + " Jul ", " Aug ", " Sep ", " Oct ", " Nov ", " Dec " +}; + +void setup() { + Serial.begin(9600); + + RTCAlarm alarm; + for (byte alarmNum = 0; alarmNum < RTC::ALARM_COUNT; ++alarmNum) { + rtc.readAlarm(alarmNum, &alarm); + Serial.print("Alarm "); + Serial.print(alarmNum + 1, DEC); + Serial.print(": "); + if (alarm.flags & 0x01) { + printDec2(alarm.hour); + Serial.print(':'); + printDec2(alarm.minute); + Serial.println(); + } else { + Serial.println("Off"); + } + } + Serial.println(); +} + +void loop() { + RTCTime time; + RTCDate date; + rtc.readTime(&time); + rtc.readDate(&date); + + Serial.print("Time: "); + printDec2(time.hour); + Serial.print(':'); + printDec2(time.minute); + Serial.print(':'); + printDec2(time.second); + Serial.println(); + + Serial.print("Date: "); + Serial.print(date.day, DEC); + Serial.print(months[date.month - 1]); + Serial.print(date.year); + Serial.println(); + + Serial.print("Temp: "); + int temp = rtc.readTemperature(); + if (temp != RTC::NO_TEMPERATURE) { + Serial.print(temp / 4.0); + Serial.println(" celcius"); + } else { + Serial.println("not available"); + } + + Serial.println(); + + delay(1000); +} + +void printDec2(int value) +{ + Serial.print((char)('0' + (value / 10))); + Serial.print((char)('0' + (value % 10))); +}