STM32 RTC clock source LSE

Publisher:luanzgcLatest update time:2016-09-24 Source: eefocusKeywords:STM32  LSE Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
At the beginning, all experiments were done on the Shenzhou board, and no RTC problems were found. It was not until we drew the board ourselves and debugged it that we found that there was a problem with the external clock source of the STM32 RTC.

This is also a chicken rib of STM32. It is too demanding for the LSE external crystal oscillator. The manual requires the use of 6pf. There are too few crystal oscillators of this specification on the market. There are a lot of good and bad crystals, and many experts and rookies have been fooled. The same is true for our own board. After many twists and turns, we have repeatedly tried to use crystal oscillators of different specifications, replace external capacitors, and resistors, but none of them can make this 32.768K LSE oscillate. However, RTC is needed to provide time. There are two main methods to consider. The first is to use an external RTC clock chip, such as DS1302. The second is to use other internal clock sources to provide the RTC clock. There is no doubt that the board has been made. Adding a clock chip will definitely cause changes in the layout of the board, and the board must be remade. Here, the second method is used.

Check the clock tree in the STM32 manual as follows:

Except for the external low-speed LSE that cannot oscillate, only the 128-division of LSI and HSE is available. The LSI is an internal 40KHz RC oscillator with a frequency floating between 30 and 60KHz. Naturally, this cannot be used for RTC timing because the error is too large.

Our board is equipped with the STM32F107 chip, and the external high-speed crystal oscillator is 25MHz. After 128 division, the frequency is 25000000 / 128 = 195312.5 Hz. Obviously, it cannot be very accurate here, and there is a small error.

Then set the RTC_PRL register and write the frequency division value of 195312 to get a frequency of 1Hz. The disadvantage of using HSE as the RTC clock is that it is not possible to use the backup battery to power the RTC after the power is disconnected to maintain the normal operation of the RTC. The host computer needs to set the time again next time.

 

