Dear Mr.LarryD,
LarryD:
You can get date and time when the sketch was complied.
Not sure of the format, but it is available if you search for it.
This will only work once as it is the values at compile time when uploaded.
Here:
DATE
and
TIME
Edit:
rtc.adjust(DateTime(F(DATE), F(TIME)));
Or maybe:
// following line sets the RTC to the date & time this sketch was compiled
rtc.begin(DateTime(F(DATE), F(TIME)));
Thank you so much for your kind support. Your suggestion is good for me.
I use your suggestion, refer to some topics, and this is my sketch for configuration RTC DS3232M to PC clock only one time. The time may be at the sketch was compiled.
By the way, I also think one more solution about configuration DS3232M to PC clock. I will write down it in another post.
#include <SoftI2C.h> // initialise required libraries
#include <DS3232RTC.h>
#include <Time.h>
SoftI2C i2c(A4, A5); // assign pins to SDA and SCL
DS3232RTC rtc(i2c);
RTCTime time;
RTCDate date;
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
bool getTime(const char *str)
{
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
time.hour = Hour;
time.minute = Min;
time.second = Sec;
return true;
}
bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
date.day = Day;
date.month = monthIndex + 1;
date.year = Year;
return true;
}
void setup() {
Serial.begin(9600);
// get the time when the compiler was run
if (getTime(TIME)) {
// and configure the RTC with this info
rtc.writeTime(&time);
}
// get the date when the compiler was run
if (getDate(DATE)) {
// and configure the RTC with this info
rtc.writeDate(&date);
}
// Read time
rtc.readTime(&time);
Serial.print("Time: ");
Serial.print(time.hour, DEC);
Serial.print(':');
Serial.print(time.minute, DEC);
Serial.print(':');
Serial.print(time.second, DEC);
// Read date
rtc.readDate(&date);
Serial.println("");
Serial.print("Date: ");
Serial.print(date.day, DEC);
Serial.print(':');
Serial.print(date.month, DEC);
Serial.print(':');
Serial.print(date.year, DEC);
}
void loop() {
// put your main code here, to run repeatedly:
}