*Rewritten working version of* Working Version of the Real Time Clock module DS1307.

Dependents:   Nucleo_praktyki

Fork of RTC-DS1307 by Henry Leinen

Committer:
amateusz
Date:
Wed Jan 31 11:50:07 2018 +0000
Revision:
10:8c0c306cee03
Parent:
Rtc_Ds1307.h@7:dca20be3ef38
Oh yeah, I changed something. The idea behind was it to be more uniform or something.. Sorry. I'm hoping this commit is my private as I only want to publish a program.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leihen 0:3940f0ad2ca5 1 /* Rtc_Ds1307.h */
leihen 0:3940f0ad2ca5 2 /*
leihen 0:3940f0ad2ca5 3 Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
leihen 7:dca20be3ef38 4
leihen 0:3940f0ad2ca5 5 Permission is hereby granted, free of charge, to any person obtaining a copy
leihen 0:3940f0ad2ca5 6 of this software and associated documentation files (the "Software"), to deal
leihen 0:3940f0ad2ca5 7 in the Software without restriction, including without limitation the rights
leihen 0:3940f0ad2ca5 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
leihen 0:3940f0ad2ca5 9 copies of the Software, and to permit persons to whom the Software is
leihen 0:3940f0ad2ca5 10 furnished to do so, subject to the following conditions:
leihen 7:dca20be3ef38 11
leihen 0:3940f0ad2ca5 12 The above copyright notice and this permission notice shall be included in
leihen 0:3940f0ad2ca5 13 all copies or substantial portions of the Software.
leihen 7:dca20be3ef38 14
leihen 0:3940f0ad2ca5 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
leihen 0:3940f0ad2ca5 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
leihen 0:3940f0ad2ca5 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
leihen 0:3940f0ad2ca5 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
leihen 0:3940f0ad2ca5 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
leihen 0:3940f0ad2ca5 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
leihen 0:3940f0ad2ca5 21 THE SOFTWARE.
leihen 0:3940f0ad2ca5 22 */
amateusz 10:8c0c306cee03 23 #include "mbed.h"
amateusz 10:8c0c306cee03 24
amateusz 10:8c0c306cee03 25 #ifndef __DS1307_H__
amateusz 10:8c0c306cee03 26 #define __DS1307_H__
leihen 0:3940f0ad2ca5 27
leihen 0:3940f0ad2ca5 28
leihen 7:dca20be3ef38 29
leihen 6:bba89618ee63 30 /** Class Rtc_Ds1307 implements the real time clock module DS1307
leihen 6:bba89618ee63 31 *
leihen 6:bba89618ee63 32 * You can read the clock and set a new time and date.
leihen 6:bba89618ee63 33 * It is also possible to start and stop the clock.
leihen 6:bba89618ee63 34 * Rtc_Ds1307 allows you to display the time in a 12h or 24h format
leihen 6:bba89618ee63 35 */
amateusz 10:8c0c306cee03 36 class DS1307
leihen 6:bba89618ee63 37 {
leihen 7:dca20be3ef38 38 public:
leihen 7:dca20be3ef38 39 /** Structure which is used to exchange the time and date
leihen 7:dca20be3ef38 40 */
leihen 7:dca20be3ef38 41 typedef struct {
leihen 7:dca20be3ef38 42 int sec; /*!< seconds [0..59] */
leihen 7:dca20be3ef38 43 int min; /*!< minutes {0..59] */
leihen 7:dca20be3ef38 44 int hour; /*!< hours [0..23] */
leihen 7:dca20be3ef38 45 int wday; /*!< weekday [1..7, where 1 = sunday, 2 = monday, ... */
leihen 7:dca20be3ef38 46 int date; /*!< day of month [0..31] */
leihen 7:dca20be3ef38 47 int mon; /*!< month of year [1..12] */
leihen 7:dca20be3ef38 48 int year; /*!< year [2000..2255] */
leihen 7:dca20be3ef38 49 } Time_rtc;
leihen 1:64274190e842 50
leihen 1:64274190e842 51
leihen 7:dca20be3ef38 52 /** RateSelect specifies the valid frequency values for the square wave output
leihen 7:dca20be3ef38 53 */
leihen 7:dca20be3ef38 54 typedef enum {
leihen 7:dca20be3ef38 55 RS1Hz = 0,
leihen 7:dca20be3ef38 56 RS4kHz = 1,
leihen 7:dca20be3ef38 57 RS8kHz = 2,
leihen 7:dca20be3ef38 58 RS32kHz = 3
leihen 7:dca20be3ef38 59 } SqwRateSelect_t;
leihen 7:dca20be3ef38 60
leihen 7:dca20be3ef38 61 protected:
amateusz 10:8c0c306cee03 62 I2C * m_rtc;
leihen 7:dca20be3ef38 63
leihen 7:dca20be3ef38 64 static const char *m_weekDays[];
leihen 7:dca20be3ef38 65
leihen 7:dca20be3ef38 66 public:
leihen 7:dca20be3ef38 67 /** public constructor which creates the real time clock object
leihen 7:dca20be3ef38 68 *
leihen 7:dca20be3ef38 69 * @param sda : specifies the pin for the SDA communication line.
leihen 7:dca20be3ef38 70 *
leihen 7:dca20be3ef38 71 * @param scl : the pin for the serial clock
leihen 7:dca20be3ef38 72 *
leihen 7:dca20be3ef38 73 */
amateusz 10:8c0c306cee03 74 DS1307(I2C * );
leihen 7:dca20be3ef38 75
amateusz 10:8c0c306cee03 76 ~DS1307();
leihen 7:dca20be3ef38 77
leihen 7:dca20be3ef38 78 /** Read the current time from RTC chip
leihen 7:dca20be3ef38 79 *
leihen 7:dca20be3ef38 80 * @param time : reference to a struct tm which will be filled with the time from rtc
leihen 7:dca20be3ef38 81 *
leihen 7:dca20be3ef38 82 * @returns true if successful, otherwise an acknowledge error occured
leihen 7:dca20be3ef38 83 */
leihen 7:dca20be3ef38 84 virtual bool getTime(Time_rtc& time);
leihen 7:dca20be3ef38 85
leihen 7:dca20be3ef38 86 /** Write the given time onto the RTC chip
leihen 7:dca20be3ef38 87 *
leihen 7:dca20be3ef38 88 * @param time : refereence to a struct which contains valid date and time information
leihen 7:dca20be3ef38 89 *
leihen 7:dca20be3ef38 90 * @param start : contains true if the clock shall start (or keep on running).
leihen 7:dca20be3ef38 91 *
leihen 7:dca20be3ef38 92 * @param thm : 12-hour-mode if set to true, otherwise 24-hour-mode will be set.
leihen 7:dca20be3ef38 93 *
leihen 7:dca20be3ef38 94 * @returns true if successful, otherwise an acknowledge error occured
leihen 7:dca20be3ef38 95 */
leihen 7:dca20be3ef38 96 virtual bool setTime(Time_rtc& time, bool start, bool thm);
leihen 7:dca20be3ef38 97
leihen 7:dca20be3ef38 98 /** Start the clock. Please note that the seconds register need to be read and
leihen 7:dca20be3ef38 99 * written in order to start or stop the clock. This can lead to an error
leihen 7:dca20be3ef38 100 * in the time value. The recommended way of starting and stoping the clock is
leihen 7:dca20be3ef38 101 * to write the actual date and time and set the start bit accordingly.
leihen 7:dca20be3ef38 102 *
leihen 7:dca20be3ef38 103 * @returns true if the clock was started, false if a communication error occured
leihen 7:dca20be3ef38 104 */
leihen 7:dca20be3ef38 105 bool startClock();
leihen 7:dca20be3ef38 106
leihen 7:dca20be3ef38 107 /** Stop the clock. Please note that the seconds register need to be read and
leihen 7:dca20be3ef38 108 * written in order to start or stop the clock. This can lead to an error
leihen 7:dca20be3ef38 109 * in the time value. The recommended way of starting and stoping the clock is
leihen 7:dca20be3ef38 110 * to write the actual date and time and set the start bit accordingly.
leihen 7:dca20be3ef38 111 *
leihen 7:dca20be3ef38 112 * @returns true if the clock was stopped, false if a communication error occured
leihen 7:dca20be3ef38 113 */
leihen 7:dca20be3ef38 114 bool stopClock();
leihen 7:dca20be3ef38 115
leihen 7:dca20be3ef38 116 /** Service function to convert a weekday into a string representation
leihen 7:dca20be3ef38 117 *
leihen 7:dca20be3ef38 118 * @param wday : day of week to convert (starting with sunday = 1, monday = 2, ..., saturday = 7
leihen 7:dca20be3ef38 119 *
leihen 7:dca20be3ef38 120 * @returns the corresponding string representation
leihen 7:dca20be3ef38 121 */
leihen 7:dca20be3ef38 122 const char* weekdayToString( int wday ) {
leihen 7:dca20be3ef38 123 return m_weekDays[wday%7];
leihen 7:dca20be3ef38 124 }
leihen 7:dca20be3ef38 125
leihen 7:dca20be3ef38 126 /** Enable Square Wave output. The function enables or disables the square wave output
leihen 7:dca20be3ef38 127 * of the module and sets the desired frequency.
leihen 7:dca20be3ef38 128 *
leihen 7:dca20be3ef38 129 * @param ena : if set to true, the square wave output is enabled.
leihen 7:dca20be3ef38 130 *
leihen 7:dca20be3ef38 131 * @param rs : rate select, can be either one of the four values defined by type /c RateSelect_t
leihen 7:dca20be3ef38 132 *
leihen 7:dca20be3ef38 133 * @return true if the operation was successful or false otherwise
leihen 7:dca20be3ef38 134 */
leihen 7:dca20be3ef38 135 bool setSquareWaveOutput(bool ena, SqwRateSelect_t rs);
amateusz 10:8c0c306cee03 136
amateusz 10:8c0c306cee03 137 void setLocalTime();
leihen 7:dca20be3ef38 138
leihen 7:dca20be3ef38 139 private:
leihen 7:dca20be3ef38 140 bool read(int address, char* buffer, int len);
leihen 7:dca20be3ef38 141 bool write(int address, char* buffer, int len);
leihen 7:dca20be3ef38 142
leihen 7:dca20be3ef38 143 static int bcdToDecimal(int bcd) {
leihen 7:dca20be3ef38 144 return ((bcd&0xF0)>>4)*10 + (bcd&0x0F);
leihen 7:dca20be3ef38 145 }
leihen 7:dca20be3ef38 146
leihen 7:dca20be3ef38 147 static int decimalToBcd(int dec) {
leihen 7:dca20be3ef38 148 return (dec%10) + ((dec/10)<<4);
leihen 7:dca20be3ef38 149 }
leihen 0:3940f0ad2ca5 150 };
leihen 7:dca20be3ef38 151
leihen 7:dca20be3ef38 152
leihen 7:dca20be3ef38 153
leihen 7:dca20be3ef38 154 typedef void (*RtcCallback_t) (void);
leihen 7:dca20be3ef38 155
leihen 7:dca20be3ef38 156
amateusz 10:8c0c306cee03 157 class RtcCls : public DS1307
leihen 7:dca20be3ef38 158 {
leihen 7:dca20be3ef38 159 protected:
leihen 7:dca20be3ef38 160 InterruptIn m_sqw;
leihen 7:dca20be3ef38 161 bool m_bUseSqw;
leihen 7:dca20be3ef38 162 time_t m_time; // Only used in case SQW is used
leihen 7:dca20be3ef38 163
leihen 7:dca20be3ef38 164 bool m_bAlarmEnabled;
leihen 7:dca20be3ef38 165 RtcCallback_t m_alarmfunc;
leihen 7:dca20be3ef38 166 time_t m_alarmTime;
leihen 7:dca20be3ef38 167
leihen 7:dca20be3ef38 168 public:
amateusz 10:8c0c306cee03 169 RtcCls(I2C * i2c, PinName sqw, bool bUseSqw);
leihen 7:dca20be3ef38 170
leihen 7:dca20be3ef38 171 protected:
leihen 7:dca20be3ef38 172 void _callback(void);
leihen 7:dca20be3ef38 173
leihen 7:dca20be3ef38 174 public:
leihen 7:dca20be3ef38 175 time_t getTime();
amateusz 10:8c0c306cee03 176 virtual bool getTime(Time_rtc& time) { return DS1307::getTime(time); }
leihen 7:dca20be3ef38 177 void setTime(time_t time);
amateusz 10:8c0c306cee03 178 virtual bool setTime(Time_rtc& time, bool start, bool thm) { return DS1307::setTime(time, start, thm); }
leihen 7:dca20be3ef38 179 public:
leihen 7:dca20be3ef38 180 void setAlarm(int nSeconds, RtcCallback_t alarmfunc) {
leihen 7:dca20be3ef38 181 m_alarmfunc = alarmfunc;
leihen 7:dca20be3ef38 182 m_alarmTime = m_time + nSeconds;
leihen 7:dca20be3ef38 183 m_bAlarmEnabled = (alarmfunc == NULL) ? false : true;
leihen 7:dca20be3ef38 184 }
leihen 7:dca20be3ef38 185 };
leihen 7:dca20be3ef38 186
amateusz 10:8c0c306cee03 187 #endif // __DS1307_H__