Click here to Skip to main content
15,888,351 members
Articles / clock

Using the Real Time Clock and Calendar (RTCC) module on a PIC24

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
8 May 2023CPOL1 min read 4.1K  
Code to set and get current time using the RTCC module on PIC24

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:

C++
__builtin_write_OSCCONL(OSCCON | 0x02);

The following function will write the specified date and time value to the RTCC module:

C++
void setRTCTime(unsigned char year, unsigned char month, unsigned char day, 
unsigned char weekday, unsigned char hour, unsigned char minute, unsigned char second)
{
    // Enable RTCC Timer Access

    /*
        NVMKEY is a write only register that is used to prevent 
        accidental writes/erasures of Flash or EEPROM memory. 
        To start a programming or an erase sequence, the following steps must be
        taken in the exact order shown:
        1. Write 0x55 to NVMKEY.
        2. Write 0xAA to NVMKEY.
    */
    NVMKEY = 0x55;
    NVMKEY = 0xAA;
    RCFGCALbits.RTCWREN = 1;

    // Disable RTCC module
    RCFGCALbits.RTCEN = 0;

    // Write to RTCC Timer
    RCFGCALbits.RTCPTR = 3;             // RTCC Value Register Window Pointer bits
    RTCVAL = bin2bcd(year);             // Set Year (#0x00YY)
    RTCVAL = (bin2bcd(month) << 8) + bin2bcd(day);    // Set Month and Day (#0xMMDD)
    RTCVAL = (bin2bcd(weekday) << 8) + bin2bcd(hour); // Set Weekday and Hour (#0x0WHH).
                                                      // Weekday from 0 to 6  
    RTCVAL = (bin2bcd(minute) << 8) + bin2bcd(second);// Set Minute and Second (#0xMMSS)

    // Enable RTCC module
    RCFGCALbits.RTCEN = 1;

    // Disable RTCC Timer Access
    RCFGCALbits.RTCWREN = 0;
}

The following code will get the current RTCC time:

C++
// Wait for RTCSYNC bit to become ‘0’
while(RCFGCALbits.RTCSYNC==1);

// Read RTCC timekeeping register
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Writer
Singapore Singapore
Since 2008, ToughDev has been publishing technical sharing articles on a wide range of topics from software development to electronics design. Our interests include, but are not limited to, Android/iOS programming, VoIP products, embedded design using Arduino/PIC microcontrollers, reverse-engineering, retro-computing, and many others. We also perform product reviews, both on new and vintage products, and share our findings with the community. In addition, our team also develops customized software/hardware solutions highly adapted to suit your needs. Contact us for more information.

Comments and Discussions

 
-- There are no messages in this forum --