Low-frequency-band fixed square wave frequency high-precision measurement method based on kernel clock

Through the core clock of the embedded system, the software logic is used to measure the duration of high and low levels in the fixed time window, solving the high accuracy problem of fixed square wave frequency measurement in the low-frequency band with limited hardware resources, and realizing high-precision frequency measurement in the fields of embedded systems and digital signal processing.

CN120385852APending Publication Date: 2025-07-29GUILIN UNIV OF ELECTRONIC TECH
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510481345.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-17
Publication Date
2025-07-29

AI Technical Summary

Technical Problem

In the case of limited hardware resources or inability to use interrupts, it is difficult for the prior art to achieve high-precision measurement of low-frequency band fixed square wave frequency.

Method used

Through the embedded system's kernel clock, the count value of the high and low level duration in the fixed time window is measured, and the frequency calculation is realized using software logic, including detecting rising and falling edges for counting, and calculating the average of the frequency.

Benefits of technology

It realizes high-precision measurement of low-frequency fixed square wave frequency when hardware resources are constrained, reduces dependence on hardware resources, and is suitable for embedded systems and digital signal processing fields.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120385852A_ABST
    Figure CN120385852A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of signal frequency measurement, in particular to a low-frequency-band fixed square wave frequency high-precision measurement method based on a kernel clock, which is suitable for the field of embedded systems and digital signal processing, is mainly used for low-frequency and fixed-frequency-band high-precision frequency measurement, and is not a frequency meter method specially used for measuring frequency. The method is suitable for measuring the fixed output frequency in a test system and the like. According to the method, a kernel clock of an embedded system (such as an STM32 single-chip microcomputer) is mainly utilized, a time constant is calibrated by measuring the count value of the duration of high and low levels of a fixed time window, and therefore the square wave frequency is obtained through calculation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of signal frequency measurement, and particularly to a high-precision measurement method for the fixed square wave frequency in a low frequency band based on the kernel clock. This method is applicable to the fields of embedded systems and digital signal processing, and is mainly used for high-precision frequency measurement of fixed and low frequency bands, rather than a frequency meter method specifically for measuring frequency, and is applicable to the measurement of fixed output frequency in a test system. Background Art

[0002] The method of the present invention mainly relies on software logic and is implemented by polling the level status of GPIO, without the need to use a hardware timer or an interrupt mechanism. Therefore, the requirements for hardware resources are low. In the case where hardware resources are limited (such as insufficient timer numbers) or interrupts cannot be used, this method can be used as an alternative solution.

[0003] The method of the present invention for measuring the square wave frequency by directly polling the GPIO level change is essentially a signal processing technology based on software sampling. The software implementation method has high flexibility and does not require specific hardware support. The sampling and calculation processes are completely controlled by software code.

[0004] In some ultra-low-cost MCUs, there may be only one or two timers, and these timers need to be used for key system tasks (such as PWM output, system clock, etc.). At this time, frequency measurement can only be achieved through software.

[0005] Or in some test systems, in order to test whether the output fixed frequency meets the requirements, and at the same time to improve performance and reduce the CPU burden, this method can be used for measuring the fixed frequency in the low frequency band. Summary of the Invention

[0006] In view of the situation mentioned in the technical background, the present invention proposes a high-precision measurement method for the fixed square wave frequency in a low frequency band based on the kernel clock. The inventive method mainly utilizes the kernel clock of an embedded system (such as an STM32 single-chip microcomputer), and calibrates the time constant by measuring the count values of the high and low level durations in a fixed time window, thereby calculating the square wave frequency.

[0007] A high-precision measurement method for the fixed square wave frequency in a low frequency band based on the kernel clock, characterized by comprising the following steps:

[0008] S1: Identify the kernel clock of the embedded system (such as STM32).

[0009] S2: Determine how many square wave periods of time need to be taken for measurement, and define this period of time as the measurement time window.

[0010] S3: First, use the rising edge of the detected square wave signal level as the counting point for starting the self-incrementing count based on the kernel clock.

[0011] S4: After detecting the rising edge, perform self-incrementing count through count1, detect the level while performing self-incrementing count, and perform self-incrementing count through count2 when it is at a low level.

[0012] S5: Sum the high and low level count values through a fixed window period to count the total count value within this window time.

[0013] S6: Calculate the frequency value f1 through a formula.

[0014] S7: Use the falling edge of the detected square wave signal level as the counting point for starting the self-incrementing count based on the kernel clock, and repeat steps S3 - S5.

[0015] S8: Calculate the frequency value f2 through a formula.

[0016] S9: Calculate the average value of the two measurements to obtain the measured frequency

[0017] Furthermore, for the inventive method, it is characterized in that the measured square wave frequency range of the measurement method should be relatively low and limited by the kernel clock frequency of the system. For example, the measurable frequency range is estimated to be between 1 Hz and 10 kHz. However, the used frequency band should be fixed. For example, the measured frequency range is in the range of 1 Hz - 3 kHz, and the peak-to-peak value of the square wave needs to conform to the input level of the used microcontroller or embedded platform.

[0018] Furthermore, for S1, it is characterized in that the working frequency of the kernel clock needs to be determined according to different platforms and different configurations, and the kernel clock of the currently used embedded system (such as STM32) should be clarified. For example, the kernel clock FCLK of STM32F103 can be up to 72 MHz.

[0019] Furthermore, for S2, it is characterized in that the number of square wave periods can be selected according to the actual calibration situation, such as 256 square wave periods or others.

[0020] Furthermore, for S6, it is characterized in that assuming 256 square wave periods are selected, the frequency calculation formula is:

[0021]

[0022] Among them, C is the constant to be calibrated, C = CCLK * 2.56x, 0 < x ≤ 1, 2.56 is to reduce the number of periods by 100 times, CCLK is the kernel clock, and Count = Count1 + Count2.

[0023] Furthermore, the calculation frequency formula of S6 is derived as follows under the conditions that the length of the measurement window is assumed to be 256 complete cycle lengths of the measured square wave frequency and the CCLK frequency is 72 MHz:

[0024] Let the total count value of the high level be count1, the total count value of the low level be count2, the total count value of the high and low levels be count, and the counting time corresponding to each count value be T machinecycle , and the time of each machine cycle is:

[0025]

[0026] Then, during the complete 256 square wave cycles, the durations of the high level and the low level are:

[0027]

[0028] Furthermore, the derivation principle of the calculation frequency formula of S6 is the definition of frequency. That is to say, the reciprocal of the time of a single square wave cycle is the frequency of the square wave. Therefore, we have:

[0029]

[0030] where T H , T L is the sum of the high and low level times of 256 cycles.

[0031] However, for accurate measurement, assuming that the time of 256 cycles is measured, the formula is transformed as:

[0032]

[0033] where T is the total time under 256 cycles and T = (T H + T L ) × 256. Substituting T h , T L can obtain:

[0034]

[0035] If it is assumed that the time spent on each self-increment counting is 1 machine cycle, then the calibration constant C is: CCLK × 2.56; if the execution time of the statement consumes 2 machine cycles, that is, the time becomes longer, it can be considered that the system clock is divided. Then, there should be:

[0036]

[0037] Therefore, in order to meet the clock requirements and instruction processing speeds of different test platforms, it can be further simplified into a general formula:

[0038]

[0039] Where x is the calibration coefficient and CCLK is the core clock. The calibration can be tested according to the actual measurement results. Since CCLK is different on different platforms, but the time of each self-increment in the algorithm is fixed, that is, the number of machine cycles required during each high-level count self-increment is fixed. Finally, the frequency measurement constant C = CCLK * 1.28x is determined, where 0 < x ≤ 1.

[0040] Further, S7 to S9 are characterized in that the detection of the falling edge is used as the time point for starting self-increment counting based on the core clock, and symmetric detection is performed with the previous time point for starting self-increment counting based on the core clock by detecting the falling edge, so as to reduce the measurement error of any duty cycle.

[0041] It should be noted that the present invention is characterized in that the method is applicable to the fields of embedded systems and digital signal processing, and is mainly used for high-precision frequency measurement in a fixed and low-frequency band, rather than a frequency meter method dedicated to measuring frequency. It is applicable to the measurement of a fixed output frequency in a test system; or in the case where hardware resources are limited (such as insufficient timer numbers) or interrupts cannot be used, this method can be used as a general alternative solution. Description of the Drawings

[0042] Figure 1 It is a flowchart of a method for high-precision measurement of the frequency of a fixed square wave in a low-frequency band based on the core clock provided by the present invention.

[0043] Figure 2 It is a test result diagram of the square wave frequencies with frequencies of 10Hz, 100Hz, 505Hz, 987Hz, 1000Hz, 2000Hz, 3000Hz, 3500Hz and a duty cycle of 50% measured based on STM32F103 provided by the present invention.

[0044] Figure 3 It is a test result diagram of the square wave frequencies with frequencies of 10Hz, 100Hz, 505Hz, 987Hz, 1000Hz, 2000Hz, 3000Hz, 3500Hz and duty cycles of 10% and 30% measured based on STM32F103 provided by the present invention.

[0045] Figure 4 It is a test result diagram of the square wave frequencies with frequencies of 10Hz, 100Hz, 505Hz, 987Hz, 1000Hz, 2000Hz, 3000Hz, 3500Hz and duty cycles of 70% and 90% measured based on STM32F103 provided by the present invention. Detailed Embodiments

[0046] To make the above objects, features, and functions of the present invention more clearly understandable, the following will briefly introduce the drawings required for the description of the embodiments.

[0047] It should be noted that the following detailed description is exemplary and is intended to provide further explanation of the present invention. For those of ordinary skill in the art, without creative effort, other drawings can also be obtained based on these drawings.

[0048] Embodiment 1.

[0049] As Figure 1 shown, this embodiment provides a flowchart of a high-precision measurement method for the fixed square wave frequency based on the kernel clock. Specifically, it includes the following steps:

[0050] S1: Use the single-chip microcomputer STM32F103C8T6, and the kernel clock FCLK = 72 MHz.

[0051] S2: Take the time of 256 square wave cycles for measurement, and define this cycle time as the measurement time window.

[0052] S3: First, use the rising edge of the detected square wave signal level as the counting point for starting the self-increment counting based on the kernel clock.

[0053] S4: After detecting the rising edge, perform self-increment counting through count1, while detecting the level and performing self-increment counting. When it is a low level, perform self-increment counting through count2.

[0054] S5: Sum the high and low level count values through the determined window period, and count the total count value Count within this window time.

[0055] S6: Calculate the frequency value f1 through the formula.

[0056] S7: Use the falling edge of the detected square wave signal level as the counting point for starting the self-increment counting based on the kernel clock, and repeat steps S3 - S5.

[0057] S8: Calculate the frequency value f2 through the formula.

[0058] S9: Calculate the average value of the two measurements to obtain the measured frequency

[0059] The above is only the preferred embodiment of the present invention. In addition, for those of ordinary skill in the technical field, various modifications and variations can be made to the invention while maintaining the principle of the present invention. If the modifications and variations fall within the scope of the claims of the present invention and their equivalent technologies, these modifications and variations should also be regarded as within the protection scope of the present invention.

Claims

1. A high-precision measurement method for the fixed square wave frequency in the low-frequency band based on the kernel clock, characterized in that It includes the following steps: S1: Determine the core clock of the embedded system (such as STM32); S2: Determine how many square wave cycles are needed for measurement, and set this cycle time as the measurement time window; S3: First, use the rising edge of the square wave signal level detection as the counting point for self-incrementing counting based on the core clock; S4: After detecting the rising edge, perform self-incrementing counting through count1, detect the level while performing self-incrementing counting, and perform self-incrementing counting through count2 when it is at a low level; S5: Sum the high and low level count values through the specified window period, and count the total value within this window time; S6: Calculate the frequency value f1 through a formula; S7: Use the falling edge of the square wave signal level detection as the counting point for self-incrementing counting based on the core clock, and repeat steps S3 - S5; S8: Calculate the frequency value f2 through a formula; S9: The measured frequency can be obtained by calculating the average value of the two measurements 2. The method according to claim 1, characterized in that, The measured square wave frequency range of the measurement method should be relatively low and limited by the core clock frequency of the system. For example, the measurable frequency range is estimated to be between 1 Hz and 10 kHz, but the operating frequency band should be fixed. For example, the measured frequency range is within the range of 1 Hz - 3 kHz, and the peak-to-peak value of the square wave needs to meet the input level of the used microcontroller or embedded platform.

3. The method according to claim 1, characterized in that, In S1, for example, the core clock FCLK of STM32F103 can be up to 72 MHz, and the core clock operating frequency needs to be determined according to different platforms and configurations.

4. The system according to claim 1, wherein The number of square wave cycles in S2 can be selected according to the actual calibration situation, such as 256 square wave cycles.

5. The method according to claim 1, characterized in that, Assuming 256 square wave cycles are selected, the formula in S6 is: Where C is the constant to be calibrated, C = CCLK * 2.56x, 0 < x ≤ 1, 2.56 is to reduce the cycle number by 100 times, CCLK is the core clock, and Count = Count1 + Count2.

6. The method according to claim 1, wherein This method is mainly used for high-precision frequency measurement of low-frequency and fixed frequency bands, rather than a frequency meter method specifically for measuring frequency, and is applicable to output fixed frequency measurement in a test system, etc.

Citation Information

Cited By

  • Software frequency measurement method and system based on interleaving sampling

    CN121899483A