Expression Tips and
Tricks
Last updated:
February 13, 2002
This document describes some interesting
ways to use expression instructions.
Convert a counting number
in one variable to an array of relay control variables:
Result |
Expression |
mod_data_out |
control==1 |
mod_data_out[1] |
control==2 |
mod_data_out[2] |
control==3 |
mod_data_out[3] |
control==4 |
-
Notice if the variable "control" has a value
of 0 then all mod_data_out elements are set to 0. This is because an expression
like "control==1" returns a 0 if false and a 1 if true.
-
If "control" has a value of 1 then the first
element of mod_data_out is set to 1 (presumably to set a relay on) and
the rest are set to 0.
-
A value of 2 for "control" sets mod_data_out[1]
to 1 and the rest 0. Etc.
Moving variable values from
one variable to another:
Result |
Expression |
temperature |
#mod_analog_in[3] |
#mod_data_out[1] |
relay |
-
The first expression simply moves the value
from #mod_analog_in[3] which is presumably an analog value read from a
sensor into the #mod_analog_in variable array from a Modbus instruction
that is a loop global variable into a local variable for use in the Module.
-
The second expression is an example of copying
the value of a local relay state to a loop global #mod_data_out array where
presumably it will be sent to a relay in a block with other relay values
with a Modbus instruction.
Modules can be duplicated and only entries
in expressions at the top and bottom of the module need to be modified.
At the top, the required global variables are copied to local variables.
Then again at the bottom, required local variables are copied to global
variables. In this manner the body of the module stays the same thus allowing
the associated HMI screen to be used unmodified.
Set a variable to a constant:
Result |
Expression |
status |
1 |
status |
3 'if' (X>=1) |
status |
X==2 |
-
The first expression simply sets the variable
"status" to a value of 1 every time the expression is evaluated.
-
The second expression sets the variable "status"
to a 3 but only if the variable X has a value of 1 or greater. Otherwise
the value of status is not changed, no matter what its previous
value was.
-
The third expression sets the value of status
to 1 if X has a value of 2 and it sets status to 0 if X has any value but
2.
PLC Ladder Logic:
It is very easy to convert ladder logic
to ICON expressions:
-
Use the ":" for the "or" function.
-
Use the "&" for the "and" function.
-
Use the compl() ICON function to perform "not".
-
Use parenthesis to group appropriately
-
Since standard PLC names such as X0, Y1 or
C2 are legal ICON names you may use these, but more description names such
as startup_timer_value and startup_timer_contact might be more useful.
Below are a number of ladder logic examples
with the ICON expressions following:
X1 Y0
|--[ ]----(OUT)
Result |
ICON Expression |
Y0 |
X1 |
X1 Y0
|--[ ]----(SET)
Result |
ICON Expression |
Y0 |
1 'if' X1 |
X1 Y0
|--[ ]----(RST)
Result |
ICON Expression |
Y0 |
0 'if' X1 |
X1 Y0
|--[/]----(OUT)
Result |
ICON Expression |
Y0 |
compl(X1) |
X1 Y0
|--[/]----(SET)
Result |
ICON Expression |
Y0 |
1 'if' compl(X1) |
X1 Y0
|--[/]----(RST)
Result |
ICON Expression |
Y0 |
0 'if' compl(X1) |
X0 X1
Y0
|---[ ]---[/]---(OUT)
Result |
Expression |
Y0 |
X0 & compl(X1) |
X0
X1 Y0
|---[ ]--|-[/]---(SET)
| X2 |
|---[ ]--|
Result |
Expression |
Y0 |
1 'if' ((X0 : X2) & compl(X1)) |
A standard PLC timer can be done as follows:
X0 X1 T0
K5
|--[ ]---[/]---[TMR]
Result |
Expression |
T0_Value |
(X0 & compl(X1)) * (T0_Value+1) |
T0_Contact |
T0_Value>=5 |
A standard PLC counter can be done as follows:
X0 CT0
K10
|--[ ]---|Count
|
| X1 | CNT
|
|--[ ]---|Reset
|
Result |
Expression |
CT0_Value |
CT0_Value+1 'if' (X0>CT0_Last) |
CT0_Value |
0 'if' X1 |
CT0_Contact |
CT0_Value>=10 |
CT0_Last |
X0 |