# Reading from the Pic RTCC?

#### spinnaker

Joined Oct 29, 2009
7,835
I am back at trying to get RTCC working on a Pic. The chip is the 18f26J53. I have some old code where I am reading the time twice, the code loops till these two reads agree. Of course I didn't comment it and can't remember why I was doing this. I have been looking through the datasheet as to what would make me read twice and loop but just not seeing it. So of course I just figured I was nuts so decided to check microchip's library. Lo and behold, they are reading multiple times too. I have posted their code below.

Why are we both reading twice than looping? Where would I have come up with that idea?

EDIT: This is the on board RTCC.

Code:
void RtccReadTime(rtccTime* pTm)
{
rtccTimeDate rTD0, rTD1;

do
{
mRtccClearRtcPtr();

rTD0.b[4]=RTCVALL;
rTD0.b[5]=RTCVALH;
rTD0.b[6]=RTCVALL;
rTD0.b[7]=RTCVALH;

mRtccClearRtcPtr();

rTD1.b[4]=RTCVALL;
rTD1.b[5]=RTCVALH;
rTD1.b[6]=RTCVALL;
rTD1.b[7]=RTCVALH;  // read the user value

}while(rTD0.f.sec!=rTD1.f.sec); // make sure you have the same sec

pTm->f.hour=rTD0.f.hour;
pTm->f.min=rTD0.f.min;
pTm->f.sec=rTD0.f.sec;  // update user's data

}

Last edited:

#### spinnaker

Joined Oct 29, 2009
7,835
Found it!

Whether RTCSYNC = 1 or 0, the user should employ a
firmware solution to ensure that the data read did not
fall on a rollover boundary, resulting in an invalid or
partial read. This firmware solution would consist of
reading each register twice and then comparing the two
values. If the two values matched, then, a rollover did
not occur.

#### Papabravo

Joined Feb 24, 2006
16,163
I always start with the default assumption that I do things for a reason, and I'm not crazy until the evidence leads in another direction. It was a good call to check the Microchip library to confirm there was a reason for the original choice.