本帖最后由 HuaWng 于 2024-10-12 09:51 编辑
RTC的英文全称是Real-Time Clock,即实时时钟。它是一个独立的定时器,拥有连续计数的计数器,可以在相应软件配置下提供时钟日历的功能。通过修改计数器的值,可以重新设置系统当前的时间和日期。
借助于ST优异的生态,在STM32CubeIDE中可以方便的透过图形化配置界面来开启RTC 日历功能,包含了日期与时间。
自动生成的RTC初始化代码如下:
static void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_PrivilegeStateTypeDef privilegeState = {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.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
hrtc.Init.BinMode = RTC_BINARY_NONE;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
privilegeState.rtcPrivilegeFull = RTC_PRIVILEGE_FULL_NO;
privilegeState.backupRegisterPrivZone = RTC_PRIVILEGE_BKUP_ZONE_NONE;
privilegeState.backupRegisterStartZone2 = RTC_BKP_DR0;
privilegeState.backupRegisterStartZone3 = RTC_BKP_DR0;
if (HAL_RTCEx_PrivilegeModeSet(&hrtc, &privilegeState) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN Check_RTC_BKUP */
/* 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_FRIDAY;
sDate.Month = RTC_MONTH_OCTOBER;
sDate.Date = 0x11;
sDate.Year = 0x24;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
在之前代码基础上,增加RTC时间,日期打印功能:
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(LD1_GPIO_PORT, LD1_Pin);
/* Insert delay 100 ms */
//HAL_Delay(100);
HAL_GPIO_TogglePin(LD2_GPIO_PORT, LD2_PIN);
/* Insert delay 100 ms */
//HAL_Delay(100);
HAL_GPIO_TogglePin(LD3_GPIO_PORT, LD3_PIN);
/* Insert delay 100 ms */
//HAL_Delay(100);
HAL_GPIO_TogglePin(LD4_GPIO_PORT, LD4_PIN);
/* Insert delay 100 ms */
HAL_Delay(1000);
//HAL_UART_Transmit(&huart4, "\r\nHello 21ic, Hello STM32H7S78-DK!", sizeof("\r\nHello 21ic, Hello STM32H7S78-DK!"), 0xFFFF);
/* Get the RTC current Time */
HAL_RTC_GetTime(&hrtc, &RTC_MY_TIME, RTC_FORMAT_BIN);
/* Get the RTC current Date */
HAL_RTC_GetDate(&hrtc, &RTC_MY_DATE, RTC_FORMAT_BIN);
/* Display date Format : mm-dd-yyyy */
sprintf((char *)uart_dateStr, "\r\n%2d-%2d-%2d", 2000 + RTC_MY_DATE.Year, RTC_MY_DATE.Month, RTC_MY_DATE.Date);
/* Display time Format : hh:mm:ss */
sprintf((char *)uart_timeStr, " %2d:%2d:%2d", RTC_MY_TIME.Hours, RTC_MY_TIME.Minutes, RTC_MY_TIME.Seconds);
//HAL_UART_Transmit(&huart4, "\r\n", sizeof("\r\n"), 0xFFFF);
HAL_UART_Transmit(&huart4, uart_dateStr, sizeof(uart_dateStr), 0xFFFF);
//HAL_UART_Transmit(&huart4, " ", sizeof(" "), 0xFFFF);
HAL_UART_Transmit(&huart4, uart_timeStr, sizeof(uart_timeStr), 0xFFFF);
}
/* USER CODE END 3 */
}
下载到板子后,打开串口监视器,可以看到日期与时间正常打印出来!
|