This chapter continues from the previous chapter and discusses the other IEC 1131-3 programming languages, that is, instruction lists (ILs), sequential function charts (SFCs), and structured text (ST).
6.1. Instruction Lists
A programming method that can be considered to be the entering of a ladder program using text is the instruction lists (IL). An instruction list gives programs as a series of instructions, with each instruction on a new line. Each instruction consists of an operator followed by one or more operands, that is, the subjects of the operator. Thus we might have:
to indicate that the operand A is to be loaded, LD being the operator used to indicate loading. In terms of ladder diagrams, an operator may be regarded as a ladder element, and LD is equivalent to starting a rung with open contacts for input A. Another instruction might be:
to indicate that there is to be an output to Q.
Mnemonic codes are used for operators, each code corresponding to an operator/ladder element. The codes used differ to some extent from manufacturer to manufacturer, though a standard IEC 1131-3 has been proposed and is being widely adopted.
Table 6.1 shows some of the codes used by manufacturers and the proposed standard for instructions used in this chapter (see later chapters for codes for other functions).
Table 6.1 Instruction Code Mnemonics
IEC 1131-3 | Mitsubishi | OMRON | Siemens | Operation | Ladder Diagram |
---|
LD | LD | LD | A | Load operand into result register. | Start a rung with open contacts. |
LDN | LDI | LD NOT | AN | Load negative operand into result register. | Start a rung with closed contacts. |
AND | AND | AND | A | Boolean AND. | Series element with open contacts. |
ANDN | ANI | AND NOT | AN | Boolean AND with negative operand. | Series element with closed contacts. |
OR | OR | OR | O | Boolean OR. | Parallel element with open contacts. |
ORN | ORI | OR NOT | ON | Boolean OR with negative operand. | Parallel element with closed contacts. |
ST | OUT | OUT | = | Store result register into operand. | An output. |
Instruction List is a low-level textual language that is simple to implement and used by a number of PLC manufacturers, mainly for small and medium-sized PLCs. It is particularly suitable for small, straightforward programs. Some manufactures do not support ILs but use only the higher-level language of structured text (ST).
As an illustration of the use of IEC 113-1 operators, consider the following:
LD | A | (*Load A*) |
AND | B | (*AND B*) |
ST | Q | (*Store result in Q, i.e. output to Q*) |
In the first line of the program, LD is the operator, A the operand, and the words at the ends of program lines and in brackets and preceded and followed by * are comments added to explain what the operation is and are not part of the program operation instructions to the PLC. LD A is thus the instruction to load the A into the memory register. It can then later be called on for further operations. The next line of the program has the Boolean operation AND performed with A and B. The last line has the result stored in Q, that is, output to Q.
Labels can be used to identify various entry points to a program, useful, as we will find later, for jumps in programs; these precede the instruction and are separated from it by a colon. Thus we might have:
with the instruction earlier in the program to jump to PUMP_OK if a particular condition is realized.
With the IEC 113-1 operators, an N after it is used to negate its value. For example, if we have:
LD | A | (*Load A*) |
ANDN | B | (*AND NOT B*) |
Thus the ANDN operator inverts the value of ladder contacts and ANDs the result.
6.1.1. Ladder Programs and Instruction Lists
When looked at in terms of ladder diagrams, whenever a rung is started, it must use a “start a rung” code. This might be LD, or perhaps A or L, to indicate that the rung is starting with open contacts, or LDI, or perhaps LDN, LD NOT, AN, or LN, to indicate it is starting with closed contacts. All rungs must end with an output or store result code. This might be OUT or = or ST. The following shows how individual rungs on a ladder are entered using the Mitsubishi mnemonics for the AND gate, shown in
Figure 6.1a.
The rung starts with LD because it is starting with open contacts. For
Figure 6.1a, since the address of the input is X400, the instruction is LD X400. This is followed by another open contacts input, and so the next program line involves the instruction AND with the address of the element; thus the instruction is AND X401. The rung terminates with an output, so the instruction OUT is used with the address of the output, that is, OUT Y430. The single rung of a ladder would thus be entered as:
For the same rung with Siemens notation (
Figure 6.1b), we have:
Consider another example: an OR gate.
Figure 6.2a shows the gate with Mitsubishi notation.
The instruction for the rung in
Figure 6.2a starts with an open contact and is LD X400. The next item is the parallel OR set of contacts X401. Thus the next instruction is OR X401. The last step is the output, hence OUT Y430. The instruction list would thus be:
Figure 6.2b shows the Siemens version of the OR gate. The following is the Siemens instruction list:
Figure 6.3a shows the ladder system for a NOR gate in Mitsubishi notation.
The rung in
Figure 6.3a starts with normally closed contacts, so the instruction is LDI. When added to Mitsubishi instruction, I is used to indicate the inverse of the instruction. The next step is a series of normally closed contacts and so ANI, again the I being used to make an AND instruction the inverse. I is also the instruction for a NOT gate. The instructions for the NOR gate rung of the ladder would thus be entered as:
Figure 6.3b shows the NOR gate with Siemens notation. Note that N added to an instruction is used to make the inverse. The instruction list then becomes:
Consider the rung shown in
Figure 6.4a in Mitsubishi notation, a NAND gate.
Figure 6.4a starts with the normally closed contacts X400 and so starts with the instruction LDI X400. The next instruction is for a parallel set of normally closed contacts; thus the instruction is ORI X401. The last step is the output, hence OUT Y430. The instruction list is thus:
Figure 6.4b shows the NAND gate in Siemens notation. The instruction list is then:
6.1.2. Branch Codes
The EXCLUSIVE OR (XOR) gate shown in
Figure 6.5 has two parallel arms with an AND situation in each arm.
Figure 6.5a shows Mitsubishi notation With such a situation, Mitsubishi uses an ORB instruction to indicate “OR together parallel branches.” The first instruction is for a normally open pair of contacts X400. The next instruction is for a series set of normally closed contacts X401, hence ANI X401. After reading the first two instructions, the third instruction starts a new line. It is recognized as a new line because it starts with LDI, all new lines starting with LD or LDI. But the first line has not been ended by an output. The PLC thus recognizes that a parallel line is involved for the second line and reads together the listed elements until the ORB instruction is reached. The mnemonic ORB (OR branches/blocks together) indicates to the PLC that it should OR the results of the first and second instructions with that of the new branch with the third and fourth instructions. The list concludes with the output OUT Y430. The instruction list would thus be entered as:
LD | X400 |
ANI | X401 |
LDI | X400 |
AND | X401 |
ORB | |
OUT | Y430 |
Figure 6.5b shows the Siemens version of an XOR gate. Brackets are used to indicate that certain instructions are to be carried out as a block. They are used in the same way as brackets in any mathematical equation. For example, (2 + 3) / 4 means that the 2 and 3 must be added before dividing by 4. Thus with the Siemens instruction list we have in step 0 the instruction A(. The brackets close in step 3. This means that the A in step 0 is applied only after the instructions in steps 1 and 2 have been applied.
Step | Instruction |
---|
0 | A( | |
1 | A | I0.0 |
2 | AN | I0.1 |
3 | ) | |
4 | O( | |
5 | AN | I0.0 |
6 | A | I0.1 |
7 | ) | |
8 | = | Q2.0 |
The IEC 1131-3 standard for such programming is to use brackets in the way used in the previous Siemens example, that is, in the same way brackets are used in normal arithmetic. This enables instructions contained within brackets to be deferred until the bracket is completed. Thus the IEC instruction list program:
Gives X + (B × (C + D)).
Figure 6.6 shows a circuit that can be considered as two branched AND blocks.
Figure 6.6a shows the circuit in Mitsubishi notation. The instruction used here is ANB. The instruction list is thus:
Step | Instruction |
---|
0 | LD | X400 |
1 | OR | X402 |
2 | LD | X401 |
3 | OR | X403 |
4 | ANB | |
5 | OUT | Y430 |
Figure 6.6b shows the same circuit in Siemens notation. Such a program is written as an instruction list using brackets. The A instruction in step 0 applies to the result of steps 1 and 2. The A instruction in step 4 applies to the result of steps 5 and 6. The program instruction list is thus:
Step | Instruction |
---|
0 | A( | |
1 | A | I0.0 |
2 | O | I0.2 |
3 | ) | |
4 | A( | |
5 | A | I0.1 |
6 | O | I0.3 |
7 | ) | |
8 | = | Q2.0 |
6.1.3. More Than One Rung
Figure 6.7a shows a ladder, in Mitsubishi notation, with two rungs. In writing the instruction list we just write the instructions for each line in turn. The instruction LD or LDI indicates to the PLC that a new rung is starting. The instruction list is thus:
LD | X400 |
OUT | Y430 |
LDI | X400 |
OUT | Y431 |
The system is one where when X400 is not activated; there is an output from Y431 but not Y430. When X400 is activated, there is then an output from Y430 but not Y431.
Figure 6.7b shows the same program in Siemens notation. The = instruction indicates the end of a line. The A or AN instruction does not necessarily indicate the beginning of a rung since the same instruction is used for AND and AND NOT. The instruction list is then:
6.1.4. Programming Examples
The following tasks are intended to illustrate the application of the programming techniques given in this section and are the examples for which ladder diagrams and function block diagrams were derived in
Section 5.7. (See that section for an explanation of the ladder diagrams; here we show the instruction lists relating to the programs.)
A signal lamp is required to be switched on if a pump is running and the pressure is satisfactory or if the lamp test switch is closed.
Figure 6.8 shows the ladder program and the related instruction list.
For a valve that is to be operated to lift a load when a pump is running and either the lift switch or a switch operated, indicating that the load has not already been lifted and is at the bottom of its lift channel,
Figure 6.9 shows the ladder program and the related instruction list.
For a system in which there has to be no output when any one of four sensors gives an output and otherwise there is to be an output,
Figure 6.10 shows the ladder program and the instruction list.
6.2. Sequential Function Charts
If we wanted to describe a traffic lamp sequence, one way we could do this would be to represent it as a sequence of functions or states such as red light state and green light state and the inputs and outputs to each state.
Figure 6.11 illustrates this idea. State 0 has an input that is triggered after the green light has been on for 1 minute and an output of red light on. State 1 has an input that is triggered after the red light has been on for 1 minute and an output of green light on.
The term sequential function chart (SFC) is used for a pictorial representation of a system's operation to show the sequence of events involved in its operation. SFC charts have the following elements:
• The operation is described by a number of separate sequentially connected states or steps that are represented by rectangular boxes, each representing a particular state of the system being controlled. The initial step in a program is represented differently from the other steps;
Figure 6.12 shows its representation.
• Each connecting line between states has a horizontal bar representing the transition condition that has to be realized before the system can move from one state to the next. Two steps can never be directly connected; they must always be separated by a transition. Two transitions can never directly follow from one to another; they must always be separated by a step.
• When the transfer conditions to the next state are realized, the next state or step in the program occurs.
• The process thus continues from one state to the next until the complete machine cycle is completed.
• Outputs/actions at any state are represented by horizontally linked boxes and occur when that state has been realized.
As an illustration,
Figure 6.13 shows part of an SFC and its equivalent ladder diagram.
As an illustration of the principles of SFC, consider the situation with, say, part of the washing cycle of a domestic washing machine where the drum is to be filled with water, and then when the drum is full, a heater has to be switched on and remain on until the temperature reaches the required level. Then the drum is to be rotated for a specified time. We have a sequence of states that can be represented in the manner shown in
Figure 6.14.
The Sequential Function Chart language is a powerful graphical technique for describing the sequential behavior of a program. Graphical languages have been used for a number of years, Grafset being a European graphical language. The IEC 1131-1 standard, that is, SFC, resembles many of the features of Grafset.
6.2.1. Branching and Convergence
Selective branching is illustrated in
Figure 6.15 and allows for different states to be realized, depending on the transfer condition that occurs.
Parallel branching (
Figure 6.16), represented by a pair of horizontal lines, allows for two or more different states to be realized and proceed simultaneously.
Figures 6.17 and
6.18 show how convergence is represented by an SFC. In
Figure 6.17 the sequence can go from state 2 to state 4 if IN 4 occurs or from state 3 to state 4 if IN 5 occurs. In
Figure 6.18 the sequence can go simultaneously from both state 2 and state 3 to state 4 if IN 4 occurs.
As an illustration of the use of the preceding,
Figure 6.19 shows part of a program represented by both its SFC and ladder programs.
6.2.2. Actions
With states, there is an action or actions that have to be performed. Such actions, such as the outputs in the preceding example, are depicted as rectangular boxes attached to the state. The behavior of the action can be given using a ladder diagram, a function block diagram, an instruction list, or structured text. Thus, where a ladder diagram is used, the behavior of the action is shown by the ladder diagram being enclosed within the action box. The action is then activated when there is a power flow into the action box.
Figure 6.20 illustrates this concept.
Action boxes are generally preceded by qualifiers to specify the conditions to exist for the action. In the absence of a qualifier or the qualifier N, the action is not stored and executed continually while the associate state is active. The qualifier P is used for a pulse action that executes only once when a step is activated. The qualifier D is used for a time-delayed action that only starts after a specified period. The qualifier L is used for a time-limited action that terminates after a specified period (
Figure 6.21).
6.3. Structured Text
Structured text is a programming language that strongly resembles the programming language Pascal. Programs are written as a series of statements separated by semicolons. The statements use predefined statements and subroutines to change variables, these being defined values, internally stored values, or inputs and outputs.
Assignment statements are used to indicate how the value of a variable is to be changed, such as
is used to indicate that a light, the variable, is to have its “value” changed, that is, switched on or off, when switch A changes its “value,” that is, is on or off. The general format of an assignment statement is:
where Y represents an expression that produces a new value for the variable X and := is the assignment symbol. The variable retains the assigned value until another assignment changes the value. Other examples are:
to indicate that the light is switched on by either switch A OR switch B. Using the AND function, we might have:
to indicate that start occurs when steam AND the pump are on.
Table 6.2 shows some of the operators, such as the OR and AND in the preceding statements, that are used in structured text programs and their relative precedence when an expression is being evaluated. Parentheses (brackets) are used to group expressions within expressions to ensure that the expression is executed in the required sequence. For example:
Table 6.2 Structured Text Operators
Operator | Description | Precedence |
---|
(…) | Parenthesized (bracketed) expression | Highest |
Function(…) | List of parameters of a function | |
** | Raising to a power | |
-, NOT | Negation, Boolean NOT | |
*, /, MOD | Multiplication, division, modulus operation | |
+, - | Addition, subtraction | |
<, >, <=, >= | Less than, greater than, less than or equal to, greater than or equal to | |
=, <> | Equality, inequality | |
AND, & | Boolean AND | |
XOR | Boolean XOR | |
OR | Boolean OR | Lowest |
OutputQ := InputA/3 + InputB/(3 - InputC)
has (3 Ð InputC) evaluated before its value is used as a divisor, so the second part of the OutputQ statement is 4/(3 Ð 2) = 4. Division has precedence over addition, so the first part of the statement is evaluated before the addition, that is, 6/3. So we have for OutputQ the value 2 + 4 = 6.
Structured text is not case sensitive; thus lowercase or capital letters can be used as is felt necessary to aid clarity. Likewise, spaces are not necessary but can be used to aid clarity; likewise indenting lines. All the identities of directly represented variables start with the % character and are followed by a one- or two-letter code to identify whether the memory location is associated with inputs, outputs, or internal memory and whether it is bits, bytes, or words, such as
%IX100 (*Input memory bit 100*)
%ID200 (*Input memory word 200*)
%QX100 (*Output memory bit 100*)
The first letter is I for input memory location, Q for output memory location, or M for internal memory. The second letter is X for bit, B for byte (8 bit), W for word (16 bits), D for double word (32 bits), or L for long word (64 bits).
AT is used to fix the memory location for a variable. Thus we might have:
Input1 AT %IX100; (*Input1 is located at input memory bit 100*)
6.3.1 Conditional Statements
The IF statement:
is used to indicate that if the fluid temp variable is ON, that is, 1, the actions following that line in the program are to occur. The IF statement:
is used to indicate that if the fluid temp variable is NOT 1, the actions following that line in the program are to occur. The IF statement:
IF fluid_temp1 OR fluid_temp2 THEN
is used to indicate that if the fluid temp variable 1, the fluid temp variable 2 is ON, that is, 1, the actions following that line in the program are to occur.
IF … THEN … ELSE is used when selected statements are to be executed when certain conditions occur. For example:
IF (Limit_switch1 AND Workpiece_Present) THEN
Note that the end of the IF statement has to be indicated. Another example, using PLC addresses, is:
So, if there is an input to I:000/00 to make it 1, output O:001/00 is 1; otherwise it's 0.
CASE is used to give the condition that selected statements are to be executed if a particular integer value occurs else some other selected statements. For example, for temperature control we might have:
0 … 40 :Furnace_switch := On;
40 … 100:Furnace_switch := Off;
Note, as with all conditional statements, the end of the CASE statement has to be indicated. Another example might be, for the operation of a motor with fans being required to operate at different speeds based on the operation of particular switch positions:
3:speed := 15; fan 1 := ON;
4:speed := 20; fan 2 := ON;
Speed :=0; speed fault := TRUE;
6.3.2 Iteration Statements
These are used where it is necessary to repeat one or more statements a number of times, depending on the state of some variable. The FOR … DO iteration statement allows a set of statements to be repeated depending on the value of the iteration integer variable. For example:
FOR Input := 10 to 0 BY −1
has the output decreasing by 1 each time the input, dropping from 10 to 0, decreases by 1.
WHILE … DO allows one or more statements to be executed while a particular Boolean expression remains true, such as:
REPEAT … UNTIL allows one or more statements to be executed and repeated while a particular Boolean expression remains true.
UNTIL (Input1 = Off) OR (OutputQ > 5)
6.3.3 Structured Text Programs
Programs have first to define the data types required to represent data, such as:
TYPE Motor: (Stopped, Running);
TYPE Valve: (Open, shut);
TYPE Pressure: REAL; (*The pressure is an analogue value*)
the variables, that is, signals from sensors and output signals to be used in a program, such as:
PumpFault : BOOL; (*Pump operating fault is a Boolean variable*)
Value: INT; (*The value is an integer*)
Input1 AT %IX100; (*Input1 is located at input memory bit 100*)
and any initial values to be given to variables, such as:
Temp : REAL =100; (*Initial value is a an analogue number 100*)
before getting down to the instruction statements.
The following is an example of a function block that might appear in a larger program and is concerned with testing voltages:
FUNCTION_BLOCK TEST_VOLTAGE
OVERVOLTS :=TRUE; RETURN;
OVERVOLTS :=TRUE; RETURN;
If the testing of volts 1, volts 2, or volts 3 indicates that any one of them is more than 12, the output OVERVOLTS is set to true and the RETURN statement called to terminate the execution of the function block. In the rest of the program, when OVERVOLTS is set to true, the program will initiate some action.
6.3.4. Comparison with Ladder Programs
Figure 6.22 shows a ladder rung and its equivalent expressions in structured text;
Figure 6.23 shows another ladder rung and equivalents in function box and STC.
Summary
A programming method that can be considered to be the entering of a ladder program using text is the instruction list (IL). An IL gives programs as a series of instructions, each instruction being on a new line. Each instruction consists of an operator followed by one or more operands, that is, the subjects of the operator. Mnemonic codes are used, each code corresponding to an operator/ladder element.
The programming method sequential function chart (SFC) is used for a pictorial representation of a system's operation to show the sequence of the events involved in its operation. The operation is described by a number of separate sequentially connected states or steps that are represented by rectangular boxes, each representing a particular state of the system being controlled. Each connecting line between states has a horizontal bar representing the transition condition that has to be realized before the system can move from one state to the next. When the transfer conditions to the next state are realized, the next state or step in the program occurs. The process thus continues from one state to the next until the complete machine cycle is completed. Outputs/actions at any state are represented by horizontally linked boxes and occur when that state has been realized.
With the programming method structured text (ST), programs are written as a series of statements separated by semicolons. The statements use predefined statements and subroutines to change variables, these being defined values, internally stored values, or inputs and outputs. Assignment statements are used to indicate how the value of a variable it to be changed, such as X := Y. Structured text is not case sensitive and spaces are not necessary but can be used to aid clarity. IF … THEN … ELSE is used when selected statements are to be executed when certain conditions occur. CASE is used to give the condition that selected statements are to be executed if a particular integer value occurs else some other selected statements. FOR … DO … allows a set of statements to be repeated depending on the value of the iteration integer variable. WHILE … DO .. allows one or more statements to be executed while a particular Boolean expression remains true. REPEAT … UNTIL … allows one or more statements to be executed and repeated while a particular Boolean expression remains true.