1.2 RTCC Module Configuration
When configuring the RTCC module, the user must first set the RTCWREN bit of the RTCCON register to disable the register write lockout function and allow RTCC registers to be updated. After setting the RTCWREN bit, the user should then disable to RTCC module, which will prevent the RTCC from incrementing the time and date registers while they are being configured. If the RTCC module were left enabled using the current time and date information while configuring, there is the possibility of an event in which one register is being written and clocked at the same time. The next step when configuring the RTCC module for the first time is to set the current time and date information by writing into the associated registers. This creates a known starting condition for the RTCC module from where it can begin to increment the date and time. When initializing the current time and date settings, the user must write the year, month, day, minutes and seconds information, each into their own associated register in memory. This information written to the time value registers must be implemented using the Binary Coded Decimal (BCD) format that is explained in Section Reading/Writing RTCC Using Binary Coded Decimal (BCD) Format.
When the RTCC registers are not being updated, the RTCWREN bit should remain cleared to enable the Write Lock function and avoid accidental writes to the RTCC registers during operation. As previously mentioned, each of the time value counters will increment based upon their defined time intervals. This increment leaves a large enough period between clocks to safely update the register without disabling the RTCC Write Lock. The user can also poll the RTCSYNC and the HALFSEC bits of the RTCCON register to observe the timing status and avoid a register getting updated and incremented simultaneously.
After the RTCC module and alarm settings have been initialized, the user can set the RTCEN bit of the RTCCON register. When enabling the RTCC module for the first time, it is recommended that the user polls the RTCEN bit to ensure that it reaches to desired logic level before the RTCWREN bit is cleared. The RTCEN bit is synchronized to the RTCC clock and will not set until the external oscillator is available, which means that the user must account for the crystal/oscillator start-up time in the initialization routine. Example 1-1 below demonstrates the first part of initializing the RTCC module, by disabling the RTCC write lock and initializing the time and date registers. Example 2-1 in Section RTCC Alarm Configuration contains the second half of the RTCC initialization routine, which includes configuring the alarm feature.
RTCC Module Initialization (Part 1)
RTCCONbits.RTCWREN = 1; // RTCC Registers can be written;
RTCCONbits.RTCEN = 0; // RTCC Module is disabled;
RTCCONbits.RTCCLKSEL = 0x00; // RTCC Clock Selection Bits - SOSC;
// Set RTCC Start Time:
YEAR = 0x18; // year;
MONTH = 0x5; // month;
WEEKDAY = 0x2; // weekday;
DAY = 0x22; // day;
HOURS = 0x12; // hours
MINUTES = 0x30; // minutes;
SECONDS = 0x0; // seconds;
RTCCAL = 0x00; // RTCC Calibration;