Transcript Document
PLC programming
Part 2: Programming
Topics
The
structure of a PLC program
PLC operation
Execution of a PLC program
Programming languages
The structure of a PLC program
A PLC
program consists of rules that make logic relation
between inputs and outputs of the controller.
Basically it uses logic operands: AND, OR, negation
The structure of the rules is IF…THEN…ELSE…
Inputs, outputs, PLC program
Inputs
Outputs
&
≥1
switch
&
Internal states
Input side
relay
Internal states
PLC program
Output side
PLC operation
Read
all field input devices via the input
interfaces. Execute the user program stored in
application memory, then, based on whatever
control scheme has been programmed by the
user. Turn the field output devices on or off, or
perform whatever control is necessary for the
process application.
PLC operation scheme
Input signals
Power-on the PLC
…
Clear the output table
Input table update
Input links
PLC program
Output links
…
Output signals
Instruction 1
Instruction 2
…
Instruction n
Output table update
Execution of a PLC program
The
PLC resolves the program rule by rule (sequential
execution).
The PLC operates in a synchronous way i.e. inputs does
not change under a scan cycle.
Order of execution
Put the result into
an internal variable
Set the output
Program languages
Standardized
by the IEC 61131-3 in 1993:
Structure Text Programming (ST)
Functional Block Programming (FB)
Instruction List (IL)
Sequential Function Chart (SFC)
Ladder Diagram (LD) - most common
Structured Text Programming
A high level language
Used to express the behavior of functions, function blocks and programs
It has a syntax very similar to PASCAL
Strongly typed language
Functions:
assignments
expressions
statements
operators
function calls
flow control
Data Types
SINT
INT
DINT
LINT
USINT
UINT
UDINT
ULINT
REAL
LREAL
short integer
integer
double integer
long integer
unsigned short integer
unsigned integer
unsigned double integer
unsigned long integer
real
long real
TIME
DATE
TOD
DT
STRING
time duration
calendar date
time of day
date and time of day
character strings
1 byte
2 bytes
4 bytes
8 bytes
1 byte
2 bytes
4 bytes
8 bytes
4 bytes
8 bytes
BOOL
BYTE
WORD
DWORD
LWORD
boolean
byte
16 bit bit string
32 bit bit string
64 bit bit string
1 bit
1 byte
16 bits
32 bits
64 bits
Derived Data Types
TYPE
(* user defined data types, this is a comment*)
pressure
: REAL;
temp
: REAL;
part_count
: INT;
END_TYPE;
Structure:
TYPE data_packet:
STRUCT
input
t
out
count
END_STRUCT;
END_TYPE;
: BOOL;
: TIME;
: BOOL;
: INT;
Variable Declarations
Local variable:
VAR
I,j,k
v
END_VAR
:
:
INT;
REAL;
Use VAR, VAR_INPUT, VAR_OUTPUT,
VAR_IN_OUT, VAR_GLOBAL,
VAR_EXTERNAL for different variable
types.
Operators and Expressions
( )
function( )
**
NOT
+-*/
MOD
< > <= >=
=
<>
AND, &
XOR
OR
parenthesized expression
function
exponentiation
negation
Boolean complement
math operators
modulus operation
comparison operators
equal
not equal
Boolean AND
Boolean XOR
Boolean OR
Y := X+1.0;
y := a AND b;
v := (v1 + v2 + v3)/3
output := (light = open) OR (door = shut);
Condition Statements
IF a > 100 THEN
redlight := on;
ELSEIF a > 50 THEN
yellowlight := on;
ELSE
greenlight := on;
END_IF;
CASE dial_setting OF
1:
x := 10;
2:
x := 15;
3:
x := 18;
4,5:
x := 20; (* 4 or 5 *)
ELSE
x := 30;
END_CASE
Iteration Statements
FOR I:= 0 to 100 BY 1
DO
light[I] := ON;
END_FOR
I := 0;
WHILE I < 100 DO
I := I + 1;
light[I] := on;
END_WHILE
I := 0;
REPEAT
I := I + 1;
light[I] := on;
UNTIL I > 100;
END_REPEAT
Functions
FUNCTION add_num : REAL
VAR_INPUT
I,J : REAL
END_VAR
add_num := I + J;
END_FUNCTION
Call a function:
x:= add_num(1.2, 5.6);
Built-in Functions:
ABS, SQRT, LN, LOG, EXP, SIN, COS, TAN, ASIn, ACOS, ATAN, ADD, MUL, SUB, DIV, MOD, EXPT,
MOVE), logic functions (AND, OR, XOR, NOT), bit string functions (SHL, SHR shift bit string left and
right , ROR, ROL rotate bit string), etc.
Programs
PROGRAM example7.1
VAR_INPUT
MSI :
BOOL;
C1
:
BOOL;
C2
:
BOOL;
C3
:
BOOL;
C4
:
BOOL;
END_VAR
VAR_OUTPUT
R1
:
BOOL : FALSE;
R2
:
BOOL : FALSE;
R3
:
BOOL : FALSE;
R4
:
BOOL : FALSE;
END_VAR
R1 := MS1 AND (NOT R4);
R2 := R4 AND (NOT C3) AND (NOT C2);
R3 := C4 AND (NOT C3);
R4 := C1;
END_PROGRAM
Functional Block Programming
Functional
block (FB) is a well packaged element of
software that can be re-used in different parts of an
application or even in different projects. Functional blocks
are the basic building blocks of a control system and can
have algorithms written in any of the IEC languages.
An Up Counter Function Block
The algorithm in Structured Text:
CU
R
PV
Q
CV
: input to be counted
: reset
: preset value
: contact output
: counter value.
FUNCTION BLOCK CTU
VAR_INPUT
CU : BOOL;
R : BOOL;
PV : INT;
END_VAR
VAR_OUTPUT
Q : BOOL;
CV : INT;
END_VAR
IF R THEN
CV := 0;
ELSEIF CU
AND (CV < PV) THEN
CV := CV + 1;
END_IF;
Q := (CV >= PV);
END_FUNCTION_BLOCK
A PID Control Function Block
SP
set point
PV
sensor feedback
KP
proportional error gain
TR
integral gain
TD
derivative gain
AUTO
calculate
XOUT
output to process
XO
manual output
adjustment
cycle
time between execution
PID block diagram
Vout K p E Tr Edt Td
dE
dt
PID control algorithm
Instruction List Programming
A
low level language which has a structure similar to an
assembly language. Since it is simple, it is easy to learn
and ideally for small hand-held programming devices.
Each line of code can be divided into four fields: label,
operator, operand, and comment.
For example:
loop
LD
ST
ANDN
MS1
R1
C3
Operators
Operator
LD
ST
S
R
AND
&
OR
XOR
ADD
SUB
MUL
DIV
Modifiers
N
N
N, (
N, (
N, (
N, (
(
(
(
(
Description
load operand into
register
store register value
into operand
set operand true
reset operand false
Boolean AND
Boolean AND
Boolean OR
Boolean XOR
addition
subtraction
multiplication
division
GT
(
greater than
GE
(
greater than and equal to
EQ
(
equal
NE
(
not equal
LE
(
less than and equal to
LT
(
less than
JMP
C, N
jump to label
CAL
C, N
call function block
RET
C, N
return from function or
function block
)
execute last deferred operator
Modifier “N” means negate. “(“ defers the
operator. “C” is a condition modifier, the
operation is executed if the register value is true.
Sequential Function Chart
A graphics language used for depicting sequential behavior. The IEC
standard grew out of the French standard Grafcet which in turn is
based on Petri-net. A SFC is depicted as a series of steps shown as
rectangular boxes connected by vertical lines. Each step represent a
state of the system being controlled. The horizontal bar indicates a
condition. It can be a switch state, a timer, etc. A condition statement
is associated with each condition bar. Each step can also have a set
of actions. Action qualifier causes the action to behave in certain
ways. The indicator variable is optional. It is for annotation purposes.
SFC
Action Qualifiers:
N
non-stored, executes while the step is
active
R
resets a store action
S
sets an action active
L
time limited action, terminates after a given
period
D
time delayed action.
P
a pulse action, executes once in a step
SD stored and time delayed
DS time delayed and stored
SL
stored and time limited
Ladder diagram
Based
on relay logic: ON/OFF
ON/OFF events mean logic values: TRUE/FALSE
Instructions have the logic form: IF…THEN…ELSE
Conditions and consequences consist of TRUE/FALSE
values combined with AND/OR and NOT operations.
Ladder diagram is a graphical interpretation of the control
laws.
Example: motor control (I.)
For a process control, it is desired to have the process
start (by turning on a motor) five seconds after a part
touched a limit switch.
The process is terminated
automatically when the finished part touches a second
limit switch. An emergency switch will stop the process
any time when it is pushed.
Example: motor control (II.)
L1
LS1
PB1
LS2
R1
R1
R1
TIMER
R2
PB1
LS1
LS2
PR=5
Limit switch 1-2 (LS1-2) is on: true
LS1-2 is off: false
Push button (PB1) is on: true
PB1 is off: false
Relay 1-2 (R1-2) is on: true
R1-2 is off: false
Timer <5 sec: false
=5 sec: true
TIMER
5
R1
Motor
R2
Example: motor control (III.)
IF (LS1 = true OR R1=true) AND PB1=false AND LS2=false THEN
R1 := true;
ELSE
R1 := false;
END_IF;
IF R1 = true AND Timer=true THEN
R2 := true;
ELSE
R2 := false;
END_IF;
Elements of a ladder diagram
In the followings the Mitsubishi nomenclature will be used.
( )
Open contact
Horizontal link
Closed contact
Vertical link
Inverter/Negation
Open branch
Coil
Closed branch
Application instruction
Rising pulse open
branch
Rising pulse
Falling pulse
Falling pulse open
branch
Normally open contact
This symbol conducts when the associated device is energized.
Ladder diagram
Device name
Program operation:
Instruction list
Normally closed contact
This symbol conducts when the associated device is de-energized.
Ladder diagram
Device name
Program operation:
Instruction list
Coil
This
symbol always appears just before the right vertical
ladder rail. It becomes energized when the logic before it
conducts. When energized, the output with the same
address becomes active. In instruction mode the
mnemonic is OUT, for OUTPUT ACTIVATE. This symbol
occupies 1 step of program space, unless being used for
a timer or counter instruction, when it can occupy up to 5
steps.
Application instruction
This
symbol usually appears just before the right vertical
ladder rail when used for bit control. This symbol is
typically used for word device commands; however there
are a few bit instructions that use the brackets as well. It
becomes energized when the logic before it conducts.
This symbol occupies multiple steps of program space
depending on the command used.
Invert
This
symbol inverts the state of all logic before it. If the
logic is true (positive) at the point of the invert, the output
of the invert is false (negative). If the logic is false, the
invert output is true.
Rising/Falling pulse
It
works similar to open contact/closed contact, however it
conducts for one cycle starting at the moment when the
signal changes, i.e. rises up from 0 to 1/falls down from 1
to 0.
AND instruction
Ladder diagram
Program operation:
Instruction list
OR instruction
Ladder diagram
Program operation:
Instruction list
The problem of multiple outputs
A typical programming
error to set the value of the
same device in several
instructions.
Condition merging solves
this problem.
SET/RST instructions
Ladder diagram
Program operation:
Instruction list
PLS/PLF instructions
Ladder diagram
Instruction list
Program operation:
Data movement (I.)
Ladder diagram
Program operation:
Instruction list
Data movement (II.)
Ladder diagram
Program operation:
Instruction list
Devices
Device name
Type
Function
X
Input
Gets the input signal of the PLC
Y
Output
Gives the output signal of the PLC
M
Auxiliary relay
A logic valued internal register
T
Timer
Timing function
C
Counter
Counts signal changes
D
Data register
16/32 bit data container
Inputs
Name: X
Role: Denotes the physical PLC inputs.
Explanation:
(1)
(2)
IF X0=true AND X1=false then
Y10:=true;
ELSE
Y10:=false;
END_IF;
Outputs
Name: Y
Role: Denotes the physical PLC outputs.
Explanation:
(1)
IF (X0=true OR Y10=true) AND X1=false then
(2)
Y10:=true;
ELSE
Y10:=false;
END_IF;
Auxiliary relays
Name: M
Role: internal programmable state marker
Explanation:
(1)
IF (X0=true OR M507=true) AND X1=false then
(2)
M507:=true;
ELSE
M507:=false;
END_IF;
Other applications: general state marker, special
internal register, PLC diagnostics
16 bit data registers
16 bit data
16 bit data range:
0000h – FFFFh (Hex)
0-65535 (Decimal)
0:= positive number
1:= negative number
32 bit data registers
32 bit data
32 bit data range:
00000000h –
FFFFFFFFh (Hex)
0-4 billion (Decimal)
0:= positive number
1:= negative number
Special data registers: constants
Notation: K
Notation: H
Role: decimal constant values
Role: Hexadecimal constant values
Types: 16 (-32768 - +32767) and
Types: 16 (0 - FFFF) and
32 (-2147483648 - +2147483647) bit 32 (0 – FFFF FFFF) bit
Usage: in counters, timers,
instruction parameters
Usage: in counters, timers,
instruction parameters
Timers
Ladder diagram
Program operation:
Instruction list
Retentive timers
Ladder diagram
Instruction list
Program operation:
Counters
Ladder diagram
Instruction list
Program operation:
References
http://www.gandhcontrols.com/Downloads/Manuals/PLC
Programming.ppt
www.dcc.ttu.ee/lap/lap5760/jy992d48301-j_fx.pdf