Well ladies and gents,
It has been a real slog to try and identify and fix an issue I had with using a DS1307RTC in my project and I thought this may help someone else.
ISSUE:
Using the DS1307RTC library and a DS1307 Module.
All seemed to work standalone on UNO, I set the clock using the example sketches, and could read this back fine. I changed the code to make a class of this and again tested on UNO. All good so far!
I then integrated the new class into my full Mega project.
All compiled fine and seemed to work, however the Mega started hanging sporadically.
NOTE: my full project was already using I2C to talk to other modules (OLED + pair of purpose built Nano's) with no problems.
After lots of debugging I eventually tracked the issue down to function call in the DS1307RTC library, specifically:
...
void clsClock::getTimeDate(void)
{
#ifdef DEBUG_TRACE_PGM_ROUTE
Serial.println("clsClock::getTimeDate()");
#endif
#ifdef DEBUG
++numgetTimeDateCalls;
Serial.print(numgetTimeDateCalls);
Serial.print(" -> IN: clsClock::getTimeDate(): ");
#endif
if ([b][color=red]RTC.read(tm)[/color][/b])
{
DS1307Error = false;
#ifdef DEBUG
Serial.print("Format data : ");
#endif
sprintf(stringDate, "%02d/%02d/%04d", tm.Day, tm.Month, tmYearToCalendar(tm.Year));
sprintf(stringTime, "%02d:%02d:%02d", tm.Hour, tm.Minute, tm.Second);
...
This call was hanging somewhere inside the library and never returning.
Running the mini sketch on the Mega managed to reproduce the hang condition.
Lots of advice on the internet (DS1307 module shorting against battery, DS1307 module running with battery, pull-up resistors on SDA/SCL lines, replacement Wire.h libraries, etc) however decided to simplify the wiring (as attached).
SOLUTION
It appears that powering the RTC module from the 5V power rail, NOT directly from the Mega 5V/GND was the issue. Since directly powering from Mega, there have been no further hangs.
Hope this helps someone.