The code is roughly as follows:

 
  1. void RTC_Configuration(void)  
  2. {  
  3.     u8 i = 0;  
  4.     /* Enable PWR and BKP clocks */  
  5.     /* PWR clock (power control) and BKP clock (RTC backup register) enable */  
  6.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);  
  7.   
  8.     /* Allow access to BKP Domain */  
  9.     /* Enable RTC and backup register access */  
  10.     PWR_BackupAccessCmd(ENABLE);  
  11.   
  12.     /* Reset Backup Domain */  
  13.     /* Reset all registers of peripheral BKP to default values ​​*/  
  14.     BKP_DeInit();  
  15.   
  16.     /* Enable LSE */  
  17.     /* Enable LSE (external 32.768KHz low speed crystal oscillator) */  
  18.     RCC_LSEConfig(RCC_LSE_ON);  
  19.     
  20.     /* Wait till LSE is ready */  
  21.     /* Wait for the external crystal oscillator to stabilize the output */  
  22.     TIM5_Init_Query(CALC_TYPE_MS); //ms level  
  23.     for (i = 0;i < 10;i++) //10 times of testing, if LSE still does not oscillate, it proves that there is something wrong with it, and jump out of the loop  
  24.     {  
  25.         if (RCC_GetFlagStatus(RCC_FLAG_LSERDY) != RESET)  
  26.             break;  
  27.         TIM5_MS_CALC(1); //1ms delay  
  28.     }  
  29.     //while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){}  
  30.     if (i == 10)  
  31.     {  
  32.         //RCC->CSR |= 0x1; //Turn on the internal low-speed crystal oscillator  
  33.         //while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);  
  34.         //RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); //Use LSI to provide RTC clock  
  35.         //Use external high-speed crystal oscillator 128 division  
  36.         RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);   
  37.     }else  
  38.     {  
  39.         /* Select LSE as RTC Clock Source */  
  40.         /*Use external 32.768KHz crystal as RTC clock */                           
  41.         RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);  
  42.     }  
  43.   
  44.     /* Enable RTC Clock */  
  45.     /* Enable RTC clock supply */  
  46.     RCC_RTCCLKCmd(ENABLE);  
  47.   
  48.     /* Wait for RTC registers synchronization */  
  49.     /*Wait for RTC register synchronization */  
  50.     RTC_WaitForSynchro();  
  51.   
  52.     /* Wait until last write operation on RTC registers has finished */  
  53.     /* Wait for the last write operation to the RTC register to complete */  
  54.     RTC_WaitForLastTask();  
  55.   
  56.     /* Enable the RTC Second */  
  57.     /* Enable RTC second interrupt */  
  58.     RTC_ITConfig(RTC_IT_SEC, ENABLE);  
  59.   
  60.     /* Wait until last write operation on RTC registers has finished */  
  61.     /* Wait for the last write operation to the RTC register to complete */  
  62.     RTC_WaitForLastTask();  
  63.     
  64.     /* Set RTC prescaler: set RTC period to 1sec */  
  65.     /* The pre-division value of the 32.768KHz crystal oscillator is 32767. If you have high requirements for accuracy, you can modify this division value to calibrate the crystal oscillator */  
  66.     if (i != 10) //LSE cannot function normally  
  67.         RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */  
  68.     else  
  69.         RTC_SetPrescaler(195312); //25000000 / 128 = 195312.5, if it is 8M / 128 = 62500, then this should be filled in as 62499  
  70.   
  71.     /* Wait until last write operation on RTC registers has finished */  
  72.     /* Wait for the last write operation to the RTC register to complete */  
  73.     RTC_WaitForLastTask();  
  74. }  
  75.   
  76. void Init_RTC(void)  
  77. {  
  78.     /* The following if...else.... if determines whether the system time has been set and the value of RTC backup register 1 
  79.      Is it the previously written 0XA5A5? If not, it means that the RTC is powered on for the first time and needs to be configured. 
  80.      Prompt the user to change the system time through the serial port, convert the actual time into the RTC count value and write it into the RTC register. 
  81.      And modify the value of backup register 1 to 0XA5A5. 
  82.      else indicates that the system time has been set, prints the reason for the last system reset, and enables the RTC second interrupt 
  83.     */  
  84.     if (BKP_ReadBackupRegister(BKP_DR1) != RTC_SEQ_ID)  
  85.     {  
  86.         /* Backup data register value is not correct or not yet programmed (when 
  87.            the first time the program is executed) */  
  88.       
  89.         /* RTC Configuration */  
  90.         RTC_Configuration();  
  91.   
  92.    
  93.         /* Adjust time by values entred by the user on the hyperterminal */  
  94.         RTC_SetCounter(Time_Regulate(YEAR_BASE,01,01,0,0,0)); //2008-1-1 0:0:0  
  95.         /* Modify the value of backup register 1 to 0XA5A5 */  
  96.         BKP_WriteBackupRegister(BKP_DR1, RTC_SEQ_ID);  
  97.     }else  
  98.     {  
  99.         /* Check if the Power On Reset flag is set */  
  100.         //RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET  
  101.         //  printf("\r\n\n Power On Reset occurred....");  
  102.           
  103.         /* Check if the Pin Reset flag is set */  
  104.         //else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)  
  105.         //  printf("\r\n\n External Reset occurred....");  
  106.         if (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)  
  107.         {  
  108.             //RCC->CSR |= 0x1; //Turn on the internal low-speed crystal oscillator  
  109.             //while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);  
  110.             //RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); //Use LSI to provide RTC clock  
  111.             //RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);  
  112.             RTC_Configuration();  
  113.         }  
  114.         //printf("\r\n No need to configure RTC....");  
  115.         /* Wait for RTC registers synchronization */  
  116.         RTC_WaitForSynchro();  
  117.   
  118.         /* Enable the RTC Second */  
  119.         RTC_ITConfig(RTC_IT_SEC, ENABLE);  
  120.       
  121.         /* Wait until last write operation on RTC registers has finished */  
  122.         RTC_WaitForLastTask();  
  123.     }  
  124.   
  125. #ifdef RTCClockOutput_Enable  
  126.   /* Enable PWR and BKP clocks */  
  127.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);  
  128.         
  129.   /* Allow access to BKP Domain */  
  130.   PWR_BackupAccessCmd(ENABLE);  
  131.   
  132.   /* Disable the Tamper Pin */  
  133.   BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper 
  134.                                  functionality must be disabled */  
  135.   
  136.   /* Enable RTC Clock Output on Tamper Pin */  
  137.   BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);  
  138. #endif  
  139.   
  140.   /* Clear reset flags */  
  141.   RCC_ClearFlag();  
  142. }  


In actual testing, the RTC effect is good, and the synchronization time with the host computer after a certain period of time can basically meet the requirements.

Damn LSE crystal, this thing is simply unbearable...

Keywords:STM32  LSE Reference address:STM32 RTC clock source LSE

Previous article:stm32 ADC non-DMA mode
Next article:Use keil to burn stm32 and pay attention to changing the address

Latest Microcontroller Articles
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号