Real Time Clock + Arduino

Hey folks.

I’m using the Sparkfun RealTime Clock Module with an older (pre-NG) Arduino. Using Wire.h, I’m able to communicate with the clock, set the time and read it, but after a few hours, the clock seems to go all crackers on me.

Typically what i have observed is this : after running well for a few hours, at the change of a new hour (has happened at 12h, 14h, and 6h), the clock will jump to 2h, for no apparent reason.

I’m using code I found on the arduinio boards :

#include <Wire.h>

int second = 0;     
int minute = 0;
int hour =0;
int day_of_week=0;
int day = 0;
int month = 0;
int year = 0;

void setup()
{  

  Wire.begin();
  Serial.begin(9600);
  delay(100); 
}


void loop()
{
  // reset register pointer
  Wire.beginTransmission(0x68);
  Wire.send(0);
  Wire.endTransmission();
  
  // request 7 bytes from ds1307
  Wire.requestFrom(104, 7);    
  second = Wire.receive();      
  minute = Wire.receive();      
  hour = Wire.receive();        
  day_of_week=Wire.receive();   
  day = Wire.receive();         
  month = Wire.receive();      
  year = Wire.receive();  

  // Convert all the BCD values that might have "tens" to decimal
  int hours=hour/16* 10 + hour % 16;
  int minutes=minute/16 * 10 + minute % 16;
  int days=day/16 * 10 + day % 16;
  int months=month/16 * 10 + month % 16;

  Serial.print( hours );
  Serial.print(":");
  Serial.print( minutes );
  Serial.println();
  //no need to rush
  delay(1000);

}

I’m using pull-up resistors in there, the circuit is built as it ought be, yet the clock (actually 2 different ones) both give me this problem.

Any ideas what is going on? Is it a code issue? A problem with using this older board? An issue with the clock modules (somehow i think that is unlikely)?

I am going to post a very similar thing at arduino.cc, mostly because I think it’s the board, but wanted to see if anyone had any insight on the clock aspect.

Sorry for the noise, but I think what was happening is that I was initially setting the clock incorrectly. Using the following code to set the clock time seems to have fixed my problems.

#include <Wire.h>

void setup(){

 Wire.begin(); 
 Serial.begin(9600);

 Wire.beginTransmission(0x68);
 Wire.send(0);
 Wire.send(0x00);         
 Wire.send(0x10);          
 Wire.send(0x80 | 0x02);    
 Wire.endTransmission();
}