SBD-DL22 / clock_sync / clock_sync.ino
clock_sync.ino
Raw
// This code includes different header files needed to run this program.
// "config.h" is a user-defined file typically used to contain project configuration information
// "ds3231.h" is a library for handling the DS3231 real-time clock (RTC)
// "Wire.h" is a library for communication over the I2C/TWI (Inter-Integrated Circuit/Two Wire Interface) bus
#include <config.h>
#include <ds3231.h>
#include <Wire.h>

// A struct "ts" is declared to hold the time and date information.
struct ts t;

// The setup function runs once when you press reset or power the board.
void setup() {
  // Begin serial communication at 9600 bits per second.
  Serial.begin(9600);

  // Begin the Wire library, which is used for I2C communication.
  Wire.begin();

  // Initialize the DS3231 real-time clock with control interrupt enabled.
  DS3231_init(DS3231_CONTROL_INTCN);

  // Set the time and date information to synchronize the clock module.
  t.hour = 12;
  t.min = 41;
  t.sec = 0;
  t.mday = 21; // Day of the month
  t.mon = 03; // Month
  t.year = 2022;

  // Set the current time and date in the DS3231 module with the provided information.
  DS3231_set(t);
}

// The loop function runs over and over again forever.
void loop() {
  // Get the current time and date from the DS3231 module.
  DS3231_get(&t);

  // Print the date to the serial monitor in the format "mm/dd/yyyy".
  Serial.print("Date : ");
  Serial.print(t.mon);
  Serial.print("/");
  Serial.print(t.mday);
  Serial.print("/");
  Serial.print(t.year);

  // Print the current hour to the serial monitor.
  Serial.print("\t Hour : ");
  Serial.print(t.hour);
  Serial.print(":");

  // Print the current minute to the serial monitor.
  Serial.print(t.min);
  Serial.print(".");

  // Print the current second to the serial monitor.
  Serial.println(t.sec);

  // Wait for a second before continuing the loop.
  delay(1000);
}