PLC Programming
PLC Programming
Chang
Chapter 7. PROGRAMMABLE
LOGIC CONTROLLER
11/28/2014
7- 1
T.C. Chang
FUNCTIONS OF CONTROLLERS
1) on-off control,
2) sequential control,
11/28/2014
7- 2
T.C. Chang
CONTROL DEVICES
1) mechanical control - cam, governor,
etc.,
2) pneumatic control - compressed air,
valves,etc.
3) electromechanical control - switches,
relays, a timer, counters, etc,
4) electronics control - similar to
electromechanical control, except uses
electronic switches.
5) computer control.
11/28/2014
7- 3
T.C. Chang
PROGRAMMABLE LOGIC
CONTROLLER
Invented in 1968 as a substitute for hardwired relay panels.
"A digitally operating electronic apparatus which uses a
programmable memory for the internal storage of instructions
by implementing specific functions such as logic sequencing,
timing, counting, and arithmetic to control, through digital or
analog input/output modules, various types of machines or
processes. The digital computer which is used to perform the
functions of a programmable controller is considered to be
within this scope.
Excluded are drum and other similar
mechanical sequencing controllers."
National Electrical Manufacturing Association (NEMA)
11/28/2014
7- 4
T.C. Chang
VENDORS
MODICOM - GOULD
ALLEN-BRADLEY
GE
SQUARE-D
etc.
11/28/2014
7- 5
T.C. Chang
PLC
CPU
Input
Input
Module
Flag
System
Output
Output
Module
User Ladder
Diagram
Working
memory
registers
11/28/2014
7- 6
T.C. Chang
Allen-Bradley PLC-3
11/28/2014
7- 7
T.C. Chang
11/28/2014
7- 8
T.C. Chang
SWITCHES
No n-lo c king
Lo c king
DPST
P1
SPDT
P2
11/28/2014
Multiple Thro w
Multiple P o le
Ma ke -be fo re -bre a k
7- 9
T.C. Chang
TERMS
Throw - number of states
B
Output
DPST
Input
Knob
How is this switch classified?
11/28/2014
7- 10
T.C. Chang
TYPES OF SWITCHES
11/28/2014
Selector switches
Pushbutton switches
Photoelectric switches
Limit Switches
Proximity switches
Level switches
Thumbwheel switches
Slide switches
RATING:
24 Volts AC/DC
48 Volts AC/DC
120 Volts AC/DC
230 Volts AC/DC
TTL level
(Transistor-to-transistor
5V)
Isolated Input
7- 11
T.C. Chang
RELAYS
A switch whose operation is activated by an electromagnet is called a "relay"
contact
coil
input
Relay coil
R1
Output contact
11/28/2014
R1
7- 12
T.C. Chang
COUNTER
Digital counters output in the form of a relay contact when a preassigned
count value is reached.
input
Re giste r
Accumulator
re se t
conta ct
output
Input
Re se t
Output
Count
11/28/2014
12
1
7- 13
T.C. Chang
TIMER
A timer consists of an internal clock, a count value register, and an
accumulator. It is used for or some timing purpose.
Clock
Accumulator
re se t
Re giste r
conta
Contact
ct
output
Clock
Reset
Output
Count
Time 5 seconds.
11/28/2014
7- 14
T.C. Chang
PB1
LS2
R1
R1
R1
TIMER
R2
PB1
LS1
LS2
PR=5
TIMER
5
Motor
R2
R1
11/28/2014
7- 15
T.C. Chang
PLC ARCHITECTURE
Programmable controllers replace most of the relay panel wiring by
software programming.
Progr am
Loade r
PC
Switche s
Printe r
Proce ssor
I/O
M odule s
M emory
Powe r
Supply
M achine s
Casse tte
Loade r
EPROM
Loade r
Pe riphe rals
A typical PLC
11/28/2014
7- 16
T.C. Chang
PLC COMPONENTS
1. Processor
2. Memory
Measured in words.
ROM (Read Only Memory),
RAM (Random Access Memory),
PROM (Programmable Read Only Memory),
EEPROM (Electric Erasable Programmable ROM),
EPROM (Erasable Programmable Read Only Memory),
EAPROM (Electronically Alterable Programmable
Read Only Memory), and
Bubble Memory.
11/28/2014
7- 17
T.C. Chang
3. I/O
PLC
COMPONENTS
Modular plug-in periphery
AC voltage input and output,
DC voltage input and output,
Low level analog input,
High level analog input and output,
Special purpose modules, e.g., high speed timers,
Stepping motor controllers, etc. PID, Motion
printer
simulator
EPROM loader
graphics processor
7- 18
T.C. Chang
Discrete I/O
AC Voltage I/O
DC Voltage I/O
numerical input and output
special-purpose modules, for example, highspeed timers, and stepping motor controllers
11/28/2014
7- 19
T.C. Chang
Analog I/O
Analog inputs:
Flow sensors
Humidity sensors
Potentiometers
Pressure sensors
Temperature sensors
Analog outputs:
Analog meters
Analog valves and actuators
DC and AC motor drives
11/28/2014
7- 20
T.C. Chang
Fast input
50 to 100 microsecond pulse signal detection.
ASCII I/O
Communicates with ASCII devices.
Servo interface
Control DC servo motor for point-to-point control and axis positioning.
PID control
The Proportional Integral Derivative is used for closed loop process
control.
Network module
11/28/2014
7- 21
T.C. Chang
11/28/2014
7- 22
T.C. Chang
LADDER DIAGRAM
A ladder diagram (also called contact symbology) is a means of
graphically representing the logic required in a relay logic system.
Rail
start
PB1
emergency stop
PB2
R1
Rung
R1
R1
A
11/28/2014
7- 23
T.C. Chang
Input
01
02
Output
PLC
01
02
20
11
12
20
03
20
11
External
switches
Stored program
11/28/2014
7- 24
T.C. Chang
Input Connections
AC
11/28/2014
DC
TTL
7- 25
T.C. Chang
Output Connections
AC
11/28/2014
DC
TTL
7- 26
T.C. Chang
SCAN
A PLC resolves the logic of a ladder diagram (program) rung by rung,
from the top to the bottom. Usually, all the outputs are updated based on
the status of the internal registers. Then the input states are checked
and the corresponding input registers are updated. Only after the I/Os
have been resolved, is the program then executed. This process is run
in a endless cycle. The time it takes to finish one cycle is called the
scan time. In some controllers the idle state is eliminated. In this case,
the scan time varies depends on the program length.
Output
Input
begin
Idle
Scan cycle
Resolve
logic
11/28/2014
7- 27
T.C. Chang
PLC Programming
11/28/2014
7- 28
T.C. Chang
11/28/2014
7- 29
T.C. Chang
LOGIC STATES
ON : TRUE, contact closure, energize, etc.
OFF: FALSE, contact open , de-energize, etc.
Do not confuse the internal relay and program with the external
switch and relay. Internal symbols are used for programming.
External devices provide actual interface.
11/28/2014
7- 30
T.C. Chang
PB2
R1
R1 = PB1.AND.PB2
AND
PB3
PB4
R2
R2 = PB2.AND.~PB4
PB1
R1
R1 = PB1 .OR. PB2
OR
11/28/2014
PB2
7- 31
T.C. Chang
R1
PB1
PB2
11/28/2014
pb3
7- 32
T.C. Chang
RELAY
A Relay consists of two parts, the coil and the contact(s).
Contacts:
a. Normally open
-| |-
b. Normally closed
-|/|-
-|P|-
-|N|()
Coil:
11/28/2014
a. Coil
-( )-
b. negative coil
-(/)-
c. Set Coil
-(S)-
d. Reset Coil
-(R)7- 33
T.C. Chang
Relay (continue)
Coil:
e. Retentive memory Coil
-(M)-
-(SM)-
-(RM)-
-(P)-
-(N)-
(set coil latches the state, reset coil deenergize the set coil.
retentive coil retain the state after power failure.)
11/28/2014
7- 34
T.C. Chang
True
a. Retentive on delay
-(RTO)-
-(RTF)-
c. Reset
-(RST)-
stop
True
counting
resume
counting stop
Counter:
11/28/2014
False
a. Counter up
-(CTU)-
b. Counter down
-(CTD)-
c. Counter reset
-(CTR)-
7- 35
T.C. Chang
SEQUENCER
Sequencers are used with machines or processes involving
repeating operating cycles which can be segmented into
steps.
Output
Step
11/28/2014
Dwell time
ON
OFF OFF
5 sec.
ON
ON OFF
10 sec.
OFF
OFF ON
3 sec.
OFF
ON OFF
9 sec.
7- 36
T.C. Chang
A-B PLC
I/O points are numbered, they correspond to the I/O slot on the
PLC.
11/28/2014
7- 37
T.C. Chang
PROGRAMMING EXAMPLE 1
Bar code re ader
microswitch
Stopper
Conveyor
Part
Robot
id
MSI
R1
C1
R2
R3
C2
R4
C3
C4
11/28/2014
Machine
description
microswitch
output to bar code reader
input from bar code reader
output robot
output robot
input from robot
output to stopper
input from machine
input from machine
state
1
1
1
1
1
1
1
1
1
explanation
part arrive
scan the part
right part
loading cycle
unloading cycle
robot busy
stopper up
machine busy
task complete
7- 38
T.C. Chang
SOLUTION
Input
MS1
Outpu t
01
11
C1
C2
02
12
03
13
C3
04
14
C4
05
01
Pro grammable
Controlle r
PLC
11
14
14
02
11/28/2014
14
04
05
03
03
12
13
R1
R2
R3
R4
15
T.C. Chang
11/28/2014
assignments
expressions
statements
operators
function calls
flow control
7- 40
T.C. Chang
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
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
11/28/2014
7- 41
T.C. Chang
Structure:
TYPE data_packet:
STRUCT
input
:
t
:
out
:
count
:
END_STRUCT;
END_TYPE;
11/28/2014
BOOL;
TIME;
BOOL;
INT;
7- 42
T.C. Chang
Variable Declarations
Local variable:
VAR
I,j,k :
v
:
END_VAR
11/28/2014
INT;
REAL;
7- 43
T.C. Chang
parenthesized expression
function( )
function
**
exponentiation
negation
NOT
Boolean complement
+-*/
math operators
MOD
modulus operation
comparison operators
equal
<>
not equal
AND, &
Boolean AND
XOR
Boolean XOR
OR
Boolean OR
11/28/2014
Y := X+1.0;
y := a AND b;
v := (v1 + v2 + v3)/3
output := (light = open) OR
(door = shut);
7- 44
T.C. Chang
Condition Statements
IF a > 100 THEN
redlight := on;
ELSEIF a > 50 THEN
yellowlight := on;
ELSE
greenlight := on;
END_IF;
11/28/2014
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
7- 45
T.C. Chang
Iteration Statements
FOR I:= 0 to 100 BY 1 DO
light[I] := ON;
END_FOR
I := 0;
WHILE I < 100 DO
I := 0;
REPEAT
I := I + 1;
light[I] := on;
UNTIL I > 100;
END_REPEAT
I := I + 1;
light[I] := on;
END_WHILE
11/28/2014
7- 46
T.C. Chang
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.
11/28/2014
7- 47
T.C. Chang
Programs
PROGRAM example7.1
R1 := MS1 AND (NOT R4);
VAR_INPUT
MSI :
BOOL;
C1
BOOL;
C2
BOOL;
R4 := C1;
C3
BOOL;
END_PROGRAM
C4
BOOL;
END_VAR
VAR_OUTPUT
R1
BOOL : FALSE;
R2
BOOL : FALSE;
R3
BOOL : FALSE;
R4
BOOL : FALSE;
END_VAR
11/28/2014
7- 48
T.C. Chang
11/28/2014
7- 49
T.C. Chang
END_VAR
VAR_OUTPUT
Q : BOOL;
CU
R
PV
Q
CV
: input to be counted
: reset
: preset value
: contact output
: counter value.
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
11/28/2014
7- 50
T.C. Chang
block diagram
SP
set point
PV
sensor feedback
KP
TR
integral gain
TD
derivative gain
AUTO
calculate
XOUT
output to process
XO
cycle
11/28/2014
Vout K p E Tr Edt Td
dE
dt
control algorithm
7- 51
T.C. Chang
loop
11/28/2014
LD
ST
ANDN
MS1
R1
C3
7- 52
T.C. Chang
Operators
Operator Modifiers Description
GT
greater than
LD
GE
ST
store register
value into operand
EQ
equal
NE
not equal
LE
reset operand
false
LT
less than
JMP C, N
jump to label
AND
N, (
Boolean AND
CAL C, N
&
N, (
Boolean AND
RET C, N
OR
N, (
Boolean OR
XOR
N, (
Boolean XOR
ADD
addition
SUB
subtraction
MUL
multiplication
DIV
division
11/28/2014
T.C. Chang
MS1
ANDN
R4
MSI
BOOL;
ST
R1
C1
BOOL;
LD
R4
C2
BOOL;
ANDN
C3
C3
BOOL;
ANDN
C2
C4
BOOL;
ST
R2
END_VAR
LD
C4
VAR_OUTPUT
ANDN
C3
R1
BOOL : FALSE;
ST
R3
R2
BOOL : FALSE;
LD
C1
R3
BOOL : FALSE;
ST
R4
R4
BOOL : FALSE;
END_VAR
11/28/2014
LD
END_PROGRAM
7- 54
T.C. Chang
7- 55
T.C. Chang
SFC
Action Qualifiers:
N
7- 56
T.C. Chang
11/28/2014
7- 57