//******************************************************************** // Ver 1.0 // 2021.11.19 1、新建文件。 //******************************************************************** #include "config.h" RTC_Str RTC_Info; /******************************************************************************/ /* Local function prototypes ('const') */ /******************************************************************************/ const uint8_t Leap_Month_Base[] = {3, 6, 0, 3, 5, 1, 3, 6, 2, 4, 0, 2}; const uint8_t NonLeap_Month_Base[] = {4, 0, 0, 3, 5, 1, 3, 6, 2, 4, 0, 2}; const uint8_t Cnst_Month_Tbl[12] = {0x31, 0x28, 0x31, 0x30, 0x31, 0x30, 0x31, 0x31, 0x30, 0x31, 0x30, 0x31}; rtc_counter_value_t RTC_Times; void RTC_BeiJinTimeToTimestamp(void) { struct tm BJ_Time; RTC_GetCounterValue(&RTC_Info.Time); BJ_Time.tm_year = RTC_Info.Time.year - 1900; BJ_Time.tm_mon = RTC_Info.Time.month - 1; BJ_Time.tm_mday = RTC_Info.Time.day; BJ_Time.tm_hour = RTC_Info.Time.hour; BJ_Time.tm_min = RTC_Info.Time.min; BJ_Time.tm_sec = RTC_Info.Time.sec; RTC_Info.Timestamp = mktime(&BJ_Time); //RTC_Info.Timestamp -= 8 * 60 * 60; } void RTC_TimestampToBeiJinTime(void) { struct tm *BJ_Time; //RTC_Info.Timestamp += (8 * 60 * 60); BJ_Time = localtime(&RTC_Info.Timestamp); RTC_Times.year = BJ_Time->tm_year + 1900; RTC_Times.month = BJ_Time->tm_mon + 1; RTC_Times.day = BJ_Time->tm_mday; RTC_Times.hour = BJ_Time->tm_hour; RTC_Times.min = BJ_Time->tm_min; RTC_Times.sec = BJ_Time->tm_sec; RTC_Times.week = BJ_Time->tm_wday; } /** ****************************************************************************** ** \brief RTC 平、闰年检测 ** ** \param [in] u8year 年十进制低两位 ** ** \retval 1 闰年 ** \retval 0 平年 ******************************************************************************/ uint8_t RTC_CheckLeapYear(uint16_t u16year) { if ((((u16year % 4) == 0) && ((u16year % 100) != 0)) || ((u16year % 400) == 0)) { return 1; } else { return 0; } } /** ****************************************************************************** ** \brief RTC根据日期计算周数 ** ** \param [in] pu8Date日期 ** ** \retval week 周数 ** ******************************************************************************/ uint8_t RTC_CalWeek(rtc_counter_value_t *prtc) { uint8_t u8week; if ((RTC_CheckLeapYear(prtc->year) == 1)) { u8week = ((prtc->year - 2000) + (prtc->year - 2000) / 4 + Leap_Month_Base[prtc->month - 1] + prtc->day + 2) % 7; } else { u8week = ((prtc->year - 2000) + (prtc->year - 2000) / 4 + NonLeap_Month_Base[prtc->month - 1] + prtc->day + 2) % 7; } return u8week; } void RTC_Init(void) { RTC_InfoInit(); RTC_GetAlarmValue(&RTC_Info.AlarmTime); if (((RTC_Info.AlarmTime.alarmww != BKP_VAR_INIT) && (RTC_Info.AlarmTime.alarmww != BKP_VAR_IAP)) || //使用周闹钟寄存器来保存RTC初始化的数据记录 (RTC_StartStatus() == 0)) { RTC_Init1(RTC_FSUB); RTC_Info.Time.year = 2021; RTC_Info.Time.month = 11; RTC_Info.Time.day = 26; RTC_Info.Time.hour = 0; RTC_Info.Time.min = 0; RTC_Info.Time.sec = 0; RTC_Info.Time.week = RTC_CalWeek(&RTC_Info.Time); RTC_Info.AlarmTime.alarmww = BKP_VAR_INIT; RTC_Info.AlarmTime.alarmwh = 10; RTC_Info.AlarmTime.alarmwm = 30; //RTC_SetConstPeriodInterruptOn(ONESEC); RTC_Start(); RTC_SetCounterValue(&RTC_Info.Time); RTC_SetAlarmValue(&RTC_Info.AlarmTime); } //EnableNvic(RTC_IRQn, IrqLevel3, TRUE); RTC_SetConstPeriodInterruptOn(ONESEC); RTC_BeiJinTimeToTimestamp(); } void RTC_Handler(void) //void IRQ22_Handler(void) { static INT16U tSec = 0; if (RTC_GetAlmfItStatus() != 0) //闹钟请求 { RTC_ClearAlmfItStatus(); INTC_ClearPendingIRQ(RTC_IRQn); } if (RTC_GetPrdItStatus() != 0) //周期请求 { RTC_ClearPrdfItStatus(); INTC_ClearPendingIRQ(RTC_IRQn); tSec ++; if(tSec > 60) { tSec = 0; if(BATT_FLAG_STA == STA_IDLE) { BATT_StorageCnt ++; } } } }