Other Parts Discussed in Thread: MSP430F2616
Dear All,
I am trying to interface RTC DS1307 with MSP430F2616.
But iam getting a problem that i am unable to get the response from DS1307 using I2C Bit-Bang method and i think there is a problem with that code. Please help me regarding sorting out this issue.
For your reference i am attaching my code below. Also I would like to say I am running my Controller with 4Mhz Clock. I am using Port3.1 and port3.2 for SDA and SCl line respectively.
#include "UART_Init.h"
#include "I2C_master.h"
#define I2C_MASTER_REN P3REN
#define I2C_MASTER_DIR P3DIR
#define I2C_MASTER_OUT P3OUT
#define I2C_MASTER_IN P3IN
//port pins
#define I2C_MASTER_SCL BIT2
#define I2C_MASTER_SDA BIT1
#define ACK 1
#define NO_ACK 0
#define SLAVE 0xD0
#define WRITE 0x00
#define READ 0x01
#define ERR_ACK 0x01
//-------------------------------
// start I2C
//-------------------------------
void Start(void)
{
I2C_MASTER_DIR|= I2C_MASTER_SDA;
I2C_MASTER_DIR|= I2C_MASTER_SCL;
I2C_MASTER_OUT|= I2C_MASTER_SDA;
I2C_MASTER_OUT|= I2C_MASTER_SCL;
// delay_us(2);
I2C_MASTER_OUT&= ~I2C_MASTER_SDA;
// delay_us(2);
I2C_MASTER_OUT&= ~I2C_MASTER_SCL;
// delay_us(2);
}
//-------------------------------
// stop I2C
//-------------------------------
void Stop(void)
{
I2C_MASTER_OUT&= ~I2C_MASTER_SDA;
// delay_us(2);
I2C_MASTER_OUT|= I2C_MASTER_SCL;
// delay_us(2);
I2C_MASTER_OUT|= I2C_MASTER_SDA;
}
//-------------------------------
// Write I2C
//-------------------------------
void WriteI2C(unsigned char Data)
{
uint8_t i = 0;
for (i=0;i<8;i++)
{
if(Data & 0x80)
I2C_MASTER_OUT|= I2C_MASTER_SDA;
else
I2C_MASTER_OUT&= ~I2C_MASTER_SDA;
I2C_MASTER_OUT|= I2C_MASTER_SCL;
I2C_MASTER_OUT&= ~I2C_MASTER_SCL;
Data<<=1;
}
I2C_MASTER_OUT|= I2C_MASTER_SCL;
delay_us(2);
I2C_MASTER_OUT&= ~I2C_MASTER_SCL;
}
//-------------------------------
// Read I2C
//-------------------------------
unsigned char ReadI2C(uint8_t ACK_Bit)
{
uint8_t i = 0;
unsigned char Data=0;
I2C_MASTER_OUT&= ~I2C_MASTER_SDA;
I2C_MASTER_DIR&= ~I2C_MASTER_SDA;
for (i=0;i<8;i++)
{
I2C_MASTER_OUT|= I2C_MASTER_SCL;
// Data<<= 1;
if( I2C_MASTER_IN & I2C_MASTER_SDA )
Data|= (0x01 << i);
else
Data|= (0x00 << i);
I2C_MASTER_OUT&= ~I2C_MASTER_SCL;
// delay_us(2);
}
I2C_MASTER_DIR|= I2C_MASTER_SDA;
if (ACK_Bit == 1)
I2C_MASTER_OUT&= ~I2C_MASTER_SDA; // Send ACK
else
I2C_MASTER_OUT|= I2C_MASTER_SDA; // Send NO ACK
// delay_us(2);
I2C_MASTER_OUT|= I2C_MASTER_SCL;
// delay_us(2);
I2C_MASTER_OUT&= ~I2C_MASTER_SCL;
return Data;
}
//-------------------------------
// Read 1 byte form I2C
//-------------------------------
unsigned char ReadBYTE(unsigned char Addr)
{
unsigned char Data;
Start();
WriteI2C(0xD0);
WriteI2C(Addr);
Start();
WriteI2C(0xD1);
Data = ReadI2C(NO_ACK);
Stop();
return(Data);
}
//-------------------------------
// Write 1 byte to I2C
//-------------------------------
void WriteBYTE(unsigned char Addr,unsigned char Data)
{
Start();
WriteI2C(0xD0);
WriteI2C(Addr);
WriteI2C(Data);
Stop();
}
//-------------------------------
// Read RTC (all real time)
//-------------------------------
void ReadRTC(unsigned char * buff)
{
Start();
WriteI2C(0xD0);
WriteI2C(0x00);
WriteI2C(0x0F);
delay_ms(2);
Start();
WriteI2C(0xD1);
*(buff+0)=ReadI2C(ACK); // Second
// *(buff+1)=ReadI2C(ACK); // Minute
// *(buff+2)=ReadI2C(ACK); // hour
// *(buff+3)=ReadI2C(ACK); // Day
// *(buff+4)=ReadI2C(ACK); // date
// *(buff+5)=ReadI2C(ACK); // month
// *(buff+6)=ReadI2C(NO_ACK); // year
Stop();
}
//-------------------------------
// Write RTC
//-------------------------------
void WriteRTC(unsigned char *buff)
{
Start();
WriteI2C(0xD0);
WriteI2C(0x00);
WriteI2C(*(buff+0));
WriteI2C(*(buff+1));
WriteI2C(*(buff+2));
WriteI2C(*(buff+3));
WriteI2C(*(buff+4));
WriteI2C(*(buff+5));
WriteI2C(*(buff+6));
Stop();
}
Thank you,
Regards,
Shravan Kumar