Regarding the inaccurate timing of the RTC of the STM32 development board

When I want to make a simple electronic clock + alarm clock, I just have a minimum system board of STM32 that I bought, so I consider using the RTC function to complete it.

hardware:

Taobao purchased the smallest system board of STM32f103c8t6, ​​which is probably long. Insert picture description here
Although this board does not have a separate battery power supply in the backup area, it also has a low-speed external crystal oscillator, which is the small black square next to the LED. As for the STM32, why should two crystal oscillators be designed? The statement is that a high-speed crystal oscillator is multiplied by the PLL and provided to the CPU for operation, and a low-frequency crystal oscillator is used for accurate timing of the RTC. ** Then one more question is why a single crystal oscillator is used for the RTC timing, and the CPU is the main Can the divided clock be used for RTC? **Of course it is possible. My personal understanding of the meaning of the 32.768KHz crystal oscillator is about two points: The first is related to the design of low power consumption. When the MCU is running at full power, all clock oscillators are working. However, in order to achieve low power consumption in some scenarios, the MCU has designed a low power consumption mode. At this time, most peripherals and high-speed oscillators are turned off to reduce power consumption. At this time, a separate crystal oscillator is used to power the backup area separately. Time can also be possible in low-power mode; the second reason is that the accuracy of the low-speed crystal oscillator will be higher (rumors, without careful exploration, why is the same passive crystal oscillator 32.768 more accurate or because it does not multiply the frequency, it is easier to travel accurately. Also unknown, interested fat friends can explore and communicate,)

software design:

The RTC function directly realizes a full set of clock + timing functions. Use external low-speed crystal drive.

Steps to use RTC:

  1. Enable power clock and backup area
  2. Cancel write protection of backup area
  3. Reset the backup area and turn on the external low-speed oscillator
  4. Select RTC clock source and enable
  5. Set crossover and other parameters
  6. Configure interrupt
  7. Write interrupt function, end

test:

After downloading the program, I found that the RTC was running normally, so I plugged it in and went out. Since there is no battery in the back-up area, the smallest system board is powered directly through the USB port. When I went home, I checked the clock and was surprised to find that I went out for about 8 hours. The timing is slow for about two or three minutes, which is more than 150 seconds. I thought that the watch was not properly calibrated and re-calibrated it. The next day, I found that it was still the same. I didn’t think about it. I didn’t even think about it. I didn’t need to be very accurate. I slept at night. It is found that the low count is not linear, and the compensation is not easy to compensate. So I checked my program carefully and found that there was nothing wrong with it. I browsed some information on the Internet and found that everyone was boasting how accurate the 32.768 crystal oscillator was. It was not a second after a few months, and there was a problem of inaccurate travel time. Not much. It is suspected that my low-speed crystal oscillator is drifting, but due to lack of equipment, I did not look at the crystal waveform alone, so I probably checked it. I opened a TIMER with HSE and compared the travel time of RTC. I found that the RTC travel time is very different. There will be a difference of about 1s in a few minutes. The RTC clock is switched to the count value of HSE discovery and TIMER, indicating that there is no problem with the RTC configuration, and the main source should be the problem of crystal drift. I simply took the mobile phone stopwatch and the RTC clock driven by HSE and found it to be more accurate. Since I don’t have low power consumption requirements, I used the HSE to drive the RTC directly. The accuracy of the time is about a few seconds after 24H. It is also acceptable, and the function is now complete.

to sum up:

Since there is not much analysis on the problem of inaccurate RTC running time on the Internet, especially the problem of such inaccurate running, I will briefly summarize here. If the RTC time is inaccurate:
First, you can use a TIMER to compare the timing of the RTC to see if there is a difference in the count driven by these two clock sources, then you can switch the RTC clock source to HSE and reconfigure the frequency division to test whether the time is running Accurate, if there are many problems with the configuration, you need to check it again;
second, if the time is normal, it is only a few seconds away, considering the crystal oscillator is not accurate, you can use the RTC calibration function, the core of the calibration function is to use a high-precision clock To calibrate the low-precision clock, the two counts for a period of time and the difference is used as the compensation value for compensation. This method has two problems. The first is that you need a high-precision clock source, and the second is only It can deal with the situation that the clock is inaccurate but very stable. If the clock is unstable, there is no way; the
third is to consider starting from the hardware, in the design, by matching the appropriate capacitor resistance and wiring design to ensure that the clock as much as possible accuracy.

In general, don't blindly believe in the accuracy of the RTC clock, and the finished product you bought may not do well.

Guess you like

Origin blog.csdn.net/weixin_41977590/article/details/108894418