Question
Unable to Set RTC on STM32F4
Posted on July 28, 2014 at 21:38
Hi,
I'm following an example as provided by the STD PERIPHERALS library, on an STM32F4 Discovery board. In spite of what's being written in the config routine, when I set the define for LSI, I read back this: Year = 20 Month = 07 Date = 36 Hours = 15 Mins = 32 Secs = 35 When I set the define for LSE, I read back this: Year = 00 Month = 01 Date = 01 Hours = 00 Mins = 00 Secs = 00 Neither of which matches the values being written in the config routine. For the LSE, a 32.768KHz xtal was installed, with 2 caps, and is oscillating happily. Here's the config routine: &sharpdefine RTC_CLOCK_SOURCE_LSE /* LSE used as RTC source clock */ &sharpdefine FIRST_DATA 0x32F2 static void RTC_Config(void) { /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); &sharpif defined (RTC_CLOCK_SOURCE_LSI) /* LSI used as RTC source clock*/ /* The RTC Clock may varies due to LSI frequency dispersion. */ /* Enable the LSI OSC */ RCC_LSICmd(ENABLE); /* Wait till LSI is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) { } /* Select the LSI as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); /* ck_spre(1Hz) = RTCCLK(LSI) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/ uwSynchPrediv = 0xFF; uwAsynchPrediv = 0x7F; &sharpelif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */ /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { } /* Select the LSE as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/ uwSynchPrediv = 0xFF; uwAsynchPrediv = 0x7F; &sharpelse &sharperror Please select the RTC Clock source inside the main.c file &sharpendif /* RTC_CLOCK_SOURCE_LSI */ /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Write to the first RTC Backup Data Register */ RTC_WriteBackupRegister(RTC_BKP_DR0, FIRST_DATA); /* Set the Time */ RTC_TimeStructure.RTC_Hours = 0x08; RTC_TimeStructure.RTC_Minutes = 0x00; RTC_TimeStructure.RTC_Seconds = 0x00; /* Set the Date */ RTC_DateStructure.RTC_Month = RTC_Month_July; RTC_DateStructure.RTC_Date = 0x27; RTC_DateStructure.RTC_Year = 0x14; RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Monday; /* Calendar Configuration */ RTC_InitStructure.RTC_AsynchPrediv = uwAsynchPrediv; RTC_InitStructure.RTC_SynchPrediv = uwSynchPrediv; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); /* Set Current Time and Date */ RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure); } I would be forever grateful if anyone....could tell me what the issue is. I have been in grief for a day now trying to figure out what is wrong. What i find really perplexing is, the routine above is directly from ST. Why doesnt it work on either LSI, or LSE? Totally at a loss here....please HELP. #stm32 #rtc