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

基于 STM32 RTC的万年历

时间:12-03 来源:互联网 点击:
例子基本是照抄官方的 万年历算法也没深入研究 主要是大赛 都要求会用DS1302 若我用STM32来做 肯定不用那个片子了。

这个用的是 LSE (片外低速时钟)配合 掉电寄存器来确定是否配置时钟。

注释很全 话不多说了。

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

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

网站地图

Top