0% found this document useful (0 votes)
16 views

02 - Programming

The document discusses PLC programming including the structure of PLC programs, PLC operation, execution of PLC programs, and common programming languages. PLC programs use logic operations like AND, OR, and negation to relate inputs and outputs. The PLC reads inputs, executes the user program to determine outputs, and writes outputs. Programs are executed rule by rule in a synchronous manner. Common languages include ladder logic, structured text, and function block programming.

Uploaded by

daniel
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

02 - Programming

The document discusses PLC programming including the structure of PLC programs, PLC operation, execution of PLC programs, and common programming languages. PLC programs use logic operations like AND, OR, and negation to relate inputs and outputs. The PLC reads inputs, executes the user program to determine outputs, and writes outputs. Programs are executed rule by rule in a synchronous manner. Common languages include ladder logic, structured text, and function block programming.

Uploaded by

daniel
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 54

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

& relay

Internal states Internal states

Input side 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 links Input table update

PLC program

Instruction 1
Instruction 2
Output links …
Instruction n

… Output table update

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

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 short integer 1 byte
INT integer 2 bytes
BOOL boolean 1 bit
DINT double integer 4 bytes BYTE byte 1 byte
LINT long integer 8 bytes WORD 16 bit bit string 16 bits
USINT unsigned short integer 1 byte DWORD 32 bit bit string 32 bits
UINT unsigned integer 2 bytes LWORD 64 bit bit string 64 bits
UDINT unsigned double integer 4 bytes
ULINT unsigned long integer 8 bytes
REAL real 4 bytes
LREAL long real 8 bytes

TIME time duration


DATE calendar date
TOD time of day
DT date and time of day
STRING character strings
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 :
BOOL;
t : TIME;
out :
BOOL;
count : INT;
END_STRUCT;
END_TYPE;
Variable Declarations
Local variable:

Use VAR, VAR_INPUT, VAR_OUTPUT,


VAR VAR_IN_OUT, VAR_GLOBAL,
I,j,k : INT; VAR_EXTERNAL for different variable
v : REAL; types.
END_VAR
Operators and Expressions
( ) parenthesized expression
function( ) function
** exponentiation
- negation
NOT Boolean complement
+-*/ math operators
Y := X+1.0;
MOD modulus operation
< > <= >= comparison operators
y := a AND b;
= equal
<> not equal v := (v1 + v2 + v3)/3
AND, & Boolean AND
XOR Boolean XOR output := (light = open) OR (door = shut);
OR Boolean OR
Condition Statements
IF a > 100 THEN CASE dial_setting OF
redlight := on; 1: x := 10;
ELSEIF a > 50 THEN 2: x := 15;
3: x := 18;
yellowlight := on;
4,5: x := 20; (* 4 or 5 *)
ELSE ELSE
greenlight := on; x := 30;
END_IF; END_CASE
Iteration Statements
FOR I:= 0 to 100 BY 1 I := 0; I := 0;
DO WHILE I < 100 DO REPEAT
light[I] := ON; I := I + 1; I := I + 1;
light[I] := on; light[I] := on;
END_FOR
END_WHILE 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 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

Closed contact Vertical link

Inverter/Negation Open branch

Coil Closed branch


( )
Application instruction Rising pulse open
branch
Rising pulse
Falling pulse open
Falling pulse branch
Normally open contact
This symbol conducts when the associated device is energized.
Ladder diagram Instruction list
Device name

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.

Condition merging solves


this problem.
SET/RST instructions
Ladder diagram Instruction list

Program operation:
PLS/PLF instructions
Ladder diagram Instruction list

Program operation:
Data movement (I.)

Ladder diagram Instruction list

Program operation:
Data movement (II.)

Ladder diagram Instruction list

Program operation:
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, Usage: in counters, timers,


instruction parameters instruction parameters
Timers
Ladder diagram Instruction list

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

You might also like