Problems with interfacing V3021 clock with PIC18F458

Status
Not open for further replies.

stroma

Advanced Member level 4
Joined
Apr 25, 2002
Messages
114
Helped
9
Reputation
18
Reaction score
6
Trophy points
1,298
Activity points
837
v3021 clock

hi all,
I’m trying to interface V3021 real time clock to PIC18F458 but have some problems. I can’t get V3021 running every time. It seems to me that it is not initialized properly. This is control functions I wrote:

#define TRISCIO TRISC2
#define INPUT 1
#define OUTPUT 0
//CS pin of V3021 is permanently connected to GND
volatile bit CWR @ ((unsigned)&PORTC*8+0); //to WR pin of V3021
volatile bit CRD @ ((unsigned)&PORTC*8+1); //to RD pin of V3021
volatile bit CIO @ ((unsigned)&PORTC*8+2); //to IO pin of V3021

void delay(void)
{
CLRWDT();
CLRWDT();
CLRWDT();
CLRWDT();
}

unsigned char rtc_get_data(unsigned char addr)
{
unsigned char i;
unsigned char data;
CLRWDT();
data = 0;
CRD = 1;
CWR = 1;
TRISCIO = OUTPUT;
delay();
for (i=0;i<4;i++)
{
CIO = addr;
delay();
CWR = 0;
delay();
CWR = 1;
addr >>= 1;
}
TRISCIO = INPUT;
for (i=0;i<8;i++)
{
data >>= 1;
delay();
CRD = 0;
delay();
if (CIO)
{
data |= 0x80;
}
CRD = 1;
}
return(data);
}

void rtc_put_data(unsigned char addr, unsigned char data)
{
unsigned char i;
CLRWDT();
CRD = 1;
CWR = 1;
TRISCIO = OUTPUT;
delay();
for (i=0;i<4;i++)
{
CIO = addr;
delay();
CWR = 0;
delay();
CWR = 1;
addr >>= 1;
}
for (i=0;i<8;i++)
{
CIO = data;
delay();
CWR = 0;
delay();
CWR = 1;
data >>= 1;
}
TRISCIO = INPUT;
}

void rtc_put_cmd(unsigned char cmd)
{
unsigned char i;
CLRWDT();
CRD = 1;
CWR = 1;
TRISCIO = OUTPUT;
for (i=0;i<4;i++)
{
CIO = cmd;
delay();
CWR = 0;
delay();
CWR = 1;
cmd >>= 1;
}
TRISCIO = INPUT;
}

void rtc_reset()
{
unsigned char i;
CLRWDT();
CRD = 1;
CWR = 1;
TRISCIO = INPUT;
for (i=0;i<8;i++)
{
CRD = 0;
delay();
CRD = 1;
}
}

Anyone has experience with this RTC ? help please!
Thanks.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…