https://www.analog.com/media/en/technical-documentation/data-sheets/DS1307.pdf
int CLOCK_READ = 0xD1;
int CLOCK_WRITE = 0xD0;
https://os.mbed.com/docs/mbed-os/v6.16/apis/i2c.html
I2C i2c(I2C_SDA , I2C_SCL);
int status;
//Send Start Signal to I2C
i2c.start();
//Write Seconds
status = i2c.write(CLOCK_WRITE);
printf("Status was %d\n", status); //Debugging check acknowledgement
i2c.write(0x00) ; //Seconds Register
//Send Read Signal
i2c.stop();
i2c.start();
status = i2c.write(CLOCK_READ); /* slave address with read mode */
printf("Read Status was %d\n", status); //Check for Ack
int seconds = i2c.read(1); // Read the slave with ACK
int minutes= i2c.read(1);
int hour = i2c.read(1);
int day = i2c.read(1);
int date = i2c.read(1);
int month = i2c.read(1);
int year = i2c.read(0); // Read the slave with not-ACK
//Stop
i2c.stop();
int newSeconds = seconds & 0x7f;
int bcdtodec( int bcd) {
int low = 0;
int high = 0;
high = bcd / 16;
low = bcd - (high * 16);
return ((high * 10) + low);
}
#include "Rtc_Ds1307.h"
Rtc_Ds1307 rtc(I2C_SDA, I2C_SCL);
Rtc_Ds1307::Time_rtc rtcTime;
rtcTime.year=2023;
rtcTime.date=23;
rtcTime.mon=3;
rtcTime.hour=12;
rtcTime.min=30;
rtcTime.sec = 12;
rtc.setTime(rtcTime, true, false);
rtc.getTime(rtcTime);
printf("%d:%d:%d Year %d\n", rtcTime.hour, rtcTime.min, rtcTime.sec, rtcTime.year);