stm32f030f4p6 RTC秒中断配置

//RTC定时唤醒:秒中断 或者 亚秒中断
static void rtc_init(void)
{
    RTC_InitTypeDef  RTC_InitStructure;
	RTC_AlarmTypeDef RTC_AlarmStructure;
    RTC_TimeTypeDef RTC_TimeStructure;

    NVIC_InitTypeDef NVIC_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
    PWR_BackupAccessCmd(ENABLE);
    
    RCC_LSICmd(ENABLE);
    while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET); /*等待LSI就绪*/
    
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);  /*选择LSI时钟*/
    RCC_RTCCLKCmd(ENABLE);
    RTC_WaitForSynchro();                               /*等待同步  */
        
    /*RTC_LSI 为40kHz   RTC_LSI = (SynchPrediv+1) * (AsynchPrediv +1)*/
    RTC_InitStructure.RTC_AsynchPrediv = 0x63;
    RTC_InitStructure.RTC_SynchPrediv	=  0x18F; //40KHZ/(399+1)/(99+1)=1HZ;
    RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
    RTC_Init(&RTC_InitStructure);  
    
    /* EXTI configuration *******************************************************/
    EXTI_ClearITPendingBit(EXTI_Line17);
    EXTI_InitStructure.EXTI_Line = EXTI_Line17;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
    
    /* Enable the RTC Wakeup Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
#ifdef RTC_SEC_WAKEUP
    RTC_AlarmCmd(RTC_Alarm_A,DISABLE);
    RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_H12_AM;
    RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = 0x01;
    RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x02;
    RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x20;//闹钟时间设定为20s
    RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
    RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
    RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
    RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);
#else
    RTC_AlarmCmd(RTC_Alarm_A,DISABLE);
    RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_H12_AM;
    RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = 0x01;
    RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x02;
    RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x20;
    RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
    RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
//    RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
    RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_All;
    RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);
    
#warning "这里需要计算填入的值与实际闹钟值的关系,,暂未理清"
    RTC_AlarmSubSecondConfig(RTC_Alarm_A, 0xFF, RTC_AlarmSubSecondMask_SS14_5);//设置亚秒闹钟值
#endif
    
    RTC_ClearFlag(RTC_FLAG_ALRAF);
    RTC_ITConfig(RTC_IT_ALRA, ENABLE);
    RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
    
    RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
    RTC_TimeStructure.RTC_Hours   = 0x01;
    RTC_TimeStructure.RTC_Minutes = 0x02;
    RTC_TimeStructure.RTC_Seconds = 0x00;  
    RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
}

void RTC_IRQHandler(void)
{   
	if(RTC_GetITStatus(RTC_IT_ALRA) != RESET)  
    {  
        /* Check if the Wake-Up flag is set */  
        if(PWR_GetFlagStatus(PWR_FLAG_WU) != RESET)  
        {  
            /* Clear Wake Up flag */  
            PWR_ClearFlag(PWR_FLAG_WU);  
        }                                                                                                                 
        
        if(WAKEUP_POWERUP == g_wakeup_source)
        {
            g_wakeup_source = WAKEUP_RTC;
        }
        
#if (1) //此处更新秒中断闹钟值,可循环产生秒中断
//        RTC_TimeTypeDef RTC_TimeStructure;
//
//        /* Set the time to 00h 00mn 00s AM */
//        RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
//        RTC_TimeStructure.RTC_Hours   = 0x01;
//        RTC_TimeStructure.RTC_Minutes = 0x02;
//        RTC_TimeStructure.RTC_Seconds = 0x00;  
//        
//        RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
#endif
        
        RTC_ClearITPendingBit(RTC_IT_ALRA);
        EXTI_ClearITPendingBit(EXTI_Line17);
    }  
}

备注:

1 若使用秒中断,则开启RTC_SEC_WAKEUP宏即可

2 若要使用亚秒中断则还需要修改。虽然屏蔽RTC_SEC_WAKEUP宏可以产生亚秒中断,但对其中RTC_AlarmSubSecondConfig的第二个参数和第三个参数该怎么填写才能设置出自己想要的亚秒级中断时间不是很清楚。后面有时间再研究。。。有大神清楚的话希望不吝赐教

你可能感兴趣的:(C,STM32)