| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- /**
- ******************************************************************************
- * @file rtc.c
- * @brief This file provides code for the configuration
- * of the RTC instances.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2021 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_DateTypeDef GetDate;
- uint8_t RTC_READ_Flag;
- /* USER CODE END 0 */
- RTC_HandleTypeDef hrtc;
- extern uint16_t Now_date;
- /* RTC init function */
- void MX_RTC_Init(void)
- {
- uint8_t count = 0;
- /** 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;
- if (HAL_RTC_Init(&hrtc) != HAL_OK)
- {
- rt_kprintf("HAL_RTC_Init failed !\r\n");
- }
- while (1)
- {
- HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);
- HAL_RTC_GetDate(&hrtc, &GetDate, RTC_FORMAT_BIN);
- rt_kprintf("%02d/%02d/%02d\r\n", 2000 + GetDate.Year, GetDate.Month, GetDate.Date);
- rt_kprintf("%02d:%02d:%02d\r\n\r\n", GetTime.Hours, GetTime.Minutes, GetTime.Seconds);
- //等待获取了当前时间再往下走
- if ((GetTime.Hours != 0) || (GetTime.Minutes != 0) || (GetTime.Seconds != 0))
- {
- Now_date = (GetDate.Year-21)*12+GetDate.Month;
- Now_date = Now_date << 8;
- Now_date |= GetDate.Date;
- break;
- }
- count++;
- //给出循环出口
- if (count > 3)
- {
- break;
- }
- }
- }
- 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 */
- }
- }
- /* USER CODE BEGIN 1 */
- void RTC_SetTime(uint8_t hour, uint8_t minute, uint8_t 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 */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|