实验目的:
通过STM32CubeMX进行简单配置生成HAL库实现内部晶振RTC。
一、STM32CubeMX配置如下:
1.时钟树配置:
因为板子没有外部RTC晶振,所以只能使用内部32KHz晶振,时钟树配置如下:
2.RTC配置:
(1)关于Asynchronous Predivider value 和 Synchronous Predivider value填写多少,请看下面:
因为内部晶振是32KHz,所以毫无疑问应该选择LSI=32KHz即:
Asynchronous Predivider value :127
Synchronous Predivider value :249
(2)关于Hours、Minutes等这些初值没啥好疑问的,直接填写,但是Year需要注意一下,它是从1970年开始算起的,所以比如填写50就是2020年的意思。
二、STM32CubeMX配置生成的HAL库如下:
/** ****************************************************************************** * File Name : RTC.c * Description : This file provides code for the configuration * of the RTC instances. ****************************************************************************** * @attention * * <h2><center>© Copyright (c) 2020 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "rtc.h" /* USER CODE BEGIN 0 */ RTC_TimeTypeDef GetTime; //µ¥Æ¬»úÄÚ²¿RTC¶Áȡʱ¼ä RTC_DateTypeDef GetDate; //µ¥Æ¬»úÄÚ²¿RTC¶ÁÈ¡ÈÕÆÚ uint8_t RTC_READ_Flag; /* USER CODE END 0 */ RTC_HandleTypeDef hrtc; /* RTC init function */ void MX_RTC_Init(void) { RTC_TimeTypeDef sTime = {0}; RTC_DateTypeDef sDate = {0}; /** Initialize RTC Only */ hrtc.Instance = RTC; hrtc.Init.HourFormat = RTC_HOURFORMAT_24; hrtc.Init.AsynchPrediv = 127; hrtc.Init.SynchPrediv = 249; 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; 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 = 19; sTime.Minutes = 07; sTime.Seconds = 00; sTime.SubSeconds = 0; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK) { Error_Handler(); } sDate.WeekDay = RTC_WEEKDAY_WEDNESDAY; sDate.Month = RTC_MONTH_SEPTEMBER; sDate.Date = 8; sDate.Year = 50; if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK) { Error_Handler(); } } void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle) { if(rtcHandle->Instance==RTC) { /* USER CODE BEGIN RTC_MspInit 0 */ /* USER CODE END RTC_MspInit 0 */ /* RTC clock enable */ __HAL_RCC_RTC_ENABLE(); __HAL_RCC_RTCAPB_CLK_ENABLE(); /* USER CODE BEGIN RTC_MspInit 1 */ /* USER CODE END RTC_MspInit 1 */ } } void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle) { if(rtcHandle->Instance==RTC) { /* USER CODE BEGIN RTC_MspDeInit 0 */ /* USER CODE END RTC_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_RTC_DISABLE(); __HAL_RCC_RTCAPB_CLK_DISABLE(); /* USER CODE BEGIN RTC_MspDeInit 1 */ /* USER CODE END RTC_MspDeInit 1 */ } }
三、时间日期获取设置等:
1、自己重绘一个设置time的函数:
/* USER CODE BEGIN 1 */ void RTC_SetTime(u8 hour, u8 minute, u8 second) { /*##-2- Configure the Time #################################################*/ GetTime.Hours = hour; GetTime.Minutes = minute; GetTime.Seconds = second; // GetTime.TimeFormat = RTC_HOURFORMAT12_AM; GetTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; GetTime.StoreOperation = RTC_STOREOPERATION_RESET; if(HAL_RTC_SetTime(&hrtc,&GetTime,RTC_FORMAT_BIN) != HAL_OK) { } } /* USER CODE END 1 */
2、调用函数实现RTC:
1)首先初始化RTC时钟:
MX_RTC_Init();
2)接着设置time时间:
RTC_SetTime(11,59,50); //设置时间为11时59分50秒
3.然后调用HAL库函数实现time和date获取,有个细节要注意一下,需要先获取日期再获取时间,亲测直接获取时间会有问题:
HAL_RTC_GetDate(&hrtc, &GetDate, RTC_FORMAT_BIN);//更新日期 HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);//更新时间
*转自CSDN,原文地址https://blog.csdn.net/qq_37449342/article/details/108491097