mirror of
https://github.com/adafruit/RTClib.git
synced 2026-09-15 11:02:35 +00:00
3f379ab (PR #166) added connection verification for the PCF8523 and DS1307 matching that of the DS3231. For each RTC, if begin() returns false, the intended behavior is to print a message, "Couldn't find RTC" to an attached serial terminal and then terminate.
This change is a follow-on that corrects two issues which were not seen as long as begin() always returned true, across all RTClib examples:
1) The method of termination in place was an endless loop: `while(1)`
However, as pointed out in PR #168, the behavior of such a loop is undefined. (See PR for interesting reading!) The `abort()` command effectively starts an endless loop, but without any side effects. It also makes clear to new users what the program is doing. H/T to contributor @edgar-bonet for this advice.
2) The message text "Couldn't find RTC" was not guaranteed to actually print on screen before the processor entered the endless loop.
This has to do with the way serial data is actually buffered and transfered down the line. On some boards, serial data is processed using interrupts. On others, such as the Feather nRF52840 which uses the TinyUSB stack, interrupts are not used at all, and serial processing waits for the CPU to become available. Whatever the case, `Serial.flush()` allows serial processing to finish before terminating with the endless loop.
An alternate way of handling 1 and 2, recommended by the TinyUSB author, is to enter the endless loop immediately, but call `yield()` inside to instruct the processor to go ahead and handle any latent requests that come in. This also makes the loop have defined behavior. E.g.:
```
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1) {
yield(); // delay(1) would also suffice
}
}
```
A fuller discussion of termination methods can be viewed in PR #166.
67 lines
2.0 KiB
Arduino
67 lines
2.0 KiB
Arduino
#include <Wire.h>
|
|
#include <RTClib.h>
|
|
|
|
RTC_DS1307 rtc;
|
|
|
|
|
|
void setup () {
|
|
Serial.begin(57600);
|
|
|
|
#ifndef ESP8266
|
|
while (!Serial); // wait for serial port to connect. Needed for native USB
|
|
#endif
|
|
|
|
if (! rtc.begin()) {
|
|
Serial.println("Couldn't find RTC");
|
|
Serial.flush();
|
|
abort();
|
|
}
|
|
|
|
if (! rtc.isrunning()) {
|
|
Serial.println("RTC is NOT running, let's set the time!");
|
|
// When time needs to be set on a new device, or after a power loss, the
|
|
// following line sets the RTC to the date & time this sketch was compiled
|
|
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
|
|
// This line sets the RTC with an explicit date & time, for example to set
|
|
// January 21, 2014 at 3am you would call:
|
|
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
|
|
}
|
|
|
|
// When time needs to be re-set on a previously configured device, the
|
|
// following line sets the RTC to the date & time this sketch was compiled
|
|
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
|
|
// This line sets the RTC with an explicit date & time, for example to set
|
|
// January 21, 2014 at 3am you would call:
|
|
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
|
|
}
|
|
|
|
void loop() {
|
|
|
|
DateTime now = rtc.now();
|
|
|
|
//buffer can be defined using following combinations:
|
|
//hh - the hour with a leading zero (00 to 23)
|
|
//mm - the minute with a leading zero (00 to 59)
|
|
//ss - the whole second with a leading zero where applicable (00 to 59)
|
|
//YYYY - the year as four digit number
|
|
//YY - the year as two digit number (00-99)
|
|
//MM - the month as number with a leading zero (01-12)
|
|
//MMM - the abbreviated English month name ('Jan' to 'Dec')
|
|
//DD - the day as number with a leading zero (01 to 31)
|
|
//DDD - the abbreviated English day name ('Mon' to 'Sun')
|
|
|
|
char buf1[] = "hh:mm";
|
|
Serial.println(now.toString(buf1));
|
|
|
|
char buf2[] = "YYMMDD-hh:mm:ss";
|
|
Serial.println(now.toString(buf2));
|
|
|
|
char buf3[] = "Today is DDD, MMM DD YYYY";
|
|
Serial.println(now.toString(buf3));
|
|
|
|
char buf4[] = "MM-DD-YYYY";
|
|
Serial.println(now.toString(buf4));
|
|
|
|
delay(1000);
|
|
}
|