Iterative method based on STM32 for equal precision frequency measurement
By adjusting the timer load value by iteratively on the STM32 microcontroller, high-precision frequency measurement is achieved, the problems of frequency measurement error and accuracy limitation in the prior art are solved, and high-precision frequency measurement effect is achieved in a wide frequency range and in a short time.
Patent Information
- Application Number
- CN202211206961.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-30
- Publication Date
- 2025-05-06
- Estimated Expiration
- 2042-09-30
AI Technical Summary
The prior art has error problems in frequency measurement, especially when the frequency of the measured signal is low or the gate time is short, the measurement error is large, and due to the fixedness of the gate time and the upper limit of the standard signal frequency, it is difficult to achieve high-precision frequency measurement in a wide frequency range and in a short time.
The frequency measurement results are output by using an iterative method based on STM32. By adjusting the load value of the first-level timer, iteratively calculate until the measurement accuracy reaches the highest required error. This method does not require peripheral circuits, and only uses the built-in timer of STM32G4 for cascading operations, simplifying hardware connection and resource utilization.
It realizes high-precision frequency measurement, reduces measurement errors, expands the frequency measurement range and speed, and quickly improves the frequency measurement accuracy in a short time, meeting the requirements of equal-precision frequency measurement.
Smart Images

Figure CN115494300B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of signal measurement, and in particular to an iterative method based on STM32 with equal precision frequency measurement. Background Art
[0002] In the field of electronic design and measurement, frequency is a very common indicator that needs to be measured. Many signal processing is based on frequency measurement, so the study of frequency measurement methods is of great significance in practical engineering applications. There are currently two commonly used frequency measurement methods: frequency measurement method and period measurement method. The frequency measurement method is to measure the number of pulses of the signal within a certain time window, and then calculate the number of pulses per unit time, which is the frequency. The period measurement method is to obtain the period of the signal by measuring the start and end time of a pulse, and take the inverse of it to obtain the frequency. But no matter which method is used, an error of ± one pulse will be generated.
[0003] The measurement error of the frequency measurement method of the single-chip microcomputer generally depends on the gate time and the frequency of the measured signal. When the frequency of the measured signal is low or the gate time is small, a large error will be generated. Later, the equal-precision frequency measurement method of the single-chip microcomputer was developed. The edge of the measured frequency signal is used as the standard for triggering counting and ending. This ensures that the time width of the actual gate signal is exactly a complete multiple of the number of cycles of the measured signal, thus ensuring constant accuracy. However, due to the fixed gate time and the upper limit of the standard signal frequency, this method needs to be improved. Summary of the invention
[0004] In view of the problems in the prior art, the purpose of the present invention is to provide an iterative method based on STM32 with equal precision frequency measurement, which is simple and easy to implement, has high precision, low error, and can be implemented without any peripheral circuit.
[0005] The specific technical solution for achieving the purpose of the present invention is:
[0006] An iterative equal-precision frequency measurement method based on STM32 is characterized in that an iterative method is used to adjust the load value of the first-level timer on the basis of the traditional equal-precision frequency measurement scheme using two timers, so that the PWM wave period output by the first-level timer is basically around 1s, until the measurement error reaches the required maximum error Below, the measurement accuracy is greater than The iteration ends and the frequency measurement results are output.
[0007] The specific steps include:
[0008] Step 1: The signal to be tested is connected to the primary timer clock, and the timer uses PWM to generate a no-output mode; the frequency division coefficient is initialized to 0 for the first time, and the load value is 1; the PWM wave is generated and connected to the secondary timer through the input capture cascade from the inside;
[0009] Step 2: The secondary timer uses the input TRC trigger input capture mode; the initialization frequency division coefficient is 0, and the load value is set to a number between 1-65535;
[0010] Step 3: Set the secondary timer overflow interrupt, and update the global variable to record the overflow times in the interrupt;
[0011] Step 4: Set the secondary timer input capture interrupt and calculate the frequency to be tested in the interrupt;
[0012] Step 5: After the secondary timer triggers an input capture interrupt and performs frequency calculation, if the measurement accuracy does not meet the minimum expected accuracy indicator The requirement is that the total trigger times of the secondary timer is less than 50M times, then iteration is performed, and the measured frequency is set as the loading value of the first-level timer, so that the PWM wave output by the first-level timer gradually approaches 1s after iteration, and then the secondary timer uses input capture measurement based on the 170M clock and calculates the specific frequency again;
[0013] Step 6: Repeat steps 3 to 5 and iterate until the measurement accuracy reaches the minimum expected accuracy index. End the frequency measurement and output the results.
[0014] The present invention connects the signal to be measured to the first-level timer clock, and the timer selects PWM to generate a no-output mode. The second-level timer selects the input TRC to trigger the input capture mode. The two-level timer is turned on, and after the frequency measurement, the measured frequency is set as the load value of the first-level timer, so that the PWM wave output by the first-level timer after iteration is close to 1s, and then the second-level timer uses input capture measurement based on the 170M main frequency clock, and calculates the specific frequency until the measurement accuracy reaches the required minimum accuracy The above process ends the iteration and outputs the measurement results.
[0015] The method of the present invention ensures that the measurement time window of the secondary timer is always an integer multiple of the measured signal period, and the actual measurement accuracy is guaranteed to be at least 100% by counting the number of times not less than 50M. The above is the frequency measurement method with equal accuracy. In a wider frequency range and in a shorter time, this method can quickly improve the frequency measurement accuracy to above.
[0016] Compared with the prior art, the present invention uses an iterative method to improve the equal-precision frequency measurement method without adding any external circuit and processing, so that the frequency measurement range, speed and accuracy are greatly improved. The present invention only uses two built-in timers of the main control chip STM32G4, and the two timers are cascaded from the inside using the slave mode controller plus input capture mode, eliminating external physical wiring, reducing the complexity of hardware and GPIO occupancy, and reducing resource utilization. And the method of the present invention is simple and easy to implement, has high precision, low error, and can be implemented without any peripheral circuit. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 A system connection block diagram for implementing the present invention;
[0018] Figure 2 The figure is a flow chart of the method of the present invention. DETAILED DESCRIPTION
[0019] The present invention is described in detail below with reference to the accompanying drawings.
[0020] According to the internal connection relationship table of the STM32G4 timer in Table 1, two general-purpose timers TIM2 and TIM4 that can be internally cascaded are selected as the first and second level timers respectively. Figure 1 , the overall system connection diagram is as follows Figure 1 , connect the signal to be tested from PA0 to the first-level timer TIM2 as the clock source and trigger source.
[0021] Table 1
[0022] From the timer ITR0(TS=000) ITR1(TS=001) ITR2(TS=010) ITR3(TS=011) TIM2 TIM1 TIM8 TIM3 TIM4 TIM3 TIM1 TIM2 TIM5 TIM4 TIM4 TIM1 TIM2 TIM3 TIM8 TIM5 TIM2 TIM3 TIM4 TIM8
[0023] See also Figure 2 , the specific steps of the present invention are as follows:
[0024] Step 1: Initialize the first-level timer TIM2. Set its master-slave mode to external clock mode 1, trigger source to TI1FP1, channel 2 to PWM generation without output mode, prescaler value PSC TIM2 To 0, set the reload register cycle ARR TIM2 When 1 is set, the counting mode is up counting, the master-slave mode of the TRGO feature is enabled, and the TRGO trigger event is set to output compare.
[0025] Step 2: Initialize the secondary timer TIM4. Set its trigger source to ITR1, which is TIM2 as shown in Table 1. The clock source is the internal clock. Since the main frequency of STM32G4 is 170M, the APB1 where TIM4 is located is 170M, so the clock source is 170M, and the standard signal fs of the secondary timer is 170M. Channel 1 selects the input capture mode triggered by TRC. Set PSCTIM4 ARR is 0 TIM4 The value is 65535, and the counting mode is up counting. After the initialization is completed, the TIM4 channel 1 input capture is turned on, and the CR1 register of TIM4 is written at the same time, so that the update event of TIM4 is only generated by the overflow of the counter CNT.
[0026] Step 3: Update the global variable overflow_num in the TIM4 count overflow interrupt processing function to record the overflow times. When the TIM4 input capture captures the rising edge, first clear the interrupt flag bit and then enter the interrupt processing function.
[0027] Step 4: The interrupt processing function is as follows: take the current count value IC2Value from the CCR1 register of TIM4, and then combine it with the overflow count overflow num , calculate the current total count times as
[0028] Ns=IC2Value+overflow num *65536
[0029] In the formula, Ns is the total count times of TIM4 of the secondary timer when entering the interruption; IC2Value is the current count value of the secondary timer TIM4; overflow num Is the number of global variable overflows.
[0030] That is, after the measured signal fx is divided by ARR in the first-level timer, it is used as input capture to trigger the second-level timer to count. The count value is Ns and the signal frequency is fs.
[0031] Step 5: At this time, the PWM wave period output by the first-stage timer is
[0032]
[0033] In the formula, T PWM HCLK is the period of the PWM wave output by the first-level timer TIM2; TIM2 That is the frequency of the clock source of the first-level timer TIM2; ARR TIM2 PSC is the loading value of the first-level timer TIM2; TIM2 It is the pre-division coefficient of the first-level timer TIM2.
[0034] The time difference between the two rising edge input captures of the secondary timer is a period T of the PWM wave output by the first-stage timer. PWM The clock source of the first-level timer TIM2 is directly connected to the measured signal. Then the measured signal frequency fx is derived according to the following formula:
[0035]
[0036]
[0037] PSC in the formula TIM4 is the pre-division value of the secondary timer TIM4; fs is the internal 170M clock of the secondary timer TIM4, that is, the frequency of the frequency measurement standard signal; fx is the frequency of the signal to be measured.
[0038] The frequency result fx of this measurement can be calculated.
[0039] Step 6: If the total count times Ns of the secondary timer is less than 50M times, it means that the measured frequency is large, the frequency measurement standard signal fs is triggered less times in one cycle, the error is large, and the measurement accuracy cannot reach the required minimum accuracy. Then set the ARR of the first-level timer TIM2 as follows
[0040] ARR TIM2 =fx
[0041] Step 7: After resetting the load value, first turn off the timer and interrupt, and reinitialize the two-stage timer. When reinitializing TIM2, you need to disable input capture first, and then reinitialize. Finally, clear the interrupt flag and continue to start frequency measurement.
[0042] At this point, after one iteration, the PWM wave output by the first-level timer after fx division is close to a period of 1s. Then, according to the time window of about 1s, the capture signal is input to measure the trigger times of the 170M standard signal, which has a very high accuracy. Step 8: Then repeat the process starting from step 3 and wait for the interrupt. When entering the interrupt processing function of step 4 next time, calculate fx again. If it still cannot meet the minimum accuracy requirement, continue to iterate until the measurement accuracy is greater than End measurement and output results.
Claims
1. A method for measuring frequency with equal precision based on the iterative method of STM32, characterized in that: The method comprises the following specific steps: Step 1: The signal to be tested is connected to the primary timer clock, and the timer uses PWM to generate a no-output mode; the frequency division coefficient is initialized to 0 for the first time, and the load value is 1; the PWM wave is generated and connected to the secondary timer through the input capture cascade from the inside; Step 2: The secondary timer uses the input TRC trigger input capture mode; the initialization frequency division coefficient is 0, and the load value is set to a number between 1-65535; Step 3: Set the secondary timer overflow interrupt, and update the global variable to record the overflow times in the interrupt; Step 4: Set the secondary timer input capture interrupt and calculate the frequency to be tested in the interrupt; Step 5: After the secondary timer triggers an input capture interrupt and performs frequency calculation, if the measurement accuracy does not meet the minimum expected accuracy indicator The requirement is that the total trigger times of the secondary timer is less than 50M times, then iteration is performed, and the measured frequency is set as the loading value of the first-level timer, so that the PWM wave output by the first-level timer gradually approaches 1s after iteration, and then the secondary timer uses input capture measurement based on the 170M clock and calculates the specific frequency again; Step 6: Repeat steps 3 to 5 and iterate until the measurement accuracy reaches the minimum expected accuracy index. End the frequency measurement and output the results.
Citation Information
Patent Citations
Singlechip adopting RISC structure
CN101339498A
Adaptive period frequency measuring system and method
CN108872702A