简体   繁体   English

仅在LSI / LSE / HSE中Stm32L中的RTC时钟配置?

[英]Clock configuration of RTC in Stm32L in LSI/LSE/HSE only?

I am implementing Real Time Clock on STM32L152RB Discovery board using IAR compiler. 我正在使用IAR编译器在STM32L152RB Discovery板上实现实时时钟。 I have implemented the Clock configuration on HSI and using PLL I have multiplied it by 4. Code --> 我在HSI上实现了时钟配置,并使用PLL将其乘以4. Code - >

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

The problem is while configuring Real Time clock I have to set the secondary clock LSE as the RTC Clock source, which in my case my source clock is HSI. 问题是在配置实时时钟时我必须将辅助时钟LSE设置为RTC时钟源,在我的情况下,我的源时钟是HSI。 Rest of the steps which includes enable PWR controller, enable rtc domain access, rtc clock source, rtc_init(), then settime and gettime, are alright as per I know. 根据我所知,其余的步骤包括启用PWR控制器,启用rtc域访问,rtc时钟源,rtc_init(),然后设置时间和gettime。 Here is the code I tried --> 这是我试过的代码 - >

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

Output I get is 0:0:0 我得到的输出是0:0:0

Solved doing this, 解决了这个问题,

/* 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 can only work with External Crystal or oscillator. LSE只能与外部晶振或振荡器配合使用。 For internal crystal LSI can be used. 对于内部晶体,可以使用LSI。

I can confirm that this works for the STM32F051 (STM32F0Discovery): 我可以确认这适用于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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM