help on RTCC

Thread Starter

oneblock

Joined May 5, 2010
1
hi BMorse,
i was trying to work on RTCC interrupt.
im a new in this, so i dont know how RTCC interrupts work.
if i want to call the RTCC interrupt service routine periodically, do you know how can i set up the alarmtime and other instantiation? or if there is any other way to do it using RTCC? it would be great if u can answer this question.
 

BMorse

Joined Sep 26, 2009
2,675
hi BMorse,
i was trying to work on RTCC interrupt.
im a new in this, so i dont know how RTCC interrupts work.
if i want to call the RTCC interrupt service routine periodically, do you know how can i set up the alarmtime and other instantiation? or if there is any other way to do it using RTCC? it would be great if u can answer this question.
Hello,
The RTCC ISR (Interrupt Service Routine) does not need polling, (i.e. you do not need to call it, it will trigger itself when ready)

To set up the interrupt:
Rich (BB code):
Init_Interrupts()
{
    mRTCCSetIntPriority(1);                //Set Real Time Clock And Calendar Priority Level
    mRTCCClearIntFlag();                //Clear RTCC Interrupt Flag
    INTEnableSystemMultiVectoredInt();            //Enable Multi vectored Interrupts
    
}//End Init Interrupts
Once you have it set up, this should be your ISR for the RTCC:

Rich (BB code):
void __ISR( _RTCC_VECTOR,ipl1) RTCCInterrupt( void)
{
    //Your ISR code goes here, try not to put too much code here so uc does not miss any other interrupts
    RtccAlarmDisable();       //Alarm Interrupt occured so disable it
    mRTCCClearIntFlag();    //Clear Interrupt Flag
    
}//RTCC Interrupt
and here are some other useful functions:
Rich (BB code):
//**********************************RTCC FUNCTIONS*************************************
int InitRTCC()
{
    SYSTEMConfigPerformance(72000000L);
    RtccInit();            // init the RTCC
    while(RtccGetClkStat()!=RTCC_CLK_ON);    // wait for the SOSC to be 
    //actually running and RTCC to have its clock source, could wait here at most 32ms
    // let's set the current date
    {
        tm.l=0;            //Why do clocks always start at 12:00??
        tm.sec=0x00;    //Set the Seconds
        tm.min=0x05;    //set the Minutes
        tm.hour=0x14;    //Set the Hour

        dt.wday=0x02;    //Set Weekday to Sat
        dt.mday=0x31;    //on the 31st
        dt.mon=0x03;    //of March
        dt.year=0x09;    //of 2009.
        RtccSetTimeDate(tm.l, dt.l);    //Set time and date into registers....
    }
    //mRtccSelectSecPulseOutput();        // select the seconds clock pulse as the function of the RTCC output pin
    //mRtccSelectAlarmPulseOutput();    // select the alarm pulse as the function of the RTCC output pin
    //mRtccOutputEnable();                // enable the Output pin of the RTCC
    return 1;
}//End InitRTCC
//***********************************************************************************
int OpenRTCC()
{
    RtccOpen(tm.l, dt.l, 0);            // set time, date and calibration in a single operation
    //check to see if the RTCC is running: check the SYNC bit
    while(mRtccGetSync());                // wait sync to be low
    while(!mRtccGetSync());                // wait to be high
    while(mRtccGetSync());                // wait sync to be low again
    // other RTCC operations
    // adjust the RTCC timing
    RtccSetCalibration(200);            // value to calibrate with at each minute
    // enabling the RTCC output pin
    //mRtccSelectSecPulseOutput();        // select the seconds clock pulse as the function of the RTCC output pin
    mRtccSelectAlarmPulseOutput();        // select the alarm pulse as the function of the RTCC output pin
    mRtccOutputEnable();                // enable the Output pin of the RTCC
    //Enable Interrupts
    mRtccEnableInt();
    return 1;
}

/*    the following will update the RTCC calibration value.*/
void CalibrateRTCC(void)
{
    int cal=0x3FD;            //10 Bits adjustment, -3 in value
    if(RTCCON&0x8000)        //if RTCC is On
    {
        unsigned int t0,t1;
        do
        {
            t0=RTCTIME;
            t1=RTCTIME;
        }while(t0!=t1);//End Do        //Read valid time value
        if((t0&0xFF)==00)
        {                            //were at 2nd 00, wait for auto-adjust to be performed
            while(!(RTCCON&0x2));    //wait until second half
        }//end if
    }//End If
}
//******************************END RTCC FUNCTIONS************************************
And here is an example on how to set the alarm to trigger the interrupt (this is just as axample to give you an idea, if compiled it will not work unless you declare all variables and you have a copy of the f_HEX and c_HEX functions):

