pour olivier

Dependents:   Rtc_Ds1307_Sample

Fork of RTC-DS1307 by Henry Leinen

Committer:
leihen
Date:
Sun Jun 23 18:26:47 2013 +0000
Revision:
6:bba89618ee63
Parent:
5:30531f2121a2
Child:
7:dca20be3ef38
rewritten the Header due to some strange error while compiling.

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
leihen 6:bba89618ee63 27 /** Class Rtc_Ds1307 implements the real time clock module DS1307
leihen 6:bba89618ee63 28 *
leihen 6:bba89618ee63 29 * You can read the clock and set a new time and date.
leihen 6:bba89618ee63 30 * It is also possible to start and stop the clock.
leihen 6:bba89618ee63 31 * Rtc_Ds1307 allows you to display the time in a 12h or 24h format
leihen 6:bba89618ee63 32 */
leihen 6:bba89618ee63 33 class Rtc_Ds1307
leihen 6:bba89618ee63 34 {
leihen 6:bba89618ee63 35
leihen 2:ee81f2c5a706 36 /** Structure which is used to exchange the time and date
leihen 2:ee81f2c5a706 37 */
leihen 1:64274190e842 38 typedef struct {
leihen 2:ee81f2c5a706 39 int sec; /*!< seconds [0..59] */
leihen 2:ee81f2c5a706 40 int min; /*!< minutes {0..59] */
leihen 2:ee81f2c5a706 41 int hour; /*!< hours [0..23] */
leihen 2:ee81f2c5a706 42 int wday; /*!< weekday [1..7, where 1 = sunday, 2 = monday, ... */
leihen 2:ee81f2c5a706 43 int date; /*!< day of month [0..31] */
leihen 2:ee81f2c5a706 44 int mon; /*!< month of year [1..12] */
leihen 2:ee81f2c5a706 45 int year; /*!< year [2000..2255] */
leihen 5:30531f2121a2 46 } Time_rtc;
leihen 0:3940f0ad2ca5 47
leihen 2:ee81f2c5a706 48 /** RateSelect specifies the valid frequency values for the square wave output
leihen 2:ee81f2c5a706 49 */
leihen 2:ee81f2c5a706 50 typedef enum {
leihen 5:30531f2121a2 51 RS1Hz = 0,
leihen 5:30531f2121a2 52 RS4kHz = 1,
leihen 5:30531f2121a2 53 RS8kHz = 2,
leihen 5:30531f2121a2 54 RS32kHz = 3
leihen 5:30531f2121a2 55 } SqwRateSelect_t;
leihen 0:3940f0ad2ca5 56 I2C* m_rtc;
leihen 1:64274190e842 57
leihen 1:64274190e842 58 static const char *m_weekDays[];
leihen 1:64274190e842 59
leihen 0:3940f0ad2ca5 60 public:
leihen 0:3940f0ad2ca5 61 /** public constructor which creates the real time clock object
leihen 0:3940f0ad2ca5 62 *
leihen 0:3940f0ad2ca5 63 * @param sda : specifies the pin for the SDA communication line.
leihen 0:3940f0ad2ca5 64 *
leihen 0:3940f0ad2ca5 65 * @param scl : the pin for the serial clock
leihen 0:3940f0ad2ca5 66 *
leihen 0:3940f0ad2ca5 67 */
leihen 0:3940f0ad2ca5 68 Rtc_Ds1307(PinName sda, PinName scl);
leihen 0:3940f0ad2ca5 69
leihen 0:3940f0ad2ca5 70 ~Rtc_Ds1307();
leihen 0:3940f0ad2ca5 71
leihen 1:64274190e842 72 /** Read the current time from RTC chip
leihen 1:64274190e842 73 *
leihen 1:64274190e842 74 * @param time : reference to a struct tm which will be filled with the time from rtc
leihen 1:64274190e842 75 *
leihen 1:64274190e842 76 * @returns true if successful, otherwise an acknowledge error occured
leihen 1:64274190e842 77 */
leihen 6:bba89618ee63 78 virtual bool getTime(Time_rtc& time);
leihen 1:64274190e842 79
leihen 1:64274190e842 80 /** Write the given time onto the RTC chip
leihen 1:64274190e842 81 *
leihen 1:64274190e842 82 * @param time : refereence to a struct which contains valid date and time information
leihen 1:64274190e842 83 *
leihen 1:64274190e842 84 * @param start : contains true if the clock shall start (or keep on running).
leihen 1:64274190e842 85 *
leihen 1:64274190e842 86 * @param thm : 12-hour-mode if set to true, otherwise 24-hour-mode will be set.
leihen 1:64274190e842 87 *
leihen 1:64274190e842 88 * @returns true if successful, otherwise an acknowledge error occured
leihen 1:64274190e842 89 */
leihen 6:bba89618ee63 90 virtual bool setTime(Time_rtc& time, bool start, bool thm);
leihen 1:64274190e842 91
leihen 2:ee81f2c5a706 92 /** Start the clock. Please note that the seconds register need to be read and
leihen 2:ee81f2c5a706 93 * written in order to start or stop the clock. This can lead to an error
leihen 2:ee81f2c5a706 94 * in the time value. The recommended way of starting and stoping the clock is
leihen 2:ee81f2c5a706 95 * to write the actual date and time and set the start bit accordingly.
leihen 2:ee81f2c5a706 96 *
leihen 2:ee81f2c5a706 97 * @returns true if the clock was started, false if a communication error occured
leihen 2:ee81f2c5a706 98 */
leihen 2:ee81f2c5a706 99 bool startClock();
leihen 2:ee81f2c5a706 100
leihen 2:ee81f2c5a706 101 /** Stop the clock. Please note that the seconds register need to be read and
leihen 2:ee81f2c5a706 102 * written in order to start or stop the clock. This can lead to an error
leihen 2:ee81f2c5a706 103 * in the time value. The recommended way of starting and stoping the clock is
leihen 2:ee81f2c5a706 104 * to write the actual date and time and set the start bit accordingly.
leihen 2:ee81f2c5a706 105 *
leihen 2:ee81f2c5a706 106 * @returns true if the clock was stopped, false if a communication error occured
leihen 2:ee81f2c5a706 107 */
leihen 2:ee81f2c5a706 108 bool stopClock();
leihen 0:3940f0ad2ca5 109
leihen 1:64274190e842 110 /** Service function to convert a weekday into a string representation
leihen 1:64274190e842 111 *
leihen 1:64274190e842 112 * @param wday : day of week to convert (starting with sunday = 1, monday = 2, ..., saturday = 7
leihen 1:64274190e842 113 *
leihen 1:64274190e842 114 * @returns the corresponding string representation
leihen 1:64274190e842 115 */
leihen 1:64274190e842 116 const char* weekdayToString( int wday )
leihen 1:64274190e842 117 { return m_weekDays[wday%7]; }
leihen 1:64274190e842 118
leihen 2:ee81f2c5a706 119 /** Enable Square Wave output. The function enables or disables the square wave output
leihen 2:ee81f2c5a706 120 * of the module and sets the desired frequency.
leihen 2:ee81f2c5a706 121 *
leihen 2:ee81f2c5a706 122 * @param ena : if set to true, the square wave output is enabled.
leihen 2:ee81f2c5a706 123 *
leihen 2:ee81f2c5a706 124 * @param rs : rate select, can be either one of the four values defined by type /c RateSelect_t
leihen 2:ee81f2c5a706 125 *
leihen 2:ee81f2c5a706 126 * @return true if the operation was successful or false otherwise
leihen 2:ee81f2c5a706 127 */
leihen 5:30531f2121a2 128 bool setSquareWaveOutput(bool ena, SqwRateSelect_t rs);
leihen 2:ee81f2c5a706 129
leihen 1:64274190e842 130 private:
leihen 1:64274190e842 131 bool read(int address, char* buffer, int len);
leihen 1:64274190e842 132 bool write(int address, char* buffer, int len);
leihen 1:64274190e842 133
leihen 1:64274190e842 134 static int bcdToDecimal(int bcd)
leihen 1:64274190e842 135 { return ((bcd&0xF0)>>4)*10 + (bcd&0x0F); }
leihen 1:64274190e842 136
leihen 1:64274190e842 137 static int decimalToBcd(int dec)
leihen 1:64274190e842 138 { return (dec%10) + ((dec/10)<<4); }
leihen 1:64274190e842 139
leihen 1:64274190e842 140
leihen 0:3940f0ad2ca5 141 };
leihen 0:3940f0ad2ca5 142 #endif // __RTC_DS1307_H__