I want to initialize the internal RTC of a PIC18F67K22 with some fix time and date. Time is set properly, but date is not set properly. The problem occurs in setting the year. year is not set to whatever we give to RTC.

Find attach below RTC code, which I write to initialize internal RTC.

 RtccInitClock();                                       //Intialize the internal RTC
 RtccWrOn();                                                //Write enable for RTC                              //enable RTCC output on RTCC output pin
 PIE3bits.RTCCIE=1;                                     //Enables the alarm interrupt from RTC

       RtccTimeDate.f.hour = 2;                             //Set Hour
       RtccTimeDate.f.min =  3;                             //Set minute
       RtccTimeDate.f.sec =  4;                             //Set second
       RtccTimeDate.f.mday = 05;                            //Set day
       RtccTimeDate.f.mon =  06;                            //Se month
       RtccTimeDate.f.year = 14;                            //set year

      RtccWriteTimeDate(&RtccTimeDate,1);                       //Writes the date and time read from the external RTC
      RtccSetAlarmRpt(RTCC_RPT_SEC,1);                      //Set the alarm repeat to every minute
      ALRMCFGbits.CHIME = 1;                                    //Alarm repeat infinite times
      RtccWriteAlrmTimeDate(&RtccTimeDate);                     //Writes Alarm date and Time
      mRtccOn();                                                //Enables the RTC

Is there any setting I may have missed?


According to some RTCC documentation that I found for that library both the date and time values should be specified in binary-coded decimal. Read up on BCD encoding so that you understand how it works but for the moment to confirm that's the problem try the following code:

RtccTimeDate.f.hour = 0x02;
RtccTimeDate.f.min =  0x03;
RtccTimeDate.f.sec =  0x04;
RtccTimeDate.f.mday = 0x05;
RtccTimeDate.f.mon =  0x06;
RtccTimeDate.f.year = 0x14;

While I have changed all the values for clarity it's only the year that actually changes for your example value. The value 14 in decimal is 0x0E in hex which isn't a valid BCD value. You could use something like the following (untested) macro to covert to BCD:

#define TO_BCD(x) ((((x) / 10) << 4) + ((x) % 10))
RtccTimeDate.f.year = TO_BCD(14);
  • \$\begingroup\$ Thanks peter J. You are right. RTC accept hex value and year is convert to BCD form . \$\endgroup\$ – shail korat Aug 13 '13 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.