Skip to main content
Associate II
June 17, 2024
Solved

RTC Timer

  • June 17, 2024
  • 4 replies
  • 4858 views

Hello everyone,

I am using an STM32L152RE board to generate a delay using the RTC as a timer. I would like the onboard LED to blink every 10 seconds. I have tried using a delay via a timer and am now trying to implement the same functionality using the internal RTC.

I have configured all the ports and selected the appropriate clock, i.e., LSE. Below, I have attached the main file. Although the RTC is running, there is no change in the LED. Can you please help me with this issue?

Thank you,

Best answer by Shreayas_Acharaya

@AnanyaRavi29 Try this callback function it will work for you, (it correctly handles the overflow of seconds by wrapping around):

 

void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) {
 RTC_AlarmTypeDef sAlarm;
 HAL_RTC_GetAlarm(hrtc, &sAlarm, RTC_ALARM_A, FORMAT_BIN);

 /* Increment the alarm by 10 seconds */
 sAlarm.AlarmTime.Seconds += 10;
 if (sAlarm.AlarmTime.Seconds >= 58) {
 sAlarm.AlarmTime.Seconds -= 58;
 }

 /* Reset the alarm to trigger again after 10 seconds */
 while (HAL_RTC_SetAlarm_IT(hrtc, &sAlarm, FORMAT_BIN) != HAL_OK) {}

 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}

 

___________________________________________________________________________________________ 

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.

4 replies

KDJEM.1
Technical Moderator
June 19, 2024

Hello @AnanyaRavi29 and welcome to the Community :);

 

Could you please take a look at Getting started with RTC - stm32mcu and get inspired to configure the RTC.

This article explains how to use the RTC. It provides code examples to use RTC Alarm with interrupt and toggles led every 1 second.

I hope this help you!

Thank you.

Kaouthar

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Associate II
June 20, 2024

Hello Kaouthar,

I tried this pls.Its still not toggling the LED. WHat could be the issue pls?

Lead III
June 20, 2024

You are not following the example.

You set date to may 40th **36. Never use hex numbers when decimal numbers make more sense. I would write 2024%100. If you set the masks correctly and set the alarm in the future it will trigger once or it will retrigger again depending on the masks.

The example sets the alarm again in the interrupt. You initialize the entire RTC again.

Enable interrupts in STM32CubeMX and follow the example. It should work. Certainly if you say the RTC is running.

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."
Associate III
June 20, 2024

@AnanyaRavi29 

Did you tried the exact code for toggling the LED for exactly 1 second, as provided in the "Getting started with RTC " guide on the STM32 MCU website. I think you may have missed some steps.


Associate II
June 20, 2024

I tried the exact code pls.Its working for 1 second.When i set the interrupt for 10 seconds,it blinks for 2-3 iterations and stays on.I would like it to repeat continuously pls.

PFA the main file.

Associate III
June 20, 2024

@AnanyaRavi29 Try this callback function it will work for you, (it correctly handles the overflow of seconds by wrapping around):

 

void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) {
 RTC_AlarmTypeDef sAlarm;
 HAL_RTC_GetAlarm(hrtc, &sAlarm, RTC_ALARM_A, FORMAT_BIN);

 /* Increment the alarm by 10 seconds */
 sAlarm.AlarmTime.Seconds += 10;
 if (sAlarm.AlarmTime.Seconds >= 58) {
 sAlarm.AlarmTime.Seconds -= 58;
 }

 /* Reset the alarm to trigger again after 10 seconds */
 while (HAL_RTC_SetAlarm_IT(hrtc, &sAlarm, FORMAT_BIN) != HAL_OK) {}

 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}

 

___________________________________________________________________________________________ 

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.

Associate II
June 26, 2024

This soultuion works.Thank you.