Arduino-RTC
Real-Time Clock (RTC) library for Arduino
RTC.ino
Go to the documentation of this file.
1 #include "RTC.h"
2 
3 // Configure: Set Real-Time Clock
4 // #define SET_TIME
5 
6 // Configure: Use DS1302, DS1307 or software RTC
7 // #define USE_DS1302
8 #define USE_DS1307
9 
10 #if defined(USE_DS1302)
11 #include "GPIO.h"
12 #include "Driver/DS1302.h"
14 
15 #elif defined(USE_DS1307)
16 #include "TWI.h"
17 #include "Driver/DS1307.h"
18 // Configure: TWI bus manager (software or hardware)
19 // #define USE_SOFTWARE_TWI
20 #if defined(USE_SOFTWARE_TWI)
21 #include "GPIO.h"
22 #include "Software/TWI.h"
23 Software::TWI<BOARD::D18, BOARD::D19> twi;
24 #else
25 // Configure: Hardware TWI bus clock frequency (100 or 400 kHz)
26 #include "Hardware/TWI.h"
27 Hardware::TWI twi(100000UL);
28 // Hardware::TWI twi(400000UL);
29 #endif
30 DS1307 rtc(twi);
31 
32 #else
33 #define USE_RTC
34 RTC rtc;
35 #endif
36 
37 void setup()
38 {
39  Serial.begin(57600);
40  while (!Serial);
41 
42  // Set Central European Time Zone: UTC+01:00
44 
45 #if defined(SET_TIME)
46  // Set clock to the test
47  struct tm now(SATURDAY, 2000, JANUARY, 1, 23, 59, 30);
48  rtc.set_time(now);
49 #endif
50 }
51 
52 void loop()
53 {
54  struct tm now;
55  time_t time = 0;
56  char buf[32];
57 
58 #if defined(USE_RTC)
59  if (!rtc.tick()) return;
60 #else
61  uint32_t start = millis();
62 #endif
63 
64  // Read Real-Time Clock
65  rtc.get_time(now);
66  Serial.print(millis() / 1000.0);
67  Serial.print(':');
68 
69  // Convert from tm struct to time_t (seconds since epoch, utc)
70  Serial.print(time = mktime(&now));
71 
72  // Print calculated day-of week and year
73  Serial.print(F(":wday="));
74  Serial.print(now.tm_wday);
75  Serial.print(F(",yday="));
76  Serial.print(now.tm_yday);
77  Serial.print(F(",utc+"));
78 
79  // Print time zone adjustment
80  Serial.print(get_zone() / ONE_HOUR);
81  Serial.print(F("=\""));
82 
83  // Print date in ISO format
84  Serial.print(isotime_r(&now, buf));
85  Serial.print(F("\",utc=\""));
86 
87  // Convert from time_t to struct tm and print
88  Serial.print(isotime_r(gmtime_r(&time, &now), buf));
89  Serial.println('"');
90 
91 #if !defined(USE_RTC)
92  // Approx delay for the next tick
93  delay(1000 - (millis() - start));
94 #endif
95 }
void setup()
Definition: RTC.ino:37
int16_t tm_yday
days since January 1 [0-365].
Definition: time.h:44
time_t get_time()
Definition: RTC.h:55
void loop()
Definition: RTC.ino:52
time_t mktime(struct tm *timeptr)
Definition: mktime.cpp:35
#define ONE_HOUR
Definition: time.h:148
#define get_zone()
Definition: RTC.h:79
bool tick()
Definition: RTC.h:39
struct tm * gmtime_r(const time_t *timer, struct tm *timeptr)
Definition: gmtime_r.cpp:33
Definition: DS1307.h:47
DS1307 rtc(twi)
uint32_t time_t
Definition: time.h:30
void set_time(time_t time)
Definition: RTC.h:68
int8_t tm_wday
Days since Sunday [0-6].
Definition: time.h:41
Definition: time.h:36
#define isotime_r(tm, buf)
Definition: RTC.h:69
Hardware::TWI twi(100000UL)
Definition: DS1302.h:53
#define set_zone(x)
Definition: RTC.h:78
Definition: RTC.h:25