stm32在rt-thread上的RTC(实时时钟)

rt-thread中已经部分实现了rtc的内容 ---> rtc.c ,调用rt_hw_rtc_init()函数即可使用msh设置date和time等

rtc时钟来源有三个:

HSE, LSE, LSI, 实现如下: 在原RTC_Configuration(void)进行替换即可


    /* Enable LSE */
    #ifdef USE_LSE
        RCC_LSEConfig(RCC_LSE_ON);
        /* Wait till LSE is ready */
        while ( (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--count) );
    #else
        #ifdef USE_HSE


        #else
            RCC_LSICmd(ENABLE);
            /* Wait till LSE is ready */
            while ( (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) && (--count) );
        #endif
    #endif
    if ( count == 0 )
    {
        return -1;
    }


    /* Select LSE as RTC Clock Source */
    #ifdef USE_LSE
        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
    #else
        #ifdef USE_HSE
            RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
        #else
            RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
        #endif
    #endif
    /* 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();


    /* Set RTC prescaler: set RTC period to 1sec */
    #ifdef USE_LSE
        RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
    #else
        #ifdef USE_HSE
            RTC_SetPrescaler(93749); /* 72 MHz / 128 */
        #else
            RTC_SetPrescaler(40000); /* 40 KHz */
        #endif
    #endif


注意:/* Note:If the HSE divided by 128 is used as the RTC clock, this bit must remain set to 1. */


秒中断和ALARM中断实现:

        rt_hw_rtc_navi_init();
        RTC_WaitForLastTask();
        RTC_SetAlarm(RTC_GetCounter()+25);       //当前RTC值的基础上加时间
        RTC_WaitForLastTask();
        RTC_ITConfig(RTC_IT_SEC, ENABLE);      //使能秒中断
        RTC_WaitForLastTask();
        RTC_ITConfig(RTC_IT_ALR,ENABLE);       //使能arlarm中断

        RTC_WaitForLastTask();


void rt_hw_rtc_navi_init(void)
{
    NVIC_InitTypeDef NVIC_InitStructure; 
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);  
    NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; 
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);
}


void RTC_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();


    if(RTC_GetITStatus(RTC_IT_ALR) == SET)
    {
        rt_kprintf("RTCAlarm_IRQHandler1~~~\r\n");
        RTC_ClearITPendingBit(RTC_IT_ALR);
    }
    if(RTC_GetITStatus(RTC_IT_SEC) == SET)
    {
        RTC_ClearITPendingBit(RTC_IT_SEC);
        rt_kprintf("RTCAlarm_IRQHandler2~~~\r\n");
    }
    /* leave interrupt */
    rt_interrupt_leave();
}


ps:RTCAlarm_IRQn               = 41,     /*!< RTC Alarm through EXTI Line Interrupt                */  这个不是alarm的中断函数哟


你可能感兴趣的:(RT-thread,STM32)