hi i'm trying to interface DS1307RTC with PIC16f877. I had done interfacing of ds1307 with 89c51 and was sucessful. i have modified the same code a bit for PIC16f877 but this isn't working i'm unable to find out the problem, please help me.
One thing more, in 89c51 code i had written " unsigned char ReadI2Crtc(bit ACK_Bit)" but in pic it gave error: "bit variable must be global or static" so i changed it to "unsigned char ReadI2Crtc(unsigned char ACK_Bit)" is this causing the problem. Please help me....It's really very urgent.
thanks a lot.
#define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))
static bit SDA @ PORTBIT(PORTB,7);
static bit SCL @ PORTBIT(PORTB,6);
/////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------
// StartRTC I2C
//-------------------------------
void StartRTC(void)
{
SDA = 1;
SCL = 1;
DelayUs(5);DelayUs(5);
SDA = 0;
DelayUs(5);DelayUs(5);
SCL = 0;
DelayUs(5);DelayUs(5);
}
//-------------------------------
// StopRTC I2C
//-------------------------------
void StopRTC(void)
{
SDA = 0;
DelayUs(5);DelayUs(5);
SCL = 1;
DelayUs(5);DelayUs(5);
SDA = 1;
}
//-------------------------------
// Write I2C
//-------------------------------
void WriteI2Crtc(unsigned char Data)
{
for (i3=0;i3<8;i3++)
{
SDA = (Data & 0x80) ? 1:0;
SCL=1;SCL=0;
Data<<=1;
}
SCL = 1;
DelayUs(5);DelayUs(5);
SCL = 0;
}
//-------------------------------
// Read I2C
//-------------------------------
unsigned char ReadI2Crtc(unsigned char ACK_Bit)
{
unsigned char Data=0;
SDA = 1;
for (i3=0;i3<8;i3++)
{
SCL = 1;
Data<<= 1;
Data = (Data | SDA);
SCL = 0;
DelayUs(5);
}
if (ACK_Bit == 1)
SDA = 0; // Send ACK
else
SDA = 1; // Send NO ACK
DelayUs(5);DelayUs(5);
SCL = 1;
DelayUs(5);DelayUs(5);
SCL = 0;
return Data;
}
//-------------------------------
// Read 1 byte form I2C
//-------------------------------
unsigned char ReadRTC(unsigned int Addr)
{
unsigned char Data;
StartRTC();
WriteI2Crtc(0xD0);
//WriteI2Crtc((unsigned char)(Addr>>8)&0xFF); // ....not valid for small EEPROMs
WriteI2Crtc((unsigned char)Addr&0xFF);
StartRTC();
WriteI2Crtc(0xD1);
Data = ReadI2Crtc(NO_ACK);
StopRTC();
return(Data);
}
//-------------------------------
// Write 1 byte to I2C
//-------------------------------
void WriteRTC(unsigned int Addr,unsigned char Data)
{
StartRTC();
WriteI2Crtc(0xD0);
//WriteI2Crtc((unsigned char)(Addr>>8)&0xFF); // send address high....not valid for small EEPROMs
WriteI2Crtc((unsigned char)Addr&0xFF); // send address low
WriteI2Crtc(Data);
StopRTC();
}
void ReadAllRTC(void)
{
Minutes=ReadRTC(0x01);
Minutes=((Minutes & 0xF0)>>4)* 10 + (Minutes & 0x0F);
Hours=ReadRTC(0x02) & 0x1F;
Hours=((Hours & 0xF0)>>4)* 10 + (Hours & 0x0F);
AmPm=(ReadRTC(0x02) & 0x20)>>5;
}
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
main()
.
.
.
.
.
.
ReadAllRTC();
.
.
.
.
.
One thing more, in 89c51 code i had written " unsigned char ReadI2Crtc(bit ACK_Bit)" but in pic it gave error: "bit variable must be global or static" so i changed it to "unsigned char ReadI2Crtc(unsigned char ACK_Bit)" is this causing the problem. Please help me....It's really very urgent.
thanks a lot.
#define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))
static bit SDA @ PORTBIT(PORTB,7);
static bit SCL @ PORTBIT(PORTB,6);
/////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------
// StartRTC I2C
//-------------------------------
void StartRTC(void)
{
SDA = 1;
SCL = 1;
DelayUs(5);DelayUs(5);
SDA = 0;
DelayUs(5);DelayUs(5);
SCL = 0;
DelayUs(5);DelayUs(5);
}
//-------------------------------
// StopRTC I2C
//-------------------------------
void StopRTC(void)
{
SDA = 0;
DelayUs(5);DelayUs(5);
SCL = 1;
DelayUs(5);DelayUs(5);
SDA = 1;
}
//-------------------------------
// Write I2C
//-------------------------------
void WriteI2Crtc(unsigned char Data)
{
for (i3=0;i3<8;i3++)
{
SDA = (Data & 0x80) ? 1:0;
SCL=1;SCL=0;
Data<<=1;
}
SCL = 1;
DelayUs(5);DelayUs(5);
SCL = 0;
}
//-------------------------------
// Read I2C
//-------------------------------
unsigned char ReadI2Crtc(unsigned char ACK_Bit)
{
unsigned char Data=0;
SDA = 1;
for (i3=0;i3<8;i3++)
{
SCL = 1;
Data<<= 1;
Data = (Data | SDA);
SCL = 0;
DelayUs(5);
}
if (ACK_Bit == 1)
SDA = 0; // Send ACK
else
SDA = 1; // Send NO ACK
DelayUs(5);DelayUs(5);
SCL = 1;
DelayUs(5);DelayUs(5);
SCL = 0;
return Data;
}
//-------------------------------
// Read 1 byte form I2C
//-------------------------------
unsigned char ReadRTC(unsigned int Addr)
{
unsigned char Data;
StartRTC();
WriteI2Crtc(0xD0);
//WriteI2Crtc((unsigned char)(Addr>>8)&0xFF); // ....not valid for small EEPROMs
WriteI2Crtc((unsigned char)Addr&0xFF);
StartRTC();
WriteI2Crtc(0xD1);
Data = ReadI2Crtc(NO_ACK);
StopRTC();
return(Data);
}
//-------------------------------
// Write 1 byte to I2C
//-------------------------------
void WriteRTC(unsigned int Addr,unsigned char Data)
{
StartRTC();
WriteI2Crtc(0xD0);
//WriteI2Crtc((unsigned char)(Addr>>8)&0xFF); // send address high....not valid for small EEPROMs
WriteI2Crtc((unsigned char)Addr&0xFF); // send address low
WriteI2Crtc(Data);
StopRTC();
}
void ReadAllRTC(void)
{
Minutes=ReadRTC(0x01);
Minutes=((Minutes & 0xF0)>>4)* 10 + (Minutes & 0x0F);
Hours=ReadRTC(0x02) & 0x1F;
Hours=((Hours & 0xF0)>>4)* 10 + (Hours & 0x0F);
AmPm=(ReadRTC(0x02) & 0x20)>>5;
}
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
main()
.
.
.
.
.
.
ReadAllRTC();
.
.
.
.
.