dhakeparag81
Full Member level 2
Hello there,
I am working on RTC Ds1307 using pic18f87k22 controller from few days but i dont know where i go wrong.
I search most of the website but didnt get the solution.
Here i post my code can you please trace out where are the problems.
I used the internal library of mplabc18 compiler.
I am working on RTC Ds1307 using pic18f87k22 controller from few days but i dont know where i go wrong.
I search most of the website but didnt get the solution.
Here i post my code can you please trace out where are the problems.
Code:
#include<p18f87k22.h>
#include<i2c.h>
void delay(void);
signed char ack=0;
unsigned char sec=0,min=0,hr=0,day=0,date=0,month=0,year=0;
unsigned char temp=0,temp1=0,temp2=0,i=0;
#pragma interrupt chk_isr // used for high priority interrupt only.
void chk_isr()
{
if(PIR1bits.SSP1IF == 1)
{
PIR1bits.SSP1IF=0;
}
}
#pragma code HI_PRIO_INT=0x0008 // high priority interrupt location.
void HI_PRIO_INT(void)
{
_asm
goto chk_isr
_endasm
}
#pragma code
//-------------
void main(void)
{
OSCTUNE = 0X80; // 0xc0 for internal oscillator pll enabled
OSCCON=0X74; //16MHz
OSCCON2=0X00;
ANCON0=0x00; // make all analog channel as digital I/O.
ANCON1=0x00; // make all analog channel as digital I/O.
ANCON2=0x00;
ODCON1=0x00;
ODCON2=0x00;
PADCFG1=0x20;
CM1CON=0x00; // comparator1 OFF.
CM2CON=0x00; // comparator2 OFF.
CM3CON=0x00; // comparator3 OFF.
MEMCONbits.EBDIS=1;
ADCON0=0x00; // make all analog channel as digital I/O.
CM1CON=0x00; // comparator1 OFF.
CM2CON=0x00; // comparator2 OFF.
CM3CON=0x00; // comparator3 OFF.
TRISCbits.TRISC3=1;
TRISCbits.TRISC4=1;
OpenI2C1(MASTER,SLEW_OFF);
//SSP1CON1=0x2b;
SSP2CON2=0x080;
SSP1ADD=0x27; //for 100khz
IdleI2C1();
StartI2C1();
ack=WriteI2C1(0xD0);
delay();
ack=WriteI2C1(0x00);//add
ack=WriteI2C1(0x00);
ack=WriteI2C1(0x05);
ack=WriteI2C1(0x72);
ack=WriteI2C1(0x03);
ack=WriteI2C1(0x11);
ack=WriteI2C1(0x02);
ack=WriteI2C1(0x14);
ack=WriteI2C1(0x00);
StopI2C1();
delay();
delay();
while(1)
{
IdleI2C1();
StartI2C1();
delay();
//
// StartI2C1();
delay();
delay();
ack=WriteI2C1(0xD1);
delay();
ack=WriteI2C1(0x00);
delay();
sec=ReadI2C1();
AckI2C1();
delay();
/*
temp=0;
temp1=0;
temp2=0x80;
for(i=7;i>0;i=i-2)
{
temp1 = sec & temp2;
temp |= (temp1>>i);
temp2 >>=1;
if(i==1)
i=2;
//i=i-2;
}
for(i=1;i<8;i=i+2)
{
temp1 = sec & temp2;
temp |= (temp1<<i);
temp2 >>=1;
//i=i+2;
}
sec=temp;
*/
delay();
min=ReadI2C1();
AckI2C1();
delay();
/* temp=0;
temp1=0;
temp2=0x80;
for(i=7;i>0;i=i-2)
{
temp1 = min & temp2;
temp |= (temp1>>i);
temp2 >>=1;
if(i==1)
i=2;
//i=i-2;
}
for(i=1;i<8;i=i+2)
{
temp1 = min & temp2;
temp |= (temp1<<i);
temp2 >>=1;
//i=i+2;
}
min=temp;
*/
delay();
hr=ReadI2C1();
AckI2C1();
delay();
/*
temp=0;
temp1=0;
temp2=0x80;
for(i=7;i>0;i=i-2)
{
temp1 = hr & temp2;
temp |= (temp1>>i);
temp2 >>=1;
if(i==1)
i=2;
//i=i-2;
}
for(i=1;i<8;i=i+2)
{
temp1 = hr & temp2;
temp |= (temp1<<i);
temp2 >>=1;
//i=i+2;
}
hr=temp;
*/
delay();
day=ReadI2C1();
AckI2C1();
delay();
/*
temp=0;
temp1=0;
temp2=0x80;
for(i=7;i>0;i=i-2)
{
temp1 = day & temp2;
temp |= (temp1>>i);
temp2 >>=1;
if(i==1)
i=2;
//i=i-2;
}
for(i=1;i<8;i=i+2)
{
temp1 = day & temp2;
temp |= (temp1<<i);
temp2 >>=1;
//i=i+2;
}
day=temp;
*/
delay();
date=ReadI2C1();
AckI2C1();
delay();
/*
temp=0;
temp1=0;
temp2=0x80;
for(i=7;i>0;i=i-2)
{
temp1 = date & temp2;
temp |= (temp1>>i);
temp2 >>=1;
if(i==1)
i=2;
//i=i-2;
}
for(i=1;i<8;i=i+2)
{
temp1 = date & temp2;
temp |= (temp1<<i);
temp2 >>=1;
//i=i+2;
}
date=temp;
*/
delay();
month=ReadI2C1();
AckI2C1();
delay();
/* temp=0;
temp1=0;
temp2=0x80;
for(i=7;i>0;i=i-2)
{
temp1 = month & temp2;
temp |= (temp1>>i);
temp2 >>=1;
if(i==1)
i=2;
//i=i-2;
}
for(i=1;i<8;i=i+2)
{
temp1 = month & temp2;
temp |= (temp1<<i);
temp2 >>=1;
//i=i+2;
}
month=temp;
*/
delay();
year=ReadI2C1();
NotAckI2C1();
delay();
/*
temp=0;
temp1=0;
temp2=0x80;
for(i=7;i>0;i=i-2)
{
temp1 = year & temp2;
temp |= (temp1>>i);
temp2 >>=1;
if(i==1)
i=2;
//i=i-2;
}
for(i=1;i<8;i=i+2)
{
temp1 = year & temp2;
temp |= (temp1<<i);
temp2 >>=1;
//i=i+2;
}
year=temp;
*/
//
delay();
delay();
}
}
void delay(void)
{
unsigned int t=0;
for(t=0;t<250;t++);
}
I used the internal library of mplabc18 compiler.