A brief discussion on the STM32F10X chip RTC real-time clock

Publisher:fuehrd努力的Latest update time:2016-08-03 Source: eefocusKeywords:STM32F10X  RTC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Introduction

       After a system reset, access to the backup registers and RTC is disabled to prevent accidental writes to the backup area (BKP). The following operations will enable access to the backup registers and RTC:

l Set the PWREN and BKPEN bits of register RCC_APB1ENR to enable the power supply and backup interface clock (call: RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,ENABLE));

l Set the DBP bit in register PWR_CR to enable access to the backup registers and RTC (call: PWR_BackupAccessCmd(ENABLE)).

2. RTC precautions

The RTC_PRL, RTC_ALR, RTC_CNT and RTC_DIV registers can only be reset by the backup domain reset signal; a system reset or power reset will not affect their values;

l RTC provides APB1 interface to read the value of RTC register through ABP1, but it must wait until the RSF (synchronization flag) bit in the RTC_CRL register is set to "1" by hardware;

The configuration of RTC must be completed after the previous write operation (determine whether RTOFF in the RTC_CR register is 1, which means the update is complete), and set the CNF bit in the RTC_CRL register to make the RTC enter the configuration mode before writing to the RTC_PRL, RTC_CNT, and RTC_ALR registers. When the CNF flag is cleared, the write operation is actually effective (indicating that the RTC is dynamically configured, that is, the configuration is performed after the RTC is running);

Any flag bit in the RTC will remain pending (because OWF, ALRF, SECF and RSF can only be set by hardware and cleared by software) until the appropriate RTC_CR request bit is reset by software, indicating that all requested interrupts have been accepted;

If ALRF=1 and ALRIE=1, RTC global interrupt is allowed. If EXTI line 17 interrupt is allowed in EXTI controller, RTC global interrupt and RTC alarm interrupt are allowed. In this case, the alarm interrupt priority is generally set higher than the global interrupt. If the global interrupt priority is higher than the alarm interrupt, the alarm interrupt flag must be cleared in the global interrupt before entering the alarm interrupt processing function for further processing (because if the flag is not cleared, the interrupt will continue to be triggered, and the global interrupt priority is high, it will always be in the global interrupt and cannot jump out);

If ALRF = 1, if the interrupt mode of EXTI line 17 is set in the EXTI controller, the RTC alarm interrupt is allowed; if the event mode of EXTI line 17 is set in the EXTI controller, a pulse will be generated on this line (no RTC alarm interrupt will be generated);

When the APB1 clock is not running, the OWF, ALRF, SECF, and RSF bits are not updated;

l When the system is reset, all interrupts are disabled, there is no pending interrupt request, and the RTC register can be written;

Write operations to the RTC must be synchronized with the RTC seconds flag using one of the following procedures:

*         Use the RTC alarm interrupt and modify the RTC alarm and/or RTC counter in the interrupt handler;

*         Wait for the seconds flag SECF in the RTC control register to be set before changing the RTC alarm and/or RTC counter.

 

 

Figure 1 Simplified RTC block diagram (see manual for details)

 

3. RTC register description

l RTC control register high RTC_CRH/low RTC_CRL

l RTC prescaler loading register (RTC_PRLH/RTC_PRLL)

l RTC prescaler remainder register (RTC_DIVH/RTC_DIVL)

l RTC counter register (RTC_CNTH/RTC_CNTL)

l RTC alarm register (RTC_ALRH/RTC_ALRL)

The registers related to RTC are:

l PWREN and BKPEN of APB1 peripheral clock enable register RCC_APB1ENR enable power and backup clock

l Backup area protection bit of the power control register PWR_CR: DBP

4. RTC configuration process

? Configure RCC: select system clock, configure bus clock, enable peripheral device clock, etc.;

? Call RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE) function to enable the power supply and backup domain clock;

? Call PWR_BackupAccessCmd(ENABLE) to obtain the backup area access rights;

? Call the BKP_DeInit() function to reset all registers of the peripheral BKP to default values;

? Configure and select the RTC clock: call RCC_RTCCLKConfig(RCC_RTCCLKSource_XXX) to select LSE, HSE divided by 128 or LSI;

 

? RTC configuration:

n Call RCC_RTCCLKCmd(ENABLE) to enable the RTC clock;

n Call RTC_WaitForSynchro() to wait for the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL) to synchronize with the APB clock of the RTC (wait for the RTOFF position to be 1);

n Call the RTC_WaitForLastTask() function to wait for the most recent write operation to the RTC register to complete;

n Call the RTC configuration function (such as RTC_SetPrescaler(40000)) to configure the RTC (Note: the RTC control registers can be read and written directly; the write operation of RTC_PRL, RTC_CNT, RTC_ALR needs to enter the configuration mode, and reading them only needs to wait for the synchronization to complete (RSF is set to 1) and read through the APB1 interface);

n Each time after calling the RTC configuration function, you need to call RTC_WaitForLastTask() to wait for the current configuration to succeed.

? EXTI configuration: If you need to connect the RTC to EXTI line 17, configure EXTI line 17 to interrupt/event mode;

? NVIC configuration: If an interrupt is to be generated, configure the interrupt vector controller, enable the EXTI15_10_IRQHandler interrupt, or enable the RTC_IRQHandler interrupt;

? Write an interrupt handling function: Note that you must call the RTC_ClearITPendingBit() function in the interrupt handling function to clear the corresponding interrupt flag bit;

5. RTC configuration example

void NVIC_Configuration(void)

{

       NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM

       NVIC_SetVectorTable(NVIC_VectTab_RAM,0x00);

#else

       NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x00);

#endif

       NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

       NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;

       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

       NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

       NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

       NVIC_Init(&NVIC_InitStructure);

/* Set the alarm interrupt priority to be higher than the global interrupt */

       NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;

       NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

       NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

       NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

       NVIC_Init(&NVIC_InitStructure);

}

 

void RTC_Configuration(void)

{

       RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,ENABLE);

       PWR_BackupAccessCmd(ENABLE);

       BKP_DeInit();

       RCC_LSICmd(ENABLE);

       while(RESET == RCC_GetFlagStatus(RCC_FLAG_LSIRDY))

       {

       }

       RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

       RCC_RTCCLKCmd(ENABLE);

       RTC_WaitForSynchro();

       RTC_WaitForLastTask();

       RTC_ITConfig(RTC_IT_ALR,ENABLE);

       RTC_ITConfig(RTC_IT_SEC,ENABLE);

       RTC_SetPrescaler(6000);

       RTC_WaitForLastTask();

       RTC_SetAlarm(29);

       RTC_WaitForLastTask();

       BKP_TamperPinCmd(DISABLE);

       BKP_RTCOutputConfig(BKP_RTCOutputSource_Second);

}

void EXTI_Configuration(void)

{

       EXTI_InitTypeDef EXTI_InitStructure;

       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);

}

/* Interrupt handling function */

void RTC_IRQHandler(void)

{

       if(SET == RTC_GetITStatus(RTC_IT_SEC))

              {

                     RTC_ClearITPendingBit(RTC_IT_SEC);

       GPIO_WriteBit(GPIOB,GPIO_Pin_13,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_13)));

              }

}

 

void RTCAlarm_IRQHandler(void)

{

       if(SET == RTC_GetFlagStatus(RTC_IT_ALR))

       {

              RTC_ClearFlag(RTC_IT_ALR); /* Clear interrupt flag, including external interrupt line flag */

              if(EXTI_GetITStatus(EXTI_Line17));

              {

                     EXTI_ClearITPendingBit(EXTI_Line17);

                     GPIO_WriteBit(GPIOB,GPIO_Pin_8,(BitAction)(0));

              }

       }

}

Keywords:STM32F10X  RTC Reference address:A brief discussion on the STM32F10X chip RTC real-time clock

Previous article:A brief discussion on the SysTick system clock timer of the STM32F10X chip
Next article:Summary of STM32F10x chip GPIO/AFIO port configuration

Latest Microcontroller Articles
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
    Since development under LINUX is still quite troublesome, is there a more convenient and simple development method under WINDOWS? The answer is yes. Of course, it is not a development tool like ADS, because it ...
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
  • Learn ARM development(18)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号