├── DS1307RTC.cpp ├── DS1307RTC.h ├── docs └── issue_template.md ├── examples ├── ReadTest │ └── ReadTest.ino └── SetTime │ └── SetTime.ino ├── keywords.txt ├── library.json ├── library.properties └── readme.txt /DS1307RTC.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * DS1307RTC.h - library for DS1307 RTC 3 | 4 | Copyright (c) Michael Margolis 2009 5 | This library is intended to be uses with Arduino Time library functions 6 | 7 | The library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library; if not, write to the Free Software 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 | 21 | 30 Dec 2009 - Initial release 22 | 5 Sep 2011 updated for Arduino 1.0 23 | */ 24 | 25 | 26 | #if defined (__AVR_ATtiny84__) || defined(__AVR_ATtiny85__) || (__AVR_ATtiny2313__) 27 | #include 28 | #define Wire TinyWireM 29 | #else 30 | #include 31 | #endif 32 | #include "DS1307RTC.h" 33 | 34 | #define DS1307_CTRL_ID 0x68 35 | 36 | DS1307RTC::DS1307RTC() 37 | { 38 | Wire.begin(); 39 | } 40 | 41 | // PUBLIC FUNCTIONS 42 | time_t DS1307RTC::get() // Aquire data from buffer and convert to time_t 43 | { 44 | tmElements_t tm; 45 | if (read(tm) == false) return 0; 46 | return(makeTime(tm)); 47 | } 48 | 49 | bool DS1307RTC::set(time_t t) 50 | { 51 | tmElements_t tm; 52 | breakTime(t, tm); 53 | return write(tm); 54 | } 55 | 56 | // Aquire data from the RTC chip in BCD format 57 | bool DS1307RTC::read(tmElements_t &tm) 58 | { 59 | uint8_t sec; 60 | Wire.beginTransmission(DS1307_CTRL_ID); 61 | #if ARDUINO >= 100 62 | Wire.write((uint8_t)0x00); 63 | #else 64 | Wire.send(0x00); 65 | #endif 66 | if (Wire.endTransmission() != 0) { 67 | exists = false; 68 | return false; 69 | } 70 | exists = true; 71 | 72 | // request the 7 data fields (secs, min, hr, dow, date, mth, yr) 73 | Wire.requestFrom(DS1307_CTRL_ID, tmNbrFields); 74 | if (Wire.available() < tmNbrFields) return false; 75 | #if ARDUINO >= 100 76 | sec = Wire.read(); 77 | tm.Second = bcd2dec(sec & 0x7f); 78 | tm.Minute = bcd2dec(Wire.read() ); 79 | tm.Hour = bcd2dec(Wire.read() & 0x3f); // mask assumes 24hr clock 80 | tm.Wday = bcd2dec(Wire.read() ); 81 | tm.Day = bcd2dec(Wire.read() ); 82 | tm.Month = bcd2dec(Wire.read() ); 83 | tm.Year = y2kYearToTm((bcd2dec(Wire.read()))); 84 | #else 85 | sec = Wire.receive(); 86 | tm.Second = bcd2dec(sec & 0x7f); 87 | tm.Minute = bcd2dec(Wire.receive() ); 88 | tm.Hour = bcd2dec(Wire.receive() & 0x3f); // mask assumes 24hr clock 89 | tm.Wday = bcd2dec(Wire.receive() ); 90 | tm.Day = bcd2dec(Wire.receive() ); 91 | tm.Month = bcd2dec(Wire.receive() ); 92 | tm.Year = y2kYearToTm((bcd2dec(Wire.receive()))); 93 | #endif 94 | if (sec & 0x80) return false; // clock is halted 95 | return true; 96 | } 97 | 98 | bool DS1307RTC::write(tmElements_t &tm) 99 | { 100 | // To eliminate any potential race conditions, 101 | // stop the clock before writing the values, 102 | // then restart it after. 103 | Wire.beginTransmission(DS1307_CTRL_ID); 104 | #if ARDUINO >= 100 105 | Wire.write((uint8_t)0x00); // reset register pointer 106 | Wire.write((uint8_t)0x80); // Stop the clock. The seconds will be written last 107 | Wire.write(dec2bcd(tm.Minute)); 108 | Wire.write(dec2bcd(tm.Hour)); // sets 24 hour format 109 | Wire.write(dec2bcd(tm.Wday)); 110 | Wire.write(dec2bcd(tm.Day)); 111 | Wire.write(dec2bcd(tm.Month)); 112 | Wire.write(dec2bcd(tmYearToY2k(tm.Year))); 113 | #else 114 | Wire.send(0x00); // reset register pointer 115 | Wire.send(0x80); // Stop the clock. The seconds will be written last 116 | Wire.send(dec2bcd(tm.Minute)); 117 | Wire.send(dec2bcd(tm.Hour)); // sets 24 hour format 118 | Wire.send(dec2bcd(tm.Wday)); 119 | Wire.send(dec2bcd(tm.Day)); 120 | Wire.send(dec2bcd(tm.Month)); 121 | Wire.send(dec2bcd(tmYearToY2k(tm.Year))); 122 | #endif 123 | if (Wire.endTransmission() != 0) { 124 | exists = false; 125 | return false; 126 | } 127 | exists = true; 128 | 129 | // Now go back and set the seconds, starting the clock back up as a side effect 130 | Wire.beginTransmission(DS1307_CTRL_ID); 131 | #if ARDUINO >= 100 132 | Wire.write((uint8_t)0x00); // reset register pointer 133 | Wire.write(dec2bcd(tm.Second)); // write the seconds, with the stop bit clear to restart 134 | #else 135 | Wire.send(0x00); // reset register pointer 136 | Wire.send(dec2bcd(tm.Second)); // write the seconds, with the stop bit clear to restart 137 | #endif 138 | if (Wire.endTransmission() != 0) { 139 | exists = false; 140 | return false; 141 | } 142 | exists = true; 143 | return true; 144 | } 145 | 146 | unsigned char DS1307RTC::isRunning() 147 | { 148 | Wire.beginTransmission(DS1307_CTRL_ID); 149 | #if ARDUINO >= 100 150 | Wire.write((uint8_t)0x00); 151 | #else 152 | Wire.send(0x00); 153 | #endif 154 | Wire.endTransmission(); 155 | 156 | // Just fetch the seconds register and check the top bit 157 | Wire.requestFrom(DS1307_CTRL_ID, 1); 158 | #if ARDUINO >= 100 159 | return !(Wire.read() & 0x80); 160 | #else 161 | return !(Wire.receive() & 0x80); 162 | #endif 163 | } 164 | 165 | void DS1307RTC::setCalibration(char calValue) 166 | { 167 | unsigned char calReg = abs(calValue) & 0x1f; 168 | if (calValue >= 0) calReg |= 0x20; // S bit is positive to speed up the clock 169 | Wire.beginTransmission(DS1307_CTRL_ID); 170 | #if ARDUINO >= 100 171 | Wire.write((uint8_t)0x07); // Point to calibration register 172 | Wire.write(calReg); 173 | #else 174 | Wire.send(0x07); // Point to calibration register 175 | Wire.send(calReg); 176 | #endif 177 | Wire.endTransmission(); 178 | } 179 | 180 | char DS1307RTC::getCalibration() 181 | { 182 | Wire.beginTransmission(DS1307_CTRL_ID); 183 | #if ARDUINO >= 100 184 | Wire.write((uint8_t)0x07); 185 | #else 186 | Wire.send(0x07); 187 | #endif 188 | Wire.endTransmission(); 189 | 190 | Wire.requestFrom(DS1307_CTRL_ID, 1); 191 | #if ARDUINO >= 100 192 | unsigned char calReg = Wire.read(); 193 | #else 194 | unsigned char calReg = Wire.receive(); 195 | #endif 196 | char out = calReg & 0x1f; 197 | if (!(calReg & 0x20)) out = -out; // S bit clear means a negative value 198 | return out; 199 | } 200 | 201 | // PRIVATE FUNCTIONS 202 | 203 | // Convert Decimal to Binary Coded Decimal (BCD) 204 | uint8_t DS1307RTC::dec2bcd(uint8_t num) 205 | { 206 | return ((num/10 * 16) + (num % 10)); 207 | } 208 | 209 | // Convert Binary Coded Decimal (BCD) to Decimal 210 | uint8_t DS1307RTC::bcd2dec(uint8_t num) 211 | { 212 | return ((num/16 * 10) + (num % 16)); 213 | } 214 | 215 | bool DS1307RTC::exists = false; 216 | 217 | DS1307RTC RTC = DS1307RTC(); // create an instance for the user 218 | 219 | -------------------------------------------------------------------------------- /DS1307RTC.h: -------------------------------------------------------------------------------- 1 | /* 2 | * DS1307RTC.h - library for DS1307 RTC 3 | * This library is intended to be uses with Arduino Time library functions 4 | */ 5 | 6 | #ifndef DS1307RTC_h 7 | #define DS1307RTC_h 8 | 9 | #include 10 | 11 | // library interface description 12 | class DS1307RTC 13 | { 14 | // user-accessible "public" interface 15 | public: 16 | DS1307RTC(); 17 | static time_t get(); 18 | static bool set(time_t t); 19 | static bool read(tmElements_t &tm); 20 | static bool write(tmElements_t &tm); 21 | static bool chipPresent() { return exists; } 22 | static unsigned char isRunning(); 23 | static void setCalibration(char calValue); 24 | static char getCalibration(); 25 | 26 | private: 27 | static bool exists; 28 | static uint8_t dec2bcd(uint8_t num); 29 | static uint8_t bcd2dec(uint8_t num); 30 | }; 31 | 32 | #ifdef RTC 33 | #undef RTC // workaround for Arduino Due, which defines "RTC"... 34 | #endif 35 | 36 | extern DS1307RTC RTC; 37 | 38 | #endif 39 | 40 | 41 | -------------------------------------------------------------------------------- /docs/issue_template.md: -------------------------------------------------------------------------------- 1 | Please use this form only to report code defects or bugs. 2 | 3 | For any question, even questions directly pertaining to this code, post your question on the forums related to the board you are using. 4 | 5 | Arduino: forum.arduino.cc 6 | Teensy: forum.pjrc.com 7 | ESP8266: www.esp8266.com 8 | ESP32: www.esp32.com 9 | Adafruit Feather/Metro/Trinket: forums.adafruit.com 10 | Particle Photon: community.particle.io 11 | 12 | If you are experiencing trouble but not certain of the cause, or need help using this code, ask on the appropriate forum. This is not the place to ask for support or help, even directly related to this code. Only use this form you are certain you have discovered a defect in this code! 13 | 14 | Please verify the problem occurs when using the very latest version, using the newest version of Arduino and any other related software. 15 | 16 | 17 | ----------------------------- Remove above ----------------------------- 18 | 19 | 20 | 21 | ### Description 22 | 23 | Describe your problem. 24 | 25 | 26 | 27 | ### Steps To Reproduce Problem 28 | 29 | Please give detailed instructions needed for anyone to attempt to reproduce the problem. 30 | 31 | 32 | 33 | ### Hardware & Software 34 | 35 | Board 36 | Shields / modules used 37 | Arduino IDE version 38 | Teensyduino version (if using Teensy) 39 | Version info & package name (from Tools > Boards > Board Manager) 40 | Operating system & version 41 | Any other software or hardware? 42 | 43 | 44 | ### Arduino Sketch 45 | 46 | ```cpp 47 | // Change the code below by your sketch (please try to give the smallest code which demonstrates the problem) 48 | #include 49 | 50 | // libraries: give links/details so anyone can compile your code for the same result 51 | 52 | void setup() { 53 | } 54 | 55 | void loop() { 56 | } 57 | ``` 58 | 59 | 60 | ### Errors or Incorrect Output 61 | 62 | If you see any errors or incorrect output, please show it here. Please use copy & paste to give an exact copy of the message. Details matter, so please show (not merely describe) the actual message or error exactly as it appears. 63 | 64 | 65 | -------------------------------------------------------------------------------- /examples/ReadTest/ReadTest.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void setup() { 6 | Serial.begin(9600); 7 | while (!Serial) ; // wait for serial 8 | delay(200); 9 | Serial.println("DS1307RTC Read Test"); 10 | Serial.println("-------------------"); 11 | } 12 | 13 | void loop() { 14 | tmElements_t tm; 15 | 16 | if (RTC.read(tm)) { 17 | Serial.print("Ok, Time = "); 18 | print2digits(tm.Hour); 19 | Serial.write(':'); 20 | print2digits(tm.Minute); 21 | Serial.write(':'); 22 | print2digits(tm.Second); 23 | Serial.print(", Date (D/M/Y) = "); 24 | Serial.print(tm.Day); 25 | Serial.write('/'); 26 | Serial.print(tm.Month); 27 | Serial.write('/'); 28 | Serial.print(tmYearToCalendar(tm.Year)); 29 | Serial.println(); 30 | } else { 31 | if (RTC.chipPresent()) { 32 | Serial.println("The DS1307 is stopped. Please run the SetTime"); 33 | Serial.println("example to initialize the time and begin running."); 34 | Serial.println(); 35 | } else { 36 | Serial.println("DS1307 read error! Please check the circuitry."); 37 | Serial.println(); 38 | } 39 | delay(9000); 40 | } 41 | delay(1000); 42 | } 43 | 44 | void print2digits(int number) { 45 | if (number >= 0 && number < 10) { 46 | Serial.write('0'); 47 | } 48 | Serial.print(number); 49 | } 50 | -------------------------------------------------------------------------------- /examples/SetTime/SetTime.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const char *monthName[12] = { 6 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", 7 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 8 | }; 9 | 10 | tmElements_t tm; 11 | 12 | void setup() { 13 | bool parse=false; 14 | bool config=false; 15 | 16 | // get the date and time the compiler was run 17 | if (getDate(__DATE__) && getTime(__TIME__)) { 18 | parse = true; 19 | // and configure the RTC with this info 20 | if (RTC.write(tm)) { 21 | config = true; 22 | } 23 | } 24 | 25 | Serial.begin(9600); 26 | while (!Serial) ; // wait for Arduino Serial Monitor 27 | delay(200); 28 | if (parse && config) { 29 | Serial.print("DS1307 configured Time="); 30 | Serial.print(__TIME__); 31 | Serial.print(", Date="); 32 | Serial.println(__DATE__); 33 | } else if (parse) { 34 | Serial.println("DS1307 Communication Error :-{"); 35 | Serial.println("Please check your circuitry"); 36 | } else { 37 | Serial.print("Could not parse info from the compiler, Time=\""); 38 | Serial.print(__TIME__); 39 | Serial.print("\", Date=\""); 40 | Serial.print(__DATE__); 41 | Serial.println("\""); 42 | } 43 | } 44 | 45 | void loop() { 46 | } 47 | 48 | bool getTime(const char *str) 49 | { 50 | int Hour, Min, Sec; 51 | 52 | if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false; 53 | tm.Hour = Hour; 54 | tm.Minute = Min; 55 | tm.Second = Sec; 56 | return true; 57 | } 58 | 59 | bool getDate(const char *str) 60 | { 61 | char Month[12]; 62 | int Day, Year; 63 | uint8_t monthIndex; 64 | 65 | if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false; 66 | for (monthIndex = 0; monthIndex < 12; monthIndex++) { 67 | if (strcmp(Month, monthName[monthIndex]) == 0) break; 68 | } 69 | if (monthIndex >= 12) return false; 70 | tm.Day = Day; 71 | tm.Month = monthIndex + 1; 72 | tm.Year = CalendarYrToTm(Year); 73 | return true; 74 | } 75 | 76 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For DS1307RTC 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ####################################### 10 | # Methods and Functions (KEYWORD2) 11 | ####################################### 12 | get KEYWORD2 13 | set KEYWORD2 14 | read KEYWORD2 15 | write KEYWORD2 16 | chipPresent KEYWORD2 17 | ####################################### 18 | # Instances (KEYWORD2) 19 | ####################################### 20 | RTC KEYWORD2 21 | ####################################### 22 | # Constants (LITERAL1) 23 | ####################################### 24 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "DS1307RTC", 3 | "keywords": "i2c, rtc, time, clock", 4 | "description": "DS1307 RTC (Real-Time-Clock)", 5 | "repository": 6 | { 7 | "type": "git", 8 | "url": "https://github.com/PaulStoffregen/DS1307RTC.git" 9 | }, 10 | "frameworks": "arduino", 11 | "platforms": "atmelavr" 12 | } 13 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=DS1307RTC 2 | version=1.4.1 3 | author=Michael Margolis 4 | maintainer=Paul Stoffregen 5 | sentence=Use a DS1307 Real Time Clock chip with the Time library 6 | paragraph= 7 | category=Timing 8 | url=http://playground.arduino.cc/code/time 9 | architectures=* 10 | depends=Time 11 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | Readme file for DS1307RTC Library 2 | 3 | The DS1307RTC library is provided to demonstrate the Arduino Time library. 4 | 5 | See the TimeRTC example sketches privided with the Time library download for usage 6 | 7 | 8 | --------------------------------------------------------------------------------