//得到当前的时间 
//返回值:0,成功;其他:错误代码. 
u8 RTC_Get(void) 
{ 
        static u16 daycnt=0; 
        u32 timecount=0;  
        u32 temp=0; 
        u16 temp1=0; 
         
         timecount=RTC->CNTH;//得到计数器中的值(秒钟数) 
        timecount<<=16; 
        timecount+=RTC->CNTL;                          
 
         temp=timecount/86400;   //得到天数(秒钟数对应的) 
        if(daycnt!=temp)//超过一天了 
        {           
                daycnt=temp; 
                temp1=1970;        //从1970年开始 
                while(temp>=365) 
                {                                  
                        if(Is_Leap_Year(temp1))//是闰年 
                        { 
                                if(temp>=366)temp-=366;//闰年的秒钟数 
                                else {temp1++;break;}   
                        } 
                        else temp-=365;          //平年  
                        temp1++;   
                }    
                calendar.w_year=temp1;//得到年份 
                temp1=0; 
                while(temp>=28)//超过了一个月 
                { 
                        if(Is_Leap_Year(calendar.w_year)&&temp1==1)//当年是不是闰年/2月份 
                        { 
                                if(temp>=29)temp-=29;//闰年的秒钟数 
                                else break;  
                        } 
                        else  
                        { 
                                if(temp>=mon_table[temp1])temp-=mon_table[temp1];//平年 
                                else break; 
                        } 
                        temp1++;   
                } 
                calendar.w_month=temp1+1;        //得到月份 
                calendar.w_date=temp+1;          //得到日期  
        } 
        temp=timecount%86400;                     //得到秒钟数               
        calendar.hour=temp/3600;             //小时 
        calendar.min=(temp%3600)/60;         //分钟         
        calendar.sec=(temp%3600)%60;         //秒钟 
        calendar.week=RTC_Get_Week(calendar.w_year,calendar.w_month,calendar.w_date);//获取星期    
        return 0; 
} 
 |