Reading from the Pic RTCC?

Discussion in 'Embedded Systems and Microcontrollers' started by spinnaker, Dec 4, 2015.

  1. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    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 (Text):
    2. void RtccReadTime(rtccTime* pTm)
    3. {
    4.   rtccTimeDate rTD0, rTD1;
    6.   do
    7.   {
    8.   mRtccClearRtcPtr();
    9.   mRtccSetRtcPtr(RTCCPTR_MASK_HRSWEEK);
    11.   rTD0.b[4]=RTCVALL;
    12.     rTD0.b[5]=RTCVALH;
    13.     rTD0.b[6]=RTCVALL;
    14.     rTD0.b[7]=RTCVALH;
    15.   // read the user value
    17.   mRtccClearRtcPtr();
    18.   mRtccSetRtcPtr(RTCCPTR_MASK_HRSWEEK);
    20.   rTD1.b[4]=RTCVALL;
    21.     rTD1.b[5]=RTCVALH;
    22.     rTD1.b[6]=RTCVALL;
    23.     rTD1.b[7]=RTCVALH;  // read the user value
    25.   }while(rTD0.f.sec!=rTD1.f.sec); // make sure you have the same sec
    27.   pTm->f.hour=rTD0.f.hour;
    28.   pTm->f.min=rTD0.f.min;
    29.   pTm->f.sec=rTD0.f.sec;  // update user's data
    31. }
    Last edited: Dec 4, 2015
  2. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    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.
  3. Papabravo


    Feb 24, 2006
    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.