高级会员

- 积分
- 800
- 金钱
- 800
- 注册时间
- 2016-5-12
- 在线时间
- 173 小时
|
1金钱
#define RTC_CLOCK_SOURCE_LSE /* 选择LSE */
//#define RTC_CLOCK_SOURCE_LSI // LSI 也可以
//
#define BKP_VALUE 0x32F0
RTC_TimeTypeDef RTC_TimeStructure;
RTC_InitTypeDef RTC_InitStructure;
RTC_AlarmTypeDef RTC_AlarmStructure;
__IO uint32_t AsynchPrediv = 0, SynchPrediv = 0;//同步分频值和非同步分频值
void RTC_Init_CX()
{
if(RTC_ReadBackupRegister(RTC_BKP_DR0) != BKP_VALUE)
{
RTC_Config();
}
}
/**
* @brief Configure the RTC peripheral by selecting the clock source.
* @param None
* @retval None
*/
void RTC_Config(void)
{
/* 使能 PWR 时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* 允许访问RTC */
PWR_BackupAccessCmd(ENABLE);
#if defined (RTC_CLOCK_SOURCE_LSI) /* 当使用LSI 作为 RTC 时钟源*/
/* The RTC Clock may varies due to LSI frequency dispersion. */
/* 使能 LSI 振荡 */
RCC_LSICmd(ENABLE);
/* 等待到 LSI 预备*/
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{
}
/* 把RTC 时钟源配置为LSI */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* 定襎同步分频值和异步分频值 */
SynchPrediv = 0x18F;
AsynchPrediv = 0x63;
#elif defined (RTC_CLOCK_SOURCE_LSE) /* 当使用LSE 最为 RTC 时钟源 */
/*使能 LSE 振荡 */
// RCC_LSEConfig(RCC_LSE_ON);
do
{
for(delay = 0;delay < 90000;delay++);
RCC_LSEConfig(RCC_LSE_ON);
}while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
/*等待 LSE 预备 */
// while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
// {
// }
/* 把RTC 时钟源配置为使用LSE */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* 定襎同步分频值和异步分频值 */
SynchPrediv = 0xFF;
AsynchPrediv = 0x7F;
#else
#error Please select the RTC Clock source inside the main.c file
#endif /* RTC_CLOCK_SOURCE_LSI */
/* 使能RTC时钟 */
RCC_RTCCLKCmd(ENABLE);
/* 等待 RTC APB 寄存器同步 */
RTC_WaitForSynchro();
}
|
最佳答案
查看完整内容[请看2#楼]
解决了 原子哥 是因为 stm32f030 LSI掉电使用后备电池不能走时。要改用LSE要系统全掉电并重启,否则LSE无法就绪。
|