RTC DS1307

Photo of author

By Jackson Taylor

Here i am interfacing Real time clock with Arduino Uno and sends time to pc via serial port
download simulation and program files

//connect SDA—> A4, SCL—>A5
#include <Wire.h>
const int DS1307 = 0x68; // Address of DS1307 see data sheets
byte second = 0;
byte minute = 0;
byte hour = 0;
byte weekday = 0;
byte monthday = 0;
byte month = 0;
byte year = 0;
void setup() 
{
  Wire.begin();
  Serial.begin(9600);
  //setTime(); 
}
void loop()
{
  readTime();
  delay(1000);
}
byte bcdToDec(byte val) 
{
  int msbdec=val/16*10; //gets decimel msb of BCD value(4 bits)
  int lsbdec = val%16;//gets decimel lsb of BCD value(4 bits)
  int total=msbdec+lsbdec;
  return (total);
    //return ((val/16*10) + (val%16));
}
void readTime() 
{
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.endTransmission();
  Wire.requestFrom(DS1307, 7);
  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read());
  weekday = bcdToDec(Wire.read());
  monthday = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
   Serial.print(monthday);
    Serial.write(‘-‘); 
    Serial.print(month );
    Serial.write(‘-‘); 
    Serial.print(year);
    Serial.write(‘-‘);
    Serial.write(‘-‘);
     Serial.print(hour);
    Serial.write(‘:’);
  Serial.print(minute);
    Serial.write(‘:’);
  Serial.print(second);
  Serial.write(0xd);
}
void setTime() {
  // The following codes transmits the data to the RTC
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0)); //starts sending data writing 
  Wire.write(0x12);//second
  Wire.write(0x20); //minute
  Wire.write(0x15); //hour
  Wire.write(0x7); //weekday
  Wire.write(0x30);//monthday
  Wire.write(0x8);//month
  Wire.write(0x14);//year
  Wire.write(byte(0));
  Wire.endTransmission();
  // Ends transmission of data
}

See also
A Unified Treatment of a Family of ZVS and ZCS Resonant Inverters-SIMULATION