How to Read RTC of STM32L476G-DISCO

Posted

in

by

Create a Project in STM32 CUBE IDE for the STM32L476G-DISCO board.

Select LSE as Clock Source for RTC

The default option is LSI which uses an internal RC circuit.

LSE is the external 32KHz crystal that is provided on the board for the RTC.

Activate the Clock source and Calendar

This will enable the RTC clock source and will also enable the calendar for date and timekeeping.

If you want to set some default time and date you can set it in the RTC configuration menu. You can always change them later through your code.

After doing the above-said steps you can now generate code. This will give you an initialization code.

To read the value from the RTC registers. You need to remember the following thing.

From Section 38.3.8 of RM0351 Rev 1 Reference Manual

It says that reading the sub-second register or Time register alone will lock the value. It will remain locked until the Date register is read.

To prevent the lockup from happening. It is suggested to read time and date simultaneously one after the another.

Here I am writing working code

 MX_RTC_Init();				// RTC initalization and configuration created using integrated cube mx 
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  RTC_TimeTypeDef readTime;	// RTC Time structure 
  RTC_DateTypeDef readDate;	// RTC Date structure
  while (1)
  {
    /* USER CODE END WHILE */
    

    /* USER CODE BEGIN 3 */
/*  function to read time from RTC shadow register */
    HAL_RTC_GetTime(&hrtc, &readTime, RTC_FORMAT_BIN);

/* function to read date from RTC shadow register	*/
    HAL_RTC_GetDate(&hrtc, &readDate, RTC_FORMAT_BIN);

    printf("Minute: %d\t",readTime.Minutes);
    printf("Seconds: %d\n",readTime.Seconds);

    HAL_Delay(1000);	// HAL Delay of 1000 millisecond
  }
  /* USER CODE END 3 */
}

I am using print statements for debugging using stm32cube ide.
How to use printf using serial wire debug on STM32L476 Discovery

Comments

One response to “How to Read RTC of STM32L476G-DISCO”

  1. […] How to Read RTC of STM32L476G-DISCO […]

Leave a Reply

Your email address will not be published. Required fields are marked *