stm32万年历流程图_基于 STM32 RTC的万年历
例子基本是照抄官方的 万年历算法也没深入研究 主要是大赛 都要求会用DS1302 若我用STM32来做 肯定不用那个片子了。
这个用的是 LSE (片外低速时钟)配合 掉电寄存器来确定是否配置时钟。本文引用地址:http://www.eepw.com.cn/article/201612/325211.htm
注释很全 话不多说了。
u8 TimeDisplay;
int main(void)
{
SystemInit();
stm32_Init ();//GPIO PA8 Init
USART_Configuration();//USART2 9600-8-N-1
NVIC_Configuration();//Enable the RTC Interrupt
RTC_Configuration();//RTC的启动
start_rct();//检测是否配置时钟
Time_Show();//不断地时钟串口输出
}
void LEDToggle(void)
{
GPIOA->ODR=GPIOA->ODR^GPIO_Pin_8 ;
}
RTC.C///
#include "stm32f10x.h"
#include //用到printf函数的串口的输出函数 注意勾选MicroLIB
u32 Time_Regulate(void);
void Time_Adjust(void);
void Time_Show(void);
void Time_Display(u32 TimeVar);
u32 USART_Scanf(u32 value);
extern u8 TimeDisplay;
void RTC_Configuration(void)
{
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
//BKP_DeInit(); //记录0XA5A5 来确定是否重置时间
/* Enable LSE */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{}
/* Select LSE as RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/*******************************************************************************
* Function Name : Time_Regulate
* Description : Returns the time entered by user, using Hyperterminal.
* Input : None
* Output : None
* Return : Current time RTC counter value
*******************************************************************************/
//u32 Month_Days[13] = {0,31,28,31,30, 31, 30, 31, 31, 30, 31, 30, 31};
u32 Month_Days_Accu_C[13] = {0,31,59,90,120,151,181,212,243,273,304,334,365};
u32 Month_Days_Accu_L[13] = {0,31,60,91,121,152,182,213,244,274,305,335,366};
#define SecsPerDay (3600*24)
u32 Time_Regulate(void)
{
#if 1
u32 Tmp_Year=0xFFFF, Tmp_Month=0xFF, Tmp_Date=0xFF;
u32 LeapY, ComY, TotSeconds, TotDays;
#endif
u32 Tmp_HH = 0xFF, Tmp_MM = 0xFF, Tmp_SS = 0xFF;
printf("\r\n==============Time Settings=====================================");
#if 1
printf("\r\n Please Set Year");
while(Tmp_Year == 0xFFFF)
{
/*32-bit counter at Second Unit--> 4*1024*1024(s) --> 49710(day) --> 136(year)*/
Tmp_Year = USART_Scanf(2136);
}
printf(": %d", Tmp_Year);
printf("\r\n Please Set Month");
while(Tmp_Month == 0xFF)
{
Tmp_Month = USART_Scanf(12);
}
printf(": %d", Tmp_Month);
printf("\r\n Please Set Date");
while(Tmp_Date == 0xFF)
{
Tmp_Date = USART_Scanf(31);
}
printf(": %d", Tmp_Date);
#endif
printf("\r\n Please Set Hours");
while(Tmp_HH == 0xFF)
{
Tmp_HH = USART_Scanf(23);
}
printf(": %d", Tmp_HH);
printf("\r\n Please Set Minutes");
while(Tmp_MM == 0xFF)
{
Tmp_MM = USART_Scanf(59);
}
printf(": %d", Tmp_MM);;
printf("\r\n Please Set Seconds");
while(Tmp_SS == 0xFF)
{
Tmp_SS = USART_Scanf(59);
}
printf(": %d", Tmp_SS);
#if 1
{
/* change Year-Month-Data-Hour-Minute-Seconds into X(Second) to set RTC->CNTR */
if(Tmp_Year==2000)
LeapY = 0;
else
LeapY = (Tmp_Year - 2000 -1)/4 +1;
ComY = (Tmp_Year - 2000)-(LeapY);
if (Tmp_Year%4)
//common year
TotDays = LeapY*366 + ComY*365 + Month_Days_Accu_C[Tmp_Month-1] + (Tmp_Date-1);
else
//leap year
TotDays = LeapY*366 + ComY*365 + Month_Days_Accu_L[Tmp_Month-1] + (Tmp_Date-1);
TotSeconds = TotDays*SecsPerDay + (Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS);
}
#endif
/* Return the value to store in RTC counter register */
//return((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));
return TotSeconds;
}
/*******************************************************************************
* Function Name : Time_Adjust
* Description : Adjusts time.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Time_Adjust(void)
{
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Change the current time */
RTC_SetCounter(Time_Regulate());
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/*******************************************************************************
* Function Name : Time_Display
* Description : Displays the current time.
* Input : - TimeVar: RTC counter value.
* Output : None
* Return : None
*******************************************************************************/
#define SecsPerComYear 3153600//(365*3600*24)
#define SecsPerLeapYear 31622400//(366*3600*24)
#define SecsPerFourYear 126230400//((365*3600*24)*3+(366*3600*24))
#define SecsPerDay (3600*24)
s32 Year_Secs_Accu[5]={0,
31622400,
63158400,
94694400,
126230400};
s32 Month_Secs_Accu_C[13] = { 0,
2678400,
5097600,
7776000,
10368000,
13046400,
15638400,
18316800,
20995200,
23587200,
26265600,
28857600,
31536000};
s32 Month_Secs_Accu_L[13] = {0,
2678400,
5184000,
7862400,
10454400,
13132800,
15724800,
18403200,
21081600,
23673600,
26352000,
28944000,
31622400};
void Time_Display(u32 TimeVar)
{
#if 1
u32 TY = 0, TM = 1, TD = 0;
s32 Num4Y,NumY, OffSec, Off4Y = 0;
u32 i;
s32 NumDay; //OffDay;
#endif
u32 THH = 0, TMM = 0, TSS = 0;
#if 0
/* Compute hours */
THH = TimeVar/3600;
/* Compute minutes */
TMM = (TimeVar % 3600)/60;
/* Compute seconds */
TSS = (TimeVar % 3600)% 60;
#endif
#if 1
{
Num4Y = TimeVar/SecsPerFourYear;
OffSec = TimeVar%SecsPerFourYear;
i=1;
while(OffSec > Year_Secs_Accu[i++])
Off4Y++;
/* Numer of Complete Year */
NumY = Num4Y*4 + Off4Y;
/* 2000,2001,...~2000+NumY-1 complete year before, so this year is 2000+NumY*/
TY = 2000+NumY;
OffSec = OffSec - Year_Secs_Accu[i-2];
/* Month (TBD with OffSec)*/
i=0;
if(TY%4)
{// common year
while(OffSec > Month_Secs_Accu_C[i++]);
TM = i-1;
OffSec = OffSec - Month_Secs_Accu_C[i-2];
}
else
{// leap year
while(OffSec > Month_Secs_Accu_L[i++]);
TM = i-1;
OffSec = OffSec - Month_Secs_Accu_L[i-2];
}
/* Date (TBD with OffSec) */
NumDay = OffSec/SecsPerDay;
OffSec = OffSec%SecsPerDay;
TD = NumDay+1;
/* Compute hours */
THH = OffSec/3600;
/* Compute minutes */
TMM = (OffSec % 3600)/60;
/* Compute seconds */
TSS = (OffSec % 3600)% 60;
}
#endif
printf("Date: %0.4d-%0.2d-%0.2d Time: %0.2d:%0.2d:%0.2d\r",TY, TM, TD,THH, TMM, TSS);
}
/*******************************************************************************
* Function Name : Time_Show
* Description : Shows the current time (HH:MM:SS) on the Hyperterminal.
* Input : None
* Output : None
* Return : None
******************************************************************************/
void Time_Show(void)
{
printf("\n\r");
/* Infinite loop */
while (1)
{
/* If 1s has paased */
if(TimeDisplay == 1)
{
/* Display current time */
Time_Display(RTC_GetCounter());
TimeDisplay = 0;
}
}
}
/*******************************************************************************
* Function Name : USART_Scanf
* Description : Gets numeric values from the hyperterminal.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
u32 USART_Scanf(u32 value)
{
u32 index = 0;
u32 tmp[4] = {0, 0};
u32 Num;
if (value==2136)
Num = 4;
else
Num = 2;
while(index < Num)
{
/* Loop until RXNE = 1 */
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
{
}
tmp[index++] = (USART_ReceiveData(USART2));
if((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
{
printf("\n\rPlease enter valid number between 0 and 9");
index--;
}
}
/* Calculate the Corresponding value */
if (value!=2136)
index = ((tmp[0] - 0x30) * 10) + (tmp[1] - 0x30);
else
index = ((tmp[0] - 0x30) * 1000) + ((tmp[1] - 0x30) * 100) + ((tmp[2] - 0x30) * 10) + (tmp[3] - 0x30);
/* Checks */
if(index > value)
{
printf("\n\rPlease enter valid number between 0 and %d", value);
return 0xFF;
}
return index;
}
void start_rct(void)
{
if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
/* Backup data register value is not correct or not yet programmed (when
the first time the program is executed) */
printf("\r\n\n RTC not yet configured....");
/* RTC Configuration */
RTC_Configuration();
printf("\r\n RTC configured....");
/* Adjust time by values entred by the user on the hyperterminal */
Time_Adjust();
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
}
else
{
/* Check if the Power On Reset flag is set */
if(RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
printf("\r\n\n Power On Reset occurred....");
}
/* Check if the Pin Reset flag is set */
else if(RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
printf("\r\n\n External Reset occurred....");
}
printf("\r\n No need to configure RTC....");
/*一下都是可以省略的 RTC_Configuration 已有启用 RTC_IT_SEC */
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
}
其中这个函数决定了 printf 函数的输出目标 一定要有的。
int fputc(int ch)
{
USART_SendData(USART2, (u8) ch);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
return ch;
}
秒中断;
/******************************************************************************/
/* STM32F10x Peripherals Interrupt Handlers */
/******************************************************************************/
/**
* @brief This function handles RTC global interrupt request.
* @param None
* @retval None
*/
extern u8 TimeDisplay;
void LEDToggle(void);
void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
/* Clear the RTC Second interrupt */
RTC_ClearITPendingBit(RTC_IT_SEC);
/* Toggle LED1 闪灯*/
LEDToggle();
/* Enable time update */
TimeDisplay = 1;
}
}
不要使用串口助手 推荐使用超级终端。
stm32万年历流程图_基于 STM32 RTC的万年历相关推荐
- stm32万年历流程图_基于STM32的电子万年历系统设计
龙源期刊网 http://www.qikan.com.cn 基于 STM32 的电子万年历系统设计 作者:刘磊 来源:<科技资讯> 2015 年第 01 期 摘 要:电子万年历是集时间.日 ...
- stm32车牌识别_基于STM32单片机的车牌识别
系统介绍 使用STM32F103RCT6作为主控,摄像头使用OV7670(带FIFO).STM32进行了16倍频.识别过程分别为:图像采集,二值化,识别车牌区域,字符分割,字符匹配. 识别过程分 ...
- stm32捕获占空比_基于STM32超声波避障小车
不管是对于初学者还是对于一个玩过单片机的电子爱好者来说,或多或少都接触到过小车项目,今天给大家介绍的的一个项目基于STM32超声波避障小车.这也是我曾经的一个课设,在此开源分享给大家,全文5000多字 ...
- stm32超声波扫频_基于STM32的脉冲式及扫频式超声波除垢信号源设计
基于 STM32 的脉冲式及扫频式超声波除垢信号源设计 李连通 , 张伟光 *, 李金博 [摘 要] [摘 要] 介绍了一种基于 STM32 主控芯片产生两种不同信号源的实 现 ] 脉冲信号源可以产生 ...
- stm32采集脉冲信号_基于STM32+FPGA的数据采集系统的设计与实现
引言 由于火控系统工作环境特殊,所需采集信号复杂多样,传统的以微控制器或PC为主的采集系统往往难以胜任.针对上述问题,提出了一种基于STM32+FPGA的数据采集系统的设计方案,该方案不仅能够完成对多 ...
- stm32超声波扫频_基于STM32的超声波发生器扫频信号源的实现
基于 STM32 的超声波发生器扫频信号源的实现 张加岭,李善波,侯颖钊,赵 杰 [摘 要] 摘要:扫频信号源采用以 STM32F103 单片机为核心,实现高精度锯 齿波扫频.由于超声波电源换能器串联 ...
- stm32 交换机芯片_基于STM32的全链路语音交互 - stm32/stm8 - 电子工程世界
硬件平台:STM32F746G-DISC开发板 编译软件:KEIL 5.31 操作系统:RTX5 TCP/IP协议栈:Cyclone-TCP 语音交互平台:思必驰DUI开放平台 这里说一下使用的TCP ...
- stm32的语音识别_基于STM32的嵌入式语音识别模块设计实现
介绍了一种以ARM为核心的嵌入式语音识别模块的设计与实现.模块的核心处理单元选用ST公司的基于ARM Cortex-M3内核的32位处理器STM32F103C8T6.本模块以对话管理单元为中心,通过以 ...
- stm32的语音识别_基于stm32循迹避障语音控制金属探测蓝牙小车设计(原理图+pcb+源码+参考文档)...
功能描述及设计原理: 小车具有检测里程功能,在金属探测模式,槽型光耦会检测小车车轮的圈数,以此来计算小车行走的里程,并可以通过OLED屏幕显示出来.还可以显示小车的工作模式以及小车距离前方障碍物的距离 ...
- stm32硬件设计手册_基于STM32自制三菱PLC之软硬件设计
PLC主要适合电工以及机械相关的人群.其主要用在钢铁.石油.化工.电力.建材.机械制造等各个领域.其具有高可靠性.抗干扰能力强.易学易用.等特点. PLC的体积一般都是比极大的,其安装固定在C45导轨 ...
最新文章
- 【Android 性能优化】应用启动优化 ( 启动优化项目 | 界面启动时间 | 启动优化项目 | 方法追踪 MethodTracing )
- 一个略复杂的数据映射聚合例子及代码重构
- 1.子查询知识体系,单行子查询,多行子查询
- MySQL中的外键约束
- 关于在vue项目中使用wangEditor
- bert 中文 代码 谷歌_如何用最强模型BERT做NLP迁移学习?
- 实例方法 java 1614780086
- 在集合中根据条件来筛选数据
- JS取得RadioButtonList的Value,Text及选中值等信息
- 017—mysql问答
- 【数据库--db4o 介绍】
- 操作系统android9.0,三星公布了升级Android9.0操作系统的时间表
- windows10强制删除文件_360都删除不了顽固文件?别慌,不足1M的工具帮你强制清理...
- 抖音最火的微信推送教程
- java4android 教程_《Java4Android视频教程》学习笔记(三)
- SuperMap iClient3D for WebGL之BIM模型爆炸
- 关于Http的一些基础内容
- 浙江大学 工程伦理 第十二单元测试答案
- django项目-自助饮料机
- vulnhub——Earth靶机
热门文章
- windows android ndk开发,Windows系统下配置Android NDK开发环境
- Linux ls -l 各字段解释,硬链接软连接
- Abaqus6.14.4 Linux详细图文安装教程
- 安卓程序员需要什么证书
- java 数字转大写中文_java如何实现,把数字转换成中文的大写数字?
- jmeter使用不同数据进行post请求测试:csv配置使用
- 淘宝自动发货源码,网店自动值守发货系统 不限制域名 支持客户自助提货及自动评价...
- fw150us的linux驱动下载,FAST FW150US2.0无线网卡驱动
- 我的世界MinecraftJava版开服教程(Linux)开服器开服包下载开服网站服务器开服核心开服端开服软件mac版Java启动器
- goahead(嵌入式Web服务器)之asp、goform篇