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
Next go to configuration tab and click on RTC
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.
18 Comments. Leave new
Hello, thank you so much.
I have a problem. May you help me please?
In my project,I don’t use RTC, so the pins of microcontroller for RTC oscillator are not connected.
When the power is connected to my stm32 board, the code isn’t run until I touch the location of RTC Oscillator (32kHz crystal and 2.7pF capacitors).After this, the code is run correctly.
Thank’s for your guidance
Hello, thank you very much for your tuterial.
No matter how you slice it, 0x18 will never equal the year 2018.
it’s in the BCD format.
hi, how can I Download the code?
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
hi any idea how to set the alarm register if you wantalarm monday to friday.
sAlarm.AlarmDateWeekDay = 1;//Monday
hi, i did simlar project. It works well but except; after soft reset, ı can get correct time but date resets itself. Do you have any suggestion.?
thnx
If you are using F103C8, it have date issues with RTC
add follow Line
void set_alarm(void) {
RTC_AlarmTypeDef sAlarm;
hi
I need a library to drive 16*2 LCD (not i2c version) in CUBE IDE (using HAL) for stm32f103.
can you help me about this?
thank you
The library you are talking about, is already present on this website. Look into STM32.. or just search for Lcd
I have it a good one!
there is something wrong with my code. i try to run it but the pc doesnt register the time on the stm32
I want to use STM32 microcontroller to implement a real time clock. The problem is when the controller has no power, how can I keep the real time clock still going?
you need to connect a battery supply to Vbat pin
use cr2032 battery
Hi
I tried the code but in my screen I can see 00:00:00 01-01-2000
so you have to set date and time in RTC_init function