Sunday, February 18, 2024

Reading current date and time using RTC on STM32F407VET6

In STM32F407VET6 board we can calculate current date and time using following steps:

1. Go to Pinout & Configuration> Timers> RTC in RTC mode and Configuration.

2. Activate Clock Source & Activate Calendar should be enabled.

3. Alarm A should be selected as Internal Alarm.

4. Go to parameter configuration of RTC, set date and time to marked configuration below:


5. Enable interrupt in NVIC Settings


6. To read the date and time following code needs to be called in main.c file.

  RTC_TimeTypeDef sTime1;

  RTC_DateTypeDef sDate1;


  uint8_t buffer[20];


/* USER CODE BEGIN WHILE */


while (1)

{

        HAL_RTC_GetTime(&hrtc, &sTime1, RTC_FORMAT_BCD);

HAL_RTC_GetDate(&hrtc, &sDate1, RTC_FORMAT_BCD);


buffer[0] = (sDate1.Date / 16) + 48;

buffer[1] = (sDate1.Date % 16) + 48;

buffer[2] = '.';

buffer[3] = (sDate1.Month / 16) + 48;

buffer[4] = (sDate1.Month % 16) + 48;

buffer[5] = '.';

buffer[6] = '2';

buffer[7] = '0';

buffer[8] = (sDate1.Year / 16) + 48;

buffer[9] = (sDate1.Year % 16) + 48;

buffer[10] ='@';

buffer[11] = (sTime1.Hours / 16) + 48;

buffer[12] = (sTime1.Hours % 16) + 48;

buffer[13] = ':';

buffer[14] = (sTime1.Minutes / 16) + 48;

buffer[15] = (sTime1.Minutes % 16) + 48;

buffer[16] = ':';

buffer[17] = (sTime1.Seconds / 16) + 48;

buffer[18] = (sTime1.Seconds % 16) + 48;


}

/* USER CODE END 3 */


6. After building and flashing the code we will see result as shown below:

Decoded as 18.02.2024@20:20:46

No comments:

Post a Comment