STM32CubeMX configuration RTC-based on (STM32G070+STM32CubeMX+HAL library+RTC)

Purpose:

Simple configuration through STM32CubeMX to generate HAL library to realize internal crystal oscillator RTC.

1. The configuration of STM32CubeMX is as follows:

1. Clock tree configuration:

Because the board does not have an external RTC crystal oscillator, it can only use the internal 32KHz crystal oscillator. The clock tree configuration is as follows:
Insert picture description here

2. RTC configuration:

Insert picture description here

(1) For how much to fill in Asynchronous Predivider value and Synchronous Predivider value, please see below:

Insert picture description here
Insert picture description here

Because the internal crystal oscillator is 32KHz, there is no doubt that LSI=32KHz should be selected, namely:

Asynchronous Predivider value :127
Synchronous Predivider value :249

(2) There is no question about the initial values ​​of Hours, Minutes, etc., just fill in them directly, but Year needs to pay attention to it. It is calculated from 1970, so for example, filling in 50 means 2020.

2. The HAL library generated by STM32CubeMX configuration is as follows:

/**
  ******************************************************************************
  * File Name          : RTC.c
  * Description        : This file provides code for the configuration
  *                      of the RTC instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; 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 */
  }
} 

3. Time and date acquisition settings, etc.:

1. Redraw a function that sets time by yourself:

/* 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. Call function to realize RTC:

1. First initialize the RTC clock:

  MX_RTC_Init();

2. Then set the time:

 RTC_SetTime(11,59,50);   //设置时间为11时59分50秒

3. Then call the HAL library function to obtain the time and date. There is a detail to pay attention to. You need to get the date first and then get the time. There will be problems in obtaining the time directly from the test:

      HAL_RTC_GetDate(&hrtc, &GetDate, RTC_FORMAT_BIN);//更新日期
      HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);//更新时间

Perfect, call it a day! ! !

Guess you like

Origin blog.csdn.net/qq_37449342/article/details/108491097