📄 drv_rtc.c
字号:
/*-------------------------------------------------------------------
FILE NAME:
drv_rtc.c
DESCRIPTION:
this file includes rtc module driver.
AUTHOR:
WYF
VERSION:
2005.1122.00
COMPANY:
DATANG MICROELECTRONICS TECHNOLOGY CO,.LTD
HISTORY:
2005.1122 Creat this file
...
--------------------------------------------------------------------*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "nucleus.h"
#include "ADSmx21_defs.h"
#include "drv_defs.h"
#include "drv_extr.h"
#define ORIGINYEAR 1901
#define MAXYEAR (ORIGINYEAR + 200)
#define JAN1WEEK 2 /* Tuesday @ 1901.01.01 */
#define IsLeapYear(Y) (!((Y)%4) && (((Y)%100) || !((Y)%400)))
#define GetDayOfWeek(X) ((X+JAN1WEEK-1)%7)
static int monthtable[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int monthtable_leap[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
TDRV_RTC default_time = {2006,10,23,1,12,0,0};
static int CheckRealTime(TDRV_RTC *lpst)
{
int isleap;
int *month_tab;
isleap = IsLeapYear(lpst->year);
month_tab = (int *)(isleap? monthtable_leap : monthtable);
if ((lpst->year < ORIGINYEAR) || (lpst->year > MAXYEAR))
return FALSE;
if ((lpst->month < 1) ||(lpst->month > 12))
return FALSE;
if((lpst->date < 1) ||(lpst->date > month_tab[lpst->month-1]))
return FALSE;
if ((lpst->hour > 23) ||(lpst->minute > 59) ||(lpst->second > 59))
return FALSE;
return TRUE;
}
int rtc_GetDay(void)
{
return (DAYR&0xffff);
}
void rtc_SetDay(int day)
{
DAYR = (day&0xffff);
}
int rtc_GetSecond(void)
{
return (SECONDS&0x3f);
}
void rtc_SetSecond(int sec)
{
SECONDS = (sec&0x3f);
}
int rtc_GetMinute(void)
{
return (HOURMIN&0x3f);
}
void rtc_SetMinute(int min)
{
HOURMIN &= 0xffffffc0;
HOURMIN |= (min&0x3f);
}
int rtc_GetHour(void)
{
return ((HOURMIN>>8)&0x1f);
}
void rtc_SetHour(int hour)
{
HOURMIN &= 0xffffe0ff;
HOURMIN |= ((hour&0x1f)<<8);
}
void rtc_SetAll(int day, int hour, int min, int sec)
{
rtc_SetDay(day);
rtc_SetHour(hour);
rtc_SetMinute(min);
rtc_SetSecond(sec);
}
void rtc_GetAll(int *day, int *hour, int *min, int *sec)
{
*day = rtc_GetDay();
*hour = rtc_GetHour();
*min = rtc_GetMinute();
*sec = rtc_GetSecond();
}
int rtc_GetRealTime(TDRV_RTC *lpst)
{
int day, month, year, isleap;
int *month_tab;
lpst->hour = rtc_GetHour();
lpst->minute = rtc_GetMinute();
lpst->second = rtc_GetSecond();
day = rtc_GetDay();
lpst->week = GetDayOfWeek(day);
year = ORIGINYEAR;
while (day > 365){
if (IsLeapYear(year)){
if (day > 366){
day -= 366;
year += 1;
}
else{
break;
}
}
else
{
day -= 365;
year += 1;
}
}
isleap = IsLeapYear(year);
month_tab = (int *)((isleap)? monthtable_leap : monthtable);
for (month=0; month<12; month++) {
if (day <= month_tab[month])
break;
day -= month_tab[month];
}
month +=1;
lpst->date = day;
lpst->month = month;
lpst->year = year;
return TRUE;
}
int rtc_SetRealTime(TDRV_RTC *lpst)
{
int day, month, year, isleap;
int *month_tab;
int i;
if (CheckRealTime(lpst) == FALSE)
return FALSE;
day = lpst->date;
month = lpst->month;
year = lpst->year;
//Calculate whole number of day from orginal year
isleap = IsLeapYear(year);
month_tab = (int *)(isleap ? monthtable_leap : monthtable);
for (i=0; i<month-1; i++){
day += month_tab[i];
}
for (i=ORIGINYEAR; i<year; i++){
if (IsLeapYear(i))
day += 366;
else
day += 365;
}
rtc_SetDay(day);
rtc_SetHour(lpst->hour);
rtc_SetMinute(lpst->minute);
rtc_SetSecond(lpst->second);
return TRUE;
}
void rtc_init(void)
{
/* Reset */
PCCR1 |= (1<<29);
RCCTL |= 1;
rtc_SetRealTime(&default_time);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -