Counter and control method and device thereof, storage medium and computer program product
By introducing type parameters into counter parameters, the program logic chaos caused by counter number repetition is solved, and the type distinction between the numbered counter is realized, which simplifies programming operations and improves the accuracy of counting control and system reliability.
Patent Information
- Application Number
- CN202510822634.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-19
- Publication Date
- 2025-07-18
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
In the prior art, there may be duplication of counter numbers, causing different types of counters to access the same content, causing problems of program logic confusion.
By introducing type parameters into the counter parameters, the counter types can be distinguished, so that counters with the same number can be used for different types, avoiding the storage content access conflicts caused by duplication of numbers.
It realizes the type distinction between the same number counter, simplifies programming operations, lowers the threshold for use, avoids the storage content access conflicts caused by number duplication, and improves the accuracy of counting control and system reliability.
Smart Images

Figure CN120342388A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of counters, and particularly relates to a counter, a control method, a device, a storage medium, and a computer program product thereof. Background Art
[0002] In PLC programming, a counter is a commonly used control instruction, and its main function is to count pulses or state changes and perform corresponding operations according to preset conditions. According to different working conditions, counters can be divided into up-counters, down-counters, up-down counters, etc. according to different counting methods, and different types of timers are distinguished by counter numbers, so that different types of counters need to access different logic functions and storage resources.
[0003] However, due to the large number of counter types, users need to consult the user manual, memorize a large amount of data, and manually assign unique numbers to different types of counters to distinguish counter types. However, in the actual working process, different types of counters may be assigned the same number, and different types of counters with the same number will share the same current value register, resulting in the underlying program being unable to identify the counter type by the number, and different types of counters will access the same content, thus causing logical confusion. For example, if an up-counter and a down-counter with the number C20 both exist, they will share the same storage unit to record the current value. When one of the counters performs a counting operation, the current value of the other counter will be overwritten, resulting in an incorrect counting result.
[0004] The above content is only used to assist in understanding the technical solution of the present invention, and does not represent an admission that the above content is prior art. Summary of the Invention
[0005] The purpose of the present invention is to provide a counter, a control method, a device, a storage medium, and a computer program product thereof, so as to solve the problem that the numbers of counters in related solutions may be repeated, resulting in different types of counters accessing the same content and causing program logic confusion, and achieve the effect of distinguishing counter types by introducing type parameters, enabling the same number to be used for different types of counters, avoiding storage content access conflicts caused by repeated numbers, and solving the cumbersome problem of memorizing a large number of numbers to distinguish counter types.
[0006] The present invention provides a control method for a counter, wherein the parameters of the counter include type parameters; the type parameters are used to indicate whether the counter is an up-counter or a down-counter; the method includes: when the counter runs for the first time, obtaining the type parameters and initializing a preset value; determining whether the counter receives a reset signal; if the counter receives the reset signal, resetting the counter and controlling the output of the counter according to the type parameters; if the counter does not receive the reset signal and the counter is enabled, after a rising edge signal is received at the input end of the counter, controlling the output of the counter according to the type parameters.
[0007] In some embodiments, if the counter receives the reset signal, resetting the counter and controlling the output of the counter according to the type parameters includes: when it is determined according to the type parameters that the counter is an up-counter, controlling the output of the counter, and the output value is 0.
[0008] In some embodiments, if the counter receives the reset signal, resetting the counter and controlling the output of the counter according to the type parameters further includes: when it is determined according to the type parameters that the counter is a down-counter, controlling the output of the counter, and the output value is 1.
[0009] In some embodiments, after a rising edge signal is received at the input end of the counter, controlling the output of the counter according to the type parameters includes: when it is determined according to the type parameters that the counter is an up-counter, determining whether the current value of the counter is greater than or equal to a set value; if the current value of the counter is less than the set value, controlling the current value of the counter to increase, controlling the output of the counter, and the output value is 0; if the current value of the counter is greater than or equal to the set value, controlling the output of the counter, and the output value is 1.
[0010] In some embodiments, after a rising edge signal is received at the input end of the counter, controlling the output of the counter according to the type parameters further includes: when it is determined according to the type parameters that the counter is a down-counter, determining whether the current value of the counter is less than or equal to a set value; if the current value of the counter is greater than the set value, controlling the current value of the counter to decrease, controlling the output of the counter, and the output value is 1; if the current value of the counter is less than or equal to the set value, controlling the output of the counter, and the output value is 0.
[0011] Matched with the above method, on the other hand, the present invention provides a control device for a counter, wherein the parameters of the counter include type parameters; the type parameters are used to indicate whether the counter is an increment counter or a decrement counter; the device includes: an acquisition unit configured to acquire the type parameters and initialize preset values when the counter runs for the first time; a control unit configured to determine whether the counter receives a reset signal; the control unit is further configured to reset the counter if the counter receives the reset signal and control the output of the counter according to the type parameters; the control unit is further configured to, if the counter does not receive the reset signal and the counter is enabled, control the output of the counter according to the type parameters after a rising edge signal is received at the input end of the counter.
[0012] In some embodiments, the control unit, if the counter receives the reset signal, resets the counter and controls the output of the counter according to the type parameters, including: when it is determined according to the type parameters that the counter is an increment counter, controlling the output of the counter, and the output value is 0.
[0013] In some embodiments, the control unit, if the counter receives the reset signal, resets the counter and controls the output of the counter according to the type parameters, further includes: when it is determined according to the type parameters that the counter is a decrement counter, controlling the output of the counter, and the output value is 1.
[0014] In some embodiments, the control unit, after a rising edge signal is received at the input end of the counter, controls the output of the counter according to the type parameters, including: when it is determined according to the type parameters that the counter is an increment counter, determining whether the current value of the counter is greater than or equal to a set value; if the current value of the counter is less than the set value, controlling the current value of the counter to increase, controlling the output of the counter, and the output value is 0; if the current value of the counter is greater than or equal to the set value, controlling the output of the counter, and the output value is 1.
[0015] In some embodiments, the control unit, after a rising edge signal is received at the input end of the counter, controls the output of the counter according to the type parameters, further includes: when it is determined according to the type parameters that the counter is a decrement counter, determining whether the current value of the counter is less than or equal to a set value; if the current value of the counter is greater than the set value, controlling the current value of the counter to decrease, controlling the output of the counter, and the output value is 1; if the current value of the counter is less than or equal to the set value, controlling the output of the counter, and the output value is 0.
[0016] Matched with the above device, on the other hand, the present invention provides a counter, including: the control device of the counter described above.
[0017] Matched with the above method, on the other hand, the present invention provides a storage medium, the storage medium includes a stored program, wherein, when the program runs, it controls the device where the storage medium is located to execute the control method of the counter described above.
[0018] Matched with the above method, on the other hand, the present invention provides a computer program product, the computer program product includes a computer program, and when the computer program product is processed and executed, it realizes the steps of the control method of the counter described above.
[0019] In the solution of the present invention, the parameters of the counter include a type parameter used to indicate whether the counter is an up-counter or a down-counter. When the counter runs for the first time, the type parameter is obtained and the preset value is initialized. It is judged whether the counter receives a reset signal. If the counter receives a reset signal, the counter is reset, and the output of the counter is controlled according to the type parameter. If the counter does not receive a reset signal and the counter is enabled, after a rising edge signal is received at the input end of the counter, the output of the counter is controlled according to the type parameter. Thus, by introducing the type parameter to distinguish the counter type, the same number can be used for different types of counters, avoiding the access conflict of stored content caused by repeated numbers, simplifying the programming operation, and reducing the use threshold.
[0020] Other features and advantages of the present invention will be described in the subsequent specification, and, partly, will be obvious from the specification, or will be understood by implementing the present invention.
[0021] Next, through the drawings and embodiments, the technical solution of the present invention will be further described in detail. Description of the Drawings
[0022] Figure 1 It is a schematic flowchart of an embodiment of the control method of the counter of the present invention; Figure 2 It is a schematic structural diagram of an embodiment of the control device of the counter of the present invention; Figure 3 It is a schematic diagram of a counter instruction primitive; Figure 4 It is a schematic diagram of a counter instruction ladder diagram; Figure 5 It is a schematic diagram of the instruction timing diagram of an up-counter; Figure 6 It is a schematic diagram of the instruction timing diagram of a down-counter; Figure 7 It is a schematic flowchart of the control method of an up-counter; Figure 8Flow schematic diagram of the control method for a down-counter.
[0023] In combination with the accompanying drawings, the reference numerals in the embodiments of the present invention are as follows: 102 - Acquisition unit; 104 - Control unit. Specific embodiments
[0024] To make the objectives, technical solutions, and advantages of the present invention clearer, the technical solutions of the present invention will be clearly and completely described below in conjunction with specific embodiments of the present invention and the corresponding accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without making creative efforts shall fall within the scope of protection of the present invention.
[0025] According to an embodiment of the present invention, a control method for a counter is provided. The parameters of the counter include a type parameter, and the type parameter is used to indicate whether the counter is an up-counter or a down-counter.
[0026] As Figure 3 shown, a counter instruction usually includes four parameters, namely a counter number, a counter set value, a reset input address, and a set register. In this solution, a "type" parameter is added before these four parameters.
[0027] The type parameter is used to uniquely identify the functional type of the counter. The numerical type is of int type, and the value range is 1 or 2. When the type is 1, it represents an up-counter, and the addition counting logic is executed when the rising edge is triggered. When the type is 2, it represents a down-counter, and the subtraction counting logic is executed when the rising edge is triggered. In the traditional solution, different types of counters need to be numbered independently, which is prone to number conflicts. After introducing the type parameter, the same number can be used for different types of counters through the parameter isolation logic to avoid storage resource conflicts. Moreover, the addition / subtraction function of a counter instruction can be switched by modifying the type parameter, reducing the total number of instructions. For example, when the parameter is 1, it is an up-counter, and when the parameter is 2, it is a down-counter, without the need to write independent instructions separately.
[0028] In some embodiments, the value range of the type parameter can be further expanded to represent more types of counters. For example, when the parameter is 3, it represents a high-speed counter, and when the parameter is 4, it represents a bidirectional counter.
[0029] The counter number is used to uniquely identify a counter. It is of int type, and the range is 0 to 255. The counter number is unique among counters of the same type, and different types of counters can reuse the same number. Through the type parameter isolation logic, number conflicts are avoided. The underlying program locates the storage area of the counter through the counter number.
[0030] The counter setting value is the counting target value set by the user, which is of int type and ranges from -32768 to 32767. The setting value is the counting termination condition. For an up-counter, when the current value of the counter ≥ the setting value, the output is triggered; for a down-counter, when the current value of the counter ≤ the setting value, the output is triggered.
[0031] The reset input address is the input signal address used to trigger the counter reset, which is of boolean type and usually corresponds to an external button or a boolean variable in the program. The reset has the highest priority. Regardless of the state of the counter, when a reset signal (such as the address being conductive) is received, the reset operation is immediately executed. After the reset operation, the current value is restored to the preset value, the flag status is set to 0, and the output becomes 0 or 1 according to the type parameter.
[0032] The set register is a register used to output the counting status, which directly reflects the counter flag status. For an up-counter, the set register is conductive when the flag status is 1. For a down-counter, the set register is non-conductive when the flag status is 1. The output and disconnection of the set register are used to drive external devices or trigger subsequent program logic, such as controlling relays, motors, etc.
[0033] Figure 4 It is a schematic diagram of the ladder diagram of the up-counter instruction. The first parameter K of the counter is the type parameter, 1 is the counter type, and K1 indicates that the current counter is an up-counter. The second parameter C20 is the counter number. The third parameter K is the counter setting value, 3 is the specific value, and K3 indicates that the current setting value is 3. The fourth parameter X7.5 is the reset input address. When X7.5 is conductive, the counter instruction is reset and the count is cleared. The fifth parameter R3.1 is the flag status.
[0034] Figure 5 It is a schematic diagram of the instruction timing diagram of the up-counter. After X7.1 (rising edge) is conductive, C20 is conductive. After C20 counts three times, the current value is 3, R3.1 is set to 1, and Y6.3 is conductive. When X7.5 is conductive, C20 is reset, R3.1 is reset to zero, and Y6.3 does not work.
[0035] Figure 6 It is a schematic diagram of the instruction timing diagram of the down-counter. For the down-counter, the first parameter K is K2 (2 is the counter type), and the third parameter K is K-3 (the setting value is -3). After X7.1 (rising edge) is conductive, C20 is conductive. Before the current value of C20 reaches -3, R3.1 is 1 and Y6.3 is conductive. As the current value decreases, after the current value reaches -3, R3.1 is set to 0 and Y6.3 is disconnected. When X7.5 is conductive, C20 is reset, the current value is 0, R3.1 is set to 1, and Y6.3 is conductive again.
[0036] In some embodiments, an intermediate relay R1.0 can be introduced outside the counter. A ladder diagram program can be added in front of the branch where the counter is located. An input signal X1.0 controls whether the intermediate relay R1.0 is turned on. When the intermediate relay R1.0 is set to 1, the counter instruction counts in the addition counting mode; when the intermediate relay R1.0 is set to 0, the counter instruction counts in the subtraction counting mode.
[0037] As Figure 1 shown in the flowchart of an embodiment of the method of the present invention. The control method of the counter can include: step S110 to step S140.
[0038] At step S110, when the counter runs for the first time, obtain the type parameter and initialize the preset value.
[0039] The type parameter uniquely identifies the counter type and is the core basis for the underlying logic to select the counting mode. Initializing the preset value specifically means that when running for the first time, the preset value needs to be read from the counter parameters and stored in the current value register as the counting starting point to ensure that the counter starts counting from the initial value specified by the user and avoid counting errors caused by non-initialization.
[0040] At step S120, determine whether the counter receives a reset signal.
[0041] At step S130, if the counter receives a reset signal, reset the counter and control the counter output according to the type parameter.
[0042] The reset operation is an emergency control signal for the counter and needs to be executed prior to the counting logic. After the counter executes the reset operation, the current value of the counter is restored to the preset value input by the user, the flag status of the counter is restored to 0, and the counter outputs according to the type parameter.
[0043] In some embodiments, in step S130, if the counter receives a reset signal, reset the counter and control the counter output according to the type parameter. The specific process includes: when it is determined that the counter is an up-counter according to the type parameter, control the counter output, and the output value is 0.
[0044] When the up-counter receives a reset signal (such as the reset input address is turned on), the underlying program first restores the current value to the preset value and sets the flag status to 0. The output of the up-counter is directly determined by the flag status. When the flag status is 0, the output value is 0.
[0045] After the addition counter is reset, it outputs 0, providing a clear initial state for subsequent counting and preventing residual signal interference. By means of the type parameter, it is ensured that the reset logic of the addition counter is independent of other types, avoiding misoperation of the output caused by number reuse.
[0046] In some embodiments, in step S130, when the counter receives a reset signal, the counter is reset, and the specific process of controlling the output of the counter according to the type parameter further includes: when it is determined according to the type parameter that the counter is a subtraction counter, controlling the output of the counter, and the output value is 1.
[0047] When the subtraction counter is reset, the current value is restored to the preset value, and the flag status is set to 0, but the output logic of the subtraction counter is opposite to the flag status: when the flag status is 0, the output value is 1. After the subtraction counter is reset, it outputs 1, forming a differentiated state from the output 0 of the addition counter. Even if their numbers are the same, the types can be directly distinguished by the output value, avoiding logic confusion.
[0048] The type parameter is used to automatically distinguish the output values after the addition / subtraction counter is reset, which not only maintains the unity of the reset operation (restoring the preset value and setting the flag to 0), but also isolates the functional characteristics of different types through the output logic. And the parameterized design binds the counter type to the reset behavior, eliminating the need to rely on numbers to distinguish functions, avoiding storage access conflicts and program logic errors at the root.
[0049] In step S140, if the counter does not receive a reset signal and the counter is enabled, after a rising edge signal is received at the input end of the counter, the output of the counter is controlled according to the type parameter.
[0050] Enabling is a prerequisite for counting. Only when the enabling is effective, the counter responds to the rising edge signal. When the input end signal jumps from OFF→ON, that is, the counter receives a rising edge signal, the counter starts to perform addition / subtraction operation and controls the output of the counter according to the current value and the type parameter.
[0051] By introducing a type parameter to distinguish the counter type, the same number can be used for different types of counters, avoiding storage content access conflicts caused by repeated numbers, solving the cumbersome problem of having to remember a large number of numbers to distinguish counter types, simplifying programming operations, reducing the usage threshold, and realizing a convenient design of counter instructions.
[0052] In some embodiments, in step S140, after a rising edge signal is received at the input end of the counter, the specific process of controlling the output of the counter according to the type parameter includes: when it is determined according to the type parameter that the counter is an up-counter, determining whether the current value of the counter is greater than or equal to a set value; if the current value of the counter is less than the set value, controlling the current value of the counter to increase, controlling the counter to output, and the output value is 0; if the current value of the counter is greater than or equal to the set value, controlling the counter to output, and the output value is 1.
[0053] The up-counter only executes the counting logic when the input end signal jumps from OFF to ON (rising edge). During the counting process, if the current value of the counter < the set value, the current value increases by one. During this process, the flag state of the counter is 0 and the counter outputs 0; then when the up-counter receives a rising edge again, the size relationship between the current value and the set value is rejudged until the current value ≥ the set value, then the flag state is set to 1 and the up-counter outputs 1, that is, the up-counter is turned on. For example, after the up-counter receives a rising edge, the current value 2 < the set value 3, the current value increases by one, the current value is 3, and the flag state is 0; the next time the up-counter receives a rising edge, the current value 3 = the set value 3, the flag state is 1, and the counter outputs 1.
[0054] By controlling the counting and output according to the comparison result between the current value and the set value after the up-counter receives a rising edge signal, it is ensured that the up-counter only increases when the current value is less than the set value, outputs 1 and stops counting after reaching the standard, achieving precise counting control, avoiding logical errors caused by overcounting. At the same time, the up-counter logic is isolated through the type parameter, solving the problem of program logic confusion caused by counter number conflicts, and improving the accuracy of counting control and system reliability.
[0055] Figure 7 is a schematic flow diagram of the control method for the up-counter, as Figure 7 shown, the method includes: Step 1, first determine whether the first run flag is 0. If it is 0, initialize the user-set value of the counter. Then determine whether the counter is reset. If it is reset, the current value of the counter is restored to the user-set value, the flag state of the counter is 0, and step 3 is executed; if it is not reset, after the counter is enabled, step 2 is executed.
[0056] Step 2, after a rising edge is triggered at the input end of the counter, determine whether the current value of the counter is greater than or equal to the set value. If it is, the flag state of the counter is 1 and step 3 is executed; if not, the counter performs an increment operation, and then step 2 is executed.
[0057] Step 3: Determine whether the counter flag status is 1. If it is 1, the counter output is 1; if it is not 1, the counter output is 0.
[0058] By introducing a type identification parameter to distinguish different counter types, the trouble and inconvenience caused by the original method of distinguishing by memorizing the numbers of each type of counter are avoided. A counter function instruction can achieve multiple functions through the change of one of its parameters, enhancing versatility, reducing the total number of counter instructions, making programming more convenient, solving the problem of needing to memorize a large number of numbers to distinguish counter types, and lowering the usage threshold.
[0059] In some embodiments, in step S140, after a rising edge signal is received at the input end of the counter, the specific process of controlling the counter output according to the type parameter further includes: when it is determined according to the type parameter that the counter is a down-counter, determine whether the current value of the counter is less than or equal to the set value; if the current value of the counter is greater than the set value, control the current value of the counter to decrease, control the counter output, and the output value is 1; if the current value of the counter is less than or equal to the set value, control the counter output, and the output value is 0.
[0060] The down-counter only executes the counting logic when the input end signal triggers a rising edge. If the current value of the counter > the set value, the current value decreases by one. During this process, the flag status of the counter is 0, the counter output is 1, and the counter is turned on; then as the current value decreases, when the current value ≤ the set value, the flag status value is 1, and the counter output is 0, that is, the down-counter is turned off. For example, the current value of the down-counter is -2 and the set value is -3. At this time, the flag status is 0. After receiving the rising edge, it is determined that the current value -2 > the set value -3, and the current value is decremented by one, and the current value becomes -3; the next time it is judged, the current value -3 = the set value -3, the flag status is 1, and the counter output is 0.
[0061] By controlling the decrement and output according to the comparison result of the current value and the set value after the down-counter receives a rising edge signal, it is ensured that the down-counter only decrements when the current value is greater than the set value, outputs 0 and stops counting after reaching the standard, achieving precise decrement control, avoiding logical errors caused by undercounting or overcounting, and at the same time isolating the down-counter logic using the type parameter, solving the problem of program logic confusion caused by counter number conflicts, and improving the reliability of counting control and system compatibility.
[0062] Figure 8 is a schematic flow diagram of the control method for a down-counter, as Figure 8 shown, the method includes: Step 11: First, determine whether the first-run flag is 0. If it is 0, initialize the user-set value of the counter. Then, determine whether the counter is reset. If it is reset, restore the current value of the counter to the user-set value, set the counter flag status to 0, and execute Step 13; if it is not reset, after the counter is enabled, execute Step 12.
[0063] Step 12: After a rising edge is triggered at the counter input terminal, determine whether the current value of the counter is less than or equal to the set value. If it is, set the counter flag status to 1 and execute Step 13; if not, perform a decrement operation on the counter, and then execute Step 12.
[0064] Step 13: Determine whether the counter flag status is 1. If it is 1, the counter output is 0, and the branch where the counter set register is located is disconnected; if it is not 1, the counter output is 1, and the branch where the counter set register is located is conducting.
[0065] Adopting the technical solution of this embodiment, the parameters of the counter include a type. The type parameter is used to indicate whether the counter is an up-counter or a down-counter. When the counter runs for the first time, obtain the type parameter and initialize the preset value, determine whether the counter receives a reset signal. If the counter receives a reset signal, reset the counter, and control the counter output according to the type parameter. If the counter does not receive a reset signal and the counter is enabled, after a rising edge signal is received at the input terminal of the counter, control the counter output according to the type parameter. Thus, by introducing the type parameter to distinguish the counter type, the same number can be used for different types of counters, avoiding access conflicts of stored content caused by duplicate numbers, simplifying programming operations, and reducing the usage threshold.
[0066] According to an embodiment of the present invention, there is also provided a control device for a counter corresponding to the control method of the counter. The parameters of the counter include a type parameter; the type parameter is used to indicate whether the counter is an up-counter or a down-counter.
[0067] As Figure 3 shown, the counter instruction usually includes four parameters, namely the counter number, the counter set value, the reset input address, and the set register. This solution adds a "type" parameter before these four parameters.
[0068] The type parameter is used to uniquely identify the functional type of the counter. The numerical type is int. The value range is 1 or 2. When the type is 1, it represents an up-counter, and the addition counting logic is executed when the rising edge is triggered. When the type is 2, it represents a down-counter, and the subtraction counting logic is executed when the rising edge is triggered. In traditional solutions, different types of counters need to be numbered independently, which is prone to number conflicts. After introducing the type parameter, the same number can be used for different types of counters through parameter isolation logic to avoid storage resource conflicts. Moreover, a counter instruction can achieve the addition / subtraction function switch by modifying the type parameter, reducing the total number of instructions. For example, when the parameter is 1, it is an up-counter, and when the parameter is 2, it is a down-counter, without the need to write separate independent instructions respectively.
[0069] In some embodiments, the value range of the type parameter can be further expanded to represent more types of counters. For example, when the parameter is 3, it represents a high-speed counter, and when the parameter is 4, it represents a bidirectional counter.
[0070] The counter number is used to uniquely identify the number of a counter. It is of int type, and the range is 0 to 255. The counter number is unique among counters of the same type. Different types of counters can reuse the same number, and through the type parameter isolation logic, number conflicts are avoided. The underlying program locates the storage area of the counter through the counter number.
[0071] The counter set value is the counting target value set by the user. It is of int type, and the range is -32768 to 32767. The set value is the counting termination condition. For an up-counter, when the current value of the counter ≥ the set value, the output is triggered; for a down-counter, when the current value of the counter ≤ the set value, the output is triggered.
[0072] The reset input address is the input signal address used to trigger the counter reset. It is of boolean type and usually corresponds to an external button or a boolean variable in the program. The reset has the highest priority. Regardless of the state of the counter, when the reset signal (such as the address is conducted) is received, the reset operation is immediately executed. After the reset operation, the current value is restored to the preset value, the flag status is set to 0, and the output becomes 0 or 1 according to the type parameter.
[0073] The set register is a register used to output the counting status, which directly reflects the flag status of the counter. For an up-counter, the set register is conducted when the flag status is 1. For a down-counter, the set register is disconnected when the flag status is 1. The output and disconnection of the set register are used to drive external devices or trigger subsequent program logic, such as controlling relays, motors, etc.
[0074] Figure 4It is a schematic diagram of the ladder diagram of the up-counter instruction. The first parameter K of the counter is a type parameter, 1 is the counter type, and K1 indicates that the current counter is an up-counter. The second parameter C20 is the counter number. The third parameter K is the counter setting value, 3 is the specific value, and K3 indicates that the current setting value is 3. The fourth parameter X7.5 is the reset input address. When X7.5 is turned on, the counter instruction is reset and the count is cleared. The fifth parameter R3.1 is the flag status.
[0075] Figure 5 It is a schematic diagram of the instruction timing diagram of the up-counter. After X7.1 (rising edge) is turned on, C20 is turned on. After C20 counts three times, the current value is 3, R3.1 is set to 1, and Y6.3 is turned on. When X7.5 is turned on, C20 is reset, R3.1 is reset to zero, and Y6.3 does not work.
[0076] Figure 6 It is a schematic diagram of the instruction timing diagram of the down-counter. For the down-counter, the first parameter K is K2 (2 is the counter type), and the third parameter K is K-3 (the setting value is -3). After X7.1 (rising edge) is turned on, C20 is turned on. Before the current value of C20 reaches -3, R3.1 is 1 and Y6.3 is turned on. As the current value decreases, after the current value reaches -3, R3.1 is set to 0 and Y6.3 is turned off. When X7.5 is turned on, C20 is reset, the current value is 0, R3.1 is set to 1, and Y6.3 is turned on again.
[0077] In some embodiments, an intermediate relay R1.0 can be introduced outside the counter, and a ladder diagram program is added in front of the branch where the counter is located. An input signal X1.0 controls whether the intermediate relay R1.0 is turned on. When the intermediate relay R1.0 is set to 1, the counter instruction counts in the addition counting mode; when the intermediate relay R1.0 is set to 0, the counter instruction counts in the subtraction counting mode.
[0078] See Figure 2 The structural schematic diagram of an embodiment of the device of the present invention is shown. The control device of this counter may include: an acquisition unit 102 and a control unit 104.
[0079] The acquisition unit 102 is configured to acquire the type parameter and initialize the preset value when the counter runs for the first time. The specific functions and processes of the acquisition unit 102 are shown in step S110.
[0080] The type parameter uniquely identifies the counter type and is the core basis for the underlying logic to select the counting mode. Initializing the preset value specifically means that when running for the first time, it is necessary to read the preset value from the counter parameters and store it in the current value register as the counting starting point to ensure that the counter starts counting from the initial value specified by the user and avoid counting errors caused by non-initialization.
[0081] The control unit 104 is configured to determine whether the counter receives a reset signal. For the specific functions and processing of this control unit 104, refer to step S120.
[0082] The control unit 104 is further configured to reset the counter if the counter receives a reset signal, and control the output of the counter according to the type parameter. For the specific functions and processing of this control unit 104, refer to step S130.
[0083] The reset operation is an emergency control signal for the counter and needs to be executed prior to the counting logic. After the counter executes the reset operation, the current value of the counter is restored to the preset value input by the user, the flag status of the counter is restored to 0, and the counter outputs according to the type parameter.
[0084] In some embodiments, the control unit 104, when the counter receives a reset signal, resets the counter and controls the output of the counter according to the type parameter, including: when it is determined according to the type parameter that the counter is an up-counter, controlling the output of the counter, and the output value is 0.
[0085] When the up-counter receives a reset signal (such as when the reset input address is turned on), the underlying program first restores the current value to the preset value and sets the flag status to 0. The output of the up-counter is directly determined by the flag status, and when the flag status is 0, the output value is 0.
[0086] The up-counter outputs 0 after reset, providing a clear initial state for subsequent counting and preventing residual signals from interfering. By means of the type parameter, it is ensured that the reset logic of the up-counter is independent of other types, avoiding misoperation of the output caused by number reuse.
[0087] In some embodiments, the control unit 104, when the counter receives a reset signal, resets the counter and controls the output of the counter according to the type parameter, further includes: when it is determined according to the type parameter that the counter is a down-counter, controlling the output of the counter, and the output value is 1.
[0088] When the down-counter is reset, the current value is restored to the preset value and the flag status is set to 0, but the output logic of the down-counter is opposite to the flag status: when the flag status is 0, the output value is 1. The down-counter outputs 1 after reset, forming a differentiated state from the output 0 of the up-counter. Even if their numbers are the same, the types can be directly distinguished by the output values, avoiding logic confusion.
[0089] The automatic differentiation of the output value after the addition / subtraction counter is reset is achieved through type parameters, which not only maintains the unity of the reset operation (restoring the preset value and setting the flag to 0), but also isolates the functional characteristics of different types through the output logic. Moreover, the parametric design binds the counter type to the reset behavior, eliminating the need to rely on numbers to distinguish functions and avoiding storage access conflicts and program logic errors at the root.
[0090] The control unit 104 is further configured to, if the counter does not receive a reset signal and the counter is enabled, after a rising edge signal is received at the input end of the counter, control the output of the counter according to the type parameter. For the specific functions and processing of the control unit 104, refer to step S140.
[0091] Enabling is a prerequisite for counting. Only when the enabling is effective, the counter responds to the rising edge signal. When the input end signal jumps from OFF to ON, that is, when the counter receives a rising edge signal, the counter starts to perform addition / subtraction operations and controls the output of the counter according to the current value and the type parameter.
[0092] By introducing type parameters to distinguish counter types, the same number can be used for different types of counters, avoiding storage content access conflicts caused by duplicate numbers, solving the cumbersome problem of having to remember a large number of numbers to distinguish counter types, simplifying programming operations, reducing the usage threshold, and realizing the convenient design of counter instructions.
[0093] In some embodiments, the control unit 104, after receiving a rising edge signal at the input end of the counter, controls the output of the counter according to the type parameter, including: when it is determined according to the type parameter that the counter is an addition counter, determining whether the current value of the counter is greater than or equal to a set value; if the current value of the counter is less than the set value, controlling the current value of the counter to increase, controlling the output of the counter, and the output value is 0; if the current value of the counter is greater than or equal to the set value, controlling the output of the counter, and the output value is 1.
[0094] The addition counter only executes the counting logic when the input end signal jumps from OFF to ON (rising edge). During the execution of counting, if the current value of the counter < the set value, the current value increases by one. During this process, the flag state of the counter is 0 and the counter outputs 0; then when the addition counter receives a rising edge again, the size relationship between the current value and the set value is rejudged until the current value ≥ the set value, and then the flag state is set to 1 and the addition counter outputs 1, that is, the addition counter is turned on. For example, after the addition counter receives a rising edge, the current value 2 < the set value 3, the current value increases by one, the current value is 3, and the flag state is 0; the next time the addition counter receives a rising edge, the current value 3 = the set value 3, the flag state is 1, and the counter outputs 1.
[0095] After the rising edge signal is received by the up-counter, the counting and output are controlled according to the comparison result between the current value and the set value, ensuring that the up-counter only increments when the current value is less than the set value. After reaching the standard, it outputs 1 and stops counting, achieving precise counting control, avoiding logical errors caused by overcounting. At the same time, the up-counter logic is isolated by the type parameter, solving the problem of program logic confusion caused by counter number conflicts, and improving the accuracy of counting control and the reliability of the system.
[0096] Figure 7 It is a schematic flow diagram of the control method for the up-counter, as Figure 7 shown. This method includes: Step 1: First, determine whether the first-run flag is 0. If it is 0, initialize the user-set value of the counter. Then, determine whether the counter is reset. If it is reset, the current value of the counter is restored to the user-set value, and the counter flag status is 0. Then execute Step 3. If it is not reset, after the counter is enabled, execute Step 2.
[0097] Step 2: After a rising edge is triggered at the input end of the counter, determine whether the current value of the counter is greater than or equal to the set value. If it is, the counter flag status is 1, and execute Step 3. If not, the counter performs an increment operation, and then execute Step 2.
[0098] Step 3: Determine whether the counter flag status is 1. If it is 1, the counter output is 1. If it is not 1, the counter output is 0.
[0099] By introducing a type identification parameter to distinguish different counter types, it avoids the trouble and inconvenience brought by the original method of distinguishing by memorizing the numbers of each type of counter. A counter function instruction can achieve multiple functions by changing one of its parameters, enhancing versatility, reducing the total number of counter instructions, making programming more convenient, solving the problem of needing to memorize a large number of numbers to distinguish counter types, and lowering the usage threshold.
[0100] In some embodiments, the control unit 104, after receiving a rising edge signal at the input end of the counter, controls the counter output according to the type parameter, and further includes: in the case where it is determined that the counter is a down-counter according to the type parameter, determining whether the current value of the counter is less than or equal to the set value; if the current value of the counter is greater than the set value, controlling the current value of the counter to decrement, controlling the counter output, and the output value is 1; if the current value of the counter is less than or equal to the set value, controlling the counter output, and the output value is 0.
[0101] The down-counter only executes the counting logic when the input signal triggers a rising edge. If the current value of the counter > the set value, the current value is decremented by one. During this process, the flag status of the counter is 0, the counter outputs 1, and the counter is turned on; afterwards, as the current value decreases, when the current value ≤ the set value, the flag status value is 1, and the counter outputs 0, that is, the down-counter is turned off. For example, the current value of the down-counter is -2 and the set value is -3. At this time, the flag status is 0. After receiving the rising edge, it is judged that the current value -2 > the set value -3, and the current value is decremented by one, and the current value becomes -3; at the next judgment, the current value -3 = the set value -3, the flag status is 1, and the counter outputs 0.
[0102] After the down-counter receives the rising edge signal, by controlling the decrement and output according to the comparison result of the current value and the set value, it is ensured that the down-counter only decrements when the current value is greater than the set value, outputs 0 and stops counting after reaching the standard, achieving precise decrement control, avoiding logic errors caused by undercounting or overcounting. At the same time, the down-counter logic is isolated by using type parameters, solving the problem of program logic confusion caused by counter number conflicts, and improving the reliability of counting control and system compatibility.
[0103] Figure 8 For the flow schematic diagram of the control method of the down-counter, as Figure 8 shown, the method includes: Step 11, first judge whether the first-run flag is 0. If it is 0, initialize the user-set value of the counter. Then judge whether the counter is reset. If it is reset, the current value of the counter is restored to the user-set value, the flag status of the counter is 0, and step 13 is executed; if it is not reset, after the counter is enabled, step 12 is executed.
[0104] Step 12, after the rising edge is triggered at the counter input terminal, judge whether the current value of the counter is less than or equal to the set value. If it is, the flag status of the counter is 1, and step 13 is executed; if not, the counter performs a decrement operation, and then step 12 is executed.
[0105] Step 13, judge whether the flag status of the counter is 1. If it is 1, the output of the counter is 0, and the branch where the counter set register is located is turned off; if it is not 1, the output of the counter is 1, and the branch where the counter set register is located is turned on.
[0106] Since the processing and functions implemented by the device in this embodiment are basically corresponding to the embodiments, principles and examples of the foregoing method, for the details not described in the description of this embodiment, reference can be made to the relevant descriptions in the foregoing embodiments, and no further elaboration will be made here.
[0107] Adopting the technical solution of the present invention, the parameters of the counter include a type, and the type parameter is used to indicate whether the counter is an up-counter or a down-counter. When the counter runs for the first time, the type parameter is obtained and the preset value is initialized. It is judged whether the counter receives a reset signal. If the counter receives the reset signal, the counter is reset, and the output of the counter is controlled according to the type parameter. If the counter does not receive the reset signal and the counter is enabled, after a rising edge signal is received at the input end of the counter, the output of the counter is controlled according to the type parameter. Thus, by introducing the type parameter to distinguish the counter type, the same number can be used for different types of counters, avoiding the access conflict of stored content caused by repeated numbers, simplifying the programming operation, and reducing the usage threshold.
[0108] According to an embodiment of the present invention, there is also provided a counter corresponding to the control device of the counter. The counter may include: the control device of the counter described above.
[0109] Since the processing and functions implemented by the counter in this embodiment are basically corresponding to the embodiments, principles and examples of the foregoing device, for the details not described in the description of this embodiment, reference may be made to the relevant descriptions in the foregoing embodiments, and details will not be repeated here.
[0110] Adopting the technical solution of the present invention, the parameters of the counter include a type, and the type parameter is used to indicate whether the counter is an up-counter or a down-counter. When the counter runs for the first time, the type parameter is obtained and the preset value is initialized. It is judged whether the counter receives a reset signal. If the counter receives the reset signal, the counter is reset, and the output of the counter is controlled according to the type parameter. If the counter does not receive the reset signal and the counter is enabled, after a rising edge signal is received at the input end of the counter, the output of the counter is controlled according to the type parameter. Thus, by introducing the type parameter to distinguish the counter type, the same number can be used for different types of counters, avoiding the access conflict of stored content caused by repeated numbers, simplifying the programming operation, and reducing the usage threshold.
[0111] According to an embodiment of the present invention, there is also provided a storage medium corresponding to the control method of the counter. The storage medium includes a stored program, wherein when the program runs, it controls the device where the storage medium is located to execute the control method of the counter described above.
[0112] Since the processing and functions implemented by the storage medium in this embodiment are basically corresponding to the embodiments, principles and examples of the foregoing method, for the details not described in the description of this embodiment, reference may be made to the relevant descriptions in the foregoing embodiments, and details will not be repeated here.
[0113] Adopting the technical solution of the present invention, the parameters of the counter include the type, and the type parameter is used to indicate whether the counter is an up-counter or a down-counter. When the counter runs for the first time, the type parameter is obtained and the preset value is initialized. It is judged whether the counter receives a reset signal. If the counter receives a reset signal, the counter is reset, and the output of the counter is controlled according to the type parameter. If the counter does not receive a reset signal and the counter is enabled, after a rising edge signal is received at the input end of the counter, the output of the counter is controlled according to the type parameter. Thus, by introducing the type parameter to distinguish the counter type, the same number can be used for different types of counters, avoiding the access conflict of stored content caused by repeated numbers, simplifying the programming operation, and reducing the usage threshold.
[0114] According to an embodiment of the present invention, there is also provided a computer program product corresponding to the control method of the counter. The computer program product includes a computer program, and when the computer program product is processed and executed, the steps of the above-mentioned control method of the counter are implemented.
[0115] Since the processing and functions implemented by the computer program product of this embodiment are basically corresponding to the embodiments, principles and examples of the foregoing method, for the parts not described in detail in the description of this embodiment, reference can be made to the relevant descriptions in the foregoing embodiments, and details will not be repeated here.
[0116] Adopting the technical solution of the present invention, the parameters of the counter include the type, and the type parameter is used to indicate whether the counter is an up-counter or a down-counter. When the counter runs for the first time, the type parameter is obtained and the preset value is initialized. It is judged whether the counter receives a reset signal. If the counter receives a reset signal, the counter is reset, and the output of the counter is controlled according to the type parameter. If the counter does not receive a reset signal and the counter is enabled, after a rising edge signal is received at the input end of the counter, the output of the counter is controlled according to the type parameter. Thus, by introducing the type parameter to distinguish the counter type, the same number can be used for different types of counters, avoiding the access conflict of stored content caused by repeated numbers, simplifying the programming operation, and reducing the usage threshold.
[0117] In summary, it is easy for those skilled in the art to understand that, on the premise of no conflict, the above-mentioned advantageous ways can be freely combined and superimposed.
[0118] The above description is only for the embodiments of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included within the scope of the claims of the present invention.
Claims
1. A control method for a counter, characterized in that, The parameters of the counter include a type parameter; the type parameter is used to indicate whether the counter is an up-counter or a down-counter; The method includes: When the counter runs for the first time, obtain the type parameter and initialize the preset value; Determine whether the counter receives a reset signal; If the counter receives a reset signal, reset the counter and control the output of the counter according to the type parameter; If the counter does not receive a reset signal and the counter is enabled, after a rising edge signal is received at the input end of the counter, control the output of the counter according to the type parameter.
2. The control method of the counter according to claim 1, characterized in that, If the counter receives a reset signal, reset the counter and control the output of the counter according to the type parameter, including: When it is determined according to the type parameter that the counter is an up-counter, control the output of the counter, and the output value is 0.
3. The control method of the counter according to claim 1, wherein If the counter receives a reset signal, reset the counter and control the output of the counter according to the type parameter, and further include: When it is determined according to the type parameter that the counter is a down-counter, control the output of the counter, and the output value is 1.
4. The control method of the counter according to claim 1, wherein After a rising edge signal is received at the input end of the counter, control the output of the counter according to the type parameter, including: When it is determined according to the type parameter that the counter is an up-counter, determine whether the current value of the counter is greater than or equal to the set value; If the current value of the counter is less than the set value, control the current value of the counter to increase, control the output of the counter, and the output value is 0; If the current value of the counter is greater than or equal to the set value, control the output of the counter, and the output value is 1.
5. The control method of the counter according to claim 1, characterized in that, After a rising edge signal is received at the input end of the counter, control the output of the counter according to the type parameter, and further include: When it is determined according to the type parameter that the counter is a down-counter, determine whether the current value of the counter is less than or equal to the set value; If the current value of the counter is greater than the set value, control the current value of the counter to decrease, control the output of the counter, and the output value is 1; If the current value of the counter is less than or equal to the set value, control the output of the counter, and the output value is 0.
6. A control device for a counter, characterized in that, The parameters of the counter include a type parameter; The type parameter is used to indicate whether the counter is an up-counter or a down-counter; The device includes: An acquisition unit configured to, when the counter runs for the first time, obtain the type parameter and initialize the preset value; A control unit configured to determine whether the counter receives a reset signal; The control unit is further configured to, if the counter receives a reset signal, reset the counter and control the output of the counter according to the type parameter; The control unit is further configured to, if the counter does not receive a reset signal and the counter is enabled, after a rising edge signal is received at the input end of the counter, control the output of the counter according to the type parameter.
7. The control device of the counter according to claim 6, characterized in that, The control unit resets the counter if the counter receives a reset signal, and controls the output of the counter according to the type parameter, including: When it is determined according to the type parameter that the counter is an up-counter, control the counter to output, and the output value is 0.
8. A counter, characterized in that, Including: The control device of the counter according to claim 6 or 7.
9. A storage medium, characterized in that, The storage medium includes a stored program, wherein when the program runs, it controls the device where the storage medium is located to execute the control method of the counter according to any one of claims 1 to 5.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Counter device composed of Advanced RISC Machine (ARM) and Field Programmable Gate Array (FPGA), and implementation method thereof
CN102254218A
Mechanical switch electrical pulse anti-shake method
CN107947768A
Counting method and device, equipment and storage medium
CN117436472A
Counter circuit
US20030123602A1
