/*******************************************************************************
* 文件名 :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**************************/
#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);
}
上一篇:stm32中AT发送可变短信
下一篇:stm32的DMA空闲中断数据配置
推荐阅读最新更新时间:2024-03-16 15:13
设计资源 培训 开发板 精华推荐
- 直播已结束|Molex 和 TTI 更小型、高速、可靠的连接器推动物联网应用的新发展
- 观展有礼 | 到 e 络盟展台,领星巴克咖啡券(上海慕尼黑电子展)
- 报名直播赢【手环、摄像头、雨伞、手机支架】罗德与施瓦茨USB 3.2一致性测试
- 福禄克有奖直播|数据采集器的基础知识及其应用和校准
- 下资料赢京东卡|泰克“软硬”兼施 打造超值示波器
- EEWORLD2018开年送礼,答题赢红包
- 不可错过的一次邂逅,TTI携TE传感器样片与你相见!免费样片申请活动开始啦~
- 下载有礼:4种方法帮助您高效进行元器件表征,加速元器件检测
- 2022 Digi-Key KOL 视频系列:你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