Skip to main content
mauro2
Associate II
January 10, 2016
Question

help with rtc

  • January 10, 2016
  • 2 replies
  • 642 views
Posted on January 10, 2016 at 16:49

Hi, I'm trying the learn how to use rtc. can someone help me to understand how to inizialize rtc and how to use timestamp?

i'm using stm32f103 uC

thanks :)
    This topic has been closed for replies.

    2 replies

    Nesrine M_O
    Associate
    January 11, 2016
    Posted on January 11, 2016 at 09:39

    Hi  Mr.Cscn,

    I'd highly recommend you to start from RTC example under STM32Cube F1 package STM32Cube_FW_F1_V1.3.0\Projects\STM3210E_EVAL\Examples\RTC

    -Syrine-

    pkumar1883
    Associate III
    January 11, 2016
    Posted on January 11, 2016 at 15:59

    hi Mr.Cscn,

    Use the following code--

    int main(void)

    {

        RCC_APB1PeriphClockCmd( RCC_APB1Periph_BKP | RCC_APB1Periph_PWR  , ENABLE);

        RTC_NVIC_Config();

        RTC_Init();

    }

    void RTC_Config(void) {

        PWR_BackupAccessCmd(ENABLE);

        BKP_DeInit();

        RCC_LSICmd(ENABLE);

        while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {}

        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI  );

        RCC_RTCCLKCmd(ENABLE);

        RTC_WaitForSynchro();

        RTC_WaitForLastTask();

        RTC_SetPrescaler(32767);

        RTC_WaitForLastTask();

        RTC_ITConfig(RTC_IT_SEC, ENABLE);

        RTC_WaitForLastTask();

    }

    void RTC_NVIC_Config(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);

        RTC_ITConfig(RTC_IT_SEC, ENABLE);

    }

    void RTC_Init(void) {

        if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) {

            RTC_Configuration();

            Time_Adjust();

            BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);

        } else {

            RTC_WaitForSynchro();

            RTC_WaitForLastTask();

            PWR_BackupAccessCmd(ENABLE);

        }

        return;

    }

    void Time_Adjust(void) {

        u32 counter = 1449100000;//set equl to current unix time stamp

        RTC_WaitForLastTask();

        RTC_SetCounter(counter);

        RTC_WaitForLastTask();

    }

    void RTC_IRQHandler(void){

        if (RTC_GetITStatus(RTC_IT_SEC) != RESET){

            RTC_ClearITPendingBit(RTC_IT_SEC);

            RTC_WaitForLastTask();

        }

    }