ArduinoRTC

This program has been tested on the Arduino UNO R3. It will work on a Firewing shield that has a DS1307 Real Time Clock (RTC) installed.

Arduino Code

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; 

void setup(){
  Wire.begin();
  Serial.begin(9600);

  // enable this line to set the data and time 
  //setDateTime(); 
}

void loop(){
  printDateAndTime();
  delay(1000);
}

// use this routine to set the date and time
void setDateTime(){
  byte second =      00; //0-59
  byte minute =      42; //0-59
  byte hour =        13; //0-23
  byte weekDay =     3;  //1-7
  byte monthDay =    14; //1-31
  byte month =       11; //1-12
  byte year  =       12; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); 
  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.write(zero); 
  Wire.endTransmission();
}

// convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val){
  return ( (val/10*16) + (val%10) );
}

// convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)  {
  return ( (val/16*10) + (val%16) );
}

// output the date and time
void printDateAndTime(){

  // reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); 
  int weekDay = bcdToDec(Wire.read());        
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  // print the date and time
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(month);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
}