我正在使用IAR编译器在STM32L152RB Discovery板上实现实时时钟。我已在HSI上实现了时钟配置,并使用PLL将其乘以4.代码 - >
/* Enable HSI Clock */
RCC_HSICmd(ENABLE);
/*!< Wait till HSI is ready */
while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
RCC_PLLConfig(RCC_PLLSource_HSI,RCC_PLLMul_4,RCC_PLLDiv_2);
RCC_PLLCmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* Set HSI as sys clock*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
问题是在配置实时时钟时我必须将辅助时钟LSE设置为RTC时钟源,在我的情况下,我的源时钟是HSI。根据我所知,其余的步骤包括启用PWR控制器,启用rtc域访问,rtc时钟源,rtc_init(),然后设置时间和gettime。这是我试过的代码 - &gt;
/* Enable RTC clocks and rtc related functions */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
PWR_RTCAccessCmd(ENABLE);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //This part I think is wrong
RCC_RTCCLKCmd(ENABLE);
RTC_InitTypeStructure.RTC_HourFormat=RTC_HourFormat_12;
RTC_InitTypeStructure.RTC_AsynchPrediv=0x7F;
RTC_InitTypeStructure.RTC_SynchPrediv=0xFF;
RTC_Init(&RTC_InitTypeStructure);
/* End RTC Clock */
RTC_TimeTypeTime.RTC_Hours=18;
RTC_TimeTypeTime.RTC_Minutes=11;
RTC_TimeTypeTime.RTC_Seconds=4;
RTC_TimeTypeTime.RTC_H12=RTC_H12_PM;
RTC_SetTime(RTC_Format_BIN, &RTC_TimeTypeTime);
while(1){
f_SleepMs(10);
RTC_GetTime(RTC_Format_BIN, &RTC_TimeTypeTime);
RELEASE_MSG("\r%d:%d:%d",RTC_TimeTypeTime.RTC_Hours,RTC_TimeTypeTime.RTC_Minutes,RTC_TimeTypeTime.RTC_Seconds);
}
我得到的输出是0:0:0
答案 0 :(得分:7)
解决了这个问题,
/* Allow access to the RTC */
PWR_RTCAccessCmd(ENABLE);
/* Reset RTC Backup Domain */
RCC_RTCResetCmd(ENABLE);
RCC_RTCResetCmd(DISABLE);
/* LSE Enable */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait until LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
/* RTC Clock Source Selection */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* Enable the RTC */
RCC_RTCCLKCmd(ENABLE);
LSE只能与外部晶振或振荡器配合使用。对于内部晶体,可以使用LSI。
答案 1 :(得分:2)
我可以确认这适用于STM32F051(STM32F0Discovery):
RTC_InitTypeDef R;
RTC_TimeTypeDef T;
// Enable PWR clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Enable the Backup Domain Access */
PWR_BackupAccessCmd(ENABLE);
/* Disable RTC clock */
RCC_RTCCLKCmd(DISABLE);
/* Enable RTC clock */
RCC_RTCCLKCmd(ENABLE);
RCC_LSEDriveConfig(RCC_LSEDrive_High); // i think this is optional
/* LSE Enable */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait until the LSE crystal is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){
}
/* Set RTC clock source to LSE */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
R.RTC_AsynchPrediv = 0x7F;
R.RTC_SynchPrediv = 0xFF;
/* Enable RTC clock */
RCC_RTCCLKCmd(ENABLE);
/* Waits until the RTC Time and Date registers are synchronized with RTC APB clock.*/
RTC_WaitForSynchro();
/* Set hour format to 24hrs */
R.RTC_HourFormat = RTC_HourFormat_24;
/* initialize the RTC */
if (RTC_Init(&R) == ERROR){
printf("RTC init failed \r\n");
}
printf("RTC done. \r\n");
while(1){
RTC_GetTime(RTC_Format_BIN, &T);
printf("the time is %02d : %02d : %02d \r\n", T.RTC_Hours, T.RTC_Minutes, T.RTC_Seconds);
}