Sign In

Trouble In Getting Date And Time From Ds1307 Rtc

Madhura Manjunath Hegde madhurahegde8@gmail.com India

Hardware: Msp430g2452, I2C Ds1307 Rtc At24c32 Eeprom Software: Code Composer Studio

I'm trying to interface msp430g2452 to ds1307 rtc . I got a code from forum which interfaces msp430g2452 with ds3232 rtc. What are the changes I need to do to interface msp430g2452 with ds1307 rtc. Please help me.

Images

Program

#include <msp430.h>
#include "i2c.h"
/**
* Kerr D. Wong
* http://www.kerrywong.com
*
* D3232 RTC Example Using MSP430G2452
*/
#define HIGH 1
#define LOW 0
#define OUTPUT 1
#define INPUT 0
#define DATETIME_REG_ADDR 0x00
#define CONTROL_REG_ADDR 0x07
#define SRAM_ADDR 0x08
#define _BV(bit) (1 << (bit))
#define DS1307_I2C_ADDRESS 0x68
//pins on PORT 1
// 4-7seg display pins
#define pinOE 0
#define pinLatch 1
#define pinClk 2
#define pinData 3
//pins on PORT 2
#define pinSecondSense 1
//depends on how the 7-seg display
//is implemented, you will likely
//need to change this
const unsigned char NUMS[] = {
255-252, 255-96, 255-218, 255-242, 255-102,
255-182, 255-190, 255-224, 255-254, 255-246,
255 //blank
};
typedef struct { unsigned char second; //00H
unsigned char minute; //01H
unsigned char hour; //02H
unsigned char dayOfWeek; //03H
unsigned char dayOfMonth; //04H
unsigned char month; //05H
unsigned char year; //06H
} Date;
Date dt;
/**
* Stop the Watch Dog Timer
*/
void inline stopWatchDogTimer() {
WDTCTL = WDTPW + WDTHOLD;
}
/**
* Set the pin mode
* portNo: port (1 or 2)
* pinNo: pin number in the port specified
* pinMode: INPUT/OUTPUT
*/
void pinMode(unsigned char portNo, unsigned char pinNo, unsigned char pinMode) {
if (portNo == 1) {
if (pinMode)
P1DIR |= (1 << pinNo);
else
P1DIR &= ~(1 << pinNo);
} else if (portNo == 2) {
if (pinMode)
P2DIR |= (1 << pinNo);
else
P2DIR &= ~(1 << pinNo);
}
}
/**
* Write HIGH or LOW to a pin previously set as output
* portNo: port (1 or 2)
* pinNo: pin number in the port specified
* value: HIGH/LOW
*/
void digitalWrite(unsigned char portNo, unsigned char pinNo, unsigned char value) {
if (portNo == 1) {
if (value)
P1OUT |= 1 << pinNo;
else
P1OUT &= ~(1 << pinNo);
} else if (portNo == 2) {
if (value)
P2OUT |= 1 << pinNo;
else
P2OUT &= ~(1 << pinNo);
}
}
/**
* Read logic level from pin
* portNo: port (1 or 2)
* pinNo: pin number in the port specified
*/
unsigned char digitalRead(unsigned char portNo, unsigned char pinNo) {
int result;
result = 0;
if (portNo == 1) {
result = (P1IN & (1 << pinNo)) >> pinNo;
} else if (portNo == 2) {
result = (P2IN & (1 << pinNo)) >> pinNo;
}
return result;
}
/**
* Shift out using Port 1
*/
void shiftOut1(unsigned char dataPin, unsigned char clockPin, unsigned char value)
{
int i;
for (i = 0; i < 8; i++) {
digitalWrite(1, dataPin, (value & _BV(i)));
digitalWrite(1, clockPin, HIGH);
digitalWrite(1, clockPin, LOW);
}
}
//Display the curent time
//secInd is fed from the SQW pin (pin 5) which is used to create the
//flashing effect for the second indicator ":"
void displayTime(unsigned char hour, unsigned char min, unsigned char secInd)
{
unsigned char d1 = 0, d2 = 0, d3 = 0, d4 = 0;
d1 = hour/10;
d2 = hour - d1 * 10 ;
d3 = min/10;
d4 = min - d3 * 10;
if (d1 == 0) d1 = 10;
digitalWrite(1, pinLatch, LOW);
shiftOut1(pinData, pinClk, NUMS[d4]);
if (secInd > 0) {
shiftOut1(pinData, pinClk, NUMS[d3] - 1);
shiftOut1(pinData, pinClk, NUMS[d2] - 1);
} else {
shiftOut1(pinData, pinClk, NUMS[d3]);
shiftOut1(pinData, pinClk, NUMS[d2]);
}
shiftOut1(pinData, pinClk, NUMS[d1]);
digitalWrite(1, pinLatch, HIGH);
}
// Convert normal decimal numbers to binary coded decimal
unsigned char decToBcd(unsigned char val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
unsigned char bcdToDec(unsigned char val)
{
return ( (val/16*10) + (val%16) );
}
// Gets the date and time from the ds1307 and prints result
void getDateDS1307()
{
i2c_start();
i2c_write8(DS1307_I2C_ADDRESS << 1);
i2c_write8(DATETIME_REG_ADDR);
i2c_stop();
i2c_start();
i2c_write8(DS1307_I2C_ADDRESS << 1 | 1);
dt.second = bcdToDec(i2c_read8(0x00));
dt.minute = bcdToDec(i2c_read8(0x00));
dt.hour = bcdToDec(i2c_read8(0x00)); // Need to change this if 12 hour am/pm
dt.dayOfWeek = bcdToDec(i2c_read8(0x00)); // 0= Sunday
dt.dayOfMonth = bcdToDec(i2c_read8(0x00));
dt.month = bcdToDec(i2c_read8(0x00));
dt.year = bcdToDec(i2c_read8(0x00));
i2c_stop();
}
void setDate1307()
{
i2c_start();
i2c_write8(DS1307_I2C_ADDRESS << 1);
i2c_write8(DATETIME_REG_ADDR);
i2c_write8(decToBcd(00)); //sec
i2c_write8(decToBcd(43)); //min
i2c_write8(decToBcd(20)); //hour
i2c_write8(decToBcd(7)); //day
i2c_write8(decToBcd(05)); //date
i2c_write8(decToBcd(01)); //month
i2c_write8(decToBcd(13)); //year
i2c_stop();
}
void setControlRegisters(){
i2c_start();
i2c_write8(DS1307_I2C_ADDRESS << 1);
i2c_write8(CONTROL_REG_ADDR); //Goto register 0Eh
i2c_write8(0x00);
i2c_stop();
}
void setupRTC1307(){
i2c_init();
//setDate3232(); // call this initially to set the date
setControlRegisters();
}
void init()
{
stopWatchDogTimer();
pinMode(1, pinOE, OUTPUT);
pinMode(1, pinLatch, OUTPUT);
pinMode(1, pinClk, OUTPUT);
pinMode(1, pinData, OUTPUT);
pinMode(2, pinSecondSense, INPUT);
digitalWrite(1, pinOE, LOW);
digitalWrite(1, pinData, HIGH);
digitalWrite(1, pinLatch, HIGH);
digitalWrite(1, pinClk, HIGH);
setupRTC1307();
}
void main(void) {
int r = 0;
init();
for(;;) {
getDateDS1307();
r = digitalRead(2, pinSecondSense);
displayTime(dt.hour, dt.minute, r);
}
}




//code adapted from http://www.43oh.com/forum/viewtopic.php?f=9&t=139
//Kerry D. Wong
//http://www.kerrywong.com
#include "i2c.h"
#include <msp430.h>

#define SDA  BIT7 //P1.7
#define SCL  BIT6 //P1.6

void i2c_init(void)
{
    P1DIR |= SCL | SDA; // Set SCL, SDA as Output
    P1REN |= SCL | SDA; // Set Pull-Ups on SCL and SDA


    // enable SDA, SCL, SCLK, i2c mode, MSB, output enabled, hold in reset
    USICTL0 = USIPE7 | USIPE6 | USIMST | USIOE | USISWRST;

    // USICTL0 Upper 8bit Register of 16bit USICTL Register
	// USIPE7   = P1.7 USI Mode, i2c SDA enabled
	// USIPE6   = P1.6 USI Mode, i2c SCL enabled
	// USIPE5   = P1.5 USI Mode, i2c Clock Input? (Not Set)
	// USILSB   = LSB Mode (Not Set = MSB)
	// USIMST   = Master Mode
	// USIGE    = Output Latch (Not Set = Clock Controlled)
	// USIOE    = Data Output Enable
	// USISWRST = USI Software Reset (Set to allow changing settings)


    // SMCLK / 4, and Reverse Clock Polarity
    USICKCTL = USIDIV_1 + USISSEL_2 + USICKPL;

    // USICKCTL 8bit USI Clock Control Register
	// USIDIVx  = Clock Divider (Bit7-5, USIDIV_2 = Divide by 4)
	// USISSELx = Clock Source (For Master Mode, Bit4-2, USISSEL_2 = SMCLK)
	// USICKPL  = Clock Polarity (0 = Inactive Low, 1 = Inactive High)
	// USISWCLK = Software Clock State

    // I2C Mode
    USICTL1 = USII2C;

    // USICTL1 Lower 8bit Register of 16bit USICTL Register
	// USICKPH   = Clock Phase (0 = Data Changed, then Captured, 1 = Data Captured, then Changed)
	// USII2C    = I2C mode
	// USISTTIE  = START condition Interrupt
	// USIIE     = USI Counter Interrupt
	// USIAL     = Arbitration Lost Notification
	// USISTP    = STOP condition Notification
	// USISTTIFG = START condition Int. Flag
	// USIIFG    = USI Counter Int. Flag

    // release from reset
    USICTL0 &= ~USISWRST;
}

void i2c_start(void)
{
    // Send i2c START condition
    USISRL = 0x00; // Load USISRL Lower Byte Shift Register MSB with 0 for i2c START
    USICTL0 |= USIGE | USIOE; // Force Output Latch, And Enable Data Output Bit (High to Low SDA while SCL is High)
    USICTL0 &= ~USIGE; // Clear Output Latch (Return to Clock Control)
}

void i2c_stop(void)
{
	// Prepare i2c STOP condition
    USICTL0 |= USIOE; // Enable Data Output Bit (Turn SDA into Output)
    USISRL = 0x00; // Load USISRL Lower Byte Shift Register MSB with 0 for i2c STOP
    USICNT = 1; // Load USICNT Counter with number of Bits to Send. USIIFG Auto-Cleared
    // Data TXed by USI I2C
    while((USICTL1 & USIIFG) != 0x01); // Delay, Wait for USIIFG, Counter down to 0

    // Send i2c STOP condition
    USISRL = 0xFF; // Load USISRL Lower Byte Shift Register MSB with 1 for i2c STOP
    USICTL0 |= USIGE; // Force Output Latch (Low to High SDA while SCL is High)
    USICTL0 &= ~USIOE & ~USIGE ; // Clear Data Output Enable Bit and Output Latch (Release SCL)
}

unsigned char i2c_write8(unsigned char c)
{
// TX
    USICTL0 |= USIOE; // Enable Data Output Bit (Turn SDA into Output)
    USISRL = c; // Load USISRL Lower Byte Shift Register with 8 Bit data (Byte)
    USICNT = 8; // Load USICNT Counter with number of Bits to Send. USIIFG Auto-Cleared
    // Data TXed by USI I2C
    while((USICTL1 & USIIFG) != 0x01); // Delay, Wait for USIIFG, Counter down to 0

// RX
    // Data TXed. Ready to Receive (n)ACK from i2c Slave
    USICTL0 &= ~USIOE; // Clear Data Output Enable Bit (Turn SDA into Input)
    USICNT = 1; // Load USICNT Counter with number of Bits to Receive. USIIFG Auto-Cleared
    // Data RXed by USI I2C
    while((USICTL1 & USIIFG) != 0x01); // Delay, Wait for USIIFG, Counter down to 0

// Return Data
    c = USISRL; // LSB of USISRL Holds Ack Status of 0 = ACK (0x00) or 1 = NACK (0x01)
    return c;
}

unsigned char i2c_read8(unsigned char acknack)
{
// RX
    USICTL0 &= ~USIOE; // Clear Data Output Enable Bit (Turn SDA into Input)
    USISRL = 0x00; // Clear USISRL Lower Byte Shift Register (Byte)
    USICNT = 8; // Load USICNT Counter with number of Bits to Receive. USIIFG Auto-Cleared
    // Data RXed by USI I2C
    while((USICTL1 & USIIFG) != 0x01); // Delay, Wait for USIIFG, Counter down to 0

// Copy Data to c
    unsigned char c;
    c = USISRL; // USISRL Holds Received Data

// TX
    // Data RXed. Ready to Send (n)ACK to i2c Slave
    USICTL0 |= USIOE; // Enable Data Output Bit (Turn SDA into Output)
    USISRL = acknack; // Load USISRL Lower Byte Shift Register MSB with acknack (0x00 = Ack, 0xFF = Nack)
    USICNT = 1; // Load USICNT Counter with number of Bits to Send. USIIFG Auto-Cleared
    // Data TXed by USI I2C
    while((USICTL1 & USIIFG) != 0x01); // Delay, Wait for USIIFG, Counter down to 0

// Return Data
    return c;
}




#ifndef I2C_H
#define I2C_H

void i2c_init(void);
void i2c_start(void);
void i2c_stop(void);
unsigned char i2c_write8(unsigned char c);
unsigned char i2c_read8(unsigned char acknack);

#endif

Post a reply to Madhura Manjunath Hegde

Think you can help Madhura Manjunath Hegde ? Then post your thoughts that might help Madhura Manjunath Hegde. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.