我正在为我的 RTC 设置一个时间戳,当我立即读回它时,我注意到小时设置总是关闭 1 小时。我没有在 RTC 上启用夏令时模式,我不明白为什么小时设置关闭。我认为问题与夏令时有关,并尝试了 RTC 的 Add1H 和 Sub1H 设置,但这些设置对问题没有任何影响。
这是我的测试日志:
- Write time - 1662156522 (Timestamp is 02-09-2022 / 22:08:42)
- After converting from timestamp 22:08:42 (Timestamp converted just before setting RTC)
- 02-09-2022 / 21:08:56 (Read Time ())
我的实时时钟初始化()
- static void MX_RTC_Init(void)
- {
- /* USER CODE BEGIN RTC_Init 0 */
- /* USER CODE END RTC_Init 0 */
- RTC_TimeTypeDef sTime = {0};
- RTC_DateTypeDef sDate = {0};
- /* USER CODE BEGIN RTC_Init 1 */
- /* USER CODE END RTC_Init 1 */
- /** Initialize RTC Only
- */
- hrtc.Instance = RTC;
- 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();
- }
- /* USER CODE BEGIN Check_RTC_BKUP */
- if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != CALENDAR_INITIALIZED)
- {
- /* Write magic value RTC Backup data Register0 */
- HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, CALENDAR_INITIALIZED);
- /* USER CODE END Check_RTC_BKUP */
- /** 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();
- }
- 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();
- }
- /** Enable the WakeUp
- */
- __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF);
- if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 300, RTC_WAKEUPCLOCK_CK_SPRE_16BITS) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN RTC_Init 2 */
- }
- //Disable the Wakeup Timer on boot
- if (HAL_RTCEx_DeactivateWakeUpTimer(&hrtc))
- {
- Error_Handler();
- }
- /* USER CODE END RTC_Init 2 */
- }
我的设定时间 ()
- void RtcUTimeUpdateSntp(time_t timestamp)
- {
- RTC_DateTypeDef sdatestructure;
- RTC_TimeTypeDef stimestructure;
- struct tm ts;
- char temp[2] = {0, 0};
- /* convert SNTP time (seconds since 01-01-1900 to 01-01-1970)
- EPOCH_TIME_DIFF is equivalent to 70 years in sec
- calculated with www.epochconverter.com/date-difference
- This constant is used to delete difference between :
- Epoch converter (referenced to 1970) and SNTP (referenced to 1900) */
- /* convert time in yy/mm/dd hh:mm:sec */
- ts = *localtime(×tamp);
- ts.tm_isdst=1; //indicate DST is in effect
- printf("After converting from timestamp %02d:%02d:%02drn",ts.tm_hour,ts.tm_min, ts.tm_sec);
- /* convert date composants to hex format */
- sprintf(temp, "%ld", (unsigned long)(ts.tm_year - 100));
- sdatestructure.Year = strtol(temp, NULL, 16);
- sprintf(temp, "%ld", (unsigned long) (ts.tm_mon + 1));
- sdatestructure.Month = strtol(temp, NULL, 16);
- sprintf(temp, "%d", ts.tm_mday);
- sdatestructure.Date = strtol(temp, NULL, 16);
- /* dummy weekday */
- sdatestructure.WeekDay =0x00;
- if (HAL_RTC_SetDate(&hrtc, &sdatestructure, RTC_FORMAT_BCD) != HAL_OK)
- {
- Error_Handler();
- }
- /* convert time composants to hex format */
- sprintf(temp,"%d", ts.tm_hour);
- stimestructure.Hours = strtol(temp, NULL, 16);
- sprintf(temp,"%d", ts.tm_min);
- stimestructure.Minutes = strtol(temp, NULL, 16);
- sprintf(temp, "%d", ts.tm_sec);
- stimestructure.Seconds = strtol(temp, NULL, 16);
- stimestructure.DayLightSaving=1;
- if (HAL_RTC_SetTime(&hrtc, &stimestructure, RTC_FORMAT_BCD) != HAL_OK)
- {
- Error_Handler();
- }
- }
我的阅读时间 ()
- uint64_t RtcUGetTimeUnix(void)
- {
- RTC_TimeTypeDef RTC_Time;
- RTC_DateTypeDef RTC_Date;
- HAL_RTC_GetTime(&hrtc,&RTC_Time,RTC_FORMAT_BCD);
- HAL_RTC_GetDate(&hrtc,&RTC_Date,RTC_FORMAT_BCD);
- printf("%02x-%02x-20%02x / %02x:%02x:%02xrn",
- RTC_Date.Date, RTC_Date.Month, RTC_Date.Year,RTC_Time.Hours,RTC_Time.Minutes,RTC_Time.Seconds);
- HAL_RTC_GetTime(&hrtc,&RTC_Time,RTC_FORMAT_BIN);
- HAL_RTC_GetDate(&hrtc,&RTC_Date,RTC_FORMAT_BIN);
- uint8_t hh = RTC_Time.Hours;
- uint8_t mm = RTC_Time.Minutes;
- uint8_t ss = RTC_Time.Seconds;
- uint8_t d = RTC_Date.Date;
- uint8_t m = RTC_Date.Month;
- uint16_t y = RTC_Date.Year;
- uint16_t yr = (uint16_t)(y+2000-1900);
- struct tm tim = {0};
- tim.tm_year = yr;
- tim.tm_mon = m - 1;
- tim.tm_mday = d;
- tim.tm_hour = hh;
- tim.tm_min = mm;
- tim.tm_sec = ss;
- time_t currentTime = {0};
- currentTime = mktime(&tim);
- /*
- tim = gmtime(currentTime);
- currentTime = mktime()
- */
- /* Return number of seconds since Unix Epoch (1/1/1970 00:00:00). */
- return (uint64_t)currentTime;
- }
0
|
1个回答
|
|
|