A brief look at the internal clock of STM32

Publisher:caoda143Latest update time:2018-06-26 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In electronic design competitions, we sometimes hope to measure the speed of the friction wheel of the robot's gimbal launch mechanism. One reason is to prevent the starting current from being too large when the friction wheel is ramp-started, and the other is to prevent the speed of the friction wheel from being affected by the bullet being ejected during the firing process, resulting in speed fluctuations.

In the early stage, we had two solutions, one was to use the AS5048 encoder to measure the speed, and the other was to use the Hall sensor to measure the speed. When verifying the codes of the two solutions, since AS5048 is rarely used in China and there is little information, I had to read its English documents and found that AS5048 is actually an absolute encoder and is not suitable for measuring friction wheels with such a high speed. However, given the power of STM32F4 (high-frequency processing capability), we finally used the average method twice to calculate the speed of the friction wheel. I will talk about the speed measurement method and code of AS5048 when I have the chance.

Next, let's talk about the Hall sensor speed measurement we use. After a deeper understanding, it is found that there are actually two methods for Hall sensor speed measurement, one is the frequency measurement method, and the other is the period measurement method.

The so-called frequency measurement method is to use two timers, such as TIM2 and TIM3, where TIM2 is used to capture the falling edge of the Hall sensor and count the falling edge in the capture interrupt, and TIM3 is used to count a fixed period of time, so that the number of pulses obtained within a certain period of time can be measured. Assuming that the TIM3 timing time is T, the number of magnets installed on the friction wheel is N, and the number of pulses obtained is P, it can be known that the friction wheel speed is (P*60)/(N*T).

The so-called cycle measurement method is to use only one timer, such as TIM2, and use the TIM2 input to capture the falling edge output of the Hall sensor. However, we do not count the falling edge at this time, but directly calculate the time between two input captures, and this time is the time used for turning 1/N circles. In this way, the speed can also be obtained, and only one timer is used. However, when writing the code for the cycle measurement method, I realized an important thing, that is, the frequency of the system clock will directly affect the accuracy of the cycle measurement method.

Here we need to add some important information about the system clock. By reading P108 of the "STM32F4 Development Guide - Library Function Version", we know that "STM32F4 has two PLLs. The first one is the main PLL (PLL) which provides the clock signal from HSE or HSI and has two different output clocks. The first output PLLP is used to generate a high-speed system clock (up to 168MHz), and the second output PLLQ is used to generate the clock of USB OTG FS (48MHz), the clock of the random number generator and the SDIO clock; the second one is a dedicated PLL (PLLI2S) used to generate a precise clock, so as to achieve high-quality audio performance on the I2S interface." When we use the clock used by TIMx for input capture, this clock is actually obtained by dividing the first output of the above main PLL clock. So the question is, what is the frequency of the main clock? What is the division factor of TIMx? Only by determining these two quantities can we correctly initialize our input capture function.

Similarly, according to the "STM32F4 Development Guide - Library Function Version" P108, the calculation formula for the first output high-speed clock PLLP generated by the main PLL can be obtained, PLL=E*N/(M*P), where E is the external crystal frequency, N is the multiplier multiplication factor, and M and P are both divider division factors (for their specific locations, see the STM32 system clock diagram of "STM32F4 Development Guide - Library Function Version" P108). At the code level, E is the external crystal frequency, which can be obtained by referring to the core board schematic diagram, and N, M, and P can all be seen in the system_stm32f4xx.c file. This file also provides the bus division factors of various F4 MCUs, such as APB1 Prescaler, APB2 Prescaler, etc., which can all be found and modified.

The necessity of understanding the above is that since learners generally use the STM32F407 core board provided by Zhengdian Atom, its external crystal frequency is 8MHz, and the other three coefficients are M=8, N=336, and P=2. Substituting into the above calculation formula, the main PLL clock is 168MHz, and the frequency division coefficient of APB1 is 2. However, when we want to transplant the code, we will inevitably encounter the situation where all these values ​​have changed. For example, the STM32F427 board provided by DJI has an external crystal frequency of 12MHz. In order to ensure that the previous code in F407 can still be used, we must make certain code settings so that the calculated main PLL is still 168MHz. Simply set it to set the PLL_M value of line 316 in the system_stm32f4xx.c file to 12, and the PLL_N value of line 335 to 336. Substituting into the main PLL calculation formula, the calculation result is 168MHz. This value is generally the frequency of the system clock. In this way, the code of F407 can be easily transplanted directly to F427. It should be noted that the frequency division coefficients of each bus of F407 and F427 for the main PLL are also different, so special attention should be paid when initializing the clock. For example, after the above settings, the frequency division coefficient of APB1 of F407 is 4, while that of F401 is 2. This is very important to pay attention to in various initializations of clocks. Unless the frequency division coefficient of APB1 is 1, the clock of the general timer is equal to twice the clock of APB1.

There is another point that needs special attention. Generally speaking, the larger the count value of input capture, the better. In the general timer, TIM2 and TIM5 are both 32-bit timers, and these two timers should be used as much as possible for speed measurement. For example, we initialize the TIM5 clock to TIM5_CH2_Cap_Init(0xFFFFFFFF,42-1). Simple calculation shows that the time required to count 0xFFFFFFFF is about 4000 seconds, which is more than enough for measuring most signal frequencies. If a 16-bit timer is used, its maximum count value is 0xFFFF, and the counting time is greatly reduced to about 66ms, which is generally not suitable for input capture.

By understanding the above knowledge points, you can obtain accurate pulse period when using the period measurement method to measure speed, accurately obtain speed and perform feedback control.

In general, the frequency measurement method is only suitable for testing high-frequency signals and can give speed values ​​within a fixed time, while the period measurement method is more applicable to low-frequency and high-frequency signals, but its measurement speed is controlled by the measured pulse. When MCU resources are scarce, it is more appropriate to use the period measurement method.


Keywords:STM32 Reference address:A brief look at the internal clock of STM32

Previous article:Graphics and texts teach you to understand the STM32 microcontroller clock
Next article:STM32F0 Development Notes 7: Pin Usage Notes

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号