Basics of PLC
Programming
Processor Memory Organization
• The PLC memory space can be divided into two broad
categories: program files and data files
• Program files are the part of the processor memory
that stores the user ladder logic program. Most
instructions require one word of memory
• The data files store the information needed to carry
out the user program. This includes information such as
the status of input and output devices, timer and
counter values, data storage
• Contents of the data table can be divided into two
categories: status data and numbers or codes
S7-300 Memory Organization
• The S7-300 has three memory areas in CPU:
• Load memory
• System memory
• Work memory
S7-300 Memory Organization: Load memory
• The load memory is located
on a SIMATIC Micro Memory
Card (MMC)
•It is used for storing code
blocks and data blocks, as well
as system data (configuration,
connections, module
parameters etc.).
• It is possible to store the
entire configuration data of
project on MMC
S7-300 Memory Organization: System memory
• The integrated CPU system memory is not
expandable
• It contains:
• The address areas for memory bits, timers and
counters
• The I/O process image
• Local data
S7-300 Memory Organization: Process Input Image
Input module
• PII is a part of the
program memory L1
allocated to storing the OFF
on/off status of (open)
connected discrete inputs
ON
(closed)
1 0
Word corresponding
to input module
Input image
Connection of an open and closed
switch to the PII through the input Data table
files
module
S7-300 Memory Organization: Process Output Image
• POI is that part of the Output module
L2
program memory OFF
allocated to storing the
actual on/off status of
connected discrete ON
outputs
0 1
Word corresponding
to output module
Output image
Connections of pilot lights
to the output image table
file through the output Data table
module files
S7-300 Address areas of the system memory
S7-300 Main memory
• The main memory is integrated in the CPU and cannot
be extended
• It is used to execute the code and process user
program data
• Programs only run in the main memory and system
memory
S7-300 Memory specification
S7-300 Retentivity of load memory, system memory,
and main memory
Retentive data in the load memory
• The program in the load memory is always retentive:
It is stored on the SIMATIC Micro Memory Card, where
it is protected against power failures or memory resets
Retentive data in the system memory
• In configuration (Properties of CPU, Retenativity tab)
must be specifed which part of memory bits, timers
and counters should be kept retentive and which of
them are to be initialized with "0” on restart (warm
restart)
S7-300 Retentivity of load memory, system memory,
and main memory
Retentive data in the main memory
•The contents of retentive DBs are always retentive at
restart and POWER ON/OFF
•Retentive data blocks can be uploaded to the main
memory in accordance with the maximum limit
allowed by the main memory
• Non-retentive DBs are initialized from the load
memory with their initial values at restart or POWER
ON/OFF
• Non-retentive data blocks and code blocks can be
loaded in accordance with the maximum main memory
limit
Program Scan
The status of
external inputs
Internal checks START INPUT is written to the
on memory , speed SCAN input image table
and operation. (file or register).
Ser vice any HOUSE-
communication KEEPING
requests .
Each ladder rung
PROGRAM is scanned and solved
SCAN using the date in the
OUTPUT input file. The resulting
SCAN
logic is wr itten to the
The output image output image table
date is transf erred (file or register).
to the external output
circuits , tur ning the
output device
ON or OFF.
PLC program scan cycle
Program Scan
• When a PLC executes a program, it must know—in
real time—when external devices controlling a process
are changing
• During each operating cycle, the processor reads all
the inputs, takes these values, and energizes or de-
energizes the outputs according to the user program
• This process is known as a program scan cycles
• Single PLC operating cycle consisting of the:
• Input scan
• Program scan
• Output scan
• Housekeeping duties (communications and
diagnostics)
Program Scan
• The time required to make a single scan can vary
from about 1 millisecond to 20 milliseconds
• The scan time is a function of the following:
• The speed of the processor module
• The length of the ladder program
• The type of instructions executed
• The actual ladder true/false conditions
Program Scan
• The For each rung executed, the PLC processor will:
• Examine the status of the Process Input Image table
bits
• Solve the ladder logic in order to determine logical
continuity
• Update the appropriate Process Output Image table
bits, if necessary
• Copy the output image table status to all of the
output terminals
• Copy the status of all of the input terminals to the
input image table
Program Scan: Data flow
Input Process Process Output
Input data Input Output data Output
modules Image Image modules
table table
Examine Return
data results
Program
Check/ compare/ examine
specific conditions Take some action
Overview of the data flow during the scan
process
Program Scan
• For each rung executed, the PLC processor will:
Step 1 - Update the input image table by sensing the
voltage of the input terminals. Based on the absence
or presence of a voltage, a 0 or a 1 is stored into the
memory bit location designated for a particular input
terminal
Step 2 - Solve the ladder logic in order to determine
logical continuity. The processor scans the ladder
program and evaluates the logical continuity of each
rung by referring to the process input image table to
see if the input conditions are met. If the conditions
controlling an output are met, the processor
immediately writes a 1 in its memory location,
indicating that the output will be turned ON;
conversely, if the conditions are notmet a 0 indicating
Program Scan
• For each rung executed, the PLC processor will:
Step 3 – Update the actual states of the output
devices by transferring the output table results to the
output module, thereby switching the connected
output devices ON (1) or OFF (0). If the status of any
input devices changes when the processor is in step 2
or 3, the output condition will not react to them until
the next processor scan
Program Scan: single rung program
Input Processor memory Output
module Data module
Input Process Process Output
device Input Input device
Image Image
table table
I3.6 I3.6 Q4.7 Q4.7
Field-device Field-device
power supply power supply
I3.6 Q4.7
Program
Scan process applied to a single rung
program
Program Scan: multiple rung program
Process Input Image Table
Step 1
0 0 0 1 1 0 0 0 0 0 1 0 0 0 1 0
Read
Input
STAR T
module
Step 2
Solve the
ladder
program
Step 3
END
Transfer
to output
0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0
module
Process Output Image Table
Scan process applied to a multiple rung
program
PLC Programming Languages
• The standard IEC 61131 standardize five languages
for programming PLC :
•Ladder Diagram (LD) — a graphical depiction of a
process with rungs of logic, similar to the relay ladder
logic schemes
• Function Block Diagram (FBD) — a graphical
depiction of process flow using simple and complex
interconnecting blocks
• Sequential Function Chart (SFC) — a graphical
depiction of interconnecting steps, actions, and
transitions
•Instruction List (IL) — a low-level, text-based language
that uses mnemonic instructions.
• Structured Text (ST) — a high-level, text-based
PLC Programming Languages
PLC programming languages
Textual languages Graphical language
Instruction Structured Ladder Functional Sequential
list text diagram block diagram function chart
Standard IEC 61131 languages associated with PLC
programming
PLC Programming Languages: Ladder diagram
• The Ladder diagram language is the most commonly
used PLC language and is designed to mimic relay
logic
PB1 CR12 CR SOL
(PB1) (CR1) (CR2) SOL
LS1 A B D Y
C
(LS1)
Hardwired relay control Equivalent ladder
circuit diagram (LD) program
PLC Programming Languages: Function Block Diagram
• The Functional block diagram programming uses
instructions that are programmed as blocks wired
together on screen to accomplish certain functions.
Typical types of function blocks include logic, timers,
and counters Functional block Caution
Ladder logic diagram equivalent Sensor 1 Sensor 2 PL 1
AND_BOOL
A B
A
Ladder diagram
B
A BAND_01
OR_BOOL
BAND
A
B Boolean And Caution
B 0
Out PL 1
0
AND_BOOL Sensor 1 In1
A B
A 0
Sensor 2 In2
B
Function block diagram
Function block diagram PLC ladder and equivalent
equivalents to ladder logic function block diagram
contacts
PLC Programming Languages: Sequential Function Chart
• Sequential
function chart Initial
Step 1
Action
programming
Wire
language is similar loop Transition
to a flowchart Wire
• SFC programming Step 2 Action
is designed to
accommodate the Tr ansition
programming of
more advanced Step 3 Action
processes
Transition
Major elements of a sequential
function chart program Stop
PLC Programming Languages: Structured Text
• Structured text is a high level text language primarily
used to implement complex procedures that cannot be
easily expressed with graphical languages
Sensor 1 Sensor 2 SOL 1
Sensor 3 Sensor 4 Sensor 5
PLC ladder IF Sensor_1 AND Sensor_2 THEN
SOL_1 := 1;
and
ELSEIF Sensor_3 AND Sensor_4 AND NOT Sensor_5 THEN
equivalent SOL_1 := 1;
structured END_IF;
text
Bit Logic Instructions: ---| |--- Normally Open Contact
• Is closed when the bit value stored at the PII is equal
to "1". When the contact is closed, ladder rail power
flows across the contact and the result of logic
operation (RLO) = "1”
• Otherwise, if the signal state at the PII is "0", the
contact is open. When the contact is opened, power
does not flow across the contact and the result of logic
Symbol
operation (RLO)Normally=
Open"0"Contact
Examine if closed
Bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
number 1 Status 0
I1.4 I1.4
Instruction interpreted Instruction interpreted
as true as false
I1.4 I1.4
Examine If Closed
instruction
Bit Logic Instructions: ---| / |--- Normally Closed Contact
• Is closed when the bit value stored at the PII is equal
to “0". When the contact is closed, ladder rail power
flows across the contact and the result of logic
operation (RLO) = "1”
• Otherwise, if the signal state at the PII is “1", the
contact is open. When the contact is opened, power
does not flow across the contact and the result of logic
Symbol
operation (RLO)Normally=
Open"0"Contact
Examine if opened
Bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
number 1 Status 0
I1.4 I1.4
Instruction interpreted Instruction interpreted
as false as true
I1.4 I1.4
Examine If Opened
instruction
Bit Logic Instructions: ---( ) Output Coil
• This instruction looks and operates like a relay coil
and is associated with a POI memory bit. This
instruction signals the PLC to energize (switch on) or
de-energize (switch off ) the output
Symbol
Output coil
energize
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Output Input
data 1 data 1 1
ON
I1.1 I1.4 O2.1 I1.1
O2.1
Program I1.4
Output Input
module module
Output Coil energize
instruction
Bit Logic Instructions Addressing
• Those addresses indicates what PLC input is
connected to what input device and what PLC output
will drive what output device
• Addressing formats can vary from one PLC family to
another as well as for different manufacturers
• The assignment of an I/O address can be included in
the I/O connection diagram
Inputs Outputs
L1 L2 L1 L2
PB1
SOL1
I4.5 Q2.3
PL1
LS1
I4.6 Q3.6 R
I/O connection
Bit Logic Instructions Addressing
Process Output file (O)
Byte (4)
0 Bit(6)
1
2
Process Output Image table 3
4 Q4.6 L2
5
6
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 7
8 Energized
Q4.6 9
output
10
Bit address
0 11
12
1
13
Process 2
14
3
Input file (I) 4
15
Byte (3) Process Input Image table
5
Bit (7) 6
7
8
9 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
I3.7 10
L1 I3.7
11
12 Bit address
Closed 13
14
switch 15
I3. Q4.
7 6
User-progammed rung
Addressing format for Siemens
Multiple Branch: OR Instruction
• Branch instructions are used to create parallel paths
of input condition instructions. This allows more than
one combination of input conditions (OR logic) to
establish logic continuity in a rung
A C A B D
B
A C
D
B
E
Typical branch
Multiple Branch: OR Instruction
• Input and output branches can be nested to avoid
redundant instructions and to speed up processor scan
time
Nested contact
program
Internal Relay Instructions
• Most PLCs have an area of the memory allocated for
what are known as internal storage bits. These storage
bits are also called internal outputs, internal coils,
internal control relays, or simply internal memory bits
(M-memory) Discrete inputs
1 2 345 67 M0.0
Rung 1
Internal
relay
coil
Discrete inputs
M0.0 8 9 101 11 2
Rung 2
Internal Discrete
relay output
contact
Programmed internal relay
Modes of Operation
• A processor has basically two modes of operation: the
program mode and some variation of the run mode
Program Mode is used to enter a new program, edit or
update an existing program, upload files, download
files, document (print out) programs, or change any
software configuration file in the program. When the
PLC is switched into the program mode, all outputs
from the PLC are forced off regardless of their rung
logic status, and the ladder I/O scan sequence is halted
Run Mode is used to execute the user program. Input
devices are monitored and output devices are
energized accordingly. After all instructions have been
entered in a new program or all changes made to an
existing program, the processor is put in the run mode
Modes of Operation
Test Mode is used to operate or
monitor the user program without
energizing any outputs. The
processor still reads inputs,
executes the ladder program, and
updates the output status table
files, but without energizing the RUN REM PROG
output circuits
Remote Mode allows the PLC to be
remotely changed between
program and run mode by a
personal computer connected to the
PLC processor. The remote mode
Three-position
may be beneficial when the keyswitch used to
controller is in a location that is not select different
processor modes of
easily accessible