Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. // DS1302:  CE pin    -> Arduino Digital 6
  2. //          I/O pin   -> Arduino Digital 5
  3. //          SCLK pin  -> Arduino Digital 4
  4. //          VCC pin   -> Arduino Digital 2
  5. //          GND pin   -> Arduino Digital 3
  6.  
  7. #include <Time.h>
  8. #include "DS1302.h"
  9.  
  10. #define kCePin 6  // RST
  11. #define kIoPin 5  // DAT
  12. #define kSclkPin 4  // CLK
  13. #define kVCCPin 2 // VCC
  14. #define kGNDPin 3 // GND
  15.  
  16. DS1302 rtc(kCePin, kIoPin, kSclkPin);
  17. Time t = rtc.time();
  18.  
  19. bool isComplete = false;
  20.  
  21. void setup()
  22. {
  23.   // Setup Serial connection
  24.   Serial.begin(9600);
  25.   pinMode(kGNDPin, OUTPUT);
  26.   pinMode(kVCCPin, OUTPUT);
  27.  
  28.   digitalWrite(kVCCPin, HIGH);
  29.   digitalWrite(kGNDPin, LOW);
  30. }
  31.  
  32. void loop()
  33. {
  34.   if (isComplete)
  35.     return;
  36.    
  37.   Serial.println("TEST of the DS1302 RTC Module.");
  38.   Serial.println("Setting time to 2018-07-21 15:43:50...");
  39.   // Initialize a new chip by turning off write protection and clearing the
  40.   // clock halt flag. These methods needn't always be called. See the DS1302
  41.   rtc.writeProtect(false);
  42.   rtc.halt(false);
  43.   // Make a new time object to set the date and time.
  44.   //       yyyy, mm, dd, hh, mm, ss, wd (week day)    
  45.   Time t(2018, 07, 21, 15, 43, 50, 01);
  46.   // Set the time and date on the chip.
  47.   rtc.time(t);
  48.  
  49.   int tic = 0;
  50.   Serial.println("Ticking...");
  51.   while(tic < 5) {
  52.     printTime();
  53.     tic++;
  54.     delay(1000);
  55.   }
  56.  
  57.   tic = 0;
  58.   Serial.println("Turning the power off...");
  59.   digitalWrite(kVCCPin, LOW);
  60.   Serial.println("Wating for 60 seconds...");
  61.   while (tic < 60) {
  62.     tic++;
  63.     delay(1000);
  64.   }
  65.  
  66.   tic = 0;
  67.   Serial.println("Bringing back the power...");
  68.   digitalWrite(kVCCPin, HIGH);
  69.   t = rtc.time();
  70.   Serial.println("Ticking...(should be +1 minute +1 second)");
  71.   while(tic < 5) {
  72.     printTime();
  73.     delay(1000);
  74.     tic++;
  75.   }
  76.  
  77.   Serial.println("TEST COMPLETE");
  78.   isComplete = true;
  79. }
  80.  
  81.  
  82. String dayAsString(const Time::Day day) {
  83.   switch (day) {
  84.     case Time::kSunday: return "Sunday";
  85.     case Time::kMonday: return "Monday";
  86.     case Time::kTuesday: return "Tuesday";
  87.     case Time::kWednesday: return "Wednesday";
  88.     case Time::kThursday: return "Thursday";
  89.     case Time::kFriday: return "Friday";
  90.     case Time::kSaturday: return "Saturday";
  91.   }
  92.   return "(unknown day)";
  93. }
  94.  
  95. void printTime() {
  96.   // Get the current time and date from the chip.
  97.   Time t = rtc.time();
  98.  
  99.   // Name the day of the week.
  100.   const String day = dayAsString(t.day);
  101.  
  102.   // Format the time and date and insert into the temporary buffer.
  103.   char buf[50];
  104.   snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
  105.            day.c_str(),
  106.            t.yr, t.mon, t.date,
  107.            t.hr, t.min, t.sec);
  108.  
  109.   // Print the formatted string to serial so we can see the time.
  110.   Serial.println(buf);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement