微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 万年历 显示年月日 时分秒 星期

STM32 万年历 显示年月日 时分秒 星期

时间:11-13 来源:互联网 点击:
基于STM32处理器 的RTC只是个能靠电池维持运行的32位定时器! 并不像实时时钟芯片,读出来就是年月日时分秒。

此程序 第一次运行时候 从超级终端 输入时间
关键代码

/*******************************************************************************
* 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);
}
//Tmp_Year=2010;
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;
}

===============================================

/**
* @brief Displays the current time.
* @param TimeVar: RTC counter value.
* @retval 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(uint32_t TimeVar)
{
#if 0
uint32_t THH = 0, TMM = 0, TSS = 0;

/* Compute hours */
THH = TimeVar / 3600;
/* Compute minutes */
TMM = (TimeVar % 3600) / 60;
/* Compute seconds */
TSS = (TimeVar % 3600) % 60;

printf("Time: %0.2d:%0.2d:%0.2d\r", THH, TMM, TSS);
#endif
//----------------------------------------------
#if 1
u32 TY = 0, TM = 1, TD = 0;
s32 Num4Y,NumY, OffSec, Off4Y = 0;
u32 i;
//s32 NumDay, OffDay;
s32 NumDay;
#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;

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top