cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F405 RTC counting more than 24h!

Bruno Loureiro
Associate II
Posted on April 27, 2018 at 15:47

The RTC continues to count after 23:59:59, just changes the day at 39:59:59, what's the problem?

I use HAL Library.

RTC Output:

2018-04-06 weak5, 23:59:57

2018-04-06 weak5, 23:59:58

2018-04-06 weak5, 23:59:59

2018-04-06 weak5, 24:00:00

2018-04-06 weak5, 24:00:01

2018-04-06 weak5, 24:00:02

2018-04-06 weak5, 24:00:03

2018-04-06 weak5, 24:00:04
13 REPLIES 13
john doe
Lead
Posted on April 27, 2018 at 17:47

Looks like you have Binary Coded Decimal, when you want Bin.

there's bin2bcd and bcd2bin in the HAL RTC code file.  I forget if its in .h or .c, but they are there.

henry.dick
Senior II
Posted on April 27, 2018 at 19:10

Let's think through it logically.

It is definitely not a hardware problem: the same hardware has been used by countless others in countless applications flawlessly.

It is definitely not a software problem either you you seem to trust your code so much that you didn't even think posting it could help others help you. I'm 100 percent with you on that.

So here it is: we have a bug that is not hardware nor software related. Where else could the bug be hiding? In our minds?

Bruno Loureiro
Associate II
Posted on April 27, 2018 at 19:44

At this moment I have no idea, you're right follow the code below.

 /* Infinite loop */

while (1){

   HAL_RTC_GetTime(&hrtc, &stimestructureget, FORMAT_BCD);

   if(stimestructureget.Seconds!=lastSecond)

   {

      HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_9);

      HAL_RTC_GetDate(&hrtc, &sdatestructureget, FORMAT_BCD);

      leng=sprintf(printbuf,'20%02x-%02x-%02x weak%x, %02x:%02x:%02x\n\r',sdatestructureget.Year,       sdatestructureget.Month, sdatestructureget.Date,sdatestructureget.WeekDay,

      stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);

      HAL_UART_Transmit(&huart1,printbuf,leng,10000);

      lastSecond=stimestructureget.Seconds;

   }

}

/* RTC init function */

static void MX_RTC_Init(void)

{

   RTC_TimeTypeDef sTime;

   RTC_DateTypeDef sDate;

   /**Initialize RTC Only /

   hrtc.Instance = RTC;

   if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){

      hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

      hrtc.Init.AsynchPrediv = 127;

      hrtc.Init.SynchPrediv = 255;

      hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

      hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

      hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

   if (HAL_RTC_Init(&hrtc) != HAL_OK)

   {

      _Error_Handler(__FILE__, __LINE__);

   }

   /**Initialize RTC and set the Time and Date

   */

   sTime.Hours = 0x0;

   sTime.Minutes = 0x0;

   sTime.Seconds = 0x0;

   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_MONDAY;

   sDate.Month = RTC_MONTH_JANUARY;

   sDate.Date = 0x1;

   sDate.Year = 0x0;

   if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)

   {

      _Error_Handler(__FILE__, __LINE__);

   }

   HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);

   }

}

Posted on April 27, 2018 at 17:56

Hi,

I have already switched between Binary and BCD, but I always get the same bug.

Notice that I only have the problem in the Hour, and the day does not change either. 

I used this code in the past in STM32F407 and it worked fine.

Elivelton Santos
Associate
Posted on April 27, 2018 at 21:50

Which STM32 exactly are you using?

Some STM32 only have RTC 'timer handle' that only counter 'the time', it doesn't count days (GetDate is emulated by software).
Bruno Loureiro
Associate II
Posted on April 27, 2018 at 21:59

Hi,

STM32F405RGT6 I think he has a calendar ... Am I right?

Posted on April 27, 2018 at 22:44

Read out the RTC registers and check/post them.

I see that in your init code there's RTC_HOURFORMAT_24 mentioned, but the symptoms are similar to the AM/PM (12-hour) cycle.

JW

Posted on April 28, 2018 at 03:42

You are right. I can't see nothing wrong.

Bruno Loureiro
Associate II
Posted on April 28, 2018 at 14:16

Hi, You gave me a good idea, I changed the initialization to hrtc.Init.HourFormat = RTC_HOURFORMAT_12;

And in this configuration works :o  .... but I preferred the format 24h, I will do some more tests and put here the results.