Simple RTC class based on DS1307. Emphasis on simple. Allows you to run at 100k or 400k Hz (for newer DS1307 capable devices). MapTime() allows you to set the time() service to the same as the RTC. Uses struct tm throughout so you can use traditional time functions for manipulation.

Dependents:   AdaFruit_RGBLCD

Committer:
vtraveller
Date:
Sat Jan 24 15:59:29 2015 +0000
Revision:
17:bdc15c054ac1
Parent:
13:1ccadbd4c1bd
Fixed bug where tm struct used a month range of 0-11 and the DS3231 uses a range 1-12.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vtraveller 0:98b84d9c8c96 1 #ifndef __RTCLOCK_H__
vtraveller 0:98b84d9c8c96 2 #define __RTCLOCK_H__
vtraveller 0:98b84d9c8c96 3
vtraveller 0:98b84d9c8c96 4 #include <time.h>
vtraveller 0:98b84d9c8c96 5
vtraveller 0:98b84d9c8c96 6 class RTclock
vtraveller 0:98b84d9c8c96 7 {
vtraveller 0:98b84d9c8c96 8 public:
vtraveller 0:98b84d9c8c96 9 // Frequency values for the square wave output
vtraveller 0:98b84d9c8c96 10 enum ESquareWaveRates
vtraveller 0:98b84d9c8c96 11 {
vtraveller 0:98b84d9c8c96 12 eSWR_1Hz = 0,
vtraveller 0:98b84d9c8c96 13 eSWR_4kHz = 1,
vtraveller 0:98b84d9c8c96 14 eSWR_8kHz = 2,
vtraveller 0:98b84d9c8c96 15 eSWR_32Hz = 3
vtraveller 0:98b84d9c8c96 16 };
vtraveller 9:3a0ba8364ef2 17
vtraveller 9:3a0ba8364ef2 18 enum EClockType
vtraveller 9:3a0ba8364ef2 19 {
vtraveller 9:3a0ba8364ef2 20 eDS1311,
vtraveller 9:3a0ba8364ef2 21 eDS3231,
vtraveller 9:3a0ba8364ef2 22 };
vtraveller 0:98b84d9c8c96 23
vtraveller 0:98b84d9c8c96 24 public:
vtraveller 0:98b84d9c8c96 25 RTclock
vtraveller 0:98b84d9c8c96 26 (
vtraveller 10:e5eabd3a1ca6 27 I2C & in_cI2C,
vtraveller 13:1ccadbd4c1bd 28 uint8_t in_nAddress,
vtraveller 10:e5eabd3a1ca6 29 EClockType in_eClockType
vtraveller 0:98b84d9c8c96 30 );
vtraveller 0:98b84d9c8c96 31 virtual ~RTclock();
vtraveller 0:98b84d9c8c96 32
vtraveller 7:3621025e7949 33 bool isTwelveHour(); // true if set to a 12hr clock (adjust tm.tm_hour as appropraite)
vtraveller 7:3621025e7949 34 virtual bool mapTime(); // Maps RTC chip to C time.h time system
vtraveller 7:3621025e7949 35 virtual bool getTime(tm & out_sTM); // Get a TM structure directly
vtraveller 7:3621025e7949 36 virtual bool setSquareWaveOutput
vtraveller 0:98b84d9c8c96 37 (
vtraveller 0:98b84d9c8c96 38 bool in_bEnable,
vtraveller 0:98b84d9c8c96 39 ESquareWaveRates in_nRateSelect
vtraveller 0:98b84d9c8c96 40 );
vtraveller 7:3621025e7949 41 virtual bool setTime // Set time time using a TM structure (always starts)
vtraveller 5:d71d6e5a7eee 42 (
vtraveller 5:d71d6e5a7eee 43 const tm & in_sTM,
vtraveller 5:d71d6e5a7eee 44 bool in_bTwelveHour
vtraveller 5:d71d6e5a7eee 45 );
vtraveller 0:98b84d9c8c96 46
vtraveller 0:98b84d9c8c96 47 protected:
vtraveller 7:3621025e7949 48 static int bcdToDecimal(int in_nBCD);
vtraveller 7:3621025e7949 49 static int decimalToBcd(int in_nDecimal);
vtraveller 0:98b84d9c8c96 50 virtual bool read
vtraveller 0:98b84d9c8c96 51 (
vtraveller 11:49b987f6ae26 52 uint8_t in_nAddress,
vtraveller 0:98b84d9c8c96 53 char * out_pBuffer,
vtraveller 0:98b84d9c8c96 54 int in_nLength
vtraveller 0:98b84d9c8c96 55 );
vtraveller 7:3621025e7949 56 virtual bool setRunning(bool in_nEnable);
vtraveller 0:98b84d9c8c96 57 virtual bool write
vtraveller 0:98b84d9c8c96 58 (
vtraveller 11:49b987f6ae26 59 uint8_t in_nAddress,
vtraveller 0:98b84d9c8c96 60 const char * in_pBuffer,
vtraveller 0:98b84d9c8c96 61 int in_nLength
vtraveller 0:98b84d9c8c96 62 );
vtraveller 0:98b84d9c8c96 63
vtraveller 0:98b84d9c8c96 64 private:
vtraveller 9:3a0ba8364ef2 65 static const char * m_aWeekDays[]; // Days of the week
vtraveller 5:d71d6e5a7eee 66 bool m_bTwelveHour;
vtraveller 10:e5eabd3a1ca6 67 I2C & m_cI2C;
vtraveller 13:1ccadbd4c1bd 68 uint8_t m_nAddress;
vtraveller 9:3a0ba8364ef2 69 EClockType m_eClockType;
vtraveller 0:98b84d9c8c96 70 };
vtraveller 0:98b84d9c8c96 71
vtraveller 0:98b84d9c8c96 72 #endif // __RTCLOCK_H__