<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%;
}

STM32之RTC配置与初始化-rtc.h rtc.c的更多相关文章

  1. STM32之RTC配置与初始化

    void rtc_init() { //让电源和后备寄存器使能 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP,ENABLE) ...

  2. STM32串口通信配置(USART1+USART2+USART3+UART4)

    一.串口一的配置(初始化+中断配置+中断接收函数) 1 /*====================================================================== ...

  3. 让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean

    让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean 问题描述 实现思路 思路一 [不符合要求] 思路二[满足要求] 思路三[未试验] 问题描述 目前我工作环境下,后端主要的框架 ...

  4. 三叔学FPGA系列之二:Cyclone V中的POR、配置、初始化,以及复位

    对于FPGA内部的复位,之前一直比较迷,这两天仔细研究官方数据手册,解开了心中的诸多疑惑,感觉自己又进步了呢..... 原创不易,转载请转原文,注明出处,谢谢.   一.关于POR(Power-On ...

  5. [Winfrom]Cefsharp配置与初始化

    摘要 在做客户端程序的时候,本来打算使用wpf的,但在内嵌cefsharp的时候,发现输入法有问题,所以使用了winform作为cefsharp的容器. 系列文章 CefSharp 在同一窗口打开链接 ...

  6. 在过滤器中获取在web.xml配置的初始化参数

    在过滤器中获取在web.xml配置的初始化参数   例如 <filter> <filter-name>cross-origin</filter-name> < ...

  7. mybatis源码分析--如何加载配置及初始化

    简介 Mybatis 是一个持久层框架,它对 JDBC 进行了高级封装,使我们的代码中不会出现任何的 JDBC 代码,另外,它还通过 xml 或注解的方式将 sql 从 DAO/Repository ...

  8. nginx&http 第二章 ngx 事件event配置等初始化

    event事件模块,配置分为两层:ngx_events_module 事件模块 和 ngx_event_core_module 事件核心模块.ngx_events_module:模块类型NGX_COR ...

  9. STM32入门系列-STM32时钟系统,时钟初始化配置函数

    在前面推文的介绍中,我们知道STM32系统复位后首先进入SystemInit函数进行时钟的设置,然后进入主函数main.那么我们就来看下SystemInit()函数到底做了哪些操作,首先打开我们前面使 ...

随机推荐

  1. UnicodeEncodeError

    UnicodeEncodeError at /admin/shop/product/add/ 'ascii' codec can't encode characters in position 0-1 ...

  2. 教你开启红米的USB大容量存储选项,全网首发哦

    教你开启红米的USB大容量存储选项,全网首发哦 http://bbs.7to.cn/thread-10732-1-1.html 发表于 2014-4-29 110643 红米note入手也有两天了.各 ...

  3. WebDriver 在使用 CSS Selector 与 XPath 在查找元素时如何取舍

    开发在做Web系统时,用的是css div划分层,使用jQuery 选取元素.

  4. dede 调用四级导航

    一.修改文件:\include\taglib目录下的channel.lib.php,请将以下代码全部复制替换上述文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  5. (10)odoo控制器操作

    -----------------更新时间11:17 2016-09-18 星期日    完善讲解17:44 2016-02-17 星期三-----------------* 控制器Controlle ...

  6. win8 卸载IIS

    C:\Windows\System32\inetsrv C:\Windows\iis7.log C:\inetpub

  7. ztree check

    <link rel="stylesheet" href="${contextPath}/resources/ztree/css/demo.css" typ ...

  8. Python中T-SNE实现降维

    Python中T-SNE实现降维 from sklearn.manifold import TSNE from sklearn.datasets import load_iris from sklea ...

  9. Android开发--环境配置

    1.下载android adt和sdk adt: 新建链接http://dl.google.com/android/ADT-xx.x.x.zip下载adt 注:xx为需要下载adt的版本号,可以在官网 ...

  10. 表单重置reset

    最近一直在做各种页面的“增删改查”,只是在做新增功能的时候,自己一直在使用 reset来清空form表单,原以为这样子的清空方法是万无一失的,可惜最终还是在进行“修改”操作完了之后再“新增”的时候,就 ...