1. oneblock

    Thread Starter New Member

    May 5, 2010
    1
    0
    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.
     
  2. bertus

    Administrator

    Apr 5, 2008
    15,645
    2,344
  3. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    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:
    Code ( (Unknown Language)):
    1. Init_Interrupts()
    2. {
    3.     mRTCCSetIntPriority(1);                //Set Real Time Clock And Calendar Priority Level
    4.     mRTCCClearIntFlag();                //Clear RTCC Interrupt Flag
    5.     INTEnableSystemMultiVectoredInt();            //Enable Multi vectored Interrupts
    6.    
    7. }//End Init Interrupts
    Once you have it set up, this should be your ISR for the RTCC:

    Code ( (Unknown Language)):
    1. void __ISR( _RTCC_VECTOR,ipl1) RTCCInterrupt( void)
    2. {
    3.     //Your ISR code goes here, try not to put too much code here so uc does not miss any other interrupts
    4.     RtccAlarmDisable();       //Alarm Interrupt occured so disable it
    5.     mRTCCClearIntFlag();    //Clear Interrupt Flag
    6.    
    7. }//RTCC Interrupt
    and here are some other useful functions:
    Code ( (Unknown Language)):
    1. //**********************************RTCC FUNCTIONS*************************************
    2. int InitRTCC()
    3. {
    4.     SYSTEMConfigPerformance(72000000L);
    5.     RtccInit();            // init the RTCC
    6.     while(RtccGetClkStat()!=RTCC_CLK_ON);    // wait for the SOSC to be
    7.     //actually running and RTCC to have its clock source, could wait here at most 32ms
    8.     // let's set the current date
    9.     {
    10.         tm.l=0;            //Why do clocks always start at 12:00??
    11.         tm.sec=0x00;    //Set the Seconds
    12.         tm.min=0x05;    //set the Minutes
    13.         tm.hour=0x14;    //Set the Hour
    14.  
    15.         dt.wday=0x02;    //Set Weekday to Sat
    16.         dt.mday=0x31;    //on the 31st
    17.         dt.mon=0x03;    //of March
    18.         dt.year=0x09;    //of 2009.
    19.         RtccSetTimeDate(tm.l, dt.l);    //Set time and date into registers....
    20.     }
    21.     //mRtccSelectSecPulseOutput();        // select the seconds clock pulse as the function of the RTCC output pin
    22.     //mRtccSelectAlarmPulseOutput();    // select the alarm pulse as the function of the RTCC output pin
    23.     //mRtccOutputEnable();                // enable the Output pin of the RTCC
    24.     return 1;
    25. }//End InitRTCC
    26. //***********************************************************************************
    27. int OpenRTCC()
    28. {
    29.     RtccOpen(tm.l, dt.l, 0);            // set time, date and calibration in a single operation
    30.     //check to see if the RTCC is running: check the SYNC bit
    31.     while(mRtccGetSync());                // wait sync to be low
    32.     while(!mRtccGetSync());                // wait to be high
    33.     while(mRtccGetSync());                // wait sync to be low again
    34.     // other RTCC operations
    35.     // adjust the RTCC timing
    36.     RtccSetCalibration(200);            // value to calibrate with at each minute
    37.     // enabling the RTCC output pin
    38.     //mRtccSelectSecPulseOutput();        // select the seconds clock pulse as the function of the RTCC output pin
    39.     mRtccSelectAlarmPulseOutput();        // select the alarm pulse as the function of the RTCC output pin
    40.     mRtccOutputEnable();                // enable the Output pin of the RTCC
    41.     //Enable Interrupts
    42.     mRtccEnableInt();
    43.     return 1;
    44. }
    45.  
    46. /*    the following will update the RTCC calibration value.*/
    47. void CalibrateRTCC(void)
    48. {
    49.     int cal=0x3FD;            //10 Bits adjustment, -3 in value
    50.     if(RTCCON&0x8000)        //if RTCC is On
    51.     {
    52.         unsigned int t0,t1;
    53.         do
    54.         {
    55.             t0=RTCTIME;
    56.             t1=RTCTIME;
    57.         }while(t0!=t1);//End Do        //Read valid time value
    58.         if((t0&0xFF)==00)
    59.         {                            //were at 2nd 00, wait for auto-adjust to be performed
    60.             while(!(RTCCON&0x2));    //wait until second half
    61.         }//end if
    62.     }//End If
    63. }
    64. //******************************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):

    Code ( (Unknown Language)):
    1. //***********************************************************************************
    2. int AlarmRTCC(int AlrmMinutes)    //To set an alram for the RTCC
    3. {//Make sure we set the correct amount of time
    4. //check minutes to be set see if they will go over an hour!!
    5.     int ms;
    6.     int hs;
    7.     mRtccDisableInt();
    8.     RtccAlarmDisable();
    9.     tAlrm.l=tm.l;
    10.     ms=tAlrm.min;
    11. //--------------------------------------------------------------------
    12.     ms=f_HEX(ms);                    //convert to decimal
    13.     ms=ms+AlrmMinutes;                //add together
    14.     ms=c_HEX(ms);                    //covert back to BCD coded hex
    15.     if (ms>89)                        //have to make sure we have not rolled over on the minutes
    16.     {
    17.         hs=f_HEX(tAlrm.hour);
    18.         hs=hs+1;
    19.         tAlrm.hour=c_HEX(hs);        //if we have, increment hour by 1 and set minutes for remainder.
    20.         ms=c_HEX(ms=(f_HEX(ms)-59));//
    21.         tAlrm.min=ms;                //store minutes value
    22.     }
    23.     else
    24.     {
    25.         tAlrm.min=ms;            //store minutes value
    26.     };
    27. //--------------------------------------------------------------------
    28.     RtccChimeEnable();                //Enable Chime, rollover allowed.
    29.     RtccSetAlarmRptCount(0);        //1 Alarm
    30.     RtccSetAlarmRpt(RTCC_RPT_HOUR);    //repeat every hour?? Seems to work better than every minute!!
    31.     RtccSetAlarmTime(tAlrm.l);        //set alarm time
    32.     mRtccEnableInt();
    33.     RtccAlarmEnable();
    34. //--------------------------------------------------------------------
    35.    
    36. }
    37. //***********************************************************************************
     
    Last edited: May 7, 2010
  4. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    Correct me if I'm wrong but I'm not seeing where you are unlocking your RTC to set the time and alarm?
     
  5. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    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
     
  6. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    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? :)
     
  7. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    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):
    Code ( (Unknown Language)):
    1. int InitRTCC()
    2. {
    3.     SYSTEMConfigPerformance(72000000L);
    4.     RtccInit();            // init the RTCC
    5.     while(RtccGetClkStat()!=RTCC_CLK_ON);    // wait for the SOSC to be
    6.     //actually running and RTCC to have its clock source, could wait here at most 32ms
    7.     // let's set the current date
    8.     {
    9.         tm.l=0;            //Why do clocks always start at 12:00??
    10.         tm.sec=0x00;    //Set the Seconds
    11.         tm.min=0x05;    //set the Minutes
    12.         tm.hour=0x14;    //Set the Hour
    13.  
    14.         dt.wday=0x02;    //Set Weekday to Sat
    15.         dt.mday=0x31;    //on the 31st
    16.         dt.mon=0x03;    //of March
    17.         dt.year=0x09;    //of 2009.
    18.         RtccSetTimeDate(tm.l, dt.l);    //Set time and date into registers....
    19.     }
    20.     //mRtccSelectSecPulseOutput();        // select the seconds clock pulse as the function of the RTCC output pin
    21.     //mRtccSelectAlarmPulseOutput();    // select the alarm pulse as the function of the RTCC output pin
    22.     //mRtccOutputEnable();                // enable the Output pin of the RTCC
    23.     return 1;
    24. }//End InitRTCC
    25.  
    You open it like this:
    Code ( (Unknown Language)):
    1. int OpenRTCC()
    2. {
    3.     RtccOpen(tm.l, dt.l, 0);            // set time, date and calibration in a single operation
    4.     //check to see if the RTCC is running: check the SYNC bit
    5.     while(mRtccGetSync());                // wait sync to be low
    6.     while(!mRtccGetSync());                // wait to be high
    7.     while(mRtccGetSync());                // wait sync to be low again
    8.     // other RTCC operations
    9.     // adjust the RTCC timing
    10.     RtccSetCalibration(200);            // value to calibrate with at each minute
    11.     // enabling the RTCC output pin
    12.     //mRtccSelectSecPulseOutput();        // select the seconds clock pulse as the function of the RTCC output pin
    13.     mRtccSelectAlarmPulseOutput();        // select the alarm pulse as the function of the RTCC output pin
    14.     mRtccOutputEnable();                // enable the Output pin of the RTCC
    15.     //Enable Interrupts
    16.     mRtccEnableInt();
    17.     return 1;
    18. }
    this function is used for calibration:
    Code ( (Unknown Language)):
    1. void CalibrateRTCC(void)
    2. {
    3.     int cal=0x3FD;            //10 Bits adjustment, -3 in value
    4.     if(RTCCON&0x8000)        //if RTCC is On
    5.     {
    6.         unsigned int t0,t1;
    7.         do
    8.         {
    9.             t0=RTCTIME;
    10.             t1=RTCTIME;
    11.         }while(t0!=t1);//End Do        //Read valid time value
    12.         if((t0&0xFF)==00)
    13.         {                            //were at 2nd 00, wait for auto-adjust to be performed
    14.             while(!(RTCCON&0x2));    //wait until second half
    15.         }//end if
    16.     }//End If
    17. }
    18.  
    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....)

    Code ( (Unknown Language)):
    1. int AlarmRTCC(int AlrmMinutes)    //To set an alram for the RTCC
    2. {//Make sure we set the correct amount of time
    3. //check minutes to be set see if they will go over an hour!!
    4.     int ms;
    5.     int hs;
    6.     mRtccDisableInt();
    7.     RtccAlarmDisable();
    8.     tAlrm.l=tm.l;
    9.     ms=tAlrm.min;
    10. //--------------------------------------------------------------------
    11.     ms=f_HEX(ms);                    //convert to decimal
    12.     ms=ms+AlrmMinutes;                //add together
    13.     ms=c_HEX(ms);                    //covert back to BCD coded hex
    14.     if (ms>89)                        //have to make sure we have not rolled over on the minutes
    15.     {
    16.         hs=f_HEX(tAlrm.hour);
    17.         hs=hs+1;
    18.         tAlrm.hour=c_HEX(hs);        //if we have, increment hour by 1 and set minutes for remainder.
    19.         ms=c_HEX(ms=(f_HEX(ms)-59));//
    20.         tAlrm.min=ms;                //store minutes value
    21.     }
    22.     else
    23.     {
    24.         tAlrm.min=ms;            //store minutes value
    25.     };
    26. //--------------------------------------------------------------------
    27.     RtccChimeEnable();                //Enable Chime, rollover allowed.
    28.     RtccSetAlarmRptCount(0);        //1 Alarm
    29.     RtccSetAlarmRpt(RTCC_RPT_HOUR);    //repeat every hour?? Seems to work better than every minute!!
    30.     RtccSetAlarmTime(tAlrm.l);        //set alarm time
    31.     mRtccEnableInt();
    32.     RtccAlarmEnable();
    33. //--------------------------------------------------------------------
    34.    
    35. }
    36.  

    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: May 9, 2010
  8. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    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: May 10, 2010
  9. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    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
     
  10. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    way too cool!
     
Loading...