Aguijón Software Libraries  1.0
Documentation for the included Libraries
 All Files Functions Variables Typedefs Enumerations Enumerator Macros
realtime.c
Go to the documentation of this file.
1 /********************************************************************
2  FileName: realtime.c
3  Dependencies: See INCLUDES section
4  Hardware: Aguijón rev3.0
5  Complier: Microchip XC16(for PIC24), C30(for PIC24)
6  Company: Vinagrón Digital
7 
8  Software License Agreement:
9 
10  blah blah blah
11 
12 ********************************************************************
13  File Description:
14  Includes realtime clock functions.
15 
16 
17  Change History:
18  Rev Description
19  ---- -----------------------------------------
20  1.0 Initial release
21 ********************************************************************/
22 
23 #include <p24FJ128GB106.h>
24 #include "realtime.h"
25 #include "definitions.h"
26 
27 
39 void RTCCUnlock(void){
40  __builtin_write_RTCWEN(1); //set the RTCWREN bit
41  asm volatile("bset _RCFGCAL, #13");
42  __builtin_write_RTCWEN(0); //set the RTCWREN bit
43 }
44 
56 void RTCCSet(void)
57 {
58  IEC3bits.RTCIE = 1; //enable RTCC interrupt
59  IPC15bits.RTCIP = 4; //priority #5
60  //***********************************************************
61  // STEP 1:
62  // Unlock RTCC Registers
63  //***********************************************************/
64  mRTCCUnlock(); // ### use predefined unlock macro in realtime.h
65 
66  //***********************************************************
67  // STEP 2:
68  // Configure RTCPTR Autodecrementing pointer in RCFGCAL register
69  // to point the "-- : Year" address
70  //***********************************************************/
71  RCFGCALbits.RTCPTR = 3; // ### setup RTCPTR pointer to max value
72 
73  //***********************************************************
74  // STEP 3:
75  // Write Year, Month & Day, Weekday & Hours,
76  // Minutes & Seconds To RTCVAL in BCD format
77  //***********************************************************/
78  RTCVAL = 0x0000; // ### unused : Year
79  RTCVAL = 0x0000; // ### Month : Day
80  RTCVAL = 0x0000; // ### Weekday : Hours
81  RTCVAL = 0x0000; // ### Minutes : Seconds
82 
83  //***********************************************************
84  // STEP 4:
85  // In RCFGCAL, enable RTCC
86  //***********************************************************/
87  RCFGCALbits.RTCEN = 1; // ### enable RTCC
88 
89  //***********************************************************
90  // STEP 5:
91  // Lock RTCC Registers
92  //***********************************************************/
93  mRTCCLock(); // ### use predefined lock macro in rtcc.h
94 }
95 
107 void AlarmSet(void)
108 {
109  // UnLock Alarm Registers
110  mRTCCUnlock();
111 
112  // Configure ALCFGRPT, ALRMPTR Auto-decrementing pointer
113  ALCFGRPTbits.ALRMPTR = 0;
114  ALCFGRPTbits.CHIME = 1;
115 
116  // Write Alarm Month & Day To ALRMVAL
117  ALRMVAL = 0x0000;
118 
119  // Write Alarm Week Day & Hour To ALRMVAL
120  ALRMVAL = 0x0000;
121 
122  // Write Alarm Minute & Second To ALRMVAL
123  ALRMVAL = 0x0000;
124 
125  //***********************************************************
126  // STEP 6:
127  // In ALCFGRPT, configure alarm frequency @ 1Hz (every 1s)
128  // In ALCFGRPT, configure alarm to repeat x times
129  //***********************************************************/
130  ALCFGRPTbits.AMASK = 1; // ### setup for an alarm every 1s
131  ALCFGRPTbits.ARPT = 0xFF; // ### repeat alarm n times
132  //***********************************************************
133  // STEP 7:
134  // In ALCFGRPT, enable Alarm
135  //***********************************************************/
136  ALCFGRPTbits.ALRMEN = 1; // ### enable alarm
137 
138 
139  // Lock Alarm Registers
140  mRTCCLock(); // Lock the RTCC
141 }
142 
154 void initRTCC(void)
155 {
156  // Enables the LP OSC for RTCC operation
157  __builtin_write_OSCCONL(0x02); //Ejecuta la secuencia de unlock y escibe un 0x02 al registro OSCCON
158 
159  // Unlock sequence must take place for RTCEN to be written
160  RCFGCAL = 0x0000;
161 
162  AlarmSet();
163 
164  mRTCCSet();
165 }