STM32L151內(nèi)部RTC時(shí)鐘
Fclk=32768/[(PREDIV_S+1)*(PREDIV_A+1)]
void RTC_Config(void)
{
RTC_InitTypeDef RTC_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
PWR_RTCAccessCmd(ENABLE);
#if defined RTC_CLOCK_SOURCE_LSI
RTC_LSICmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
#elif defined RTC_CLOCK_SOURCE_LSE
RTC_LSEConfig(RCC_LSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
#else #error Please select the RTC Clock Source inside the main.c file
#endif
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;
RTC_InitStruct.RTC_AsynchPrediv = 0x7F;
RTC_InitStruct.RTC_SynchPrediv = 0xFF;
RTC_Init(&RTC_InitStruct);
}
void RTC_TimeRegulate(void)
{
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
RTC_TimeStruct.RTC_Hours = 23;
RTC_TimeStruct.RTC_Minutes = 59;
RTC_TimeStruct.RTC_Seconds = 59;
RTC_TimeStruct.RTC_H12 = RTC_H12_AM;
RTC_SetTime(RTC_Format_BIN,&RTC_TImeStruct);
RTC_DateStruct.RTC_Year = 12;
RTC_DateStruct.RTC_Date = 31;
RTC_DateStruct.RTC_Month = 12;
RTC_DateStruct.RTC_WeekDay = 1;
RTC_SetDate(RTC_Format_BIN,&RTC_DateStruct);
}
void RTC_TimeShow(unsigned char *pDate)
{
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
RTC_GetTime(RTC_Format_BIN,&RTC_TImeStruct);
RTC_GetDate(RTC_Format_BIN,&RTC_DateStruct);
pDate[0] = RTC_TimeStruct.RTC_Hours;
pDate[1] = RTC_TimeStruct.RTC_Minutes;
pDate[2] = RTC_TimeStruct.RTC_Seconds ;
pDate[3] = RTC_DateStruct.RTC_Year;
pDate[4] = RTC_DateStruct.RTC_Date;
pDate[5] = RTC_DateStruct.RTC_Month ;
pDate[6] = RTC_DateStruct.RTC_WeekDay ;
}
評(píng)論