According to the datasheet, the bits in three registers are checked: 0x00, 0x02 and 0x07

This commit is contained in:
Ogneyar 2024-08-31 14:48:18 +03:00
parent 2cae6b1fe5
commit 49882b2eb6

View File

@ -19,10 +19,12 @@ void setup()
// Initializing the device as a master on the I2C bus // Initializing the device as a master on the I2C bus
Wire.begin(); Wire.begin();
// Set new time if necessary // Set new time if necessary
if (RTC_SET_NEW_TIME) if (RTC_SET_NEW_TIME) {
initDS1307(); // Initialization of ds1307
writeTimeToRtc(); // setting time is written inside the function writeTimeToRtc(); // setting time is written inside the function
}
} }
void loop() void loop()
@ -33,6 +35,47 @@ void loop()
} }
// ------------------- rtc ------------------- // // ------------------- rtc ------------------- //
// Function for initializing the DS1307 chip
void initDS1307(void)
{
/* According to the datasheet, the bits in three registers
are checked: 0x00, 0x02 and 0x07
*/
uint8_t oneByte;
Wire.requestFrom(RTC_ADDR, 1, 0x00, 1, 1);
// Get data
if(Wire.available())
oneByte = Wire.read();
if (oneByte & 0x80) {
Wire.beginTransmission(RTC_ADDR); // Start of transaction
Wire.write(0x00); // Writing command for a transaction
Wire.write(oneByte & ~0x80); // Writing data for a transaction
Wire.endTransmission(); // Completing a transaction
}
Wire.requestFrom(RTC_ADDR, 1, 0x02, 1, 1);
// Get data
if(Wire.available())
oneByte = Wire.read();
if (oneByte & 0x40) {
Wire.beginTransmission(RTC_ADDR); // Start of transaction
Wire.write(0x02); // Writing command for a transaction
Wire.write(oneByte & ~0x40); // Writing data for a transaction
Wire.endTransmission(); // Completing a transaction
}
Wire.requestFrom(RTC_ADDR, 1, 0x07, 1, 1);
// Get data
if(Wire.available())
oneByte = Wire.read();
if ((oneByte & 0x03) || !(oneByte & 0x10)) {
Wire.beginTransmission(RTC_ADDR); // Start of transaction
Wire.write(0x07); // Writing command for a transaction
Wire.write(oneByte & ~0x03 | 0x10); // Writing data for a transaction
Wire.endTransmission(); // Completing a transaction
}
}
// Function to write new time to rtc // Function to write new time to rtc
void writeTimeToRtc(void) void writeTimeToRtc(void)
{ {