02 - Programming
02 - 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
& relay
PLC program
Instruction 1
Instruction 2
Output links …
Instruction n
Output signals
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
TYPE data_packet:
STRUCT
input :
BOOL;
t : TIME;
out :
BOOL;
count : INT;
END_STRUCT;
END_TYPE;
Variable Declarations
Local variable:
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 R1 := MS1 AND (NOT R4);
MSI : BOOL; R2 := R4 AND (NOT C3) AND (NOT C2);
C1 : BOOL;
R3 := C4 AND (NOT C3);
C2 : BOOL;
R4 := C1;
C3 : BOOL;
C4 : BOOL; END_PROGRAM
END_VAR
VAR_OUTPUT
R1 : BOOL : FALSE;
R2 : BOOL : FALSE;
R3 : BOOL : FALSE;
R4 : BOOL : FALSE;
END_VAR
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: FUNCTION BLOCK CTU
VAR_INPUT
CU : BOOL;
R : BOOL;
PV : INT;
END_VAR
VAR_OUTPUT
Q : BOOL;
CV : INT;
END_VAR
IF R THEN
CU : input to be counted CV := 0;
R : reset ELSEIF CU
PV : preset value AND (CV < PV) THEN
Q : contact output CV := CV + 1;
CV : counter value. 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 PID block diagram
TD derivative gain
AUTO calculate
XOUT output to process
Vout K p E Tr Edt Td dE
dt
XO manual output
adjustment PID control algorithm
cycle time between execution
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:
LD MS1
ST R1
loop ANDN C3
Operators
Operator Modifiers Description
LD N load operand into register
GT ( greater
ST N store register value into than
operand
GE ( greater
S set operand true
than and equal to
R reset operand false
AND N, ( Boolean EQ ( equal
AND
& N, ( Boolean AND
NE ( not equal
ORN, ( Boolean OR LE ( less than
XOR N, ( Boolean and equal to
XOR
ADD ( addition LT ( less than
SUB ( subtraction JMP C, N jump to
MUL ( multiplication
label
DIV ( division
CAL C, N call
function block
Modifier “N” means negate. “(“ defers the
RET modifier,
operator. “C” is a condition C, N thereturn
operation is executedfrom function
if the or function
register block
value is true.
) execute last
deferred operator
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
TIMER R2
R1 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 TIMER
PB1 is off: false
Relay 1-2 (R1-2) is on: true 5
Motor
R1-2 is off: false R2
Timer <5 sec: false
=5 sec: true R1
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
Program operation:
Normally closed contact
This symbol conducts when the associated device is de-energized.
Ladder diagram Instruction list
Device name
Program operation:
Coil
Thissymbol 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
Thissymbol 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
Itworks 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 Instruction list
Program operation:
OR instruction
Ladder diagram Instruction list
Program operation:
The problem of multiple outputs
A typical programming
error to set the value of the
same device in several
instructions.
Program operation:
PLS/PLF instructions
Ladder diagram Instruction list
Program operation:
Data movement (I.)
Program operation:
Data movement (II.)
Program operation:
Devices
Device name Type Function
Explanation:
(1) (2)
IF X0=true AND X1=false then
Y10:=true;
ELSE
Y10:=false;
END_IF;
Outputs
Name: Y
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
00000000h –
FFFFFFFFh (Hex)
0-4 billion (Decimal)
0:= positive number
1:= negative number
Special data registers: constants
Notation: K Notation: H
Program operation:
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