stm32 利用备份寄存器保存实时时钟数据

本文介绍了如何在STM32微控制器中使用备份寄存器来保存实时时钟(RTC)数据,确保在设备复位或掉电时仍能维持RTC的正常运行,实现低功耗模式下的时钟保持。
摘要由CSDN通过智能技术生成

在实际应用中,会出现许多复位或者掉电的情况,下面提供了一种方法使即使是在掉电和复位事件发生时,仍旧可以利用低功耗模式继续对于实时时钟进行供电,保证时钟的正常运行!


//bsp_rtc.h
#ifndef _BSP_RTC_H
#define _BSP_RTC_H
#include "misc.h"


/*全局变量*/	   
 
uint8_t RTCInterruptFlag=0;	 //RTC 中断标志

uint32_t RTC_TimeNum=0;			  // 设置时间变量


uint16_t Year;

uint8_t Month;

uint8_t Day;

/* RTC hardware init*/
void RTC_NVIconfigration(void);

void RTC_configration(void);

void RTC_Init(void);

/*日历及时间输入*/
uint8_t RTC_InputTime(uint32_t border);

uint32_t RTC_TimeCollate(void);

void RTC_RxIntHandler(void); 

 /*获得年月日*/
uint16_t GetYear();

uint8_t GetMonth();

uint8_t GetDay();

void CalenderSet(void);

void  CalenderCount(void);

static uint8_t Choice_MonthDay(uint16_t temp_year,uint8_t temp_month);

/*显示*/
void RTC_TimeDisplay(uint32_t TimeVar);

 /*测试*/
void Text_RTC(void); 


#endif/*_BSP_RTC_H*/
 


bsp_rtc.c

#include "bsp_rtc.h"

#define RTCClockSource_LSE

uint32_t TimeDisplay=0;	// 用于显示测试 每次进入中断改变  

/***************************************************************************************************
*\Function      RTC_NVIconfigration()
*\Description   设置RTC中断优先级
*\Parameter     void
*\Return        void
*\Note          
*\Log          	2014年7月24日
***************************************************************************************************/
void RTC_NVIconfigration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);	  //已经在bsp_usart.c进行了设置

    /* Enable the RTC Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;					//配置外部中断源(秒中断)
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

/***************************************************************************************************
*\Function      RTC_configration(void)
*\Description   RTC配置函数
*\Parameter     void
*\Return        void
*\Note          
*\Log          	2014年7月24日
***************************************************************************************************/
void RTC_configration(void)
{
    /* 使能 PWR 和 BKP 的时钟 */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

    PWR_DeInit();
    /* 允许访问BKP区域 */
    PWR_BackupAccessCmd(ENABLE);

    /* 复位BKP */
    BKP_DeInit();

#ifdef RTCClockSource_LSI
    /* 使能内部RTC时钟 */
    RCC_LSICmd(ENABLE);
    /* 等待RTC内部时钟就绪 */
    while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
    {
    }
    /* 选择RTC内部时钟为RTC时钟 */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
#elif defined	RTCClockSource_LSE
    /* 使能RTC外部时钟 */
    RCC_LSEConfig(RCC_LSE_ON);
    /* 等待RTC外部时钟就绪 */
    while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
    {
    }

    /* 选择RTC外部时钟为RTC时钟 */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
#endif
    /* 使能RTC时钟 */
    RCC_RTCCLKCmd(ENABLE);


#ifdef RTCClockOutput_Enable
    /* Disable the Tamper Pin 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值