D05 - PLC Programming
D05 - PLC Programming
PLC Programming
• Programing languages
• Bit level instructions
• Timers, Counters, Comparison,
Math
• Word logic
• Data areas, memory areas, data
rules, and address access
Programing languages
• Ladder
• Boolean
• Grafcet
Ladder
The programmable controller was developed for ease of programming using existing relay
ladder symbols and expressions to represent the program logic needed to control the
machine or process. The resulting programming language, which used these original basic
relay ladder symbols, was given the name ladder language.
I
Example in Series – AND Logic Example in Parallel- OR Logic
Bit Logic Instructions in FBD - NO
Normally Open Contact
The block “&” is TRUE when the assigned bit value I0.0 is TRUE or
equal to 1 and is FALSE when the assigned bit value I0.0 is FALSE or
equal to 0 .
I
Example in Series – NOR Logic Example in Parallel- NAND Logic
Bit Logic Instructions in FBD - NC
Normally Open Contact
The block “&” is TRUE when the assigned bit value I0.0 is FALSE or
equal to 0 and is FALSE when the assigned bit value I0.0 is TRUE of
equal to 1
LAD
FBD
Exercise Example 1
Design XOR gate such that output is TRUE if only one
of the two inputs is TRUE. - Assume you have two
toggle buttons connected to PLC input
Ladder Solution
FBD Solution
Bit Logic Instructions in LAD – SET and RESET
SET Instruction
Example
Exercise Example 2
Write a Logic to latch two outputs Q0.0 & Q0.1 with I0.0 and unlatch it using I0.1
Required Condition
Ladder Solution
Bit Logic Instructions – SET and RESET (Field)
SET Instruction (Field)
Example
Exercise Example 3
Write a Logic to latch three outputs Q0.0 ~ Q0.2 with I0.0 and unlatch all using I0.1
Required Condition
Ladder Solution
Bit Logic Instructions – SET Dominant & RESET Dominant
SET/ Reset Flip Flop
FBD Solution
Bit Logic Instructions in LAD – Positive & Negative Edge
Positive Edge – SCAN Operand
The state of this contact is TRUE when a positive transition (OFF to-
ON) is detected on the assigned bit I0.0. The bit M0.0 will be ON for
1 clock cycle.
The state of this contact is TRUE when a positive transition (ON to-
OFF) is detected on the assigned bit I0.1. The bit M0.1 will be ON
for 1 clock cycle.
The P & N contact can be located anywhere in the network except the end of a branch.
Exercise Example 5
Write a Logic to latch the motor when I0.0 goes from OFF to ON and unlatch the
motor when I0.1 goes from ON to OFF.
Ladder Solution
FBD Solution
Bit Logic Instructions in LAD – Positive & Negative Edge
P_TRIG - SCAN RLO
P_TRIG & N_TRIG instruction cannot be located at the beginning or end of a network.
Exercise Example 6
Write a Logic to latch the motor when either I0.0 or I0.1 goes from OFF to ON and
unlatch the motor when either I0.2 or I0.3 goes from ON to OFF.
Ladder Solution
FBD Solution
Timer Operations
NOTE
• Changing PT has no effect
while the timer runs.
• Changing IN has no effect
while the timer runs.
Exercise Example
Write a Logic to generate continuous pulse output at Q0.0. Consider pulse time to be 1
second.
Exercise Example
Write a Logic to generate continuous pulse output at Q0.0 and Q0.1 with I0.0. Consider pulse
time to be 1 second.
Timer Operations in LAD – TON (On-Delay Timer)
On-Delay Timer The TON timer sets the output (Q) to ON after a
preset time delay.
NOTE
• Changing PT has no effect
while the timer runs.
• Changing IN to FALSE, while
the timer runs, resets and
stops the timer.
Exercise Example
Write a Logic to generate output at Q0.0, Q0.1 and Q0.2 in sequence with delay of 3 seconds
each.
Timer Operations in LAD – TOF (OFF Delay Timer)
OFF-Delay Timer The TOF timer sets the output (Q) to ON and
then resets the output to OFF after a preset time
delay.
NOTE
• Changing PT has no effect while the
timer runs.
• Changing IN to TRUE, while the
timer runs, resets and inits the
timer
Exercise Example
Write a logic to achieve the following objective:
• Start a Motor 1 (Q0.0) with I0.0 (0 - 1) and after 10s delay start a Motor 2 (Q0.1)
• Stop the Motor 1 (Q0.0) with I0.0 (1 – 0) and after 10s delay stop the Motor 2 (Q0.1)
Timer Operations in LAD – TOF (OFF Delay Timer)
Retentive The TONR timer sets the output (Q) to ON after a
ON Delay Timer preset time delay. The elapsed time is accumulated
over multiple timing periods until the reset (R)
input reset the elapsed time.
• Count Down
• Count Up
• Count Up-Down
Counter Operations – CTD (Count Down)/CTU (Count Up)
Count Down The "count down" counter (CTD) counts down by 1 when the value of input
parameter CD changes from 0 to 1.
Count Up The "count up" counter (CTU) counts up by 1 when the value of the input
parameter CU changes from 0 to 1.
The number of counters that you can use in your user program is
limited only by the amount of memory in the CPU. Individual
counters use 3 bytes (for SInt or USInt), 6 bytes (for Int or UInt), or
12 bytes (for DInt or UDInt).
Counter Operations – CTUD (Count Up Down)
Count Up-Down The "count up and down" counter (CTUD) counts up or down by 1 on the
0 to 1 transition of the count up (CU) or count down (CD) inputs.
Math Operations
• Data types
• Various Math operations
Data Types (1) – S7 1200
Data Types The data type define the properties of the data, for example, the
representation of the contents and the valid memory areas.
the user program, you can use predefined data type or also data types that
you have defined.
• Sign: The sign is determined by the signal state of bit 31. The bit 31 assume the value "0"
(positive) or "1" (negative).
• 8-bit exponents to basis 2: The exponent is increased by a constant (base, +127), so that it
has a value range of 0 to 255.
• 23-bit mantissa: Only the fraction part of the mantissa is shown. The integer part of the
mantissa is always 1.
Math Operations
ADDITION – for addition of two or more inputs
Status of OUT1 before addition Status of OUT1 after addition
• MOV
• MOV_BLK
• FILL_BLK
• SWAP
Move Operations - MOVE
Move Instructions
The MOVE instruction copies a single data element from the source address specified by the
IN parameter to the destination addresses specified by the OUT parameter.
Example:
Copying the value from MW10 to MW12
The MOVE_BLK instructions have an additional COUNT parameter. The COUNT specifies how
many data elements are copied. The number of bytes per element copied depends on the
data type assigned to the IN and OUT parameter tag names in the PLC tag table.
Example:
Copying the array element from source to destination
Output Array
Input Array
Count
Move Operations - MOVE_BLK
Fill Block Instruction
Use the "Fill block" instruction to fill a memory area (destination area) with the
value of the IN input. The destination area is filled beginning with the address
specified at the OUT output.
Example:
Copying the constant value (45) to Block_1 Array_1[2]
Move Operations - SWAP
SWAP Instruction
Use the "Swap" instruction to change the order of the bytes at input IN and query the result
at output OUT. The following figure shows how the bytes of an operand of the DWORD data
type are swapped using the "Swap" instruction.
Exercise Example
SWAP the values of MW0 and store it in MW10
Before Execution After Executio
Example: Make a ladder logic to continuously update the date from one block to another
block after every 10 seconds