Rich (BB code):
//***********************************************************************************
int AlarmRTCC(int AlrmMinutes)    //To set an alram for the RTCC
{//Make sure we set the correct amount of time
//check minutes to be set see if they will go over an hour!!
    int ms;
    int hs;
    mRtccDisableInt();
    RtccAlarmDisable();
    tAlrm.l=tm.l;
    ms=tAlrm.min;
//--------------------------------------------------------------------
    ms=f_HEX(ms);                    //convert to decimal
    ms=ms+AlrmMinutes;                //add together
    ms=c_HEX(ms);                    //covert back to BCD coded hex
    if (ms>89)                        //have to make sure we have not rolled over on the minutes
    {
        hs=f_HEX(tAlrm.hour);
        hs=hs+1;
        tAlrm.hour=c_HEX(hs);        //if we have, increment hour by 1 and set minutes for remainder.
        ms=c_HEX(ms=(f_HEX(ms)-59));//
        tAlrm.min=ms;                //store minutes value
    }
    else
    {
        tAlrm.min=ms;            //store minutes value
    };
//--------------------------------------------------------------------
    RtccChimeEnable();                //Enable Chime, rollover allowed.
    RtccSetAlarmRptCount(0);        //1 Alarm
    RtccSetAlarmRpt(RTCC_RPT_HOUR);    //repeat every hour?? Seems to work better than every minute!!
    RtccSetAlarmTime(tAlrm.l);        //set alarm time
    mRtccEnableInt();
    RtccAlarmEnable();
//--------------------------------------------------------------------
    
}
//***********************************************************************************
 
Last edited:

BMorse

Joined Sep 26, 2009
2,675
Correct me if I'm wrong but I'm not seeing where you are unlocking your RTC to set the time and alarm?

Under the Init_RTCC function is where the time and date is set, under the AlarmRTCC function is where the Alarm and interrupt is set.


B. Morse
 

maxpower097

Joined Feb 20, 2009
816
Ok is this an external RTC? I ask because when I was working with the PIC32 onboard RTC we had to write 2 instructions in ASM then the following 3rd instruction would actually set the clock. Otherwise you couldn't unlock the RTC without the 2 ASM instructions. Am I missing this in the code somewhere? Or is this an external RTC? Or am I just plain wrong? :)
 

BMorse

Joined Sep 26, 2009
2,675
Ok is this an external RTC? I ask because when I was working with the PIC32 onboard RTC we had to write 2 instructions in ASM then the following 3rd instruction would actually set the clock. Otherwise you couldn't unlock the RTC without the 2 ASM instructions. Am I missing this in the code somewhere? Or is this an external RTC? Or am I just plain wrong? :)

This is the internal RTCC of the Pic32MX uc, this code is all based on the RTCC library by Microchip.

I have used this code plenty of times and I don't recall having to write any ASM instructions to get it running....

You Initialize the internal RTCC this way (note this is a setup for 72Mhz not 80):
Rich (BB code):
int InitRTCC()
{
    SYSTEMConfigPerformance(72000000L);
    RtccInit();            // init the RTCC
    while(RtccGetClkStat()!=RTCC_CLK_ON);    // wait for the SOSC to be 
    //actually running and RTCC to have its clock source, could wait here at most 32ms
    // let's set the current date
    {
        tm.l=0;            //Why do clocks always start at 12:00??
        tm.sec=0x00;    //Set the Seconds
        tm.min=0x05;    //set the Minutes
        tm.hour=0x14;    //Set the Hour

        dt.wday=0x02;    //Set Weekday to Sat
        dt.mday=0x31;    //on the 31st
        dt.mon=0x03;    //of March
        dt.year=0x09;    //of 2009.
        RtccSetTimeDate(tm.l, dt.l);    //Set time and date into registers....
    }
    //mRtccSelectSecPulseOutput();        // select the seconds clock pulse as the function of the RTCC output pin
    //mRtccSelectAlarmPulseOutput();    // select the alarm pulse as the function of the RTCC output pin
    //mRtccOutputEnable();                // enable the Output pin of the RTCC
    return 1;
}//End InitRTCC
You open it like this:
Rich (BB code):
int OpenRTCC()
{
    RtccOpen(tm.l, dt.l, 0);            // set time, date and calibration in a single operation
    //check to see if the RTCC is running: check the SYNC bit
    while(mRtccGetSync());                // wait sync to be low
    while(!mRtccGetSync());                // wait to be high
    while(mRtccGetSync());                // wait sync to be low again
    // other RTCC operations
    // adjust the RTCC timing
    RtccSetCalibration(200);            // value to calibrate with at each minute
    // enabling the RTCC output pin
    //mRtccSelectSecPulseOutput();        // select the seconds clock pulse as the function of the RTCC output pin
    mRtccSelectAlarmPulseOutput();        // select the alarm pulse as the function of the RTCC output pin
    mRtccOutputEnable();                // enable the Output pin of the RTCC
    //Enable Interrupts
    mRtccEnableInt();
    return 1;
}
this function is used for calibration:
Rich (BB code):
void CalibrateRTCC(void)
{
    int cal=0x3FD;            //10 Bits adjustment, -3 in value
    if(RTCCON&0x8000)        //if RTCC is On
    {
        unsigned int t0,t1;
        do
        {
            t0=RTCTIME;
            t1=RTCTIME;
        }while(t0!=t1);//End Do        //Read valid time value
        if((t0&0xFF)==00)
        {                            //were at 2nd 00, wait for auto-adjust to be performed
            while(!(RTCCON&0x2));    //wait until second half
        }//end if
    }//End If
}
this is a function I wrote to set the alarm interrupt for a given amount of minutes, (This function will not function properly without the other functions for converting to BCD coded HEX and vise versa....)

