I use a real-time clock on the STM32L152RB detection board using the IAR compiler. I implemented Clock configuration on HSI and using PLL, I multiplied it by 4. Code β
RCC_HSICmd(ENABLE); 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); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
The problem is setting the real time clock. I have to set the secondary LSE clock as the source of the RTC clock, which in my case is the original HSI clock. The remaining steps, which include turning on the PWR controller, turning on access to the rtc domain, the clock source rtc, rtc_init (), and then setting up and gettime, are fine, as I know. 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); }
The output I get is 0:0:0
c embedded real-time stm32
Ishmeet
source share