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