Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

HElp usiung DS1307 real time clock with pic 18f452

Status
Not open for further replies.

engr.waqas

Full Member level 3
Joined
Jul 21, 2009
Messages
172
Helped
13
Reputation
26
Reaction score
10
Trophy points
1,298
Location
karachi,Pakistan
Activity points
2,342
I want to interface DS1307 with Pic 18f452 using I2C interface.
I am not able to read data properly and only "??" is displaying on LCD.

My Coding in MPLAB C18 is provided below
NOTE: This coding is only for "second halting" (enabeling CH bit in second register of DS1307)

#include<p18f452.h>
#include <i2c.h>
#define rs PORTBbits.RB0
#define rw PORTBbits.RB1
#define en PORTBbits.RB2

void MSDelay(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}

void lcdcmd(unsigned char value)
{
PORTD=value;
rs=0;
rw=0;
en=1;
MSDelay(1);
en=0;
}



void BCDtoASCIIandSEND(unsigned char myvalue)
{
unsigned char tmp=myvalue;
//LCD initilization

TRISD=0;
TRISB=0;
en=0;
MSDelay(15);
lcdcmd(0x38);
MSDelay(15);
lcdcmd(0x0C);
MSDelay(15);
lcdcmd(0x01);
MSDelay(15);
lcdcmd(0x06);
MSDelay(15);
lcdcmd(0x86);
MSDelay(15);


tmp=tmp&0xF0;//mask lower nibble
tmp=tmp>>4;//swap it
tmp=tmp|0x30;//make it ASCII

PORTD=tmp;
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
MSDelay(1);
tmp=myvalue;//for other digit
tmp=tmp&0x0F;//mask upper nibble
tmp=tmp|0x30;//make it ASCII

PORTD=tmp;
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
MSDelay(1);

PORTD=':';
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
MSDelay(1);
}


void SDELAY(int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<135;j++);
}

void init(void)
{
TRISCbits.TRISC3 =1; //RC3 as INPUT (SCL)
TRISCbits.TRISC4 =1; //RC4 as INPUT (SDA)
//MSSP Configuration
SSPCON1 =0x28; //Enable MSSP in Master mode
SSPCON2 =0;
SSPSTATbits.SMP =1; //Disable slew rate control
SSPSTATbits.CKE =0; //I2C compatible
SSPADD =0x09; //Fscl = 100kHz pour Fosc=4MHz
//Fscl = Fosc/(4*(SSPADD+1))
TRISD=0;
TRISB=0;
en=0;
MSDelay(15);
lcdcmd(0x38);
MSDelay(15);
lcdcmd(0x0C);
MSDelay(15);
lcdcmd(0x01);
MSDelay(15);
lcdcmd(0x06);
MSDelay(15);
lcdcmd(0x86);
MSDelay(15);

}
void main (void)
{

unsigned char temp1=0,i;
init();


while (1) {
StartI2C();
if (!DataRdyI2C())
WriteI2C(0x00); //Address
IdleI2C();
if (!DataRdyI2C())
WriteI2C(0x80); //DATA
IdleI2C();
StopI2C();
IdleI2C();



if (!DataRdyI2C())
temp1 = ReadI2C();
StopI2C();
if (i==4)
NotAckI2C(); //NACK before STOP
else
AckI2C(); //ACK for next read i2c_idle();

BCDtoASCIIandSEND(temp1);
}

StopI2C();

}
 

Hi,I was reading your code i I can notice that your i2c writing sequence is wrong, you should use the next sequence:
start
write(SLAVE_ADDRESS+WR=0)
write(SLAVE_REGISTER)
write(SLAVE DATA)
stop
And in this case SLAVE_ADDRES is 0xC0 =0b1101000+0
I hope it can help you

Added after 3 minutes:

almost a year later,sorry

Added after 14 minutes:

Sorry SLAVE_ADDRESS is 0xd0 nor 0xc0
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top