/******************************************************************************** * File Name: RTC.c * Function Describe: 实时时间 万年历 软模拟 * Explain: * Writer: ShiLiangWen * Date: 2019-2-17 *******************************************************************************/ #include "includes.h" SUT_RTC sutRTC={2000,1,1,0,0,0}; /* 判断当月有多少天 输入:year 如:2019 month 从1开始 */ int MonthDays(int year, int month)//判断当月共有多少天 { int month_days; if(year<2000)year+=2000; if (month == 2) { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { month_days = 29; } else { month_days = 28; } } else if (month == 4 || month == 6 || month == 9 || month == 11) { month_days = 30; } else { month_days = 31; } return month_days; } /* RTC 增加1秒 */ void RTCIncSecond(void) { tsk_lock(); if(++sutRTC.Seconds>59){ sutRTC.Seconds=0; if(++sutRTC.Minute>59){ sutRTC.Minute=0; if(++sutRTC.Hours>23){ sutRTC.Hours=0; if(++sutRTC.Days>MonthDays(sutRTC.Years,sutRTC.Months)){ sutRTC.Days=1; if(++sutRTC.Months>12){ sutRTC.Months=1; sutRTC.Years++; } } } } } tsk_unlock(); } void RTC_Process(void) { unsigned char BCD[6]; static unsigned long lastTick=100; unsigned long curTick; curTick=os_time_get(); if(curTick>lastTick){ lastTick=curTick+100; RTCIncSecond(); //printf("%d-%d-%d %d:%d:%d\r\n",sutRTC.Years,sutRTC.Months,sutRTC.Days,sutRTC.Hours,sutRTC.Minute,sutRTC.Seconds); //RTC_Get(BCD); //TraceData(BCD,6); } } /* */ void RTC_Set(unsigned char *BCD) { tsk_lock(); sutRTC.Years=(int)(BCD[0]>>4)*10; sutRTC.Years+=(int)BCD[0]&0xF; sutRTC.Years+=2000; sutRTC.Months=(int)(BCD[1]>>4)*10; sutRTC.Months+=(int)BCD[1]&0xF; sutRTC.Days=(int)(BCD[2]>>4)*10; sutRTC.Days+=(int)BCD[2]&0xF; sutRTC.Hours=(int)(BCD[3]>>4)*10; sutRTC.Hours+=(int)BCD[3]&0xF; sutRTC.Minute=(int)(BCD[4]>>4)*10; sutRTC.Minute+=(int)BCD[4]&0xF; sutRTC.Seconds=(int)(BCD[5]>>4)*10; sutRTC.Seconds+=(int)BCD[5]&0xF; tsk_unlock(); } void RTC_Get(unsigned char *BCD) { tsk_lock(); int y=sutRTC.Years-2000; BCD[0]=((y/10)<<4)| (y%10); BCD[1]=((sutRTC.Months/10)<<4)|(sutRTC.Months%10); BCD[2]=((sutRTC.Days/10)<<4)|(sutRTC.Days%10); BCD[3]=((sutRTC.Hours/10)<<4)|(sutRTC.Hours%10); BCD[4]=((sutRTC.Minute/10)<<4)|(sutRTC.Minute%10); BCD[5]=((sutRTC.Seconds/10)<<4)|(sutRTC.Seconds%10); tsk_unlock(); } //判断RTC时间是否有效 int RTC_Effective(void) { if(sutRTC.Years>2001)return 1; else return 0; }