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 Aug 30 15:55:28 2014 +0000
Revision:
9:3a0ba8364ef2
Parent:
7:3621025e7949
Child:
10:e5eabd3a1ca6
Added support for DS3231

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