MODULO TEMPORIZADOR

Dependents:   IngresoHORA Tarea_5

Fork of RTC-DS1307 by Henry Leinen

Committer:
Jesse
Date:
Wed Apr 30 23:43:58 2014 +0000
Revision:
10:00077b7605fd
Parent:
7:dca20be3ef38
Ejemplo RTC

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