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