STM32之RTC配置与初始化-rtc.h rtc.c

时间:2023-03-09 19:36:11
STM32之RTC配置与初始化-rtc.h rtc.c
<rtc.h>
#include "stm32f10x.h"
#ifndef _RTC_H
#define _RTC_H
typedef struct
{
vu8 hour;
vu8 mintue;
vu8 second;
vu16 year;
vu8 month;
vu8 day;
vu8 week;
}_calendar_obj;
// extern _calendar_obj Calendar;
u8 RTC_Init(void);
u8 Is_Leap_Year(u16 _year);
_calendar_obj RTC_GetCalendar(void);
u8 RTC_GetWeek(u16 _year,u8 _month,u8 _day);
u8 RTC_SetCalendar(u16 _year,u8 _month,u8 _day,u8 _hour,u8 _min,u8 _second);
#endif <rtc.c>
#include "rtc.h" const u8 month_table[]={,,,,,,,,,,,};
const u8 week_table[]={,,,,,,,,,,,}; _calendar_obj Calendar; void delay_ms(u16 time)
{
u16 i=;
while(time--)
{
i=;
while(i--);
}
} u8 Is_Leap_Year(u16 _year)
{
if((_year% && _year%!=) | (_year%==)) return ;
else return ;
} u8 RTC_Init(void)
{
u16 temp=;
//enable RTC_CLK outside
if(BKP_ReadBackupRegister(BKP_DR1)!=0x5050)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP,ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY)==RESET)
{
temp++;
delay_ms();
}
if(temp>) return ;
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC,ENABLE);
RTC_WaitForLastTask();
RTC_SetPrescaler();
RTC_WaitForLastTask();
RTC_SetCalendar(,,,,,);
RTC_ExitConfigMode();
BKP_WriteBackupRegister(BKP_DR1,0x5050);
}
else
{
RTC_WaitForSynchro();
RTC_ITConfig(RTC_IT_SEC,ENABLE);
RTC_WaitForLastTask();
}
return ;
} _calendar_obj RTC_GetCalendar(void)
{
static u16 daycount=;
u32 timecount=RTC_GetCounter();
u32 daynum=timecount/; //day
u16 temp=; //Calendar year
if(daycount!=daynum) //>1
{
daycount=daynum;
temp=;
while(daynum>)
{
if(Is_Leap_Year(temp))
{
if(daynum>=)
daynum=daynum-;
else
break;
}
else
daynum=daynum-;
temp++;
}
Calendar.year=temp; // set year
temp=; //reset temp // Calendar month
while(daynum>=)
{
if(Is_Leap_Year(Calendar.year) && temp==)
{
if(daynum>=) daynum=daynum-;
else break;
}
else
{
if(daynum>=month_table[temp])
daynum=daynum-month_table[temp];
else
break;
}
temp++;
}
Calendar.month=temp+; //set month
Calendar.day=daynum+; //set day
} daynum=timecount%;
Calendar.hour=daynum/; //set hour
Calendar.mintue=(daynum%)/; //set mintue
Calendar.second=(daynum%)%; //set second return Calendar;
} u8 RTC_SetCalendar(u16 _year,u8 _month,u8 _day,u8 _hour,u8 _min,u8 _second)
{
u16 i;
u32 seccount=; //second total
if(_year< || _year>) return ; //valid year //calculate year
for(i=;i<_year;i++)
{
if(Is_Leap_Year(i))
seccount=seccount+**; //3600*24*366=31622400
else
seccount=seccount+**; //3600*24*365=31536000
}
//calculate month
_month=_month-;
for(i=;i<_month;i++)
{
seccount=seccount+(u32)month_table[i]**;
if(Is_Leap_Year(_year) && i==) //February,add 1 day
{
seccount=seccount+*;
}
} //calculate day
seccount=seccount+(u32)(_day-)**;
seccount+=(u32)_hour*;
seccount+=(u32)_min*;
seccount+=_second; //set Calendar
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP,ENABLE);
PWR_BackupAccessCmd(ENABLE);
RTC_SetCounter(seccount);
RTC_WaitForLastTask(); return ;
} u8 RTC_GetWeek(u16 _year,u8 _month,u8 _day)
{
u16 temp;
u8 yearH,yearL;
yearH=_year/; // 2016/100=20
yearL=_year%; // 2016%100=16 if(yearH>) yearL+=;
temp=yearL+yearL/;
temp=temp+_day+week_table[_month-];
if(yearL%== && _month<)
temp=temp-; return temp%;
}