/*****************************************************************************
*
* Real Time Clock Calender
*
*****************************************************************************
* FileName: rtcc.c
* Dependencies:
* Processor: PIC24
* Compiler: C30 xx.xx or higher
* Linker: MPLINK 03.20.01 or higher
* Company: Microchip Technology Incorporated
*
* Software License Agreement
*
* The software supplied herewith by Microchip Technology Incorporated
* (the "Company") is intended and supplied to you, the Company's
* customer, for use solely and exclusively with products manufactured
* by the Company.
*
* The software is owned by the Company and/or its supplier, and is
* protected under applicable copyright laws. All rights are reserved.
* Any use in violation of the foregoing restrictions may subject the
* user to criminal sanctions under applicable laws, as well as to
* civil liability for the breach of the terms and conditions of this
* license.
*
* THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Chris Valenti 05-26-05 ...
* Ross Fosler 06-06-2005 Several changes
* Anton Alkhimenok 10-21-2005 Get/Set functions
*****************************************************************************/
#include "system.h"
/*****************************************************************************
* Structures: _time and _time_chk
*
* Overview: These structures contain the time and date.
* RTCCProcessEvents updates them. _time_chk is used as input/output for
* get/set operations.
*
*****************************************************************************/
RTCC _time;
RTCC _time_chk;
/*****************************************************************************
* Arrays: _time_str and _date_str
*
* Overview: These arrays contain the time and date strings.
* RTCCProcessEvents updates them.
*
*****************************************************************************/
char _time_str[16] = " "; // Sat 10:01:15
char _date_str[16] = " "; // Sep 30, 2005
// The flag stops updating time and date and used for get/set operations.
unsigned char _rtcc_flag;
/*****************************************************************************
* Function: RTCCProcessEvents
*
* Preconditions: RTCCInit must be called before.
*
* Overview: The function grabs the current time from the RTCC and translate
* it into strings.
*
* Input: None.
*
* Output: It update time and date strings _time_str, _date_str,
* and _time, _time_chk structures.
*
*****************************************************************************/
void RTCCProcessEvents(void)
{
// Process time object only if time is not being set
while (!_rtcc_flag) {
// Grab the time
RCFGCALbits.RTCPTR = 0;
_time.prt00 = RTCVAL;
RCFGCALbits.RTCPTR = 1;
_time.prt01 = RTCVAL;
RCFGCALbits.RTCPTR = 2;
_time.prt10 = RTCVAL;
RCFGCALbits.RTCPTR = 3;
_time.prt11 = RTCVAL;
// Grab the time again
RCFGCALbits.RTCPTR = 0;
_time_chk.prt00 = RTCVAL;
RCFGCALbits.RTCPTR = 1;
_time_chk.prt01 = RTCVAL;
RCFGCALbits.RTCPTR = 2;
_time_chk.prt10 = RTCVAL;
RCFGCALbits.RTCPTR = 3;
_time_chk.prt11 = RTCVAL;
// Verify there is no roll-over
if ((_time.prt00 == _time_chk.prt00) &&
(_time.prt01 == _time_chk.prt01) &&
(_time.prt10 == _time_chk.prt10) &&
(_time.prt11 == _time_chk.prt11))
{
switch (_time.mth) {
default:
case 0x01: _date_str[0] = 'J'; _date_str[1] = 'a'; _date_str[2] = 'n'; break;
case 0x02: _date_str[0] = 'F'; _date_str[1] = 'e'; _date_str[2] = 'b'; break;
case 0x03: _date_str[0] = 'M'; _date_str[1] = 'a'; _date_str[2] = 'r'; break;
case 0x04: _date_str[0] = 'A'; _date_str[1] = 'p'; _date_str[2] = 'r'; break;
case 0x05: _date_str[0] = 'M'; _date_str[1] = 'a'; _date_str[2] = 'y'; break;
case 0x06: _date_str[0] = 'J'; _date_str[1] = 'u'; _date_str[2] = 'n'; break;
case 0x07: _date_str[0] = 'J'; _date_str[1] = 'u'; _date_str[2] = 'l'; break;
case 0x08: _date_str[0] = 'A'; _date_str[1] = 'a'; _date_str[2] = 'g'; break;
case 0x09: _date_str[0] = 'S'; _date_str[1] = 'e'; _date_str[2] = 'p'; break;
case 0x10: _date_str[0] = 'O'; _date_str[1] = 'c'; _date_str[2] = 't'; break;
case 0x11: _date_str[0] = 'N'; _date_str[1] = 'o'; _date_str[2] = 'v'; break;
case 0x12: _date_str[0] = 'D'; _date_str[1] = 'e'; _date_str[2] = 'c'; break;
}
_date_str[3] = ' ';
_date_str[6] = ',';
_date_str[7] = ' ';
_date_str[8] = '2';
_date_str[9] = '0';
_date_str[4] = (_time.day >> 4) + '0';
_date_str[5] = (_time.day & 0xF) + '0';
_date_str[10] = (_time.yr >> 4) + '0';
_date_str[11] = (_time.yr & 0xF) + '0';
switch (_time.wkd) {
default:
case 0x00: _time_str[0] = 'S'; _time_str[1] = 'u'; _time_str[2] = 'n'; break;
case 0x01: _time_str[0] = 'M'; _time_str[1] = 'o'; _time_str[2] = 'n'; break;
case 0x02: _time_str[0] = 'T'; _time_str[1] = 'u'; _time_str[2] = 'e'; break;
case 0x03: _time_str[0] = 'W'; _time_str[1] = 'e'; _time_str[2] = 'd'; break;
case 0x04: _time_str[0] = 'T'; _time_str[1] = 'h'; _time_str[2] = 'u'; break;
case 0x05: _time_str[0] = 'F'; _time_str[1] = 'r'; _time_str[2] = 'i'; break;
case 0x06: _time_str[0] = 'S'; _time_str[1] = 'a'; _time_str[2] = 't'; break;
}
_time_str[3] = ' ';
_time_str[6] = ':';
_time_str[9] = ':';
_time_str[4] = (_time.hr >> 4) + '0';
_time_str[5] = (_time.hr & 0xF) + '0';
_time_str[7] = (_time.min >> 4) + '0';
_time_str[8] = (_time.min & 0xF) + '0';
_time_str[10] = (_time.sec >> 4) + '0';
_time_str[11] = (_time.sec & 0xF) + '0';
break;
}
}
}
/*****************************************************************************
* Function: RTCCInit
*
* Preconditions: RTCCInit must be called before.
*
* Overview: Enable the oscillator for the RTCC
*
* Input: None.
*
* Output: None.
*****************************************************************************/
void RTCCInit(void)
{
// Enables the LP OSC for RTCC operation
asm("mov #OSCCON,W1");
asm("mov.b #0x02, W0");
asm("mov.b #0x46, W2");
asm("mov.b #0x57, W3");
asm("mov.b W2, [W1]");
asm("mov.b W3, [W1]");
asm("mov.b W0, [W1]");
// Unlock sequence must take place for RTCEN to be written
RCFGCAL = 0x0000;
mRTCCUnlock();
RCFGCALbits.RTCEN = 1;
mRTCCOn();
mRTCCSetSec(0x00);
mRTCCSetMin(0x10);
mRTCCSetHour(0x10);
mRTCCSetWkDay(0x1);
mRTCCSetDay(0x10);
mRTCCSetMonth(0x10);
mRTCCSetYear(0x05);
mRTCCSet();
}
/*****************************************************************************
* Function: RTCCSet
*
* Preconditions: None.
*
* Overview: The function upload time and date from _time_chk into clock.
*
* Input: _time_chk - structure containing time and date.
*
* Output: None.
*
*****************************************************************************/
void RTCCSet(void)
{
mRTCCUnlock(); // Unlock the RTCC
// Set the time
RCFGCALbits.RTCPTR = 0;
RTCVAL = _time_chk.prt00;
RCFGCALbits.RTCPTR = 1;
RTCVAL = _time_chk.prt01;
RCFGCALbits.RTCPTR = 2;
RTCVAL = _time_chk.prt10;
RCFGCALbits.RTCPTR = 3;
RTCVAL = _time_chk.p
评论0
最新资源