分为两个文件:
/*******************************************************************************
* 文件名 :rtc.c
* 描绘 :wit_yuan
* 论坛 :
**********************************************************************************/
#include “rtc.h”
#include “stdio.h”
#include “misc.h”
uint8_t const *WEEK_STR[] = {“日”, “一”, “二”, “三”, “四”, “五”, “六”};
uint8_t const *zodiac_sign[] = {“猪”, “鼠”, “牛”, “虎”, “兔”, “龙”, “蛇”, “马”, “羊”, “猴”, “鸡”, “狗”};
/* 秒中止标志,进入秒中止时置1,当时刻被改写之后清0 */
__IO uint32_t TimeDisplay;
extern struct rtc_time systemtime;
/*
* 函数名:NVIC_Configuration
* 描绘 :装备RTC秒中止的主中止优先级为1,次优先级为0
* 输入 :无
* 输出 :无
* 调用 :外部调用
*/
void RTC_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the RTC Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*
* 函数名:RTC_Configuration
* 描绘 :装备RTC
* 输入 :无
* 输出 :无
* 调用 :外部调用
*/
void RTC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
}
/*
* 函数名 :Time_Regulate
* 描绘 :回来用户在超级终端中输入的时刻值,并将值储存在
* RTC 计数寄存器中。
* 输出 :用户在超级终端中输入的时刻值,单位为 s
* 调用 :内部调用
* editted : 2014-07-05
* author : wit_yuan
*/
extern struct rtc_time systemtime;
uint32_t Time_Regulate(struct rtc_time *tm)
{
tm->tm_year = systemtime.tm_year + 2000;
tm->tm_mon = systemtime.tm_mon;
tm->tm_mday = systemtime.tm_mday;
tm->tm_hour = systemtime.tm_hour;
tm->tm_min = systemtime.tm_min;
tm->tm_sec = systemtime.tm_sec;
/* Return the value to store in RTC counter register */
return(((systemtime.tm_hour) * 3600 + (systemtime.tm_min) * 60 + systemtime.tm_sec));
}
/*
* 函数名:Time_Adjust
* 描绘 :时刻调理
* 输入 :无
* 输出 :无
* 调用 :外部调用
*/
void Time_Adjust(struct rtc_time *tm)
{
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Get time entred by the user on the hyperterminal */
Time_Regulate(tm);
/* Get wday */
GregorianDay(tm);
/* Change the current time */
RTC_SetCounter(mktimev(tm));
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/*
* 函数名:Time_Display
* 描绘 :显现当时时刻值
* 输入 :-TimeVar RTC计数值,单位为 s
* 输出 :无
* 调用 :内部调用
*/
void Time_Display(uint32_t TimeVar,struct rtc_time *tm)
{
volatile uint32_t THH = 0, TMM = 0, TSS = 0;
uint32_t BJ_TimeVar;
/*把规范时刻转换为北京时刻*/
BJ_TimeVar =TimeVar + 8*60*60;
to_tm(BJ_TimeVar, tm);/*把定时器的值转换为北京时刻*/
}
/*
* 函数名:Time_Show
* 描绘 :在超级终端中显现当时时刻值
* 输入 :无
* 输出 :无
* 调用 :外部调用
*/
void Time_Show(void)
{
}
uint8_t USART_Scanf(uint32_t value)
{
uint32_t index = 0;
uint32_t tmp[2] = {0, 0};
while (index < 2)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{}
tmp[index++] = (USART_ReceiveData(USART1));
if ((tmp[index – 1] < 0x30) || (tmp[index - 1] > 0x39))
{
index–;
}
}
index = (tmp[1] – 0x30) + ((tmp[0] – 0x30) * 10);
if (index > value)
{
return 0xFF;
}
return index;
}
void Time_Config(struct rtc_time *tm)
{
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
RTC_Configuration();
Time_Adjust(tm);
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
}
else
{
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
}
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
}
RTC_WaitForSynchro();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
}
#ifdef RTCClockOutput_Enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_TamperPinCmd(DISABLE);
BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif
RCC_ClearFlag();
//Time_Show();
}
/*********************************************************
* funciton : drv_set_time
* description : set the rtc time
* time : 2015-08-22 pm
* author : wit_yuan
***********************************************************/
void drv_set_time(struct rtc_time *tm)
{
RTC_Configuration();
Time_Adjust(tm);
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
RCC_ClearFlag();
}
/*********************************************************
* funciton : wit_get_time
* description : get the rtc time
* time : 2015-07-07 am
* author : wit_yuan
***********************************************************/
void drv_get_time(struct rtc_time *tm)
{
uint32_t TimeVar;
//struct rtc_time tm;
uint32_t BJ_TimeVar;
TimeVar = RTC_GetCounter();
*tm = systemtime;
BJ_TimeVar =TimeVar + 8 * 60 * 60;
to_tm(BJ_TimeVar, tm);/*把定时器的值转换为北京时刻*/
// printf(“%d年%d月%d日 %d时%d分%d秒 \n”,tm.tm_year,tm.tm_mon,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
// return tm;
}
void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
RTC_ClearITPendingBit(RTC_IT_SEC);
TimeDisplay = 1;
if (TimeDisplay == 1)
{
TimeDisplay = 0;
}
RTC_WaitForLastTask();
}
}
void init_rtc(void )
{
RTC_NVIC_Configuration();
Time_Config(&systemtime);
}
/*****************************END OF FILE****2015-08-23 guide by wit_yuan**************************/
2、date.c
#include “date.h”
#define FEBRUARY 2
#define STARTOFTIME 1970
#define SECDAY 86400L
#define SECYR (SECDAY * 365)
#define leapyear(year) ((year) % 4 == 0)
#define days_in_year(a) (leapyear(a) ? 366 : 365)
#define days_in_month(a) (month_days[(a) – 1])
static int month_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/*
* This only works for the Gregorian calendar – i.e. after 1752 (in the UK)
*/
/*核算公历*/
void GregorianDay(struct rtc_time * tm)
{
int leapsToDate;
int lastYear;
int day;
int MonthOffset[] = { 0,31,59,90,120,151,181,212,243,273,304,334 };
lastYear=tm->tm_year-1;
/*核算从公元元年到计数的前一年之中总共阅历了多少个闰年*/
leapsToDate = lastYear/4 – lastYear/100 + lastYear/400;
/*如若计数的这一年为闰年,且计数的月份在2月之后,则日数加1,不然不加1*/
if((tm->tm_year%4==0) &&
((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
(tm->tm_mon>2)) {
/*
* We are past Feb. 29 in a leap year
*/
day=1;
} else {
day=0;
}
day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] + tm->tm_mday ; /*核算从公元元年元旦到计数日期总共有多少天*/
tm->tm_wday=day%7;
}
/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
* Assumes input in normal date format, i.e. 1980-12-31 23:59:59
* => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
*
* [For the Julian calendar (which was used in Russia before 1917,
* Britain & colonies before 1752, anywhere else before 1582,
* and is still in use by some communities) leave out the
* -year/100+year/400 terms, and add 10.]
*
* This algorithm was first published by Gauss (I think).
*
* WARNING: this function will overflow on 2106-02-07 06:28:16 on
* machines were long is 32-bit! (However, as time_t is signed, we
* will already get problems at other places on 2038-01-19 03:14:08)
*ADD by fire:本函数在工程中的输入参数为北京时刻,
所以在转换成时刻戳时最终要从北京时刻转换为规范时刻的时刻戳
*/
u32 mktimev(struct rtc_time *tm)
{
if (0 >= (int) (tm->tm_mon -= 2)) { /* 1..12 -> 11,12,1..10 */
tm->tm_mon += 12; /* Puts Feb last since it has leap day */
tm->tm_year -= 1;
}
return (((
(u32) (tm->tm_year/4 – tm->tm_year/100 + tm->tm_year/400 + 367*tm->tm_mon/12 + tm->tm_mday) +
tm->tm_year*365 – 719499
)*24 + tm->tm_hour /* now have hours */
)*60 + tm->tm_min /* now have minutes */
)*60 + tm->tm_sec-8*60*60; /* finally seconds */
/*Add by fire: -8*60*60 把输入的北京时刻转换为规范时刻,
再写入计时器中,保证计时器的数据为规范的UNIX时刻戳*/
}
void to_tm(u32 tim, struct rtc_time * tm)
{
register u32 i;
register long hms, day;
day = tim / SECDAY;
hms = tim % SECDAY;
/* Hours, minutes, seconds are easy */
tm->tm_hour = hms / 3600;
tm->tm_min = (hms % 3600) / 60;
tm->tm_sec = (hms % 3600) % 60;
/* Number of years in days */ /*算出当时年份,开始的计数年份为1970年*/
for (i = STARTOFTIME; day >= days_in_year(i); i++) {
day -= days_in_year(i);
}
tm->tm_year = i;
/* Number of months in days left */ /*核算当时的月份*/
if (leapyear(tm->tm_year)) {
days_in_month(FEBRUARY) = 29;
}
for (i = 1; day >= days_in_month(i); i++) { //程序编写时刻是2013年8月
day -= days_in_month(i);
}
days_in_month(FEBRUARY) = 28;
tm->tm_mon = i;
/* Days are what is left over (+1) from all that. *//*核算当时日期*/
tm->tm_mday = day + 1;//+ 1; //2013年8月3日修正,完成功用
/*
* Determine the day of week
*/
GregorianDay(tm);
}