IMHO, the Arduino makes I2C interfacing doggoned darned difficult... That said, someone else wrote a working demo for the DS1307 that I have expanded on (though a few hairs went gray in the process). Perhaps this will help.
BTW... bit 7 of the hours register does NOT control the 12/24 hour aspect of the clock; it is bit 6, and when bit 6 is zero the clock is in 24-hour mode. You should download the DS1307 data sheet as you're working with the part; it shows how to use the square wave output as well as the extra (battery backed) RAM of the DS1307.
// DS1307 demo
// -- by Jon McPhalen (www.jonmcphalen.com) -- based on work by others
// -- 29 DEC 2007
// SDA pin is Analog4
// SCL pin is Analog5
#include <Wire.h>
#define DS1307 0xD0 >> 1 // shift required by Wire.h (silly...)
// DS1307 clock registers
#define R_SECS 0
#define R_MINS 1
#define R_HRS 2
#define R_WKDAY 3
#define R_DATE 4
#define R_MONTH 5
#define R_YEAR 6
#define R_SQW 7
byte second = 0x00; // default to 01 JAN 2007, midnight
byte minute = 0x00;
byte hour = 0x00;
byte wkDay = 0x02;
byte day = 0x01;
byte month = 0x01;
byte year = 0x07;
byte ctrl = 0x00;
void setup()
{
Wire.begin();
Serial.begin(9600);
second = 0x45; // demo time
minute = 0x59;
hour = 0x23;
wkDay = 0x07;
day = 0x29;
month = 0x12;
year = 0x07;
ctrl = 0x00; // disable SQW output
setClock();
}
void loop()
{
getClock();
printHex2(hour);
Serial.print(":");
printHex2(minute);
Serial.print(":");
printHex2(second);
Serial.print(" ");
printDayName(bcd2Dec(wkDay));
Serial.print(" ");
printHex2(day);
Serial.print(" ");
printMonthName(bcd2Dec(month));
Serial.print(" 20");
printHex2(year);
Serial.println();
//no need to rush
delay(1000);
}
void setClock()
{
Wire.beginTransmission(DS1307);
Wire.send(R_SECS);
Wire.send(second);
Wire.send(minute);
Wire.send(hour);
Wire.send(wkDay);
Wire.send(day);
Wire.send(month);
Wire.send(year);
Wire.send(ctrl);
Wire.endTransmission();
}
void getClock()
{
Wire.beginTransmission(DS1307);
Wire.send(R_SECS);
Wire.endTransmission();
Wire.requestFrom(DS1307, 8);
second = Wire.receive();
minute = Wire.receive();
hour = Wire.receive();
wkDay = Wire.receive();
day = Wire.receive();
month = Wire.receive();
year = Wire.receive();
ctrl = Wire.receive();
}
byte bcd2Dec(byte bcdVal)
{
return bcdVal / 16 * 10 + bcdVal % 16;
}
void printHex2(byte hexVal)
{
if (hexVal < 0x10)
Serial.print("0");
Serial.print(hexVal, HEX);
}
void printDec2(byte decVal)
{
if (decVal < 10)
Serial.print("0");
Serial.print(decVal, DEC);
}
void printDayName(byte d)
{
switch (d) {
case 1:
Serial.print("SUN");
break;
case 2:
Serial.print("MON");
break;
case 3:
Serial.print("TUE");
break;
case 4:
Serial.print("WED");
break;
case 5:
Serial.print("THU");
break;
case 6:
Serial.print("FRI");
break;
case 7:
Serial.print("SAT");
break;
default:
Serial.print("???");
}
}
void printMonthName(byte m)
{
switch (m) {
case 1:
Serial.print("JAN");
break;
case 2:
Serial.print("FEB");
break;
case 3:
Serial.print("MAR");
break;
case 4:
Serial.print("APR");
break;
case 5:
Serial.print("MAY");
break;
case 6:
Serial.print("JUN");
break;
case 7:
Serial.print("JUL");
break;
case 8:
Serial.print("AUG");
break;
case 9:
Serial.print("SEP");
break;
case 10:
Serial.print("OCT");
break;
case 11:
Serial.print("NOV");
break;
case 12:
Serial.print("DEC");
break;
default:
Serial.print("???");
}
}