Here Iam interfacing DS1307 with msp430g2553 in Energia 0101E0011 , using "wire.h" .
The hour, minute ,and second is displayed on energia serial monitor.The remaining can be easily added.
The output can displayed on LCD if it is interfaced with this .
The common mistake everyone encounter with ds1307 and msp430 is that ds1307 is working in minimum 4.5 volt max 5v and msp430g2553 is low power max 3.5v. Here I changed the pullup voltage to 3.5 volt for ds1307 and vcc is 5v and also low power for msp430.
http://datasheets.maximintegrated.com/en/ds/DS1307.pdf this is the ds1307 datasheet.
The code is added below.
/////////////////////////////////
#include "Wire.h" //for DS1307 I2C
#include <string.h>
#include <math.h>
#define DS1307_I2C_ADDRESS 0x68
byte second, minute, hour, dayOfWeek,
dayOfMonth, month, year;
void setup()
{
Wire.begin();
Serial.begin(9600);
/*
//////here programmer set the current time ,from where the ds runs
*/
second = 45;
minute = 30;
hour
= 16;
dayOfWeek = 5;
dayOfMonth = 20;
month = 3;
year
= 12;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}
void loop()
{
getDateDs1307(&second, &minute,
&hour, &dayOfWeek, &dayOfMonth, &month, &year);///to get
current time from ds1307
timedisplay();
}
void timedisplay() ///////////Function to display
current time
{
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.println(" ");
delay(1000);
}
void getDateDs1307(byte *second, // Gets the date and time from the ds1307
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
//
Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A
few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read()
& 0x7f);
*minute =
bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read()
& 0x3f); // Need to change this if
12 hour am/pm
*dayOfWeek =
bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month =
bcdToDec(Wire.read());
*year =
bcdToDec(Wire.read());
}
void setDateDs1307(byte second, // 0-59 ///to set time in ds1307
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second));
// 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
// If you want 12 hour am/pm you need to set
// bit 6
(also need to change readDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
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) );
}
The circuit is below