Skip to content
Newer
Older
100644 121 lines (105 sloc) 4.43 KB
13ffa45 @davidhbrown Initial release; functional
authored Apr 29, 2011
1 /*
2 RealTimeClockDS1307 - library to control a DS1307 RTC module
3 Copyright (c) 2011 David H. Brown. All rights reserved
adaf390 Updated for Arduino 1.00
Andreas Giemza authored Dec 11, 2011
4
5 v0.92 Updated for Arduino 1.00; not re-tested on earlier versions
13ffa45 @davidhbrown Initial release; functional
authored Apr 29, 2011
6
7 Much thanks to John Waters and Maurice Ribble for their
8 earlier and very helpful work (even if I didn't wind up
9 using any of their code):
10 - http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock
11 - http://www.glacialwanderer.com/hobbyrobotics/?p=12
12
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 2.1 of the License, or (at your option) any later version.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28 #ifndef RealTimeClockDS1307_h
29 #define RealTimeClockDS1307_h
adaf390 Updated for Arduino 1.00
Andreas Giemza authored Dec 11, 2011
30
31 #if defined(ARDUINO) && ARDUINO >= 100
32 #include "Arduino.h"
33 #else
34 #include "WProgram.h"
35 #endif
36
13ffa45 @davidhbrown Initial release; functional
authored Apr 29, 2011
37 //#include <HardwareSerial.h>
adaf390 Updated for Arduino 1.00
Andreas Giemza authored Dec 11, 2011
38 //#include <WConstants.h> //need/want 'boolean' and 'byte' types used by Arduino
13ffa45 @davidhbrown Initial release; functional
authored Apr 29, 2011
39 //#undef round is required to avoid a compile-time
40 //"expected unqualified-id before 'double'" error in math.h
41 //see: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1247924528/3
42 #undef round
43 #include <Wire.h>
44
45 #define ARDUINO_PIN_T uint8_t
46
47 class RealTimeClockDS1307
48 {
49 private:
50 byte _reg0_sec;
51 byte _reg1_min;
52 byte _reg2_hour;
53 byte _reg3_day;
54 byte _reg4_date;
55 byte _reg5_month;
56 byte _reg6_year;
57 byte _reg7_sqw;
58 byte decToBcd(byte);
59 byte bcdToDec(byte);
60 char lowNybbleToASCII(byte);
61 char highNybbleToASCII(byte);
62 public:
63 RealTimeClockDS1307();
64 void readClock();//read registers (incl sqw) to local store
65 void setClock();//update clock registers from local store
66 void stop();//immediate; does not require setClock();
67 void start();//immediate; does not require setClock();
68 void sqwEnable(byte);//enable the square wave with the specified frequency
69 void sqwDisable(boolean);//disable the square wave, setting output either high or low
70 void writeData(byte, byte);//write a single value to a register
03d98e2 @davidhbrown added multibyte read/write to RAM
authored Apr 29, 2011
71 void writeData(byte, void *, int);//write several values consecutively
13ffa45 @davidhbrown Initial release; functional
authored Apr 29, 2011
72 byte readData(byte);//read a single value from a register
03d98e2 @davidhbrown added multibyte read/write to RAM
authored Apr 30, 2011
73 void readData(byte, void *, int);//read several values into a buffer
13ffa45 @davidhbrown Initial release; functional
authored Apr 29, 2011
74
75 int getHours();
76 int getMinutes();
77 int getSeconds();
78 int getYear();
79 int getMonth();
6abd8c7 @hurik Reverse renaming of getDate() and setDate(), now getDay() is calling …
hurik authored Apr 6, 2013
80 int getDate();
d8142d9 @sudar Renamed getDate to getDay. Fixes #2
sudar authored Mar 30, 2013
81 int getDay();
13ffa45 @davidhbrown Initial release; functional
authored Apr 29, 2011
82 int getDayOfWeek();
83 boolean is12hour();
84 boolean isPM();
85 boolean isStopped();
86 //getFormatted writes into a char array provided by you. Format is:
87 // YY-MM-DD HH:II:SS ... plus "A" or "P" if in 12-hour mode
88 //and of course a NULL terminator. So, [18] for 24h or [19] for 12h
89 void getFormatted(char *);//see comment above
adaf390 Updated for Arduino 1.00
Andreas Giemza authored Dec 11, 2011
90 void getFormatted2k(char *);//as getFormatted, but with "20" prepended
13ffa45 @davidhbrown Initial release; functional
authored Apr 29, 2011
91
92 //must also call setClock() after any of these
93 //before next readClock(). Note that invalid dates are not
94 //corrected by the clock. All the clock knows is when it should
95 //roll over to the next month rather than the next date in the same month.
96 void setSeconds(int);
97 void setMinutes(int);
98 //setHours rejects values out of range for the current 12/24 mode
99 void setHours(int);
100 void setAM();//does not consider hours; see switchTo24()
101 void setPM();//does not consider hours; see switchTo24()
102 void set24h();//does not consider hours; see switchTo24()
103 void switchTo24h();//returns immediately if already 24h
104 void switchTo12h();//returns immediately if already 12h
105 void setDayOfWeek(int);//incremented at midnight; not set by date (no fixed meaning)
6abd8c7 @hurik Reverse renaming of getDate() and setDate(), now getDay() is calling …
hurik authored Apr 5, 2013
106 void setDate(int);//allows 1-31 for *all* months.
107 void setDay(int);
13ffa45 @davidhbrown Initial release; functional
authored Apr 29, 2011
108 void setMonth(int);
109 void setYear(int);
110
111 //squarewave frequencies:
112 static const byte SQW_1Hz=0x00;
113 static const byte SQW_4kHz=0x01;//actually 4.096kHz
114 static const byte SQW_8kHz=0x02;//actually 8.192kHz
115 static const byte SQW_32kHz=0x03;//actually 32.768kHz
116 };
adaf390 Updated for Arduino 1.00
Andreas Giemza authored Dec 11, 2011
117
13ffa45 @davidhbrown Initial release; functional
authored Apr 29, 2011
118 extern RealTimeClockDS1307 RTC;
adaf390 Updated for Arduino 1.00
Andreas Giemza authored Dec 11, 2011
119
d8142d9 @sudar Renamed getDate to getDay. Fixes #2
sudar authored Mar 30, 2013
120 #endif
Something went wrong with that request. Please try again.