Skip to main content
Associate
July 31, 2023
Question

Internal RTC STM32F1 - Want to store Time before RTC switches to VBAT

  • July 31, 2023
  • 11 replies
  • 3277 views

I am using STM32F103ZGT6 

I have initialize code From STMCubeMx with  settings-> PLL -ON , HSE -ON , LSE- ON  for Internal RTC

I have written code to retain date for internal RTC. I want the event when RTC switches from VDD to VBAT , as per my application requirement I want to store Time at which power offs and Time at which power come , Accordingly I want show how many hr or min or sec power was off .

I have checked the Datasheet it is written that Power Down Reset(PDR) occurs when VDD shifts to VBAT I have tried with that flag in the code but didn't work

Can you suggest me some solution for this   

 

This topic has been closed for replies.

11 replies

RhSilicon
Lead
July 31, 2023

When the power fails, there will be no power for the application to record data. Wouldn't it be simpler to use some means of temporary energy storage, perhaps a capacitor, or a small battery, so that the processor has time to perform tasks, in conjunction with a monitoring input pin to detect the lack of energy?

For example, when an emergency light is powered from the socket (220V), the light is off, because a detector (transistor Q1) monitors the socket input (indirectly, because the socket voltage was converted to a different value, in this case 6V) and when there is a power failure, the light turns on (the base of the transistor receives a current bias that allows current to flow from the collector to the emitter).

 

(Image source: https://www.eleccircuit.com/mini-emergency-light-circuit/)

In the case of the microcontroller, it could use an input pin with interruption. When there is a power failure, the pin changes state, the interruption occurs and the microcontroller registers the timedate.

On the other hand, when the power comes back, the microcontroller will turn on for the first time, so it can register the moment it was initialized, and then it goes into an infinite loop of waiting with nothing being done, or it could go into hibernation, so as not to waste energy for nothing.

Note that the example of the emergency light is just didactic, it would be necessary to adapt the voltages to the operating range of the microcontroller (STM32 generally operates with 3.3V).

Graduate II
July 31, 2023

Hello

Just continously write RTC time/date to some backup data registers, for example in 1 second interval. Then, when power is set off, the time of power loss stays on the backup register.

When device boots, check the backup register and calculate the difference to RTC value -> you got the down time. Then start saving RTC value again.

Br J.T

DeoyaniAuthor
Associate
July 31, 2023

I am trying to store time but not able to store it in backup register whenever I read the backup register (any backup register) I am getting current time only not the time at power loss or not the time which I Stored.

 

DeoyaniAuthor
Associate
August 3, 2023
if (HAL_UART_Receive(&huart1, (uint8_t *)RXBuffer, sizeof(RXBuffer), 5000))
{
RTC_GetTime(&_hours, &_minutes, &_seconds);
    RTC_StoreTime();
uint32_t backuptime;
// RTC_GetTime(&_hours, &_minutes, &_seconds);
  backuptime= HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR4>>16);
  backuptime |=HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR5&0xffff);
memcpy (&Time_s, &backuptime, 4);
 
RTC_GetTime(&Time_s.hours, &Time_s.minutes, &Time_s.second);
sprintf((char*)Time,"\nstored:%02d:%02d:%02d\n",Time_s.hours, Time_s.minutes, Time_s.second); 
HAL_UART_Transmit(&huart1, (uint8_t *)Time, sizeof(Time), 100);
HAL_Delay(2000);
 
}
 
I have written like this  but every time I am calling reading register I am getting current time not the time I have stored .
Below is the store time function
void RTC_StoreTime(void)
{
/* Store the Time in the backup registers */
RTC_TimeTypeDef sTime = {0};
uint32_t TimeToStore;

memcpy(&TimeToStore, &sTime, 3);
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR4, TimeToStore>>16);
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR4+1, TimeToStore&0xffff);



}
Graduate II
August 3, 2023

Hello

Re-think your code, especially what you must do and in which order. I think you should divide the upper part in two. First when program starts -> check the down time once. And then in loop update the time to backup data registers. Also, now the RTC_StoreTime doesnt read the RTC time at all, you just declare variable, but not read the current time to sTime.

But you will get it working :thumbs_up:

Br J.T

DeoyaniAuthor
Associate
August 4, 2023

I have changed the code and tried that After receiving "Store"  string from UART then get the time and write the time into backup register , 

I am reading the backup register  before the 

if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
 
Error_Handler();
}
then also I am getting current time in only not the time I have stored after receiving "store" from UART.
How to store it ,write it time  into backup register .when ever I read backup register I am getting current time .