Advertisement
DrAungWinHtut

ds1302_modified.ino

Oct 19th, 2022
1,642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // CONNECTIONS:
  2. // DS1302 CLK/SCLK --> 5
  3. // DS1302 DAT/IO --> 4
  4. // DS1302 RST/CE --> 2
  5. // DS1302 VCC --> 3.3v - 5v
  6. // DS1302 GND --> GND
  7.  
  8. #include <ThreeWire.h>
  9. #include <RtcDS1302.h>
  10.  
  11. ThreeWire myWire(4, 5, 2);  // IO, SCLK, CE
  12. RtcDS1302<ThreeWire> Rtc(myWire);
  13.  
  14. void setup() {
  15.   Serial.begin(57600);
  16.  
  17.   Serial.print("compiled: ");
  18.   Serial.print(__DATE__);
  19.   Serial.println(__TIME__);
  20.  
  21.   Rtc.Begin();
  22.  
  23.   RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
  24.   printDateTime(compiled);
  25.   Serial.println();
  26.  
  27.   if (!Rtc.IsDateTimeValid()) {
  28.     // Common Causes:
  29.     //    1) first time you ran and the device wasn't running yet
  30.     //    2) the battery on the device is low or even missing
  31.  
  32.     Serial.println("RTC lost confidence in the DateTime!");
  33.     Rtc.SetDateTime(compiled);
  34.   }
  35.  
  36.   if (Rtc.GetIsWriteProtected()) {
  37.     Serial.println("RTC was write protected, enabling writing now");
  38.     Rtc.SetIsWriteProtected(false);
  39.   }
  40.  
  41.   if (!Rtc.GetIsRunning()) {
  42.     Serial.println("RTC was not actively running, starting now");
  43.     Rtc.SetIsRunning(true);
  44.   }
  45.  
  46.   RtcDateTime now = Rtc.GetDateTime();
  47.   if (now < compiled) {
  48.     Serial.println("RTC is older than compile time!  (Updating DateTime)");
  49.     Rtc.SetDateTime(compiled);
  50.   } else if (now > compiled) {
  51.     Serial.println("RTC is newer than compile time. (this is expected)");
  52.   } else if (now == compiled) {
  53.     Serial.println("RTC is the same as compile time! (not expected but all is fine)");
  54.   }
  55. }
  56.  
  57. void loop() {
  58.   RtcDateTime now = Rtc.GetDateTime();
  59.   Serial.print("RTC time reading is: ");
  60.   printDateTime(now);
  61.   Serial.println();
  62.   String sMonth = getMonth(now);
  63.   Serial.print("Month: ");
  64.   Serial.println(sMonth);
  65.  
  66.   String sDay = getDay(now);
  67.   Serial.print("Day: ");
  68.   Serial.println(sDay);
  69.  
  70.   String sYear = getYear(now);
  71.   Serial.print("Year: ");
  72.   Serial.println(sYear);
  73.  
  74.   String sHour = getHour(now);
  75.   Serial.print("Hour: ");
  76.   Serial.println(sHour);
  77.  
  78.   String sMinute = getMinute(now);
  79.   Serial.print("Minute: ");
  80.   Serial.println(sMinute);
  81.  
  82.   String sSecond = getSecond(now);
  83.   Serial.print("Second: ");
  84.   Serial.println(sSecond);
  85.  
  86.   const char* sDateTime = getDateTime(now);
  87.   Serial.print("DateTime: ");
  88.   for (int k = 12; k < 14; k++) {
  89.     Serial.print(sDateTime[k]);
  90.   }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.   if (!now.IsValid()) {
  97.     // Common Causes:
  98.     //    1) the battery on the device is low or even missing and the power line was disconnected
  99.     Serial.println("RTC lost confidence in the DateTime!");
  100.   }
  101.  
  102.   delay(10000);  // ten seconds
  103. }
  104.  
  105. #define countof(a) (sizeof(a) / sizeof(a[0]))
  106.  
  107. void printDateTime(const RtcDateTime& dt) {
  108.   char datestring[20];
  109.  
  110.   snprintf_P(datestring,
  111.              countof(datestring),
  112.              PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
  113.              dt.Month(),
  114.              dt.Day(),
  115.              dt.Year(),
  116.              dt.Hour(),
  117.              dt.Minute(),
  118.              dt.Second());
  119.   Serial.print(datestring);
  120. }
  121. String getMonth(const RtcDateTime& dt) {
  122.   char monthString[20];
  123.  
  124.   snprintf_P(monthString,
  125.              countof(monthString),
  126.              PSTR("%02u"),
  127.              dt.Month());
  128.   return monthString;
  129. }
  130. String getDay(const RtcDateTime& dt) {
  131.   char dayString[20];
  132.  
  133.   snprintf_P(dayString,
  134.              countof(dayString),
  135.              PSTR("%02u"),
  136.              dt.Day());
  137.   return dayString;
  138. }
  139. String getYear(const RtcDateTime& dt) {
  140.   char yearString[20];
  141.  
  142.   snprintf_P(yearString,
  143.              countof(yearString),
  144.              PSTR("%04u"),
  145.              dt.Year());
  146.   return yearString;
  147. }
  148. String getHour(const RtcDateTime& dt) {
  149.   char hourString[20];
  150.  
  151.   snprintf_P(hourString,
  152.              countof(hourString),
  153.              PSTR("%02u"),
  154.              dt.Hour());
  155.   return hourString;
  156. }
  157. String getMinute(const RtcDateTime& dt) {
  158.   char minuteString[20];
  159.  
  160.   snprintf_P(minuteString,
  161.              countof(minuteString),
  162.              PSTR("%02u"),
  163.              dt.Minute());
  164.   return minuteString;
  165. }
  166. String getSecond(const RtcDateTime& dt) {
  167.   char secondString[20];
  168.  
  169.   snprintf_P(secondString,
  170.              countof(secondString),
  171.              PSTR("%02u"),
  172.              dt.Second());
  173.   return secondString;
  174. }
  175. const char* getDateTime(const RtcDateTime& dt) {
  176.   static char datestring[20];
  177.  
  178.   snprintf_P(datestring,
  179.              countof(datestring),
  180.              PSTR("%02u%02u%04u%02u%02u%02u"),
  181.              dt.Month(),
  182.              dt.Day(),
  183.              dt.Year(),
  184.              dt.Hour(),
  185.              dt.Minute(),
  186.              dt.Second());
  187.   return datestring;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement