在坛里翻了一下以贴子,在外振上加了2个电容,解决了起振难的问题;同时按照香主的意见,把VCC至VBAT的二极管去掉了,硬件如下图:
代码部分如下:
#include "Includes.h"
#include "Constants.h"
#include "Functions.h"
#include "Globals.h"
#define RTC_BKP_VALUE 0xA5A5
/*****************************************************************
@Fn: ConfigRTC()
@Br: 配置RTC
@Pa: 无
@Rt: 无
@Sp: 无
*****************************************************************/
void ConfigRTC( void ){
RCC_APB1PeriphClockCmd( RCC_APB1Periph_PWR, ENABLE ); /* Enable the PWR clock */
PWR_BackupAccessCmd( ENABLE ); /* Allow access to RTC */
if( RTC_ReadBackupRegister( RTC_BKP_DR0 ) != RTC_BKP_VALUE )
{
RTC_InitTypeDef RTC_InitStructure;
RCC_LSEConfig( RCC_LSE_ON ); /* Enable the LSE OSC */
while( RCC_GetFlagStatus( RCC_FLAG_LSERDY ) == RESET ); /* Wait till LSE is ready */
RCC_RTCCLKConfig( RCC_RTCCLKSource_LSE ); /* Select the RTC Clock Source */
RCC_RTCCLKCmd( ENABLE ); /* Enable the RTC Clock */
RTC_WaitForSynchro(); /* Wait for RTC APB registers synchronisation */
RTC_InitStructure.RTC_AsynchPrediv = 127;
RTC_InitStructure.RTC_SynchPrediv = 255;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
if( RTC_Init( &RTC_InitStructure ) == ERROR )
{
/* 此处作设置失败处理 */
}
ConfigCalendarTime( 13, 1, 10 );
}
else
{
// RCC_APB1PeriphClockCmd( RCC_APB1Periph_PWR, ENABLE ); /* Enable the PWR clock */
// PWR_BackupAccessCmd( ENABLE ); /* Allow access to RTC */
RTC_WaitForSynchro(); /* Wait for RTC APB registers synchronisation */
RTC_ClearFlag( RTC_FLAG_ALRAF ); /* Clear the RTC Alarm Flag */
}
}
/*****************************************************************
@Fn: ConfigCalendarTime()
@Br: 配置日历时间
@Pa: 时,分,秒
@Rt: 无
@Sp: 无
*****************************************************************/
void ConfigCalendarTime( uchar Hr, uchar Min, uchar Sec ){
RTC_TimeTypeDef RTC_TimeStructure;
RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
RTC_TimeStructure.RTC_Hours = Hr;
RTC_TimeStructure.RTC_Minutes = Min;
RTC_TimeStructure.RTC_Seconds = Sec;
if( RTC_SetTime( RTC_Format_BIN, &RTC_TimeStructure ) == ERROR )
{
/* 此处作设置失败处理 */
}
else
RTC_WriteBackupRegister( RTC_BKP_DR0, RTC_BKP_VALUE ); /* Indicator for the RTC configuration */
}
/*****************************************************************
@Fn: ConfigCalendarDate()
@Br: 配置日历日期
@Pa: 周,年,月,日
@Rt: 无
@Sp: 无
*****************************************************************/
void ConfigCalendarDate( uchar Week, uchar Yr, uchar Mont, uchar Day ){
RTC_DateTypeDef RTC_DateStructure;
RTC_DateStructure.RTC_WeekDay = Week;
RTC_DateStructure.RTC_Year = Yr;
RTC_DateStructure.RTC_Month = Mont;
RTC_DateStructure.RTC_Date = Day;
if( RTC_SetDate( RTC_Format_BIN, & RTC_DateStructure ) == ERROR )
{
/* 此处作设置失败处理 */
}
}
/*****************************************************************
@Fn: ConfigRTC_Alarm()
@Br: 配置RTC报警
@Pa: 时,分,秒
@Rt: 无
@Sp: 无
*****************************************************************/
void ConfigRTC_Alarm( uchar Hr, uchar Min, uchar Sec ){
RTC_AlarmTypeDef RTC_AlarmStructure;
RTC_AlarmCmd( RTC_Alarm_A, DISABLE ); /* Disable the Alarm A */
RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = Hr;
RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = Min;
RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = Sec;
/* Set the Alarm A */
RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
RTC_SetAlarm( RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure ); /* Configure the RTC Alarm A register */
RTC_ITConfig( RTC_IT_ALRA, ENABLE ); /* Enable the RTC Alarm A Interrupt */
RTC_AlarmCmd( RTC_Alarm_A, ENABLE ); /* Enable the alarm A */
}
/*****************************************************************
@Fn: ConfigRTC_Interrupt()
@Br: 配置RTC中断
@Pa: 无
@Rt: 无
@Sp: 无
*****************************************************************/
void ConfigRTC_Interrupt( void ){
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; /* Enable the RTC Alarm Interrupt */
NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( &NVIC_InitStructure );
}
/*****************************************************************
@Fn: GetRTC_Time()
@Br: 读取日历时间
@Pa: 时,分,秒
@Rt: 无
@Sp: 无
*****************************************************************/
void GetRTC_Time( uchar *Hr, uchar *Min, uchar *Sec ){
RTC_TimeTypeDef RTC_TimeStructure;
RTC_GetTime( RTC_Format_BIN, &RTC_TimeStructure );
*Hr = RTC_TimeStructure.RTC_Hours;
*Min = RTC_TimeStructure.RTC_Minutes;
*Sec = RTC_TimeStructure.RTC_Seconds;
}
/*****************************************************************
@Fn: GetCalendarDate()
@Br: 读取日历日期
@Pa: 周,年,月,日
@Rt: 无
@Sp: 无
*****************************************************************/
void GetCalendarDate( uchar *Week, uchar *Yr, uchar *Mont, uchar *Day ){
RTC_DateTypeDef RTC_DateStructure;
RTC_GetDate( RTC_Format_BIN, &RTC_DateStructure );
*Week = RTC_DateStructure.RTC_WeekDay;
*Yr = RTC_DateStructure.RTC_Year;
*Mont = RTC_DateStructure.RTC_Month;
*Day = RTC_DateStructure.RTC_Date;
}
/*****************************************************************
@Fn: GetRTC_Alarm()
@Br: 读取日历报警时间值
@Pa: 时,分,秒
@Rt: 无
@Sp: 无
*****************************************************************/
void GetRTC_Alarm( uchar *Hr, uchar *Min, uchar *Sec ){
RTC_AlarmTypeDef RTC_AlarmStructure;
RTC_GetAlarm( RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure );
*Hr = RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours;
*Min = RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes;
*Sec = RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds;
}
void RTC_IRQHandler( void ){
if( RTC_GetITStatus( RTC_IT_ALRA ) != RESET )
{
RTC_ClearITPendingBit( RTC_IT_ALRA );
}
}
|