/*******************************************************************************
* Function Name : RTC_Configuration
* Description : Configures the RTC.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RTC_Configuration(void)
{
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* Enable LSE */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{}
/* Select LSE as RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Enable the RTC ALARM */
RTC_ITConfig(RTC_IT_ALR, ENABLE);
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
void RTC_Init(void)
{
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
/* Backup data register value is not correct or not yet programmed (when
the first time the program is executed) */
printf("\r\n RTC not yet configured....");
RTC_Configuration();
printf("\r\n RTC configured....");
/* Adjust time by values entred by the user on the hyperterminal */
Time_SetCalendarTime(time_now);
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
}
else
{
/* Check if the Power On Reset flag is set */
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
printf("\r\n\n Power On Reset occurred....");
}
/* Check if the Pin Reset flag is set */
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
printf("\r\n\n External Reset occurred....");
}
printf("\r\n No need to configure RTC....");
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Enable the RTC ALARM */
RTC_ITConfig(RTC_IT_ALR, ENABLE);
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC Alarm register with the new value */
RTC_SetAlarm(tmp);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Save the Alarm value in the Backup register */
BKP_WriteBackupRegister(BKP_DR6, (tmp & 0x0000FFFF));
BKP_WriteBackupRegister(BKP_DR7, (tmp >> 16));
PWR_BackupAccessCmd(DISABLE);
}
void Set_RTCAlarm(void)
{
uint32_t tmp = 0;
tmp = RTC_GetCounter();
/* Save the Alarm value in the Backup register */
BKP_WriteBackupRegister(BKP_DR6, (tmp & 0x0000FFFF));
BKP_WriteBackupRegister(BKP_DR7, (tmp >> 16));
Alarm_PreAdjust();
/* Request to enter STOP mode with regulator in low power */
// PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
/*******************************************************************************
* Function Name : RTC_IRQHandler
* Description : This function handles RTC global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RTC_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
Time_Show();
/* 清除 RTC 秒中断 */
RTC_ClearITPendingBit(RTC_IT_SEC);
/* 更新时间显示标志位 */
// TimeDisplay = 1;
}
if (RTC_GetITStatus(RTC_IT_ALR) != RESET){
AlarmStatus = 1;
alarm_flag = 1;
// Set_STOPModeStatus();
LED1 = 1;
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Clear the Alarm Pending Bit */
RTC_ClearITPendingBit(RTC_IT_ALR);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Clear the EXTI Line 17/ */
EXTI_ClearITPendingBit(EXTI_Line17);