📄 rtcc.c
字号:
// 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.prt11;
mRTCCLock(); // Lock the RTCC
_rtcc_flag = 0; // Release the lock on the time
#endif
}
/*****************************************************************************
* Function: RTCCUnlock
*
* Preconditions: None.
*
* Overview: The function allows a writing into the clock registers.
*
* Input: None.
*
* Output: None.
*
*****************************************************************************/
void RTCCUnlock(void)
{
#if 0
asm volatile("disi #5");
asm volatile("mov #0x55, w7");
asm volatile("mov w7, _NVMKEY");
asm volatile("mov #0xAA, w8");
asm volatile("mov w8, _NVMKEY");
// asm volatile("bset _NVMCON, #15");
asm volatile("bset _RCFGCAL, #13");
asm volatile("nop");
asm volatile("nop");
// EECON2 = 0x55;
// EECON2 = 0xAA;
// RCFGCALbits.RTCWREN = 1;
#endif
}
/*****************************************************************************
* Function: RTCCSetBinSec
*
* Preconditions: None.
*
* Overview: The function verifies setting seconds range, translates it into
* BCD format and writes into _time_chk structure. To write the structure into
* clock RTCCSet must be called.
*
* Input: Seconds binary value.
*
* Output: Checked BCD value in _time_chk structure.
*
*****************************************************************************/
void RTCCSetBinSec(unsigned char Sec)
{
if(Sec == 0xff) Sec = 59;
if(Sec == 60) Sec = 0;
mRTCCSetSec(mRTCCBin2Dec(Sec));
}
/*****************************************************************************
* Function: RTCCSetBinMin
*
* Preconditions: None.
*
* Overview: The function verifies a setting minutes range, translates it into
* BCD format and writes into _time_chk structure. To write the structure into
* clock RTCCSet must be called.
*
* Input: Minutes binary value.
*
* Output: Checked BCD value in _time_chk structure.
*
*****************************************************************************/
void RTCCSetBinMin(unsigned char Min)
{
if(Min == 0xff) Min = 59;
if(Min == 60) Min = 0;
mRTCCSetMin(mRTCCBin2Dec(Min));
}
/*****************************************************************************
* Function: RTCCSetBinHour
*
* Preconditions: None.
*
* Overview: The function verifies a setting hours range, translates it into
* BCD format and writes into _time_chk structure. To write the structure into
* clock RTCCSet must be called.
*
* Input: Hours binary value.
*
* Output: Checked BCD value in _time_chk structure.
*
*****************************************************************************/
void RTCCSetBinHour(unsigned char Hour)
{
if(Hour == 0xff) Hour = 23;
if(Hour == 24) Hour = 0;
mRTCCSetHour(mRTCCBin2Dec(Hour));
}
/*****************************************************************************
* Function: RTCCCalculateWeekDay
*
* Preconditions: Valid values of day, month and year must be presented in
* _time_chk structure.
*
* Overview: The function reads day, month and year from _time_chk and
* calculates week day. Than It writes result into _time_chk. To write
* the structure into clock RTCCSet must be called.
*
* Input: _time_chk with valid values of day, month and year.
*
* Output: Zero based week day in _time_chk structure.
*
*****************************************************************************/
void RTCCCalculateWeekDay()
{
const char MonthOffset[] =
//jan feb mar apr may jun jul aug sep oct nov dec
{ 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
unsigned Year;
unsigned Month;
unsigned Day;
unsigned Offset;
// calculate week day
Year = mRTCCGetBinYear();
Month = mRTCCGetBinMonth();
Day = mRTCCGetBinDay();
// 2000s century offset = 6 +
// every year 365%7 = 1 day shift +
// every leap year adds 1 day
Offset = 6 + Year + Year/4;
// Add month offset from table
Offset += MonthOffset[Month-1];
// Add day
Offset += Day;
// If it's a leap year and before March there's no additional day yet
if((Year%4) == 0)
if(Month < 3)
Offset -= 1;
// Week day is
Offset %= 7;
mRTCCSetWkDay(Offset);
}
/*****************************************************************************
* Function: RTCCSetBinDay
*
* Preconditions: None.
*
* Overview: The function verifies a setting day range, translates it into
* BCD format and writes into _time_chk structure. To write the structure into
* clock RTCCSet must be called.
*
* Input: Day binary value.
*
* Output: Checked BCD value in _time_chk structure.
*
*****************************************************************************/
void RTCCSetBinDay(unsigned char Day){
const char MonthDaymax[] =
//jan feb mar apr may jun jul aug sep oct nov dec
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
unsigned Daymax;
unsigned Month;
unsigned Year;
Month = mRTCCGetBinMonth();
Year = mRTCCGetBinYear();
Daymax = MonthDaymax[Month-1];
// February has one day more for a leap year
if(Month == 2)
if( (Year%4) == 0)
Daymax++;
if(Day == 0) Day = Daymax;
if(Day > Daymax) Day = 1;
mRTCCSetDay(mRTCCBin2Dec(Day));
}
/*****************************************************************************
* Function: RTCCSetBinMonth
*
* Preconditions: None.
*
* Overview: The function verifies a setting month range, translates it into
* BCD format and writes into _time_chk structure. To write the structure into
* clock RTCCSet must be called.
*
* Input: Month binary value.
*
* Output: Checked BCD value in _time_chk structure.
*
*****************************************************************************/
void RTCCSetBinMonth(unsigned char Month){
if(Month < 1) Month = 12;
if(Month > 12) Month = 1;
mRTCCSetMonth(mRTCCBin2Dec(Month));
}
/*****************************************************************************
* Function: RTCCSetBinYear
*
* Preconditions: None.
*
* Overview: The function verifies a setting year range, translates it into
* BCD format and writes into _time_chk structure. To write the structure into
* clock RTCCSet must be called.
*
* Input: Year binary value.
*
* Output: Checked BCD value in _time_chk structure.
*
*****************************************************************************/
void RTCCSetBinYear(unsigned char Year){
if(Year == 0xff) Year = 99;
if(Year == 100) Year = 0;
mRTCCSetYear(mRTCCBin2Dec(Year));
// Recheck day. Leap year influences to Feb 28/29.
RTCCSetBinDay(mRTCCGetBinDay());
}
/*****************************************************************************
* EOF
*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -