Internal RTC in STM32

Most of the STM32 devices have RTC (Real Time Clock) built in which can keep the track of the current time and date. RTC can be used for chronometers, alarm clocks, watches, small electronic agendas, and many other devices and today we are going to learn HOW to access internal RTC in STM32.

We will see how to set up the time, Getting the time and displaying it on the LCD and also How to use the alarm feature. So let’s begin the Process…

CubeMX Setup

The clock configuration is as follows

RTC CLOCK STM32

Next go to configuration tab and click on RTC

We are going to set time and date in the code itself so just set random here

Generate the project and open it.






Some Insight into the CODE

We are going to set the time and date in the program itself. So first let’s write a function to set the time and date

set_time

void set_time (void) 
{ 
  RTC_TimeTypeDef sTime; 
  RTC_DateTypeDef sDate; 
  sTime.Hours = 0x10; // set hours 
  sTime.Minutes = 0x20; // set minutes 
  sTime.Seconds = 0x30; // set seconds 
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; 
  sTime.StoreOperation = RTC_STOREOPERATION_RESET; 
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) 
  { 
    _Error_Handler(__FILE__, __LINE__); 
  } 
  sDate.WeekDay = RTC_WEEKDAY_THURSDAY; // 
  day 
  sDate.Month = RTC_MONTH_AUGUST; // 
  month 
  sDate.Date = 0x9; // date 
  sDate.Year = 0x18; // year 
  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK) 
  { 
    _Error_Handler(__FILE__, __LINE__); 
  } 
  HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2); // backup register 
}

The time should be entered in a HEX format. The time above is 10:20:30 and the date is 09-Aug-2018.

Also note that

HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2);

is a backup register and is used to store time and date. It’s use is explained in the video attached in the last tab.

The value (0x32F2) is just a random value. you can use some other value also.


Now let’s write a function to read the time and date

get_time

void get_time(void) 
{ 
 RTC_DateTypeDef gDate; 
 RTC_TimeTypeDef gTime; 
/* Get the RTC current Time */ 
 HAL_RTC_GetTime(&hrtc, &gTime, RTC_FORMAT_BIN); 
/* Get the RTC current Date */ 
 HAL_RTC_GetDate(&hrtc, &gDate, RTC_FORMAT_BIN); 
/* Display time Format: hh:mm:ss */ 
 sprintf((char*)time,"%02d:%02d:%02d",gTime.Hours, gTime.Minutes, gTime.Seconds); 
/* Display date Format: dd-mm-yy */ 
 sprintf((char*)date,"%02d-%02d-%2d",gDate.Date, gDate.Month, 2000 + gDate.Year); 
}

The above function is self explanatory. sprintf is used to convert the values into the char so that we can display it on the LCD


display_time on LCD

void display_time (void) 
{ 
 lcd_send_cmd (0x80); // send cursor to 0,0 
 lcd_send_string (time); 
 lcd_send_cmd (0xc0); // send cursor to 1,0 
 lcd_send_string (date); 
}

set_alarm

The following function is used to set ALARM and we will also write another function to be called when this alarm get triggered.

void set_alarm (void) 
{ 
  sAlarm.AlarmTime.Hours = 0x10; // hours 
  sAlarm.AlarmTime.Minutes = 0x31; // min 
  sAlarm.AlarmTime.Seconds = 0x00; //seconds 
  sAlarm.AlarmTime.SubSeconds = 0x0; 
  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; 
  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; 
  sAlarm.AlarmMask = RTC_ALARMMASK_NONE; 
  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL; 
  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; 
  sAlarm.AlarmDateWeekDay = 0x9; // DATE 
  sAlarm.Alarm = RTC_ALARM_A; 
  if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK) 
  { 
    _Error_Handler(__FILE__, __LINE__); 
  } 
}

ALARM Event Callback Function

void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) 
{ 
  HAL_GPIO_WritePin (GPIOA, GPIO_PIN_5, 1); // turn on the LED 
}

This function is called when the alarm gets triggered and it will turn ON the LED at PA5.



Result

Check out the Video Below










Info

You can help with the development by DONATING
To download the code, click DOWNLOAD button and view the Ad. The project will download after the Ad is finished.

18 Comments. Leave new

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

keyboard_arrow_up

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add controllerstech.com to your ad blocking whitelist or disable your adblocking software.

×