Working Version of the Real Time Clock module DS1307.

Dependents:   Alarma_rtc

Fork of RTC-DS1307 by Henry Leinen

Committer:
leihen
Date:
Sun Jun 02 18:57:26 2013 +0000
Revision:
1:64274190e842
Parent:
0:3940f0ad2ca5
Child:
2:ee81f2c5a706
First complete Version - untested.

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 0:3940f0ad2ca5 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 0:3940f0ad2ca5 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 0:3940f0ad2ca5 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 */
leihen 0:3940f0ad2ca5 23 #ifndef __RTC_DS1307_H__
leihen 0:3940f0ad2ca5 24 #define __RTC_DS1307_H__
leihen 0:3940f0ad2ca5 25
leihen 0:3940f0ad2ca5 26 #include "mbed.h"
leihen 0:3940f0ad2ca5 27
leihen 1:64274190e842 28 typedef struct {
leihen 1:64274190e842 29 int sec;
leihen 1:64274190e842 30 int min;
leihen 1:64274190e842 31 int hour;
leihen 1:64274190e842 32 int wday;
leihen 1:64274190e842 33 int date;
leihen 1:64274190e842 34 int mon;
leihen 1:64274190e842 35 int year;
leihen 1:64274190e842 36 } Time;
leihen 0:3940f0ad2ca5 37
leihen 0:3940f0ad2ca5 38 /** Class Rtc_Ds1307 implements the real time clock module DS1307
leihen 0:3940f0ad2ca5 39 *
leihen 0:3940f0ad2ca5 40 * You can read the clock and set a new time and date.
leihen 0:3940f0ad2ca5 41 * It is also possible to start and stop the clock.
leihen 0:3940f0ad2ca5 42 * Rtc_Ds1307 allows you to display the time in a 12h or 24h format
leihen 0:3940f0ad2ca5 43 */
leihen 0:3940f0ad2ca5 44 class Rtc_Ds1307
leihen 0:3940f0ad2ca5 45 {
leihen 0:3940f0ad2ca5 46 I2C* m_rtc;
leihen 1:64274190e842 47
leihen 1:64274190e842 48 static const char *m_weekDays[];
leihen 1:64274190e842 49
leihen 0:3940f0ad2ca5 50 public:
leihen 0:3940f0ad2ca5 51 /** public constructor which creates the real time clock object
leihen 0:3940f0ad2ca5 52 *
leihen 0:3940f0ad2ca5 53 * @param sda : specifies the pin for the SDA communication line.
leihen 0:3940f0ad2ca5 54 *
leihen 0:3940f0ad2ca5 55 * @param scl : the pin for the serial clock
leihen 0:3940f0ad2ca5 56 *
leihen 0:3940f0ad2ca5 57 */
leihen 0:3940f0ad2ca5 58 Rtc_Ds1307(PinName sda, PinName scl);
leihen 0:3940f0ad2ca5 59
leihen 0:3940f0ad2ca5 60 ~Rtc_Ds1307();
leihen 0:3940f0ad2ca5 61
leihen 1:64274190e842 62 /** Read the current time from RTC chip
leihen 1:64274190e842 63 *
leihen 1:64274190e842 64 * @param time : reference to a struct tm which will be filled with the time from rtc
leihen 1:64274190e842 65 *
leihen 1:64274190e842 66 * @returns true if successful, otherwise an acknowledge error occured
leihen 1:64274190e842 67 */
leihen 1:64274190e842 68 bool getTime(Time& time);
leihen 1:64274190e842 69
leihen 1:64274190e842 70 /** Write the given time onto the RTC chip
leihen 1:64274190e842 71 *
leihen 1:64274190e842 72 * @param time : refereence to a struct which contains valid date and time information
leihen 1:64274190e842 73 *
leihen 1:64274190e842 74 * @param start : contains true if the clock shall start (or keep on running).
leihen 1:64274190e842 75 *
leihen 1:64274190e842 76 * @param thm : 12-hour-mode if set to true, otherwise 24-hour-mode will be set.
leihen 1:64274190e842 77 *
leihen 1:64274190e842 78 * @returns true if successful, otherwise an acknowledge error occured
leihen 1:64274190e842 79 */
leihen 1:64274190e842 80 bool setTime(Time& time, bool start, bool thm);
leihen 1:64274190e842 81
leihen 0:3940f0ad2ca5 82
leihen 1:64274190e842 83 /** Service function to convert a weekday into a string representation
leihen 1:64274190e842 84 *
leihen 1:64274190e842 85 * @param wday : day of week to convert (starting with sunday = 1, monday = 2, ..., saturday = 7
leihen 1:64274190e842 86 *
leihen 1:64274190e842 87 * @returns the corresponding string representation
leihen 1:64274190e842 88 */
leihen 1:64274190e842 89 const char* weekdayToString( int wday )
leihen 1:64274190e842 90 { return m_weekDays[wday%7]; }
leihen 1:64274190e842 91
leihen 1:64274190e842 92 private:
leihen 1:64274190e842 93 bool read(int address, char* buffer, int len);
leihen 1:64274190e842 94 bool write(int address, char* buffer, int len);
leihen 1:64274190e842 95
leihen 1:64274190e842 96 static int bcdToDecimal(int bcd)
leihen 1:64274190e842 97 { return ((bcd&0xF0)>>4)*10 + (bcd&0x0F); }
leihen 1:64274190e842 98
leihen 1:64274190e842 99 static int decimalToBcd(int dec)
leihen 1:64274190e842 100 { return (dec%10) + ((dec/10)<<4); }
leihen 1:64274190e842 101
leihen 1:64274190e842 102
leihen 0:3940f0ad2ca5 103 };
leihen 0:3940f0ad2ca5 104
leihen 0:3940f0ad2ca5 105
leihen 0:3940f0ad2ca5 106 #endif // __RTC_DS1307_H__