Electronic – PIC18F66K22 – RTCC with external crystal

pic

Project details: (though I'm more interested in theoretical answers)

  • MCU: PIC18F66K22
  • IDE: MPLAB X IDE
  • Programmer: PICKit 3
  • Compiler: C18
  • Project: Real Time Calendar Clock

I've started on getting the intern RTCC from my PIC to work.
I find that there are little tutorials on this subject and none who are theoretical or withouth all too many (microcontroller specific) technical details.
The datasheet, which is specific for my microcontroller, on the other hand, seems to lack information (not sure if it's common knowledge I'm missing?)

How do I set up the Clock/external Crystal and RTCC?

  • Do I have to set TRIS (Tristate/Datadirection) registers for the Crystal pins?
  • Do I have to set the input of the Crystal high for it in order to start oscillating? (or a short pulse or something?)
  • Is the part below the right configuration?

    #pragma config RTCOSC = SOSCREF // RTCC Clock Select (RTCC uses SOSC)

  • RTSECSEL (RTCC Seconds Clock Output Select) should be set to (10)?: RTCC source clock is selected for the RTCC pin (the pin can be LF-INTOSC or SOSC, depending
    on the RTCOSC (CONFIG3L<1>) bit setting)
    ? I have another peripheral connected to the RTCC pin, will this become an output for the Clock Seconds? (Somehow confusing, but I think the Clock Output Select can be used to select the input? :#)


For those who don't like theory

The test code

Maybe a quick error is spotted here. The assembly thing is from the datasheet.

(datasheet: "For the RTCWREN bit to be set, there is only one
instruction cycle time window allowed between the 55h/AA sequence and
the setting of RTCWREN.")

Also #pragma config RTCOSC = SOSCREF // RTCC Clock Select (RTCC uses SOSC)
And TRISC registers are set.

_asm
movlw 0x55
movwf EECON2,0
movlw 0xAA
movwf EECON2,0
bsf RTCCFG,5,0
_endasm

RtccInitClock();
PADCFG1bits.RTSECSEL1 = 1;
PADCFG1bits.RTSECSEL0 = 0;
RTCCFGbits.RTCEN = 1;
RTCCFGbits.RTCWREN = 0;

Best Answer

My RTCC is now running! By some proper debugging I found out that RTCWREN wasn't being set, thus I could never set RTCEN. It seems that the unlocking sequence for the RTC write had to have an interrupt disable. The assembly code from the datasheet wasn't directly useable in my code nor was this described in the datasheet, but some trial and error solved that aswell. I did not have to set LAT/input/output for the osc pins.

But mind you should set the right configuration bits (#pragma config RTCOSC = SOSCREF // RTCC Clock Select (RTCC uses SOSC)

void rtccEnable(){
    rtccUnlock();//RTCWREN can only be written with the unlock sequence (see Example 18-1).

    RTCCFGbits.RTCEN = 1;

    rtccLock();
}

void rtccUnlock(){//RTCWREN can only be written with the unlock sequence (see Example 18-1).
//Refer to Section 18.0 “Real-Time Clock and Calendar (RTCC)” for the unlock sequence (Example 18-1).
    _asm
    BCF INTCON, 7,1
    MOVLW 0x55
    MOVWF EECON2,1
    MOVLW 0xAA
    MOVWF EECON2,1
    bsf RTCCFG,5,1
    _endasm
}
void rtccLock(){
    RTCCFGbits.RTCWREN = 0;
    INTCONbits.GIE = 1; //Re-enable interrupts.
}

void rtccDebug(){
    xportSendTextDebug("-----[RTCC]-----");
    if(RTCCFGbits.RTCEN){
        while((BOOL)RTCCFGbits.RTCSYNC){}
        xportSendTextDebug("[RTCC] is on");
    }else{
        xportSendTextDebug("[RTCC] is off!");
    }
}