Timer-based Scanning Period Determination Method and Device
By using two timers in the PLC control system, obtaining their count values and determining the scanning cycle according to specific conditions, the problem of inaccurate PLC scanning cycles in the prior art is solved, and more accurate and stable scanning cycle calculations are achieved.
Patent Information
- Application Number
- CN202310065237.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-02-06
- Publication Date
- 2025-06-10
- Estimated Expiration
- 2043-02-06
AI Technical Summary
The scanning period of the PLC calculated by the prior art is inaccurate, especially when the count value of the timer reaches the upper limit value or 0.
By using two timers (first timer and second timer) in the control system, the second timer is started in the first count cycle after the first timer is started, the count values of the two timers are obtained, and the scanning period is determined based on the respective count values under specific conditions.
It solves the time error problem caused by the timer from enabled to unenable and from unenable to enabled, ensuring the accuracy and stability of the scanning cycle calculation.
Smart Images

Figure CN116027729B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of timers, and in particular to a method and device for determining a scan cycle based on a timer. Background Art
[0002] The calculation method of the scan cycle T of a PLC is generally T=(k - k0)*t, where k is the count value of the timer read in the current scan cycle, k0 is the count value of the timer read in the previous scan cycle, and t is the timing unit of the timer, usually 0.1 ms. It can be seen that T depends on the readings of k and k0. However, when the count value of the timer reaches the upper limit value or 0, the timer stops the timing function, and the calculated scan cycle is inaccurate at this time. Summary of the Invention
[0003] The present invention provides a method and device for determining a scan cycle based on a timer, and solves the technical problem that the calculated scan cycle of a PLC in the prior art is inaccurate.
[0004] On the one hand, an embodiment of the present invention provides the following technical solution:
[0005] A method for determining a scan cycle based on a timer, which is applied to a control system including a first timer and a second timer, and the first timer and the second timer have the same maximum count value. The method includes:
[0006] Control the first timer to start;
[0007] Within the first counting cycle after the first timer starts, control the second timer to start;
[0008] Obtain the count values of the first timer and the second timer;
[0009] After the count value of the first timer reaches a first preset value and before the count value of the second timer reaches a second preset value, determine the scan cycle according to the count value of the first timer;
[0010] After the count value of the second timer reaches the second preset value and before the count value of the first timer reaches the first preset value, determine the scan cycle according to the count value of the second timer.
[0011] Preferably, the first preset value is the same as the second preset value.
[0012] Preferably, determining the scan cycle according to the count value of the first timer includes: T=(x2 - x1)*t, where T is the scan cycle, x2 is the count value of the first timer in the current scan cycle, x1 is the count value of the first timer in the previous scan cycle, and t is the timing unit;
[0013] Determining the scanning period according to the count value of the second timer includes: T = (y2 - y1) * t, where y2 is the count value of the second timer in the current scanning period, and y1 is the count value of the second timer in the previous scanning period.
[0014] Preferably, the maximum count values of the first timer and the second timer are both 2 31 - 1.
[0015] On the other hand, the embodiment of the present invention also provides the following technical solution:
[0016] A scanning period determination device based on a timer, which is applied to a control system including a first timer and a second timer, and the maximum count values of the first timer and the second timer are the same. The device includes:
[0017] A timer control module, configured to control the start of the first timer;
[0018] The timer control module is further configured to control the start of the second timer within the first counting cycle after the first timer starts;
[0019] A count value acquisition module, configured to acquire the count values of the first timer and the second timer;
[0020] A scanning period determination module, configured to determine the scanning period according to the count value of the first timer after the count value of the first timer reaches a first preset value and before the count value of the second timer reaches a second preset value;
[0021] The scanning period determination module is further configured to determine the scanning period according to the count value of the second timer after the count value of the second timer reaches the second preset value and before the count value of the first timer reaches the first preset value.
[0022] Preferably, the first preset value is the same as the second preset value.
[0023] Preferably, when the scanning period determination module determines the scanning period according to the count value of the first timer, it includes: T = (x2 - x1) * t, where T is the scanning period, x2 is the count value of the first timer in the current scanning period, x1 is the count value of the first timer in the previous scanning period, and t is the timing unit;
[0024] When the scanning period determination module determines the scanning period according to the count value of the second timer, it includes: T = (y2 - y1) * t, where y2 is the count value of the second timer in the current scanning period, and y1 is the count value of the second timer in the previous scanning period.
[0025] Preferably, the maximum count values of the first timer and the second timer are 2 31 -1.
[0026] On the other hand, the embodiments of the present invention further provide the following technical solutions:
[0027] An electronic device includes a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the program, the above-mentioned any timer-based scan cycle determination method is implemented.
[0028] On the other hand, the embodiments of the present invention further provide the following technical solutions:
[0029] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the above-mentioned any timer-based scan cycle determination method is implemented.
[0030] One or more technical solutions provided by the present invention have at least the following technical effects or advantages:
[0031] The present invention first controls the first timer to start, then controls the second timer to start within the first counting cycle after the first timer starts, obtains the count values of the first timer and the second timer, and determines the scan cycle according to the count value of the first timer after the count value of the first timer reaches the first preset value and before the count value of the second timer reaches the second preset value; after the count value of the second timer reaches the second preset value and before the count value of the first timer reaches the first preset value, the scan cycle is determined according to the count value of the second timer, which solves the problem of time error caused by the timer from being enabled to disabled and from being disabled to enabled. The calculation of the scan cycle is based on the calculation when the first timer and the second timer are in a stable and normal state, ensuring the accuracy and stability of the scan cycle calculation. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following will briefly introduce the drawings required for the description of the embodiments. Obviously, the following drawings are some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0033] Figure 1 It is a schematic diagram of the timer in the embodiment of the present invention;
[0034] Figure 2 It is a control timing diagram of the timer;
[0035] Figure 3Flow chart of the method for determining the scan cycle based on a timer in an embodiment of the present invention;
[0036] Figure 4 Control timing diagram of the first timer and the second timer in an embodiment of the present invention;
[0037] Figure 5 Structural schematic diagram of the device for determining the scan cycle based on a timer in an embodiment of the present invention. Detailed implementation manners
[0038] By providing the method and device for determining the scan cycle based on a timer in the embodiments of the present invention, the technical problem that the scan cycle of the PLC calculated in the prior art is inaccurate is solved.
[0039] To better understand the technical solution of the present invention, the technical solution of the present invention will be described in detail below in conjunction with the accompanying drawings of the specification and specific implementation manners.
[0040] Figure 1 Schematic diagram of a timer, having two inputs and two outputs. The two inputs are input IN and input PT, and the two outputs are output Q and output ET. Input IN is the enable signal, and the timer starts counting when input IN is 1; the value input to input PT is the maximum value of the timer count value, which can be 2 31 -1 = 2147483647 in this embodiment; the value of output Q is 0 when the count value of the timer is less than the maximum value and 1 when the count value of the timer is at the maximum value; output ET outputs the current count value of the timer, and output ET is 0 when input IN is 0. If the timing unit of the timer is 0.1 ms and input IN is 1, output ET is incremented by 1 every 0.1 ms. After the value of output ET reaches the maximum value, if input IN is still 1, the value of output ET remains at the maximum value.
[0041] Figure 2 Control timing diagram of the timer, from top to bottom are input IN, the maximum count value, and the count value of the timer. It can be seen that:
[0042] (1) As at point A in Figure 2 , if input IN of the current scan cycle is 1 and the count value of the timer has reached the maximum value for a period of time before the current scan cycle, the count difference between the count value of the timer read in the current scan cycle and the count value of the timer read in the previous scan cycle is obviously small, and the calculated scan cycle T is small;
[0043] (2) As at Figure 2At point B in [the figure], if the input IN in the current scan cycle is 0 and the input IN in the previous scan cycle is 1, then the timer count value read in the current scan cycle is 0, and the timer count value read in the previous scan cycle is greater than 0. The calculated scan cycle T is negative, which is obviously incorrect;
[0044] (3) As Figure 2 At point C in [the figure], if the input IN just becomes 1 in the current scan cycle and the input IN in the previous scan cycle is 0, then the timer just starts counting. The timer count values read in the current scan cycle and the previous scan cycle are both 0, and the calculated scan cycle T is 0, which is obviously incorrect.
[0045] It can be seen that in many cases, the scan cycle calculated from the count value of a single timer is not accurate.
[0046] Therefore, as Figure 3 shown, this embodiment provides a method for determining the scan cycle based on a timer, which is applied to a control system including a first timer and a second timer. The first timer and the second timer have the same maximum count value. The method includes:
[0047] Step S1, control the first timer to start;
[0048] Step S2, within the first counting cycle after the first timer starts, control the second timer to start;
[0049] Step S3, obtain the count values of the first timer and the second timer;
[0050] Step S4, after the count value of the first timer reaches the first preset value and before the count value of the second timer reaches the second preset value, determine the scan cycle according to the count value of the first timer;
[0051] Step S5, after the count value of the second timer reaches the second preset value and before the count value of the first timer reaches the first preset value, determine the scan cycle according to the count value of the second timer.
[0052] The control system of this embodiment can be a PLC. The maximum count values of the first timer and the second timer can be 2 31 -1. As Figure 4 shown, the first timer is denoted as T1, the second timer is denoted as T2, the first preset value is denoted as n1, the second preset value is denoted as m1, and A is defined as a boolean quantity, and A is an asymmetric square wave. After T1 and T2 start, their count values both cycle from 0 to the maximum value. In step S2, the first counting cycle is the first cycle in which the count value of T1 changes from 0 to the maximum value. The start time of T2 is preferably away from the 0 value and the maximum value of the first counting cycle of T1.
[0053] Take the moment when the count value of T1 is greater than n1 as the rising edge of A, and take the moment when the count value of T2 is greater than m1 as the falling edge of A. Then, before the count value of T2 reaches m1 after the count value of T1 reaches n1, the value of A is 1; before the count value of T1 reaches n1 after the count value of T2 reaches m1, the value of A is 0. Then, in step S4, determine the scan cycle according to the count value of T1 when the value of A is 1; in step S5, determine the scan cycle according to the count value of T2 when the value of A is 0. Of course, vice versa, the moment when the count value of T1 is greater than n1 can be taken as the falling edge of A, and the moment when the count value of T2 is greater than m1 can be taken as the rising edge of A. Since the rising edge and falling edge of A are only for 1 scan cycle, designing the boolean quantity A can ensure counting within a certain time interval.
[0054] In this embodiment, the value of n1 must ensure that the count value of the previous scan cycle has a value and is accurate. The data of the timer from enabled to disabled and from disabled to enabled is inaccurate. Therefore, it is only necessary for T1 to run for several scan cycles. Since the scan cycles of the PLC are in milliseconds, such as 50 ms, 30 ms, etc., but there may also be abnormal situations where the cycle reaches 1 s. It is recommended that n1 = 5 s / 0.1 ms = 50000. The reason for not choosing a larger value is that when the PLC is powered on for the first time and T1 starts to execute, there is no scan cycle when the count value has not reached n1 yet; since there are many operation behaviors to be executed after the control system is powered on, this 5 s will be normally consumed. If the time is very long, it may mean that the PLC control function starts to be enabled, and then no scan cycle time will cause the system to be abnormal.
[0055] The value of m1 is not as demanding as the value of n1. m1 can be the same as n1 or different. The time when the count value of T2 reaches m1 should be after several cycles of calculating the scan time using T1 and before the end of the counting cycle of T1, and it cannot be exactly at the critical point of the end of the counting cycle of T1. The timing when the count value of T2 reaches m1 is also related to the start time of T2. In short, it is relatively simple to make the values of m1 and n1 the same.
[0056] In step S4, determining the scan cycle according to the count value of the first timer includes: T = (x2 - x1) * t, where T is the scan cycle, x2 is the count value of the first timer in the current scan cycle, x1 is the count value of the first timer in the previous scan cycle, and t is the timing unit;
[0057] In step S5, determining the scan cycle according to the count value of the second timer includes: T = (y2 - y1) * t, where y2 is the count value of the second timer in the current scan cycle, and y1 is the count value of the second timer in the previous scan cycle.
[0058] As can be seen from the above, the method for determining the scan period based on a timer in this embodiment solves the problem of time error generated when the timer goes from enabled to disabled and from disabled to enabled. The calculation of the scan period is based on the calculation when the first timer and the second timer are in a stable and normal state, ensuring the accuracy and stability of the scan period calculation.
[0059] As Figure 5 shown, this embodiment further provides a device for determining the scan period based on a timer, which is applied to a control system including a first timer and a second timer. The maximum counting values of the first timer and the second timer are the same. The device includes:
[0060] A timer control module, configured to control the first timer to start;
[0061] The timer control module is further configured to control the second timer to start within the first counting cycle after the first timer starts;
[0062] A count value acquisition module, configured to acquire the count values of the first timer and the second timer;
[0063] A scan period determination module, configured to determine the scan period according to the count value of the first timer after the count value of the first timer reaches a first preset value and before the count value of the second timer reaches a second preset value;
[0064] The scan period determination module is further configured to determine the scan period according to the count value of the second timer after the count value of the second timer reaches the second preset value and before the count value of the first timer reaches the first preset value.
[0065] Wherein, the first preset value and the second preset value may be the same.
[0066] Wherein, the scan period determination module determines the scan period according to the count value of the first timer, including: T=(x2 - x1)*t, where T is the scan period, x2 is the count value of the first timer in the current scan period, x1 is the count value of the first timer in the previous scan period, and t is the timing unit;
[0067] The scan period determination module determines the scan period according to the count value of the second timer, including: T=(y2 - y1)*t, where y2 is the count value of the second timer in the current scan period, and y1 is the count value of the second timer in the previous scan period.
[0068] Wherein, the maximum counting values of the first timer and the second timer may be 2 31 -1.
[0069] Based on the same inventive concept as the timer-based scan cycle determination method described above, this embodiment further provides an electronic device, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the program, it implements the steps of any of the methods of the timer-based scan cycle determination method described above.
[0070] Among them, the bus architecture (represented by the bus), the bus can include any number of interconnected buses and bridges, and the bus links various circuits including one or more processors represented by the processor and the memory represented by the memory together. The bus can also link various other circuits such as peripheral devices, voltage regulators, and power management circuits, which are well known in the art, so they will not be further described herein. The bus interface provides an interface between the bus and the receiver and transmitter. The receiver and transmitter can be the same element, that is, a transceiver, which provides a unit for communicating with various other devices on the transmission medium. The processor is responsible for managing the bus and general processing, and the memory can be used to store data used by the processor when executing operations.
[0071] Since the electronic device introduced in this embodiment is the electronic device used to implement the timer-based scan cycle determination method in the embodiments of the present invention, based on the timer-based scan cycle determination method introduced in the embodiments of the present invention, those skilled in the art can understand the specific implementation manners and various variations of the electronic device in this embodiment. Therefore, the specific implementation of how this electronic device implements the method in the embodiments of the present invention will not be described in detail here. As long as those skilled in the art implement the electronic device used in the timer-based scan cycle determination method in the embodiments of the present invention, it falls within the scope of protection of the present invention.
[0072] Based on the same inventive concept as the above-mentioned timer-based scan cycle determination method, the present invention further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, it implements any of the above-mentioned timer-based scan cycle determination methods.
[0073] Those skilled in the art should understand that the embodiments of the present invention can be provided as a method, a system, or a computer program product. Therefore, the present invention can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk memory, CD-ROM, optical memory, etc.) containing computer-usable program code.
[0074] The present invention is described with reference to the flowcharts and / or block diagrams of methods, apparatuses (systems), and computer program products according to embodiments of the present invention. It should be understood that each flow and / or block in the flowchart and / or block diagram, and the combination of flows and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing device to generate a machine, such that the instructions executed by the processor of the computer or other programmable data processing device produce a means for implementing the functions specified in one flow Figure 1 one flow or multiple flows and / or blocks Figure 1 or multiple blocks.
[0075] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, such that the instructions stored in the computer-readable memory produce a manufactured article including an instruction means that implements the functions specified in one flow Figure 1 one flow or multiple flows and / or blocks Figure 1 or multiple blocks.
[0076] These computer program instructions can also be loaded onto a computer or other programmable data processing device, such that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process, so that the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one flow Figure 1 one flow or multiple flows and / or blocks Figure 1 or multiple blocks.
[0077] Although the preferred embodiments of the present invention have been described, those skilled in the art can make additional changes and modifications once they learn the basic creative concept. Therefore, the appended claims are intended to be construed to include the preferred embodiments and all changes and modifications falling within the scope of the present invention.
[0078] Obviously, those skilled in the art can make various changes and variations to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalent technologies, the present invention is also intended to include these modifications and variations.
Claims
1. A method for determining a scan period based on a timer, which is applied to a control system including a first timer and a second timer, and the maximum counting values of the first timer and the second timer are the same. Characterized in that: The method includes: Controlling the first timer to start; Within the first counting cycle after the first timer starts, controlling the second timer to start; Obtaining the counting values of the first timer and the second timer; After the counting value of the first timer reaches a first preset value and before the counting value of the second timer reaches a second preset value, determining the scan period according to the counting value of the first timer; After the counting value of the second timer reaches the second preset value and before the counting value of the first timer reaches the first preset value, determining the scan period according to the counting value of the second timer.
2. The method for determining a scan period based on a timer according to claim 1, Characterized in that: The first preset value is the same as the second preset value.
3. The method for determining a scan period based on a timer according to claim 1, Characterized in that: Determining the scan period according to the counting value of the first timer includes: T = (x2 - x1) * t, where T is the scan period, x2 is the counting value of the first timer in the current scan period, x1 is the counting value of the first timer in the previous scan period, and t is the timing unit; Determining the scan period according to the counting value of the second timer includes: T = (y2 - y1) * t, where y2 is the counting value of the second timer in the current scan period and y1 is the counting value of the second timer in the previous scan period.
4. The method for determining a scan period based on a timer according to claim 1, Characterized in that: The maximum count values of the first timer and the second timer are 2 31 -1.
5. A device for determining a scan period based on a timer, which is applied to a control system including a first timer and a second timer, and the maximum counting values of the first timer and the second timer are the same. Characterized in that: The device includes: A timer control module for controlling the first timer to start; The timer control module is further configured to control the second timer to start within the first counting cycle after the first timer starts; A counting value acquisition module for acquiring the counting values of the first timer and the second timer; A scan period determination module for determining the scan period according to the counting value of the first timer after the counting value of the first timer reaches a first preset value and before the counting value of the second timer reaches a second preset value; The scan period determination module is further configured to determine the scan period according to the counting value of the second timer after the counting value of the second timer reaches the second preset value and before the counting value of the first timer reaches the first preset value.
6. The device for determining a scan period based on a timer according to claim 5, Characterized in that: The first preset value is the same as the second preset value.
7. The device for determining a scan period based on a timer according to claim 5, Characterized in that: The scan period determination module determines the scan period according to the count value of the first timer, including: T = (x2 - x1) * t, where T is the scan period, x2 is the count value of the first timer in the current scan period, x1 is the count value of the first timer in the previous scan period, and t is the timing unit; The scan period determination module determines the scan period according to the count value of the second timer, including: T = (y2 - y1) * t, where y2 is the count value of the second timer in the current scan period, and y1 is the count value of the second timer in the previous scan period.
8. The timer-based scan period determination device according to claim 5, characterized in that The maximum count values of the first timer and the second timer are 2 31 -1.
9. An electronic device, characterized in that it includes a memory, a processor, and a computer program stored on the memory and executable on the processor, and when the processor executes the program, it implements the timer-based scan period determination method according to any one of claims 1-4.
10. A computer-readable storage medium, characterized in that a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, it implements the timer-based scan period determination method according to any one of claims 1-4.
Citation Information
Patent Citations
PLC (Programmable Logic Controller) timing method, PLC timer and PLC
CN110174870A
Lifting control device and method for steelmaking desulfurization stirring head
CN114460896A