This optimization method can significantly reduce the number of program statements, making PLC programs more concise and readable. Since time-consuming type conversions are eliminated, program execution efficiency is also improved. Furthermore, the greater the amount of mathematical computation, the more significant the efficiency improvement.
The downside is that it requires two extra bytes of memory, and VW0 cannot be used in the program afterwards. However, the S7-200 has a large amount of RAM, which is generally more than enough. For example, the 226 has as much as 10KB of RAM, and I have never used more than 1KB. This RAM was purchased with money, so it's better to use it than not use it.
Similarly, if byte-type variables frequently need to be converted to word-type variables, avoid wasting a byte by having the byte variable occupy a word's memory width, thus preventing type conversion. The specific steps are as follows:
1. Based on the actual needs of the project, plan the functional blocks and write subroutines.
In a PLC , a subroutine is a relatively independent program written for specific control purposes. When executing a subroutine call instruction (CALL), if the conditions for the subroutine call are not met, the program scan will only occur in the main program, and the subroutine will not be scanned again, thus reducing unnecessary scan time.
2: Control output by sending single or double-word data to the DO point.
In PLC applications, there are usually a lot of output controls. Using word or double word data to transmit to DO points to control the output can improve speed. As long as the output addresses are reasonably allocated and the control words are changed according to the requirements of the actual application, the number of steps executed by the PLC program can be greatly reduced, thereby speeding up the PLC program running speed.
3: Pulse-triggered SET, RESET
In PLCs, the SET instruction only needs to be executed once, not every time the scan is performed, making it ideal for use with pulse output (PLS/PLF) instructions. Some engineers overlook this and use conventional methods to drive the SET instruction, unintentionally increasing the PLC program's scan execution time.
4. Avoid type conversion, the method is as follows:
Taking the S7-200 as an example, its memory format is the opposite of that of a typical PC; it uses high word first and low word last. Therefore, we can place word variables in the last two bytes and clear the first two bytes to zero during program initialization (these two bytes must not be used elsewhere in the program).
If we define a word variable in VW2 while keeping the value of VW0 zero, then the program can access this variable as a word using VW2, and also as a double word using VD0, avoiding type conversion.
To avoid confusion during use, it's best to clearly distinguish between word types and double-word types using explicit symbolic definitions. Hungarian notation is strongly recommended: use a prefix to indicate the variable type, and use a combination of meaningful English words with the first letter capitalized as the variable name. I personally prefer to use the following suffix: b — byte type variable (byte)
w — word type variable
d — double variable
r — real variable
f — bit variable (flag)
btn — Self-resetting button input
sw — Toggle switch or self-locking button input (switch)
sig — Sensor, encoder, and other level signal input (signal)
rly — Output relay position (relay)
...
Of course, this depends on personal habits and there is no fixed rule. It is mainly to help oneself distinguish.
If we have a variable named VarName of type Var, we can define it as follows to use the conversion techniques mentioned earlier:
wVarName————VW2
dVarName————VD0
During program initialization, clear VW0 (or simply clear dVarName if it's a variable that doesn't need to be remembered) or set VW0 to zero within the data block. Then, when you need to access the variable as a word, use wVarName; when you need to access it as a double word, use dVarName. No type casting is required.
This method can significantly reduce the number of program statements, making the program more concise and readable. Since time-consuming type conversions are unnecessary, program execution efficiency is also improved. Furthermore, the greater the amount of mathematical computation, the more significant the efficiency improvement.
The downside is that it requires two extra bytes of memory, and VW0 cannot be used in the program afterwards. However, the S7-200 has a large amount of RAM, which is generally more than enough. For example, the 226 has as much as 10KB of RAM, and I have never used more than 1KB. This RAM was purchased with money, so it's better to use it than not use it.
Similarly, if byte-type variables frequently need to be converted to word-type variables, byte-type variables should not occupy the memory width of a word, thus avoiding the waste of a byte and type conversion.