STM32WB55 RTC万年历

本文详细介绍了STM32微控制器中RTC模块的初始化配置过程,包括使用HAL库进行时间与日期设置的方法,以及如何显示当前的时间和日期。通过实例代码展示了如何设置RTC的异步和同步预分频器,以及如何在软复位后判断RTC是否为首次初始化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifndef __RTC_H_
#define __RTC_H_

#include <stdint.h>
#if 0
     用外部低速时钟作为时钟源
#endif
#define RTC_CLOCK_SOURCE_LSE

#ifdef RTC_CLOCK_SOURCE_LSI
#define RTC_ASYNCH_PREDIV    0x7C
#define RTC_SYNCH_PREDIV     0xF9
#endif
#ifdef RTC_CLOCK_SOURCE_LSE
#define RTC_ASYNCH_PREDIV  0x7F
#define RTC_SYNCH_PREDIV   0x00FF
#endif

void RTC_Init(void);
void RTC_CalendarShow(void);

#endif
#include <string.h>
#include <stdio.h>
#include "Rtc.h"
#include "stm32wbxx_hal.h"
#include "app_conf.h"
#include "ble_dbg_conf.h"
#include "dbg_trace.h"

/* Private variables ---------------------------------------------------------*/
RTC_HandleTypeDef hrtc;
#define RTC_DBG			APP_DBG_MSG

/**
  * @brief RTC Initialization Function
  * @param None
  * @retval None
  */
void 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 = RTC_ASYNCH_PREDIV;
  hrtc.Init.SynchPrediv = RTC_SYNCH_PREDIV;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
   // Error_Handler();
  }
#if 0
	通过读备份寄存器的值判断是不是上电后RTC首次初始化
#endif
  if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2)
  {
  #if 0
  初始化RTC时钟模块 设置时间和日期
  #endif
  sTime.Hours = 0x0D;
  sTime.Minutes = 0x0;
  sTime.Seconds = 0x0;
  sTime.SubSeconds = 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_TUESDAY;
  sDate.Month = RTC_MONTH_AUGUST;
  sDate.Date = 0x18;
  sDate.Year = 0x20;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
  {
//    Error_Handler();
  }
#if 0
    给备份寄存器写入一个值,用于软复位判断
#endif
  HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x32F2);
  }
  else
  {
    /* Check if the Power On Reset flag is set */
    if (__HAL_RCC_GET_FLAG(RCC_FLAG_BORRST) != RESET)
    {
    }

    /* Check if Pin Reset flag is set */
    if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
    {
    }
  }
  /* Clear source Reset Flag */
  __HAL_RCC_CLEAR_RESET_FLAGS();
}
/**
  * @brief  Display the current time and date.
  * @param  showtime : pointer to buffer
  * @param  showdate : pointer to buffer
  * @retval None
  */
void RTC_CalendarShow(void)
{
  RTC_DateTypeDef sdatestructureget;
  RTC_TimeTypeDef stimestructureget;

  /* Get the RTC current Time */
  HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
  /* Get the RTC current Date */
  HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
  /* Display time Format : hh:mm:ss */
  RTC_DBG("\nTime %02d:%02d:%02d.\t", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
  /* Display date Format : mm-dd-yy */
  RTC_DBG("Data %2d-%02d-%02d.\n\n", 2000 + sdatestructureget.Year, sdatestructureget.Month, sdatestructureget.Date);
}

void StartDefaultTask(void *argument)
{
	MAIN_DBG("2020/08/18 proj run!\r\n");
  /* Infinite loop */
  for(;;)
  {
		//osThreadFlagsWait(1,osFlagsWaitAll,osWaitForever);
		osDelay(1000);
		RTC_CalendarShow();
  }
  /* USER CODE END 5 */ 
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值