Advertisement
orneto

Untitled

May 30th, 2023
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.14 KB | None | 0 0
  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 <TinyWireM.h>
  28. #define Wire TinyWireM
  29. #else
  30. #include <Wire.h>
  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. _ctime_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.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement