我参考STM32F0固件库设置RTC时钟,每次运行到RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure) == ERROR时都是设置失败,找不到原因,请大家帮忙看看,谢谢。
主函数只调用一个函数:RTC_Init_Define();
RTC设置如下:
[mw_shl_code=c,true]#include "rtc.h"
#include "usart1.h"
__IO uint32_t AsynchPrediv = 0, SynchPrediv = 0;
RTC_TimeTypeDef RTC_TimeStructure;
RTC_InitTypeDef RTC_InitStructure;
RTC_AlarmTypeDef RTC_AlarmStructure;
void SET_RTC_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
PWR_BackupAccessCmd(ENABLE);
RCC_LSEConfig(RCC_LSE_ON);[/mw_shl_code]
[mw_shl_code=c,true]
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
;
}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
SynchPrediv = 0xFF;
AsynchPrediv = 0x7F;
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
if (RTC_Init(&RTC_InitStructure) == ERROR)
{
printf("\n\r /!\\***** RTC Prescaler Config failed ********/!\\ \n\r");
}
RTC_TimeRegulate();
}
void Check_RTC_SET(void)
{
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
printf("\r\n Power On Reset occurred....\n\r");
}
else
{
if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
printf("\r\n External Reset occurred....\n\r");
}
}
printf("\n\r No need to configure RTC....\n\r");
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
PWR_BackupAccessCmd(ENABLE);
RTC_WaitForSynchro();
RTC_ClearFlag(RTC_FLAG_ALRAF);
RTC_TimeShow();
RTC_AlarmShow();
}
void RTC_NVIC_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void RTC_Init_Define(void)
{
if(RTC_ReadBackupRegister(RTC_BKP_DR0) != BKP_VALUE)
{
SET_RTC_Init();
}
else
{
Check_RTC_SET();
}
RTC_NVIC_Init();
}
void RTC_TimeShow(void)
{
/* Get the current Time */
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
printf("\n\r The current time is : %0.2d:%0.2d:%0.2d \n\r", RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds);
}
void RTC_AlarmShow(void)
{
/* Get the current Alarm */
RTC_GetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);
printf("\n\r The current alarm is : %0.2d:%0.2d:%0.2d \n\r", RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours, RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes, RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds);
}
void RTC_TimeRegulate(void)
{
uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;
printf("\n\r==============Time Settings=====================================\n\r");
RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
printf(" Please Set Hours:\n\r");
while (tmp_hh == 0xFF)
{
tmp_hh = USART_Scanf(23);//最大设置值为23
RTC_TimeStructure.RTC_Hours = tmp_hh;
}
printf(" %0.2d\n\r", tmp_hh);
printf(" Please Set Minutes:\n\r");
while (tmp_mm == 0xFF)
{
tmp_mm = USART_Scanf(59);
RTC_TimeStructure.RTC_Minutes = tmp_mm;
}
printf(" %0.2d\n\r", tmp_mm);
printf(" Please Set Seconds:\n\r");
while (tmp_ss == 0xFF)
{
tmp_ss = USART_Scanf(59);
RTC_TimeStructure.RTC_Seconds = tmp_ss;
}
printf(" %0.2d\n\r", tmp_ss);
/* 每次运行到此处都出错,设置不了 */
if(RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure) == ERROR)
{
printf("\n\r>> !! RTC Set Time failed. !! <<\n\r");
}
else
{
printf("\n\r>> !! RTC Set Time success. !! <<\n\r");
RTC_TimeShow();
/* Indicator for the RTC configuration */
RTC_WriteBackupRegister(RTC_BKP_DR0, BKP_VALUE);
}
tmp_hh = 0xFF;
tmp_mm = 0xFF;
tmp_ss = 0xFF;
/* Disable the Alarm A */
RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
printf("\n\r==============Alarm A Settings=====================================\n\r");
RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
printf(" Please Set Alarm Hours:\n\r");
while (tmp_hh == 0xFF)
{
tmp_hh = USART_Scanf(23);
RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = tmp_hh;
}
printf(" %0.2d\n\r", tmp_hh);
printf(" Please Set Alarm Minutes:\n\r");
while (tmp_mm == 0xFF)
{
tmp_mm = USART_Scanf(59);
RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = tmp_mm;
}
printf(" %0.2d\n\r", tmp_mm);
printf(" Please Set Alarm Seconds:\n\r");
while (tmp_ss == 0xFF)
{
tmp_ss = USART_Scanf(59);
RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = tmp_ss;
}
printf(" %0.2d", tmp_ss);
/* Set the Alarm A */
RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
/* Configure the RTC Alarm A register */
RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);
printf("\n\r>> !! RTC Set Alarm success. !! <<\n\r");
RTC_AlarmShow();
/* Enable the RTC Alarm A Interrupt */
RTC_ITConfig(RTC_IT_ALRA, ENABLE);
/* Enable the alarm A */
RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
}[/mw_shl_code]
中断函数
[mw_shl_code=c,true]void RTC_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_ALRA) != RESET)
{
RTC_ClearITPendingBit(RTC_IT_ALRA);
}
}[/mw_shl_code]
|