Home › Forums › Mastering STM32 book support forum › RTC ERROR (Lost DATE when reset)
- This topic has 0 replies, 1 voice, and was last updated 7 years, 9 months ago by Skalibur.
-
AuthorPosts
-
February 22, 2017 at 12:44 am #6276SkaliburParticipant
Hello everyone.
I need help with RTC, im trying to implement RTC on STM32f1.
I configure with CubeMX
12345678910111213141516171819RTC_HandleTypeDef hrtc;RTC_TimeTypeDef sTime;RTC_DateTypeDef sDate;/* RTC init function */void MX_RTC_Init(void){/**Initialize RTC Only*/hrtc.Instance = RTC;hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE;if (HAL_RTC_Init(&hrtc) != HAL_OK){Error_Handler();}}Later in my main
12345678910111213141516171819202122232425262728293031323334353637383940414243case ‘q’: // Read{HAL_RTC_GetDate(&hrtc, &sDate, FORMAT_BCD);HAL_RTC_GetTime(&hrtc, &sTime, FORMAT_BCD);fecha[0] = (sDate.Date >> 4) + 0x30;fecha[1] = (sDate.Date & 0x0F) + 0x30;fecha[3] = (sDate.Month >> 4) + 0x30;fecha[4] = (sDate.Month & 0x0F) + 0x30;fecha[6] = (sDate.Year >> 4) + 0x30;fecha[7] = (sDate.Year & 0x0F) + 0x30;hora[0] = (sTime.Hours >> 4) + 0x30;hora[1] = (sTime.Hours & 0x0F) + 0x30;hora[3] = (sTime.Minutes >> 4) + 0x30;hora[4] = (sTime.Minutes & 0x0F) + 0x30;hora[6] = (sTime.Seconds >> 4) + 0x30;hora[7] = (sTime.Seconds & 0x0F) + 0x30;hora[8] = 0x09;hora[9] = 0x0A;hora[10] = 0x0D;HAL_UART_Transmit(&huart2, (uint8_t*) fecha, strlen(fecha), HAL_MAX_DELAY);HAL_UART_Transmit(&huart2, (uint8_t*) hora, strlen(hora), HAL_MAX_DELAY);}break;case ‘Q’: // Write{sTime.Hours = 0x16;sTime.Minutes = 0x10;sTime.Seconds = 0x00;if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK){Error_Handler();}sDate.WeekDay = RTC_WEEKDAY_TUESDAY;sDate.Month = RTC_MONTH_FEBRUARY;sDate.Date = 0x21;sDate.Year = 0x17;if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK){Error_Handler();}}break;I have a 3v battery to vBat CR2032 and test on Therminal. (For unknown reason can’t use debug on eclipse) but this is the problem.
At first init if press “q” (Read sTime and sDate) return this:
01/01/00 00:04:37 // Wrong DATE Correct Time
If press “Q” (to write) and then “q” (to read) return this:
21/02/17 00:04:10 // Correct Date and TIME
If disconnect power supply of STM32, and then connect and press “q” (Read sTime and sDate) return this:
01/01/00 00:05:01 // Lost DATE but TIME its correct!!
Resume, I need write DATE every time after reset or lost power, but TIME always its correct.
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK) { Error_Handler(); }
Why lost DATE?
-
AuthorPosts
- You must be logged in to reply to this topic.