/*******************************************************************************************
* 俏蝶擋慾熱� 擇畫愿皖�� 闐縝� *
* �曇�� 0.1 *
* 拔闐錙 27 琵愚羲� 2005 廈滓 *
* 版衝雛瘟 28 蝌��虛� 2005 廈滓 *
* �錙� 棠慟蝴 寐慷 �痞蝓羈閎� *
*******************************************************************************************/
#include "rtc.h"
#include "service.h"
__flash unsigned char month_day_table[]={31,28,31,30,31,30,31,31,30,31,30,31};
rtc_type rtc_time; //縊羲粳瘟� 陲粳�
//位摟廓鞅頑甕� 闐縝�
void rtc_init(void)
{
rtc_set_time(0,0,0); //�粳� 膳摘琯� � 00:00:00
rtc_set_date(1,1,2000); //丑雕 膳摘琯� � 1 �牖痙� 2000 廈滓
}
//拔擋孃鈇璃� 闐縝�
void rtc_service(void)
{
unsigned char day_tmp;
rtc_time.seconds++;
if(rtc_time.seconds>59)
{
rtc_time.seconds=0;
rtc_time.minutes++;
if(rtc_time.minutes>59)
{
rtc_time.minutes=0;
rtc_time.hours++;
if(rtc_time.hours>23)
{
rtc_time.hours=0;
rtc_time.day++;
//綁鞅 廈� - 閎縝蜻縞癮 � 篦碟痘 繆陲畫�
if(leap_year(rtc_time.year) && (rtc_time.month==2))
day_tmp=29; //錙 蜻鞅雛羲隆 準筮 - 29
else
day_tmp=month_day_table[rtc_time.month-1]; //慚皓� - 蒙� 螂躑瘟
if(rtc_time.day > day_tmp)
{
rtc_time.day=1;
rtc_time.month++;
if(rtc_time.month>12)
{
rtc_time.month=1;
rtc_time.year++;
}
}
}
}
}
}
//降錶熱隋弊琯� 縊羲粳瘟� 陲粳�
void rtc_set_time(unsigned char hours, unsigned char minutes, unsigned char seconds)
{
__disable_interrupt();
rtc_time.hours=hours;
rtc_time.minutes=minutes;
rtc_time.seconds=seconds;
__enable_interrupt();
}
//降錶熱隋弊琯� 縊羲粳皚� 滓雕
void rtc_set_date(unsigned char day, unsigned char month, unsigned int year)
{
__disable_interrupt();
rtc_time.day=day;
rtc_time.month=month;
rtc_time.year=year;
__enable_interrupt();
}
//俠骯闐綏 鋼蝕嚼� 陲粳� � 擋髡羨琯� � rtc_var
void rtc_get_time(rtc_type *rtc_var)
{
__disable_interrupt();
memcpy(rtc_var,&rtc_time,sizeof(rtc_time));
__enable_interrupt();
}
//�頊擋麴綏 1, 綁鞅 廈� test_year - 閎縝蜻縞癮
unsigned char leap_year(unsigned int test_year)
{
if((test_year%4==0) && (test_year%100) || (test_year%400==0))
return 1;
else
return 0;
}