PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rtcc.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 #include "pic24_all.h"
31 #include <stdio.h>
32 
33 
34 /** \file
35 Program uses the Real Time Clock module and an external 32.768 kHZ crystal for timekeeping.
36 */
37 
38 typedef union _unionRTCC {
39  struct { //four 16 bit registers
40  uint8_t yr;
41  uint8_t null;
42  uint8_t date;
43  uint8_t month;
44  uint8_t hour;
45  uint8_t wday;
46  uint8_t sec;
47  uint8_t min;
48  } u8;
49  uint16_t regs[4];
50 } unionRTCC;
51 
52 unionRTCC u_RTCC;
53 
54 uint8_t getBCDvalue(char *sz_1) {
55  char sz_buff[8];
56  uint16_t u16_bin;
57  uint8_t u8_bcd;
58  outString(sz_1);
59  inStringEcho(sz_buff,7);
60  sscanf(sz_buff,"%d", (int *)&u16_bin);
61  u8_bcd = u16_bin/10; //most significant digit
62  u8_bcd = u8_bcd << 4;
63  u8_bcd = u8_bcd | (u16_bin%10);
64  return(u8_bcd);
65 }
66 
67 void getDateFromUser(void) {
68  u_RTCC.u8.yr = getBCDvalue("Enter year (0-99): ");
69  u_RTCC.u8.month = getBCDvalue("Enter month (1-12): ");
70  u_RTCC.u8.date = getBCDvalue("Enter day of month (1-31): ");
71  u_RTCC.u8.wday = getBCDvalue("Enter week day (0-6): ");
72  u_RTCC.u8.hour = getBCDvalue("Enter hour (0-23): ");
73  u_RTCC.u8.min = getBCDvalue("Enter min (0-59): ");
74  u_RTCC.u8.sec = getBCDvalue("Enter sec(0-59): ");
75 }
76 
77 //set date
78 void setRTCC(void) {
79  uint8_t u8_i;
80  __builtin_write_RTCWEN(); //enable write to RTCC, sets RTCWEN
81  RCFGCALbits.RTCEN = 0; //disable the RTCC
82  RCFGCALbits.RTCPTR = 3; //set pointer reg to start
83  for (u8_i=0; u8_i<4; u8_i++) RTCVAL = u_RTCC.regs[u8_i];
84  RCFGCALbits.RTCEN = 1; //Enable the RTCC
85  RCFGCALbits.RTCWREN = 0; //can clear without unlock
86 }
87 
88 void readRTCC(void) {
89  uint8_t u8_i;
90  RCFGCALbits.RTCPTR = 3; //set pointer reg to start
91  for (u8_i=0; u8_i<4; u8_i++) u_RTCC.regs[u8_i] = RTCVAL;
92 }
93 
94 void printRTCC(void) {
95  printf ("day(wday)/mon/yr: %2x(%2x)/%2x/%2x, %02x:%02x:%02x \n",
96  (uint16_t) u_RTCC.u8.date,(uint16_t) u_RTCC.u8.wday, (uint16_t) u_RTCC.u8.month,
97  (uint16_t) u_RTCC.u8.yr, (uint16_t) u_RTCC.u8.hour, (uint16_t) u_RTCC.u8.min, (uint16_t) u_RTCC.u8.sec);
98 }
99 
100 int main(void) {
101  __builtin_write_OSCCONL(OSCCON | 0x02); // OSCCON.SOSCEN=1;
102  configBasic(HELLO_MSG); //say Hello!
103  getDateFromUser(); //get initial date/time
104  setRTCC(); //set the date/time
105  while (1) {
106  while (!RCFGCALbits.RTCSYNC) doHeartbeat();
107  readRTCC();
108  printRTCC();
109  DELAY_MS(30);
110  }
111 }