Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DS1302: CE pin -> Arduino Digital 6
- // I/O pin -> Arduino Digital 5
- // SCLK pin -> Arduino Digital 4
- // VCC pin -> Arduino Digital 2
- // GND pin -> Arduino Digital 3
- #include <Time.h>
- #include "DS1302.h"
- #define kCePin 6 // RST
- #define kIoPin 5 // DAT
- #define kSclkPin 4 // CLK
- #define kVCCPin 2 // VCC
- #define kGNDPin 3 // GND
- DS1302 rtc(kCePin, kIoPin, kSclkPin);
- Time t = rtc.time();
- bool isComplete = false;
- void setup()
- {
- // Setup Serial connection
- Serial.begin(9600);
- pinMode(kGNDPin, OUTPUT);
- pinMode(kVCCPin, OUTPUT);
- digitalWrite(kVCCPin, HIGH);
- digitalWrite(kGNDPin, LOW);
- }
- void loop()
- {
- if (isComplete)
- return;
- Serial.println("TEST of the DS1302 RTC Module.");
- Serial.println("Setting time to 2018-07-21 15:43:50...");
- // Initialize a new chip by turning off write protection and clearing the
- // clock halt flag. These methods needn't always be called. See the DS1302
- rtc.writeProtect(false);
- rtc.halt(false);
- // Make a new time object to set the date and time.
- // yyyy, mm, dd, hh, mm, ss, wd (week day)
- Time t(2018, 07, 21, 15, 43, 50, 01);
- // Set the time and date on the chip.
- rtc.time(t);
- int tic = 0;
- Serial.println("Ticking...");
- while(tic < 5) {
- printTime();
- tic++;
- delay(1000);
- }
- tic = 0;
- Serial.println("Turning the power off...");
- digitalWrite(kVCCPin, LOW);
- Serial.println("Wating for 60 seconds...");
- while (tic < 60) {
- tic++;
- delay(1000);
- }
- tic = 0;
- Serial.println("Bringing back the power...");
- digitalWrite(kVCCPin, HIGH);
- t = rtc.time();
- Serial.println("Ticking...(should be +1 minute +1 second)");
- while(tic < 5) {
- printTime();
- delay(1000);
- tic++;
- }
- Serial.println("TEST COMPLETE");
- isComplete = true;
- }
- String dayAsString(const Time::Day day) {
- switch (day) {
- case Time::kSunday: return "Sunday";
- case Time::kMonday: return "Monday";
- case Time::kTuesday: return "Tuesday";
- case Time::kWednesday: return "Wednesday";
- case Time::kThursday: return "Thursday";
- case Time::kFriday: return "Friday";
- case Time::kSaturday: return "Saturday";
- }
- return "(unknown day)";
- }
- void printTime() {
- // Get the current time and date from the chip.
- Time t = rtc.time();
- // Name the day of the week.
- const String day = dayAsString(t.day);
- // Format the time and date and insert into the temporary buffer.
- char buf[50];
- snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
- day.c_str(),
- t.yr, t.mon, t.date,
- t.hr, t.min, t.sec);
- // Print the formatted string to serial so we can see the time.
- Serial.println(buf);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement