Merged with dev

This commit is contained in:
Огънеяръ Яковлев 2024-08-31 14:56:58 +03:00
commit fa310cfa0d

View File

@ -21,8 +21,10 @@ void setup()
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)
{ {