系列文章目录
STM32CubeIDE(CUBE-MX hal库)----初尝点亮小灯
STM32CubeIDE(CUBE-MX hal库)----按键控制
STM32CubeIDE(CUBE-MX hal库)----串口通信
STM32CubeIDE(CUBE-MX hal库)----定时器
STM32CubeIDE(CUBE-MX hal库)----蓝牙模块HC-05(详细配置)
前言
RTC是一个独立的定时器,可为系统提供时钟和日历的功能
一、可视化配置
1.配置RCC时钟源,LSE外部低速时钟,HSE外部高速时钟
2.配置RTC,Activate Clock Source 激活时钟源,Activate calendar激活日历,RTC全局中断
3.设置参数,设置当前的时间。BCD data format BCD码进制,23为2023年的后两位
4.使能串口,将日期发送到上位机
5.时钟源设置, 设置RTC时钟为LSE
二、修改代码
1.头文件加入<stdio.h>因为加入了printf
/* USER CODE BEGIN Includes */
#include <stdio.h>/* USER CODE END Includes */
2.在USER CODE BEGIN 0中加入重定向代码
/* USER CODE BEGIN 0 */
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endifPUTCHAR_PROTOTYPE
{HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1,0xFFFF);return ch;
}int fputc(int ch,FILE *f)
{HAL_UART_Transmit(&huart1,(uint8_t*)&ch,1,0xffff);return ch;
}/* USER CODE END 0 */
3.在while (1)中加入获取时间和日期代码
while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 *//* Get the RTC current Time */HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);/* Get the RTC current Date */HAL_RTC_GetDate(&hrtc, &GetData, RTC_FORMAT_BIN);/* Display date Format : yy/mm/dd */printf("%02d/%02d/%02d\r\n",2000 + GetData.Year, GetData.Month, GetData.Date);/* Display time Format : hh:mm:ss */printf("%02d:%02d:%02d\r\n",GetTime.Hours, GetTime.Minutes, GetTime.Seconds);printf("\r\n");HAL_Delay(1000);}
三、实验现象
实现时钟的
四、扩展
RTC掉电或者复位时时间会进行重置,如果不进行重置则加上改动下面代码
void MX_RTC_Init(void)
{/* USER CODE BEGIN RTC_Init 0 */RTC_TimeTypeDef time; //时间结构体参数RTC_DateTypeDef datebuff; //日期结构体参数/* USER CODE END RTC_Init 0 */RTC_TimeTypeDef sTime = {0};RTC_DateTypeDef DateToUpdate = {0};/* USER CODE BEGIN RTC_Init 1 */__HAL_RCC_BKP_CLK_ENABLE(); //开启后备区域时钟__HAL_RCC_PWR_CLK_ENABLE(); //开启电源时钟/* USER CODE END RTC_Init 1 *//**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();}/* USER CODE BEGIN Check_RTC_BKUP */if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR1)!= 0x5051){/* USER CODE END Check_RTC_BKUP *//**Initialize RTC and set the Time and Date */sTime.Hours = 0x23;sTime.Minutes = 0x28;sTime.Seconds = 0x20;if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK){Error_Handler();}DateToUpdate.WeekDay = RTC_WEEKDAY_THURSDAY;DateToUpdate.Month = RTC_MONTH_DECEMBER;DateToUpdate.Date = 0x7;DateToUpdate.Year = 0x23;if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK){Error_Handler();}/* USER CODE BEGIN RTC_Init 2 */__HAL_RTC_SECOND_ENABLE_IT(&hrtc,RTC_IT_SEC); //开启RTC时钟秒中断datebuff = DateToUpdate; //把日期数据拷贝到自己定义的data中HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x5051);//向指定的后备区域寄存器写入数据HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, (uint16_t)datebuff.Year);HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR3, (uint16_t)datebuff.Month);HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR4, (uint16_t)datebuff.Date);HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR5, (uint16_t)datebuff.WeekDay);}else{datebuff.Year = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2);datebuff.Month = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);datebuff.Date = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR4);datebuff.WeekDay = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR5);DateToUpdate = datebuff;if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK){Error_Handler();}__HAL_RTC_SECOND_ENABLE_IT(&hrtc,RTC_IT_SEC); //开启RTC时钟秒中断 }}
参考文章:https://blog.csdn.net/as480133937/article/details/105741893