Advertisement
Guest User

DS1307RTC.cpp

a guest
Aug 16th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 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.h 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. #include <Wire.h>
  26. #include "DS1307RTC.h"
  27.  
  28. #define DS1307_CTRL_ID 0x68
  29.  
  30. DS1307RTC::DS1307RTC()
  31. {
  32. Wire.begin();
  33. }
  34.  
  35. // PUBLIC FUNCTIONS
  36. time_t DS1307RTC::get() // Aquire data from buffer and convert to time_t
  37. {
  38. tmElements_t tm;
  39. if (read(tm) == false) return 0;
  40. return(makeTime(tm));
  41. }
  42.  
  43. bool DS1307RTC::set(time_t t)
  44. {
  45. tmElements_t tm;
  46. breakTime(t, tm);
  47. tm.Second |= 0x80; // stop the clock
  48. write(tm);
  49. tm.Second &= 0x7f; // start the clock
  50. write(tm);
  51. }
  52.  
  53. // Aquire data from the RTC chip in BCD format
  54. bool DS1307RTC::read(tmElements_t &tm)
  55. {
  56. uint8_t sec;
  57. Wire.beginTransmission(DS1307_CTRL_ID);
  58. #if ARDUINO >= 100
  59. Wire.write((uint8_t)0x00);
  60. #else
  61. Wire.send(0x00);
  62. #endif
  63. if (Wire.endTransmission() != 0) {
  64. exists = false;
  65. return false;
  66. }
  67. exists = true;
  68.  
  69. // request the 7 data fields (secs, min, hr, dow, date, mth, yr)
  70. Wire.requestFrom(DS1307_CTRL_ID, tmNbrFields);
  71. if (Wire.available() < tmNbrFields) return false;
  72. #if ARDUINO >= 100
  73. sec = Wire.read();
  74. tm.Second = bcd2dec(sec & 0x7f);
  75. tm.Minute = bcd2dec(Wire.read() );
  76. tm.Hour = bcd2dec(Wire.read() & 0x3f); // mask assumes 24hr clock
  77. tm.Wday = bcd2dec(Wire.read() );
  78. tm.Day = bcd2dec(Wire.read() );
  79. tm.Month = bcd2dec(Wire.read() );
  80. tm.Year = y2kYearToTm((bcd2dec(Wire.read())));
  81. #else
  82. sec = Wire.receive();
  83. tm.Second = bcd2dec(sec & 0x7f);
  84. tm.Minute = bcd2dec(Wire.receive() );
  85. tm.Hour = bcd2dec(Wire.receive() & 0x3f); // mask assumes 24hr clock
  86. tm.Wday = bcd2dec(Wire.receive() );
  87. tm.Day = bcd2dec(Wire.receive() );
  88. tm.Month = bcd2dec(Wire.receive() );
  89. tm.Year = y2kYearToTm((bcd2dec(Wire.receive())));
  90. #endif
  91. if (sec & 0x80) return false; // clock is halted
  92. return true;
  93. }
  94.  
  95. bool DS1307RTC::write(tmElements_t &tm)
  96. {
  97. Wire.beginTransmission(DS1307_CTRL_ID);
  98. #if ARDUINO >= 100
  99. Wire.write((uint8_t)0x00); // reset register pointer
  100. Wire.write(dec2bcd(tm.Second)) ;
  101. Wire.write(dec2bcd(tm.Minute));
  102. Wire.write(dec2bcd(tm.Hour)); // sets 24 hour format
  103. Wire.write(dec2bcd(tm.Wday));
  104. Wire.write(dec2bcd(tm.Day));
  105. Wire.write(dec2bcd(tm.Month));
  106. Wire.write(dec2bcd(tmYearToY2k(tm.Year)));
  107. #else
  108. Wire.send(0x00); // reset register pointer
  109. Wire.send(dec2bcd(tm.Second)) ;
  110. Wire.send(dec2bcd(tm.Minute));
  111. Wire.send(dec2bcd(tm.Hour)); // sets 24 hour format
  112. Wire.send(dec2bcd(tm.Wday));
  113. Wire.send(dec2bcd(tm.Day));
  114. Wire.send(dec2bcd(tm.Month));
  115. Wire.send(dec2bcd(tmYearToY2k(tm.Year)));
  116. #endif
  117. if (Wire.endTransmission() != 0) {
  118. exists = false;
  119. return false;
  120. }
  121. exists = true;
  122. return true;
  123. }
  124.  
  125. // PRIVATE FUNCTIONS
  126.  
  127. // Convert Decimal to Binary Coded Decimal (BCD)
  128. uint8_t DS1307RTC::dec2bcd(uint8_t num)
  129. {
  130. return ((num/10 * 16) + (num % 10));
  131. }
  132.  
  133. // Convert Binary Coded Decimal (BCD) to Decimal
  134. uint8_t DS1307RTC::bcd2dec(uint8_t num)
  135. {
  136. return ((num/16 * 10) + (num % 16));
  137. }
  138.  
  139. bool DS1307RTC::exists = false;
  140.  
  141. DS1307RTC RTC = DS1307RTC(); // create an instance for the user
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement