Square wave generation method and device with feedback mechanism
Patent Information
- Application Number
- CN202311027274.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-15
- Publication Date
- 2026-10-30
- Estimated Expiration
- 2043-08-15
AI Technical Summary
现有技术往往通过fpag的硬件方案实现,但硬件实现成本较高,而且不够灵活
[0032]本发明的有益效果是:本发明的优点是可以规避掉定时器的误差造成的影响,通过CPU晶振频率精准判断波形的频率误差,并通过反馈机制来修正误差,可以通过迭代后把误差控制到一个很小的范围。
Smart Images

Figure CN117075682B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of industrial control safety technology, and in particular to a square wave generation method and apparatus with a feedback mechanism. Background Technology
[0002] In certain scenarios within the industrial safety field, testing equipment needs to output a square wave of a specified frequency to the device under test (PLC, DCS, etc.) to control the device's business logic. Existing technologies often implement this through FPGA hardware solutions, but hardware implementation is costly and lacks flexibility.
[0003] Based on this, the present invention proposes a square wave generation method and apparatus with a feedback mechanism, which can accurately control the output frequency of the square wave. Summary of the Invention
[0004] This invention provides a square wave generation method with a feedback mechanism, comprising:
[0005] It receives the frequency set by the user and outputs a square wave according to the frequency set by the user.
[0006] When the timer times out, the timer processing function is called to change the current voltage based on the relationship between the timer precision and the frequency set by the user.
[0007] Determine the frequency error of the square wave by checking the CPU crystal oscillator frequency;
[0008] Use frequency error correction to correct the user-set frequency;
[0009] The corrected frequency is fed back to the timer processing function through a feedback mechanism to correct the output square wave.
[0010] The square wave generation method with feedback mechanism described above includes determining the timer precision T, setting a value N such that N = 1 / T, and setting the user-defined frequency Q, where Q <= N.
[0011] The square wave generation method with feedback mechanism described above, wherein changing the current voltage based on the relationship between the timer accuracy and the user-set frequency, specifically includes the following sub-steps:
[0012] Step S1: Define a variable SUM;
[0013] Step S2: Calculate SUM = SUM + 2 * Q;
[0014] Step S3: If SUM >= N, then change the current voltage;
[0015] Step S4: Update variable SUM = SUM - N, then return to step S2.
[0016] The square wave generation method with feedback mechanism described above, wherein the frequency error of the square wave is determined by the CPU crystal oscillator frequency, specifically includes the following sub-steps:
[0017] An accumulator is created for the generated square wave to accumulate the number of square wave transformations;
[0018] When the cumulative number of square wave transformations reaches the preset number of transformations, obtain the CPU crystal oscillator oscillation number M corresponding to the preset number of transformations;
[0019] Convert the CPU crystal oscillator oscillation count M into time TT;
[0020] Calculate the time error of the square wave for the preset number of transformations based on the converted time TT and the current time, and calculate the average time error DT of each square wave.
[0021] Calculate the frequency error DQ corresponding to the time error DT.
[0022] As described above, a square wave generation method with a feedback mechanism is provided, wherein a threshold is set, and when the frequency error DQ is less than the threshold, no correction or feedback processing is performed; when the frequency error DQ is greater than the preset value, the frequency Q input by the user is corrected.
[0023] The square wave generation method with feedback mechanism described above, wherein Q = Q - DQ.
[0024] As described above, a square wave generation method with a feedback mechanism is used where, if a timer needs to output multiple different frequencies, multiple sets of variables are defined to achieve this.
[0025] The present invention also provides a square wave generation device with a feedback mechanism, comprising:
[0026] The square wave output module is used to receive the frequency set by the user and output a square wave according to the frequency set by the user.
[0027] The voltage changing module is used to call the timer processing function when the timer times out, and change the current voltage according to the relationship between the timer accuracy and the frequency set by the user.
[0028] The error correction module is used to determine the frequency error of the square wave based on the CPU crystal oscillator frequency; and to correct the user-set frequency using the frequency error.
[0029] The feedback module is used to feed the corrected frequency back to the timer processing function through a feedback mechanism to correct the output square wave.
[0030] The square wave generation device with feedback mechanism described above is a microcontroller including a timer.
[0031] The present invention also provides a computer-readable storage medium, characterized in that the computer storage medium contains one or more program instructions, the one or more program instructions being executed by a processor as described in any of the preceding claims, a square wave generation method with feedback mechanism.
[0032] The beneficial effects of this invention are: the invention can avoid the influence of timer errors, accurately determine the frequency error of the waveform through the CPU crystal oscillator frequency, and correct the error through a feedback mechanism. Through iteration, the error can be controlled to a very small range. Attached Figure Description
[0033] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort, wherein:
[0034] Figure 1 This is a flowchart of a square wave generation method with feedback mechanism provided in Embodiment 1 of the present invention;
[0035] Figure 2 This is a schematic diagram of a square wave generation device with a feedback mechanism provided in Embodiment 2 of the present invention. Detailed Implementation
[0036] The technical solutions in the embodiments of the present invention will be clearly and completely described below. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of them. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0037] Example 1
[0038] Embodiment 1 of this application uses a microcontroller and software-based method to precisely control the output frequency of a square wave. Since a square wave is a periodic waveform, a microcontroller timer is required. However, timers have limited minimum precision; for example, the timing precision of a microcontroller might be 10ms, 1ms, or 0.1ms. If the voltage is directly changed within the timer to generate the waveform, the error may be significant. For instance, outputting a 777Hz square wave using a 1ms timer would result in a very large timer error, and one timer can only output one waveform.
[0039] To solve this problem, such as Figure 1 The present invention provides a square wave generation method with a feedback mechanism, comprising:
[0040] Step 110: Receive the frequency set by the user and output a square wave according to the frequency set by the user;
[0041] Specifically, determine the timer precision T, such as 0.1ms, 1ms, 10ms, etc.; set a value N, let N = 1 / T, and set the user-defined frequency Q, where Q <= N.
[0042] Step 120: When the timer times out, call the timer processing function to change the current voltage according to the relationship between the timer accuracy and the frequency set by the user;
[0043] Create a timer function `func`. When the timer expires, call the `func` function to change the voltage.
[0044] The current voltage is changed based on the relationship between the timer accuracy and the user-set frequency, specifically including the following sub-steps:
[0045] Step S11: Define a variable SUM;
[0046] Step S12: Calculate SUM = SUM + 2 * Q;
[0047] The frequency Q is doubled here because one cycle requires half the time at high voltage and half the time at low voltage.
[0048] Step S13: If SUM >= N, then change the current voltage;
[0049] If it is high voltage, it will become low voltage; if it is low voltage, it will become high voltage.
[0050] Step S14: Update variable SUM = SUM - N, then return to step S12 for repeated processing.
[0051] See back Figure 1 Step 130: Determine the frequency error of the square wave by using the CPU crystal oscillator frequency;
[0052] Because timer errors exist, the output frequency may have some error. To eliminate this error, this application performs the following operations:
[0053] Step S21: Add an accumulator to the generated square wave to accumulate the number of square wave transformations;
[0054] Step S22: When the cumulative number of square wave transformations reaches the preset number of transformations, obtain the CPU crystal oscillator oscillation number M corresponding to the preset number of transformations;
[0055] For example, when the cumulative transformation is exactly 100 times, obtain the number of crystal oscillations M corresponding to these 100 transformations.
[0056] Step S23: Convert the CPU crystal oscillator oscillation count M into time TT;
[0057] Step S24: Calculate the time error of the square wave for the preset number of transformations based on the converted time TT and the current time, and calculate the average time error DT of each square wave.
[0058] Step S25: Calculate the frequency error DQ corresponding to the time error DT.
[0059] See back Figure 1 Step 140: Correct the user-set frequency using frequency error correction;
[0060] Preferably, a threshold is set. When the frequency error is less than the threshold, no correction or feedback processing is performed. When the frequency error is greater than the preset value, the frequency Q input by the user is corrected, where Q = Q - DQ.
[0061] Step 150: Feed the corrected frequency back to the timer processing function through a feedback mechanism to correct the output square wave.
[0062] The corrected frequency Q is fed back into the timer processing function for further calculation to obtain the corrected frequency (this feedback process is the feedback mechanism).
[0063] Furthermore, if a timer needs to output multiple different frequencies, the above steps can be repeated to define several sets of variables to achieve this, which will not be elaborated here.
[0064] Example 2
[0065] like Figure 2 As shown, Embodiment 2 of the present invention provides a square wave generation device with a feedback mechanism. The device is a microcontroller including a timer, specifically comprising:
[0066] Square wave output module 21 is used to receive the frequency set by the user and output a square wave according to the frequency set by the user.
[0067] The voltage changing module 22 is used to call the timer processing function when the timer times out, and change the current voltage according to the relationship between the timer accuracy and the frequency set by the user.
[0068] Error correction module 23 is used to determine the frequency error of the square wave based on the CPU crystal oscillator frequency; and to correct the user-set frequency using the frequency error.
[0069] Feedback module 24 is used to feed the corrected frequency back to the timer processing function through a feedback mechanism to correct the output square wave.
[0070] Corresponding to the above embodiments, the present invention provides a square wave generation device with a feedback mechanism, comprising: at least one memory and at least one processor;
[0071] The memory is used to store one or more program instructions;
[0072] A processor is used to run one or more program instructions to execute a square wave generation method with a feedback mechanism.
[0073] Corresponding to the above embodiments, this embodiment of the invention provides a computer-readable storage medium containing one or more program instructions, which are executed by a processor to provide a square wave generation method with a feedback mechanism.
[0074] The embodiments disclosed in this invention provide a computer-readable storage medium storing computer program instructions that, when executed on a computer, cause the computer to perform the aforementioned square wave generation method with a feedback mechanism.
[0075] In this embodiment of the invention, the processor can be an integrated circuit chip with signal processing capabilities. The processor can be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0076] The various methods, steps, and logic diagrams disclosed in the embodiments of this invention can be implemented or executed. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of this invention can be directly implemented by a hardware decoding processor, or implemented by a combination of hardware and software modules in the decoding processor. The software modules can reside in random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or other mature storage media in the art. The processor reads information from the storage medium and, in conjunction with its hardware, completes the steps of the above methods.
[0077] The storage medium can be memory, such as volatile memory or non-volatile memory, or may include both volatile and non-volatile memory.
[0078] Among them, non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory.
[0079] Volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDRSDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous linked dynamic random access memory (Synchlink DRAM, SLDRAM), and direct memory bus RAM (DRRAM).
[0080] The storage media described in the embodiments of the present invention are intended to include, but are not limited to, these and any other suitable types of memory.
[0081] Those skilled in the art will recognize that, in one or more of the examples above, the functions described in this invention can be implemented using a combination of hardware and software. When applied as software, the corresponding functions can be stored in a computer-readable medium or transmitted as one or more instructions or code on a computer-readable medium. Computer-readable media include computer storage media and communication media, wherein communication media include any medium that facilitates the transmission of computer programs from one place to another. Storage media can be any available medium that can be accessed by a general-purpose or special-purpose computer.
[0082] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made on the basis of the technical solution of the present invention should be included within the scope of protection of the present invention.
Claims
1. A method for generating square waves with a feedback mechanism, characterized in that, include: It receives the frequency set by the user and outputs a square wave according to the frequency set by the user. When the timer times out, the timer processing function is called to change the current voltage based on the relationship between the timer precision and the frequency set by the user. Determine the frequency error of the square wave by checking the CPU crystal oscillator frequency; Use frequency error correction to correct the user-set frequency; The corrected frequency is fed back to the timer processing function through a feedback mechanism to correct the output square wave; Determine the timer precision T, set a value N, let N = 1 / T, and set the user-defined frequency Q, where Q <= N; The current voltage is changed based on the relationship between the timer accuracy and the user-set frequency, specifically including the following sub-steps: Step S1: Define a variable SUM; Step S2: Calculate SUM = SUM + ; Step S3: If SUM >= N, then change the current voltage; Step S4: Update variable SUM = SUM - N, then return to step S2; Determining the frequency error of a square wave using the CPU crystal oscillator frequency involves the following sub-steps: An accumulator is created for the generated square wave to accumulate the number of square wave transformations; When the cumulative number of square wave transformations reaches the preset number of transformations, obtain the CPU crystal oscillator oscillation number M corresponding to the preset number of transformations; Convert the CPU crystal oscillator oscillation count M into time TT; Calculate the time error of the square wave for the preset number of transformations based on the converted time TT and the current time, and calculate the average time error DT of each square wave. Calculate the frequency error DQ corresponding to the time error DT; Set a threshold. When the frequency error DQ is less than the threshold, no correction or feedback processing is performed. When the frequency error DQ is greater than the threshold, the frequency Q input by the user is corrected. Q = Q - DQ.
2. The square wave generation method with feedback mechanism as described in claim 1, characterized in that, If a timer needs to output multiple different frequencies, then define multiple sets of variables to achieve this.
3. A square wave generator with a feedback mechanism, performing the method as described in any one of claims 1-2, characterized in that, The device includes: The square wave output module is used to receive the frequency set by the user and output a square wave according to the frequency set by the user. The voltage changing module is used to call the timer processing function when the timer times out, and change the current voltage according to the relationship between the timer accuracy and the frequency set by the user. The error correction module is used to determine the frequency error of the square wave based on the CPU crystal oscillator frequency; and to correct the user-set frequency using the frequency error. The feedback module is used to feed the corrected frequency back to the timer processing function through a feedback mechanism to correct the output square wave.
4. A square wave generator with feedback mechanism as described in claim 3, characterized in that, The device is a microcontroller that includes a timer.
5. A computer-readable storage medium, characterized in that, The computer storage medium contains one or more program instructions, which are executed by a processor as described in any one of claims 1-2, to generate a square wave with a feedback mechanism.
Citation Information
Patent Citations
Anti-interference method and system for CPU system, and its error measuring method and system
CN101470637A
A signal generating circuit used for detecting a vehicle system
CN202974671U
