2.cubeide配置:
如图,配置好DEBUG和RCC时钟之后,如图配置RTC定时器:
其他部分如串口沿用上一篇帖子的配置。
3.编辑代码
在MX_RTC_Init()的定义可以看到
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.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();
}
/* USER CODE BEGIN Check_RTC_BKUP */
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x20;
sTime.Minutes = 0x10;
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_APRIL;
sDate.Date = 0x29;
sDate.Year = 0x0;
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的参数全部被封装在stm32u0xx_hal_rtc.h文件的数个结构体中,还写有每个参数的注释,可以直接从中调用。
typedef struct
{
uint8_t Hours; /*!< Specifies the RTC Time Hour.
This parameter must be a number between Min_Data = 0 and Max_Data = 12
if the RTC_HourFormat_12 is selected.
This parameter must be a number between Min_Data = 0 and Max_Data = 23
if the RTC_HourFormat_24 is selected */
uint8_t Minutes; /*!< Specifies the RTC Time Minutes.
This parameter must be a number between Min_Data = 0 and Max_Data = 59 */
uint8_t Seconds; /*!< Specifies the RTC Time Seconds.
This parameter must be a number between Min_Data = 0 and Max_Data = 59 */
uint8_t TimeFormat; /*!< Specifies the RTC AM/PM Time.
This parameter can be a value of @ref RTC_AM_PM_Definitions */
uint32_t SubSeconds; /*!< Specifies the RTC_SSR RTC Sub Second register content.
This field is not used by HAL_RTC_SetTime.
If the free running 32 bit counter is not activated (mode binary none)
- This parameter corresponds to a time unit range between [0-1] Second
with [1 Sec / SecondFraction +1] granularity
else
- This parameter corresponds to the free running 32 bit counter. */
uint32_t SecondFraction; /*!< Specifies the range or granularity of Sub Second register content
corresponding to Synchronous pre-scaler factor value (PREDIV_S)
This parameter corresponds to a time unit range between [0-1] Second
with [1 Sec / SecondFraction +1] granularity.
This field will be used only by HAL_RTC_GetTime function */
uint32_t DayLightSaving; /*!< This interface is deprecated. To manage Daylight
Saving Time, please use HAL_RTC_DST_xxx functions */
uint32_t StoreOperation; /*!< This interface is deprecated. To manage Daylight
Saving Time, please use HAL_RTC_DST_xxx functions */
} RTC_TimeTypeDef;
/**
* @brief RTC Date structure definition
*/
typedef struct
{
uint8_t WeekDay; /*!< Specifies the RTC Date WeekDay.
This parameter can be a value of @ref RTC_WeekDay_Definitions */
uint8_t Month; /*!< Specifies the RTC Date Month (in BCD format).
This parameter can be a value of @ref RTC_Month_Date_Definitions */
uint8_t Date; /*!< Specifies the RTC Date.
This parameter must be a number between Min_Data = 1 and Max_Data = 31 */
uint8_t Year; /*!< Specifies the RTC Date Year.
This parameter must be a number between Min_Data = 0 and Max_Data = 99 */
} RTC_DateTypeDef;
/**
* @brief RTC Alarm structure definition
*/
typedef struct
{
RTC_TimeTypeDef AlarmTime; /*!< Specifies the RTC Alarm Time members */
uint32_t AlarmMask; /*!< Specifies the RTC Alarm Masks.
This parameter can be a value of @ref RTC_AlarmMask_Definitions */
uint32_t AlarmSubSecondMask; /*!< Specifies the RTC Alarm SubSeconds Masks.
if Binary mode is RTC_BINARY_ONLY or is RTC_BINARY_MIX
This parameter can be a value of
@ref RTCEx_Alarm_Sub_Seconds_binary_Masks_Definitions
else if Binary mode is RTC_BINARY_NONE
This parameter can be a value of @ref RTC_Alarm_Sub_Seconds_Masks_Definitions */
uint32_t BinaryAutoClr; /*!< Clear synchronously counter (RTC_SSR) on binary alarm.
RTC_ALARMSUBSECONDBIN_AUTOCLR_YES must only be used if Binary mode is
RTC_BINARY_ONLY
This parameter can be a value of
@ref RTCEx_Alarm_Sub_Seconds_binary_Clear_Definitions */
uint32_t AlarmDateWeekDaySel; /*!< Specifies the RTC Alarm is on Date or WeekDay.
This parameter can be a value of @ref RTC_AlarmDateWeekDay_Definitions */
uint8_t AlarmDateWeekDay; /*!< Specifies the RTC Alarm Date/WeekDay.
If the Alarm Date is selected, this parameter must be set to
a value in the 1-31 range.
If the Alarm WeekDay is selected, this parameter can be a value of
@ref RTC_WeekDay_Definitions */
uint32_t FlagAutoClr; /*!< Specifies the alarm trigger generation. This feature is meaningful to avoid any
RTC software execution after configuration.
When FlagAutoClr is set in interrupt mode, EXTI is configured as EVENT instead of
interrupt to avoid useless IRQ handler execution.
This parameter can be a value of @ref RTC_ALARM_Flag_AutoClear_Definitions */
uint32_t Alarm; /*!< Specifies the alarm .
This parameter can be a value of @ref RTC_Alarms_Definitions */
} RTC_AlarmTypeDef;
新建两个参数以调用出RTC的数值
/* USER CODE BEGIN PM */
RTC_DateTypeDef Get_Data;
RTC_TimeTypeDef Get_Time;
/* USER CODE END PM */
在主函数中调用
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_RTC_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if (HAL_RTC_GetTime(&hrtc, &Get_Time, RTC_FORMAT_BIN) == HAL_OK)
{
char buffer[20];
sprintf(buffer, "Time: %02d:%02d:%02d\n", Get_Time.Hours, Get_Time.Minutes, Get_Time.Seconds);
HAL_UART_Transmit(&hlpuart1, (uint8_t *)buffer, strlen(buffer), 10);
}
//HAL_RTC_GetTime(&hrtc, &Get_Time, RTC_FORMAT_BIN);
//HAL_UART_Transmit(&hlpuart1,&Get_Time.Seconds,5,2);
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
按理说这个代码是可以运行的,但是在这里遇到了一个严重的BUG,RTC的时间不会工作
串口会一直发回一个固定设置好的时间。。。。
翻了好久的手册都没有消息,最后在一篇帖子里发现,STM32的RTC需要读取TIME后读取DATE,否则会一直卡住
之后稍微修改就可以得到准确的时间值了:
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if (HAL_RTC_GetTime(&hrtc, &Get_Time, RTC_FORMAT_BIN) == HAL_OK)
{
if (HAL_RTC_GetDate(&hrtc, &Get_Data, RTC_FORMAT_BIN) == HAL_OK)
{
char buffer[20];
sprintf(buffer, "Time: %02d:%02d:%02d\n,Date: %02d/%02d/%02d WeekDay: %d\n", Get_Time.Hours, Get_Time.Minutes, Get_Time.Seconds, Get_Data.Date,Get_Data.Month,Get_Data.Year,Get_Data.WeekDay);
HAL_UART_Transmit(&hlpuart1, (uint8_t *)buffer, strlen(buffer), 10);
}
}
//HAL_RTC_GetTime(&hrtc, &Get_Time, RTC_FORMAT_BIN);
//HAL_UART_Transmit(&hlpuart1,&Get_Time.Seconds,5,2);
HAL_Delay(1000);
}
之后就可以得到精准的RTC定时器输出的时间了: