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%;
}
STM32之RTC配置与初始化-rtc.h rtc.c的更多相关文章
- STM32之RTC配置与初始化
void rtc_init() { //让电源和后备寄存器使能 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP,ENABLE) ...
- STM32串口通信配置(USART1+USART2+USART3+UART4)
一.串口一的配置(初始化+中断配置+中断接收函数) 1 /*====================================================================== ...
- 让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean
让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean 问题描述 实现思路 思路一 [不符合要求] 思路二[满足要求] 思路三[未试验] 问题描述 目前我工作环境下,后端主要的框架 ...
- 三叔学FPGA系列之二:Cyclone V中的POR、配置、初始化,以及复位
对于FPGA内部的复位,之前一直比较迷,这两天仔细研究官方数据手册,解开了心中的诸多疑惑,感觉自己又进步了呢..... 原创不易,转载请转原文,注明出处,谢谢. 一.关于POR(Power-On ...
- [Winfrom]Cefsharp配置与初始化
摘要 在做客户端程序的时候,本来打算使用wpf的,但在内嵌cefsharp的时候,发现输入法有问题,所以使用了winform作为cefsharp的容器. 系列文章 CefSharp 在同一窗口打开链接 ...
- 在过滤器中获取在web.xml配置的初始化参数
在过滤器中获取在web.xml配置的初始化参数 例如 <filter> <filter-name>cross-origin</filter-name> < ...
- mybatis源码分析--如何加载配置及初始化
简介 Mybatis 是一个持久层框架,它对 JDBC 进行了高级封装,使我们的代码中不会出现任何的 JDBC 代码,另外,它还通过 xml 或注解的方式将 sql 从 DAO/Repository ...
- nginx&http 第二章 ngx 事件event配置等初始化
event事件模块,配置分为两层:ngx_events_module 事件模块 和 ngx_event_core_module 事件核心模块.ngx_events_module:模块类型NGX_COR ...
- STM32入门系列-STM32时钟系统,时钟初始化配置函数
在前面推文的介绍中,我们知道STM32系统复位后首先进入SystemInit函数进行时钟的设置,然后进入主函数main.那么我们就来看下SystemInit()函数到底做了哪些操作,首先打开我们前面使 ...
随机推荐
- opencl gauss filter优化(一)
Platform: LG G3, Adreno 330 ,img size 3264x2448 C code neon GPU 300 60 29 单位:ms 1. 目前按如下行列分解的方式最快29m ...
- phalcon: model 验证数据完整性
The above example performs a validation using the built-in validator “InclusionIn”. It checks the va ...
- xocde 静态类库 相对路径 与 绝对路径
xocde 静态类库 相对路径 与 绝对路径 导入别人的 静态类库,通常我直接用鼠标把文件夹拉到了 xcode的项目里面: 这时,导入的静态类库路径变成了,绝对路径:如果你的项目,不换位置那么编译运行 ...
- 【CodeVS 1014】装箱问题
题目描述 有一个箱子容量为V(正整数,0<=V<=20000),同时有n个物品(0<n<=30),每个物品有一个体积(正整数). 要求n个物品中,任取若干个装入箱内,使箱子的剩 ...
- Ubuntu 系统下 mongodb 安装和配置
安装 MongoDB sudo apt-get install mongodb sudo apt-get install mongodb 关闭/启动 sudo service mongodb stop ...
- text-overflow:ellipsis实现超出隐藏时省略号显示
text-overflow:ellipsis;要达到的效果是:文字超出容器宽度时,文字被隐藏的文字用省略号代替.所以该属性只能用于块状元素或行内块元素中,对行内元素是不起作用的. 一般和white-s ...
- 转 http://www.5icool.org/a/201106/a654.html CSS开发中常用的公用样式
overflow:hidden 隐藏溢出 一.自己总结的公用样式解析 html, body, div, p, ul, li, dl, dt, dd, h1, h2, h3, h4, h5, h6, f ...
- cookie、sessionStorage、localStorage区别
相同:不管sessionStorage localStorage 还是 cookie 都是存储用户数据的. 不同: 1.cookie的存储空间小, cookie的数据是会通过http请求带到服务器的( ...
- Valid Palindrome [LeetCode]
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- tsne降维可视化
Python代码:准备训练样本的数据和标签:train_X4000.txt.train_y4000.txt 放于tsne.py当前目录.(具体t-SNE – Laurens van der Maate ...