PLC programming optimization method, let the program run faster!
The optimization of this method can greatly reduce the number of program statements and make the PLC program more concise and readable. As no time-consuming type conversion is needed, the program operation efficiency is also improved. And the greater the amount of mathematical operations, the more significant increase in efficiency.
The disadvantage is that it takes up two bytes of memory, and VW0 cannot be used later in the program. However, the S7-200 has a large amount of RAM, which is generally used up. Take 226 as an example, there are as many as 10K of RAM, and the couple never exceeds 1K. These RAMs are all bought for money. They are not used and they are not wasted.
Similarly, if there are byte-type variables often need to convert with word-type variables, so that the byte variables occupy a byte of memory width, avoiding type conversion. Specific steps are as follows:
1: According to the actual needs of the project, carry out functional block planning, write subroutines
Subprograms in the PLC are relatively independent programs that are programmed for specific control purposes. Executing a subroutine call instruction CALL, etc. If the condition does not satisfy the subroutine call, the program scan is performed only in the main program, and the subroutine is no longer scanned, thus reducing unnecessary scan time.
2: Use word or double word data to send to DO point method to control output
In the application of PLC, there will usually be a large number of output controls. Using word or double word data to transfer to the DO point method to control the output can increase the speed, as long as the output address is reasonably allocated according to the requirements of practical applications, and the control output control word can be changed. Greatly reduce the number of steps in the PLC program execution, thereby speeding up the PLC program running speed.
3: Pulse trigger SET, RESET
In the PLC, the SET instruction is used only once. It is not necessary to execute this instruction every scan. It is suitable for use with pulse output (PLS/PLF) instructions. Some engineers ignored this problem and used routine methods to drive the SET instructions, inadvertently increasing the PLC program scan run time.
4: Avoid type conversion as follows:
Taking the S7-200 as an example, its memory format is just the opposite of what we commonly use in PCs. It is high-word first and low-word second. So we can put the word variable in the last two bytes and clear the first two bytes when the program is initialized (these two bytes must not be used elsewhere in the program).
If we define a symbol, we define the word variable at VW2 while keeping the value of VW0 at zero. The program can use VW2 to access the variable in a font, and it can also access VD0 with a double word to avoid the type conversion.
In order to avoid confusion during use, it is better to distinguish word types and double word types with explicit symbol definitions. The Hungarian nomenclature is strongly recommended here: the prefix variable type is indicated by the prefix, and the group cooperation variable name with the first letter capitalized meaningful English word. I used to use the following suffix: b --- byte variable (byte)
w————word variable(word)
d————Double word variable (double)
r————Real variable (real)
f————bit variable (flag)
Btn --- Self-resetting button type input (button)
Sw————Switch or self-locking button switch
Sig - - sensor, code, etc. level signal input
Rly——-output relay (relay)
......
Of course, according to personal habits, there are no rules, mainly to facilitate their own distinction.
If you have a word type variable named VarName, to use the previous conversion technique, we can define it like this:
wVarName --——VW2
dVarName————VD0
VW0 is cleared at program initialization (if it is a variable that does not need to be memorized, dVarName can be cleared directly) or VW0 can be set to zero in the data block. You need to use wVarName when you need to access variables in the word type, and use dVarName when you need to access variables in the double word type. No type conversion is required at all.
This method can greatly reduce the number of program statements and make the program more concise and readable. As no time-consuming type conversion is needed, the program operation efficiency is also improved. And the greater the amount of mathematical operations, the more significant increase in efficiency.
The disadvantage is that it takes up two bytes of memory, and VW0 cannot be used later in the program. However, the S7-200 has a large amount of RAM, which is generally used up. Take 226 as an example, there are as many as 10K of RAM, and the couple never exceeds 1K. These RAMs are all bought for money. They are not used and they are not wasted.
Similarly, if there are byte-type variables often need to convert with word-type variables, so that the byte variables occupy a byte of memory width, avoiding type conversion.