Rich (BB code):
int AlarmRTCC(int AlrmMinutes)    //To set an alram for the RTCC
{//Make sure we set the correct amount of time
//check minutes to be set see if they will go over an hour!!
    int ms;
    int hs;
    mRtccDisableInt();
    RtccAlarmDisable();
    tAlrm.l=tm.l;
    ms=tAlrm.min;
//--------------------------------------------------------------------
    ms=f_HEX(ms);                    //convert to decimal
    ms=ms+AlrmMinutes;                //add together
    ms=c_HEX(ms);                    //covert back to BCD coded hex
    if (ms>89)                        //have to make sure we have not rolled over on the minutes
    {
        hs=f_HEX(tAlrm.hour);
        hs=hs+1;
        tAlrm.hour=c_HEX(hs);        //if we have, increment hour by 1 and set minutes for remainder.
        ms=c_HEX(ms=(f_HEX(ms)-59));//
        tAlrm.min=ms;                //store minutes value
    }
    else
    {
        tAlrm.min=ms;            //store minutes value
    };
//--------------------------------------------------------------------
    RtccChimeEnable();                //Enable Chime, rollover allowed.
    RtccSetAlarmRptCount(0);        //1 Alarm
    RtccSetAlarmRpt(RTCC_RPT_HOUR);    //repeat every hour?? Seems to work better than every minute!!
    RtccSetAlarmTime(tAlrm.l);        //set alarm time
    mRtccEnableInt();
    RtccAlarmEnable();
//--------------------------------------------------------------------
    
}

This is part of the same code I used and submitted during the MyPic32 contest that Microchip sponsored (I took 3rd in the world) check it out at www.MyPic32.com, my entry was the B.U.M. System...

B. Morse
 
Last edited:

maxpower097

Joined Feb 20, 2009
816
Ok I did some digging and it seems its only on the PIC24. They are so close I thought it was was the same for the PIC32 but I guess not. I went throught the datasheet and didn't see anything about it. Then I went through the PIC24 ds and it was there in the RTCC section.

So did you ever get any cash or cool stuff from the competition? I heard lots of people got full retail compilers, and such. I thought about trying to do something simple with it but was a lil late in my timing.
 
Last edited:

BMorse

Joined Sep 26, 2009
2,675
Ok I did some digging and it seems its only on the PIC24. They are so close I thought it was was the same for the PIC32 but I guess not. I went throught the datasheet and didn't see anything about it. Then I went through the PIC24 ds and it was there in the RTCC section.

So did you ever get any cash or cool stuff from the competition? I heard lots of people got full retail compilers, and such. I thought about trying to do something simple with it but was a lil late in my timing.

Yes, I did "win" a lot of cool stuff, a bunch of different Pic32 based development boards, Hi-Tech C Compiler Pro for the Pic32, full version of Microchips C Compiler and other stuff like fully licensed RTOS etc. (around $7500.00 worth of stuff! Too bad I got a 1099 from them and had to pay taxes!!:eek:) Plus they flew me out to San Jose for 3 days for the Embeded Systems Conference and I got to mingle and chit chat with some top professionals from Microchip, Digikey, & Techinsights, plus others... It was definitely an experience I will not soon forget.

B. Morse
 
Top