微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 基于 STM32 RTC的万年历

基于 STM32 RTC的万年历

时间:12-03 来源:互联网 点击:

/*******************************************************************************
* 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;
}

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

网站地图

Top