1 /*********************************************************************
  2     FileName:           RTCC.c
  3     Dependencies:       See #includes
  4     Processor:          PIC32MZ
  5     Hardware:           MainBrain MZ
  6     Complier:           XC32 4.40
  7     Author:             Larry Knight 2023
  8 *********************************************************************
  9  
 10     Software License Agreement:
 11  
 12     Licensed under the Apache License, Version 2.0 (the "License");
 13     you may not use this file except in compliance with the License.
 14     You may obtain a copy of the License at
 15 
 16     http://www.apache.org/licenses/LICENSE-2.0
 17 
 18     Unless required by applicable law or agreed to in writing, software
 19     distributed under the License is distributed on an "AS IS" BASIS,
 20     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 21     See the License for the specific language governing permissions and
 22     limitations under the License.
 23  
 24     Description:
 25         System Clock = 200 - 250 MHz
 26 
 27     File Description:
 28 
 29     Change History:
 30  
 31 ***********************************************************************/
 32 
 33 #include "xc.h"
 34 #include "MainBrain.h"
 35 
 36 void RTCC_init(void)
 37 {
 38     RTCCONbits.ON = 0;
 39     
 40     IPC41bits.RTCCIP = 4;
 41     IPC41bits.RTCCIS = 3;
 42     
 43     IFS5bits.RTCCIF = 0;
 44     IEC5bits.RTCCIE = 0;
 45     
 46     //Clock Select
 47     RTCCONbits.RTCCLKSEL = 1;
 48     
 49     OSCCONbits.SOSCEN = 1;
 50     
 51     //RTC write enable
 52     RTCCONbits.RTCWREN = 1;
 53     
 54     //Setup alarm for interrupt
 55     RTCALRMbits.ARPT = 0xff;
 56     RTCALRMbits.CHIME = 1;
 57     RTCALRMbits.AMASK = 1;      //4 = every 10 minutes
 58     RTCALRMbits.ALRMEN = 1;
 59     
 60     //calibration - clocks per minute +/-
 61     RTCCONbits.CAL = 10;
 62     
 63     //Set the time
 64     //Default is zeroed
 65     RTCDATEbits.YEAR10 = 2;
 66     RTCDATEbits.YEAR01 = 4;
 67     RTCDATEbits.MONTH10 = 0;
 68     RTCDATEbits.MONTH01 = 0; 
 69     RTCDATEbits.DAY10 = 0;
 70     RTCDATEbits.DAY01 = 0;
 71     RTCTIMEbits.HR10 = 0;
 72     RTCTIMEbits.HR01 = 0;
 73     RTCTIMEbits.MIN10 = 0;
 74     RTCTIMEbits.MIN01 = 0;
 75     RTCTIMEbits.SEC10 = 0;
 76     RTCTIMEbits.SEC01 = 0; 
 77     
 78     //Set actual time here
 79 //    RTCDATEbits.YEAR10 = 2;
 80 //    RTCDATEbits.YEAR01 = 4;
 81 //    RTCDATEbits.MONTH10 = 1;
 82 //    RTCDATEbits.MONTH01 = 2; 
 83 //    RTCDATEbits.DAY10 = 2;
 84 //    RTCDATEbits.DAY01 = 4;
 85 //    RTCTIMEbits.HR10 = 0;
 86 //    RTCTIMEbits.HR01 = 5;
 87 //    RTCTIMEbits.MIN10 = 1;
 88 //    RTCTIMEbits.MIN01 = 9;
 89 //    RTCTIMEbits.SEC10 = 0;
 90 //    RTCTIMEbits.SEC01 = 0;    
 91 
 92     //set alarm 
 93     ALRMTIMEbits.HR10 = 0;
 94     ALRMTIMEbits.HR01 = 0;
 95     ALRMTIMEbits.MIN10 = 0;
 96     ALRMTIMEbits.MIN01 = 0;
 97     ALRMTIMEbits.SEC10 = 0;
 98     ALRMTIMEbits.SEC01 = 0;
 99     
100     RTCCONbits.ON = 1;
101     
102     //lock the registers
103     RTCCONbits.RTCWREN = 0;    
104     
105     IEC5bits.RTCCIE = 1;
106 }
107 
108 //Real Time Clock
109 void __attribute__((vector(_RTCC_VECTOR), interrupt(ipl4srs), nomips16)) RTCC_Handler()
110 {
111     RTC_delay_counter++;
112     
113     //Turns ON LED0
114     PORTFbits.RF3 = 1;
115     
116     //Starts timer 6 which controls the time LED0 is ON
117     TMR6= 0;
118     T6CONbits.ON = 1; 
119 
120     IFS5bits.RTCCIF = 0;
121 }