cancel
Showing results for 
Search instead for 
Did you mean: 

RTC Timer

AnanyaRavi29
Associate II

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Shreayas_Acharaya
Associate III

@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.

View solution in original post

8 REPLIES 8
KDJEM.1
ST Employee

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.

Hello Kaouthar,

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

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.
Shreayas_Acharaya
Associate III

@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.


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.

Shreayas_Acharaya
Associate III

@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.

This soultuion works.Thank you.


@Shreayas_Acharaya wrote:

@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);
}

 

 


Can you explain where the magic number 58 comes from? Minutes have 60 seconds, which means the max value would be 59 (60 in case of leap seconds, but we can ignore that), not 58.

if initial time for alarm is 4, then the sequence will be:
4,14,24,34,44,54,6,16,26,36,46,56,8,18,28,38,48,0,10,20,30,40,50,2,
So the time difference will be:
12,10,10,10,10,10,12,10,10,10,10,10,12,10,10,10,10,12,10,10,10,10,10,12,10,10,10,10,10,12,10,10,10,10,10,12,10,10,10,10,10,12,10,10,10,10,12,10,10,10,10,10,12,10,10,10,10,10,12,10,10,10,10,10,12,10,10,10,10,10,12,10,10,10,
Meaning sometimes there is 10 seconds and sometimes 12 seconds difference. What's worse is that the interval between the 12 second periods is mostly 6, but sometimes 5 cycles because one cycle that starts with 8 skips 5x completely.

Correct me if I'm wrong.

 

 

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.