This article shares the source code which I have written to set and get the current time using the Real Time Clock and Calendar (RTCC) module on the PIC24. It is tested with the PIC24FJ64GA002 but will work with other similar PICs with little modification. I decided to post it here as I found very little information on this on the Internet.
First, before you get too excited and think you will no longer need an external RTC module such as the DS1307, take note that unlike the DS1307, there is no time keeping battery for the RTCC module – it shares power with the PIC. So to keep it running for extended periods, you will probably need to put the PIC into standby when not in use to save power while still keeping the RTCC running.
The following code will enable the secondary oscillator for the RTCC module:
__builtin_write_OSCCONL(OSCCON | 0x02);
The following function will write the specified date and time value to the RTCC module:
void setRTCTime(unsigned char year, unsigned char month, unsigned char day,
unsigned char weekday, unsigned char hour, unsigned char minute, unsigned char second)
{
NVMKEY = 0x55;
NVMKEY = 0xAA;
RCFGCALbits.RTCWREN = 1;
RCFGCALbits.RTCEN = 0;
RCFGCALbits.RTCPTR = 3; RTCVAL = bin2bcd(year); RTCVAL = (bin2bcd(month) << 8) + bin2bcd(day); RTCVAL = (bin2bcd(weekday) << 8) + bin2bcd(hour); RTCVAL = (bin2bcd(minute) << 8) + bin2bcd(second);
RCFGCALbits.RTCEN = 1;
RCFGCALbits.RTCWREN = 0;
}
The following code will get the current RTCC time:
while(RCFGCALbits.RTCSYNC==1);
RCFGCALbits.RTCPTR=3;
year = bcd2bin(RTCVAL);
unsigned int month_date=RTCVAL;
month = bcd2bin(month_date >> 8);
day = bcd2bin(month_date & 0xFF);
unsigned int wday_hour=RTCVAL;
weekday = bcd2bin(wday_hour >> 8);
hour = bcd2bin(wday_hour & 0xFF);
unsigned int min_sec=RTCVAL;
minute = bcd2bin(min_sec >> 8);
second = bcd2bin(min_sec & 0xFF);
The date and time values are stored internally as binary coded decimals (BCD). I have written functions bcd2bin
and bin2bcd
to assist in the conversion of the values. The completed source code, with the BCD conversion functions, can be downloaded here.