Skip to content

adjust()

Arnd edited this page Mar 18, 2021 · 4 revisions

This function is used to set the RTC date and time. When called with no parameters the last compile date and time of the library header file is used to set the RTC. Otherwise it needs to be called using a DateTime class instance in order to set the clock.


Example:

...
MCP7940_Class MCP7940; // Create an instance of the MCP7940
...
void setup() {
  Serial.begin(SERIAL_SPEED);
  while (!MCP7940.begin()) { // Initialize RTC communications
    Serial.println("Unable to find MCP7940. Checking again in 1 second.");
    delay(1000);
  } // of loop until device is located
  MCP7940.adjust(); // Set the RTC to the Date/Time of library compile
  DateTime now = MCP7940.now(); // get the date and time
  Serial.print("Year:   ");Serial.println(now.year());
  Serial.print("Month:  ");Serial.println(now.month());
  Serial.print("Day:    ");Serial.println(now.day());
  Serial.print("Hour:   ");Serial.println(now.hour());
  Serial.print("Minute: ");Serial.println(now.minute());
  Serial.print("Second: ");Serial.println(now.second());
  MCP7940.adjust(DateTime(2017,8,5,18,19,20)); // Set the RTC to "2017-08-05 18:19:20"
  Serial.print("Year:   ");Serial.println(now.year());
  Serial.print("Month:  ");Serial.println(now.month());
  Serial.print("Day:    ");Serial.println(now.day());
  Serial.print("Hour:   ");Serial.println(now.hour());
  Serial.print("Minute: ");Serial.println(now.minute());
  Serial.print("Second: ");Serial.println(now.second());
...
} // of setup
Clone this wiki locally