Thursday 29 May 2014

MSP430G2553 Interfacing with DS1307

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


1 comment:

  1. after compiling it shows below error


    e:/texas_instruments/design/ti_recomended_download/energia-0101e0011-windows/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld.exe: region `ram' overflowed by 46 bytes
    e:/texas_instruments/design/ti_recomended_download/energia-0101e0011-windows/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld.exe: region `rom' overflowed by 1204 bytes
    e:/texas_instruments/design/ti_recomended_download/energia-0101e0011-windows/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3\libcrt0.a(_copy_data.o): In function `__do_copy_data':
    /root/mspgcc-20120406/BUILD/gcc/gcc/../../../gcc-4.6.3/gcc/config/msp430/crt0.S:195: relocation truncated to fit: R_MSP430_16_BYTE against symbol `__data_load_start' defined in *ABS* section in sketch_may24a.cpp.elf
    core.a(TimerSerial.cpp.o): In function `TimerSerial':
    E:\Texas_Instruments\design\TI_Recomended_download\energia-0101E0011-windows\energia-0101E0011\hardware\msp430\cores\msp430/TimerSerial.cpp:90: relocation truncated to fit: R_MSP430_16_BYTE against symbol `vtable for TimerSerial' defined in .rodata._ZTV11TimerSerial section in core.a(TimerSerial.cpp.o)
    core.a(TimerSerial.cpp.o): In function `TimerSerial::begin(unsigned long)':
    E:\Texas_Instruments\design\TI_Recomended_download\energia-0101E0011-windows\energia-0101E0011\hardware\msp430\cores\msp430/TimerSerial.cpp:100: relocation truncated to fit: R_MSP430_16 against symbol `pinMode_int' defined in .text.pinMode_int section in core.a(wiring_digital.c.o)
    E:\Texas_Instruments\design\TI_Recomended_download\energia-0101E0011-windows\energia-0101E0011\hardware\msp430\cores\msp430/TimerSerial.cpp:101: relocation truncated to fit: R_MSP430_16 against symbol `pinMode_int' defined in .text.pinMode_int section in core.a(wiring_digital.c.o)
    E:\Texas_Instruments\design\TI_Recomended_download\energia-0101E0011-windows\energia-0101E0011\hardware\msp430\cores\msp430/TimerSerial.cpp:111: relocation truncated to fit: R_MSP430_16 against symbol `__udivsi3' defined in .text.libgcc section in e:/texas_instruments/design/ti_recomended_download/energia-0101e0011-windows/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3\libgcc.a(_udivmod32.o)
    core.a(Wire.cpp.o): In function `TwoWire::write(unsigned char const*, unsigned int)':
    Wire.cpp:(.text._ZN7TwoWire5writeEPKhj+0x34): relocation truncated to fit: R_MSP430_16 against symbol `twi_transmit' defined in .text.twi_transmit section in core.a(twi.c.o)
    core.a(Wire.cpp.o): In function `TwoWire::write(unsigned char)':
    Wire.cpp:(.text._ZN7TwoWire5writeEh+0x3a): relocation truncated to fit: R_MSP430_16 against symbol `twi_transmit' defined in .text.twi_transmit section in core.a(twi.c.o)
    core.a(Wire.cpp.o): In function `TwoWire::TwoWire()':
    Wire.cpp:(.text._ZN7TwoWireC2Ev+0x10): relocation truncated to fit: R_MSP430_16_BYTE against symbol `vtable for TwoWire' defined in .rodata._ZTV7TwoWire section in core.a(Wire.cpp.o)
    core.a(Wire.cpp.o): In function `TwoWire::begin()':
    Wire.cpp:(.text._ZN7TwoWire5beginEv+0x12): relocation truncated to fit: R_MSP430_16 against symbol `twi_init' defined in .text.twi_init section in core.a(twi.c.o)
    core.a(Wire.cpp.o): In function `TwoWire::requestFrom(unsigned char, unsigned char, unsigned char)':
    Wire.cpp:(.text._ZN7TwoWire11requestFromEhhh+0x12): relocation truncated to fit: R_MSP430_16 against symbol `twi_readFrom' defined in .text.twi_readFrom section in core.a(twi.c.o)
    core.a(Wire.cpp.o): In function `TwoWire::requestFrom(int, int)':
    Wire.cpp:(.text._ZN7TwoWire11requestFromEii+0x4): additional relocation overflows omitted from the output
    collect2: ld returned 1 exit status

    ReplyDelete