cancel
Showing results for 
Search instead for 
Did you mean: 

RTC GET_TIME / SET_TIME STM32-F4 DISCOVERY BOARD

maor
Associate III
Posted on November 07, 2014 at 10:22

hello all ,

does any one have a working code example  for keil  for RTC / calander ...

for get or set time and date ( i need it to home automation system) thanks in advanced.

(i don't conect 32k ... external crystal)

#interrupt #stm32f4 #rtc
2 REPLIES 2
Posted on November 07, 2014 at 22:01

//******************************************************************************
#define RTC_CLOCK_SOURCE_LSI /* LSI used as RTC source clock */
#define FIRST_DATA 0x4321
void RTC_Configuration(void)
{
RTC_InitTypeDef RTC_InitStructure;
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
uint32_t uwSynchPrediv, uwAsynchPrediv;
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
#if 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; // 17 KHz min, 32 KHz typ, 47 KHz max, ie pretty hideous

uwAsynchPrediv = 0x7F;
#elif 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;
#else
#error Please select the RTC Clock source inside the main.c file
#endif /* 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);
// 8 AM 27-July-2014 using HEX because setting with BCD digits, 24 HR format
/* 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);
}
//******************************************************************************
void RTC_TimeShow(void)
{
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
/* Get the current Time */
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure); // Binary - Decimal
RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
/* Display time Format : hh:mm:ss DD/MM/YY */
printf(''%0.2d:%0.2d:%0.2d %0.2d/%0.2d/%0.2d
'',
RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds,
RTC_DateStructure.RTC_Date, RTC_DateStructure.RTC_Month, RTC_DateStructure.RTC_Year );
}
//******************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
maor
Associate III
Posted on December 01, 2014 at 02:09

thank you ! big help .