100% found this document useful (1 vote)
819 views197 pages

CoDeSys Version 3.5 Textbook Version 1.1

The document is a textbook for the CoDeSys version 3.5 programming environment, which adheres to the IEC 61131-3 standard for industrial automation and programmable logic controllers (PLCs). It covers installation, programming languages, project structure, and various programming techniques, including structured text, ladder diagrams, and function block diagrams. The textbook serves as a comprehensive guide for users to effectively utilize CoDeSys in developing automation systems.

Uploaded by

brendsriverasy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
819 views197 pages

CoDeSys Version 3.5 Textbook Version 1.1

The document is a textbook for the CoDeSys version 3.5 programming environment, which adheres to the IEC 61131-3 standard for industrial automation and programmable logic controllers (PLCs). It covers installation, programming languages, project structure, and various programming techniques, including structured text, ladder diagrams, and function block diagrams. The textbook serves as a comprehensive guide for users to effectively utilize CoDeSys in developing automation systems.

Uploaded by

brendsriverasy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 197

Learning system for automation and communications

Programmable Logic Controller

IEC 61131-3 Programming Language


CoDeSys ver. 3.5

Textbook

Festo Didactic
Version Control Record

Version Effective Date Changes Author


1.0 20 February 2013 Draft Edward
1.1 2 May 2013 Release version Edward

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
3
Table of Contents

Table of Contents
Table of Contents ....................................................................................3

1.0 Introduction .......................................................... 11


1.1 Where to Find 3S- Smart Software Solutions .............................11
1.2 Local PC “Administrator” Rights .................................................11
1.3 Download ...................................................................................11
1.4 Registration ................................................................................12
1.5 Select CoDeSys Version ..............................................................12
1.6 Installing CoDeSys Festo Version ...............................................13
1.7 CoDeSys Structure......................................................................23
1.7.1 Development Layer.........................................................24
1.7.2 Communication Layer .....................................................24
1.7.3 Device Layer ...................................................................24
1.8 Structure of a Project ..................................................................24
1.8.1 POU (Program Organisation Unit)...................................24
1.8.2 Data Types ......................................................................25
1.8.3 Visualisation ...................................................................25
1.8.4 Resources .......................................................................25
1.9 Main Program PLC_PRG ..............................................................26
1.10 The 5 Programming Languages ..................................................26

2.0 First Steps ............................................................. 29


2.1 Starting Up the CoDeSys Software .............................................29
2.2 Setting the Options ....................................................................30
2.3 Adding in New Packages for the Hardware .................................34
2.4 Getting Started ...........................................................................39
2.5 The IOs........................................................................................42
2.5.1 IEC 61131-3 Addressing .................................................45
2.6 The Library ..................................................................................46
2.7 The Program ...............................................................................47
2.7.1 Program in Different Languages .....................................52
2.8 Compiling the Program ...............................................................54
2.9 Controlling the Communication Settings ....................................54
2.10 Task Configuration .....................................................................58
2.11 Setting the Communication Parameter.......................................61
2.12 Downloading the Program ..........................................................62
2.13 Running the Program ..................................................................63
2.13.1 Monitoring the Program .................................................64
2.13.2 Logging Out ....................................................................65
2.14 Saving the Program/Project .......................................................66

3.0 Structured Text (ST) ............................................. 69


3.1 Structured Text Programming ....................................................69
3.2 The Garage Door Example ..........................................................69
3.3 Changing the “Global Variables” list ..........................................70

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
4
Table of Contents

3.4 Starting to Write the Program Code and Implementing


The Variables Automatically ...................................................... 71
3.5 Building the Project.................................................................... 71
3.6 Download and Run the Program ................................................ 71

4.0 The Ladder Diagram Language (LD) ..................... 75


4.1 The Ladder Diagram Language .................................................. 75
4.2 Making a New Program .............................................................. 76
4.3 The Program Itself ...................................................................... 77
4.4 Download and Run the Program ................................................ 78

5.0 The Function Block Diagram (FBD)....................... 81


5.1 Making a New Program .............................................................. 81
5.2 The Function Block Diagram (FBD) ............................................. 81
5.3 Writing the Program ................................................................... 83

6.0 The Instruction List Program (IL) ......................... 87


6.1 The Instruction List Program (IL) ................................................ 87
6.2 Writing the Program ................................................................... 87
6.3 Toggling Between LD/FBD/IL ..................................................... 88

7.0 Using Memories .................................................... 91


7.1 The Idea ..................................................................................... 91
7.2 Implementing the Memory Variable Automatically .................... 91
7.3 Implementing the Memory Variable Manually ........................... 92
7.4 Implementing the New Variable a Second Time ......................... 92
7.5 Download and Run the Program ................................................ 92

8.0 Using Timers ......................................................... 95


8.1 Description of the Example ........................................................ 95
8.2 Three Types of Timers ................................................................ 95
8.2.1 Function Block TP, Pulse Timer ...................................... 95
8.2.2 Function Block TON, Switch-on Signal Delay ................. 96
8.2.3 Function Block TOF, Switch-off Signal Delay .................. 96
8.3 Inserting the Timer in Our Example ............................................ 97

9.0 Using Counters ...................................................... 103


9.1 Description of the Example ........................................................ 103
9.2 Types of Counters ...................................................................... 103
9.2.1 Function Block CTU, Incremental Counter ...................... 103
9.2.2 Function Block CTD, Decremental Counter .................... 103
9.2.3 Function Block CTUD, Combined
Incremental/Decremental Counter ................................ 103
9.3 Implementing Two New Variables .............................................. 105

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
5
Table of Contents

9.4 The Program ...............................................................................105


9.5 Download and Run the Program .................................................106

10.0 The Sequential Function Chart (SFC) ................... 109


10.1 Introduction ................................................................................109
10.2 The Sequential Function Chart (SFC) ..........................................110
10.2.1 Step ................................................................................111
10.2.2 IEC-Extending Step Actions ............................................111
10.2.3 Transition or Condition ...................................................113
10.2.4 Active Step .....................................................................114
10.2.5 IEC Step ..........................................................................114
10.3 Branches.....................................................................................116
10.3.1 Alternative Branches (OR Function)................................116
10.3.2 Parallel Branches (AND Function) ...................................117
10.3.3 Jumps..............................................................................118
10.4 SFC Editor ...................................................................................119
10.5 Start a New Project .....................................................................119
10.6 Writing the Program ...................................................................120
10.7 Making Steps and Transitions ....................................................121
10.8 Download and Run the Program .................................................126

11.0 Running Multiple Programs ................................. 129

11.0 Running Multiple Programs ........................................................129


11.1 Creating a Global Variable List ...................................................129
11.1.1 Adding in the Global Variable .........................................130
11.2 Create a New Program ................................................................131
11.2.1 Adding Programs in the Main Task .................................133

12.0 Simulation and Visualisation ............................... 137

12.1 Sample Program for Simulation..................................................137


12.2 Starting the Simulator ................................................................137
12.3 Simulation Device .......................................................................140
12.4 Simulating Your Program............................................................142
12.5 Using the Visualisation Function ................................................147
12.6 Running the Simulation ..............................................................156

13.0 Functions and Function Blocks............................. 161

13.1 Introduction ................................................................................161


13.2 Types of Functions ......................................................................161
13.3 Function Blocks ..........................................................................163
13.4 Common Functions and Function Blocks ....................................164
13.4.1 AND Function ..................................................................164
13.4.2 OR Function ....................................................................165
13.4.3 EXOR Function ................................................................165
13.4.4 Inversion/Negation ........................................................165
13.4.5 RS Function Block (Reset Dominant) ..............................165
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
6
Table of Contents

13.4.6 SEMA (Software SEMAfore) ........................................... 166


13.4.7 SR Function Block (Set Dominant) ................................. 166
13.4.8 CTD Function Block (Decrementer, Count Down) ........... 166
13.4.9 CTU Function Block (Increment, Count Up) .................... 167
13.4.10 CTUD Function Block (Combined
Incrementer/Decrementer, Count Up & Down) ........... 167
13.4.11 RTC Function Block (Runtime Clock) ............................ 168
13.4.12 TOF Function Block (Delayed Turn-off) ........................ 169
13.4.13 TON Function Block (Timed Turn-on) ........................... 169
13.4.14 TP Function Block (Pulse Timer) .................................. 169
13.4.15 F_trig Function Block (Falling Edge Trigger) ................ 170
13.4.16 R_trig Function Block (Rising Edge Trigger)................. 170

14.0 Structured Text Instructions ................................. 173

14.1 Instructions ................................................................................ 173


14.1.1 assignment, := ............................................................... 173
14.1.2 Using Subprograms ....................................................... 174
14.1.3 Using Function Blocks .................................................... 174
14.1.4 RETURN Instruction ........................................................ 176
14.1.5 IF..THEN..ELSIF..THEN..ELSE..END_IF Instructions ......... 176
14.1.6 CASE..or..ELSE..END_CASE Instructions ........................ 176
14.1.7 FOR..TO..BY..DO_END_FOR Instructions ........................ 177
14.1.8 WHILE..DO..END_WHILE Instructions ............................. 178
14.1.9 REPEAT..UNTIL..END_REPEAT Instructions .................... 178
14.1.10 EXIT Instruction ........................................................... 178
14.1.11 AND, & Instruction ....................................................... 179
14.1.12 XOR Instruction............................................................ 180
14.1.13 OR Instruction.............................................................. 180

15.0 Instruction List Instructions ................................. 183

15.1 Instructions ................................................................................ 183


15.1.1 Labels............................................................................. 183
15.1.2 Modifiers ........................................................................ 183
15.1.3 The IL Operators and Modifiers ...................................... 184
15.1.4 Load (LD) Instruction...................................................... 184
15.1.5 Store (ST) Instruction ..................................................... 185
15.1.6 Set (S) Instruction .......................................................... 185
15.1.7 Reset (R) Instruction ...................................................... 186
15.1.8 AND Instruction .............................................................. 186
15.1.9 OR Instruction ................................................................ 186
15.1.10 XOR Instruction............................................................ 186
15.1.11 JuMP, JMP Instruction .................................................. 187
15.1.12 CAL Instruction ............................................................ 188
15.1.13 CAL Program ................................................................ 189
15.1.14 RET Instruction ............................................................ 190
15.1.15 Working With “( )” ....................................................... 191

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
7
Table of Contents

16.0 Ladder Diagram Symbols ..................................... 195

16.1 Symbols ......................................................................................195


16.1.1 Normally Open Contact ..................................................195
16.1.2 Normally Closed Contact ................................................195
16.1.3 Coil .................................................................................195
16.1.4 Negated Coil ...................................................................195
16.1.5 Set Coil ...........................................................................196
16.1.6 Reset Coil........................................................................196
16.1.7 Rising Edge .....................................................................196
16.1.8 Falling Edge ....................................................................196
16.1.9 Box with AND Function ...................................................197

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
8
Table of Contents

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
9
Introduction

Chapter 1

Introduction

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
10
Introduction

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
11
Introduction

1.0 Introduction
CoDeSys is a comprehensive software tool for industrial automation. It
is a programming standard for controllers and PLCs as per IEC 61131-3
CoDeSys (Controller Development System). It consists of two basic
components:

 The programming environment CoDeSys


 The runtime system CoDeSys SP

The run time system turns any system into an IEC 61131-3 controller
that can be programmed with CoDeSys. Integrated compilers ensure
that the program code is processed at maximum speed.

More than 200 innovative manufacturers from various industrial


sectors program their automation systems with CoDeSys. As a result,
thousands of end users over the entire world, the majority of them
machine manufacturers, use CoDeSys every day for the development
of a vast range of automation tasks.

As of today, CoDeSys is the most widely-used IEC 61131-3


development tool in Europe, and has proven to be the standard for
programming of control systems and PLCs.

And the best thing about it: you can download CoDeSys for free, once
you have registered.

1.1 Where to Find 3S-Smart Software Solutions


The software is provided by:

3S-Smart Software Solutions GmbH


Memminger Straße 151
87439 Kempten / Germany

Email: info@3s-software.com
Internet: www.3s-software.com

1.2 Local PC “Administrator” Rights


To be able to install and use the CoDeSys software on a PC, the user
must have at least local administrator rights!

1.3 Download
To download, go to http://www.3s-software.com/

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
12
Introduction

This will take you to the download page. Here you first need to enter
your registration. If you already have a registration code, then you can
click on the button “Enter download area”. (Continue with1.5 Select
CoDeSys version).

1.4 Registration
If you do not have a registration code, go to:
http://www.3s-software.com/index.shtml?en_contact

 In the download page, click on the button.

(authorization required)

1.5 Select CoDeSys Version


Note that CoDeSys V3 is a completely new development. Devices that
are presently programmable with CoDeSys version 2.3 cannot be
programmed with CoDeSys V3. The manufacturers of CoDeSys
controllers are currently busy implementing the new runtime system
so that the control systems can be programmed with CoDeSys V3.

NOTE! Be sure to select the correct Version!

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
13
Introduction

However, with version 3.5, you are able to convert the files written in
version 2.3 but again as earlier stated, the devices might not be able
to handle the new version.

The file that you download contains all of the necessary files and
documentation that you will need.

1.6 Installing CoDeSys Festo Version


You can download the software from the Festo website.

 Click on “Support/Downloads”

 Type in “CoDeSys” and click “Find”


Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
14
Introduction

 Click on the tab “Engineering software”

 Choose the right program and double-click

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
15
Introduction

 Save the zipped file into your PC.


 After completing, unzip the file.

 Double-click on “Setup_CoDeSysV3.exe”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
16
Introduction

 Click “Next”

 Click “Yes” to accept the general terms and conditions.

 Accept or change the destination folder and click “Next”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
17
Introduction

 Click “Next”

 Accept the default and click “Next”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
18
Introduction

 Accept or change the program folder and click “Next”

 Click “Next” to start copying files.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
19
Introduction

 If the process takes too long, click “Cancel” but do not exit the
setup.
 The screen will be updated.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
20
Introduction

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
21
Introduction

 Choose “Typical setup” and then click “Next”

 Click “Next”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
22
Introduction

 Click “Finish” to complete the installation.


 To start CoDeSys, you can access it from the main toolbar.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
23
Introduction

1.7 CoDeSys Structure


The structure of CoDeSys is built up of 3 “layers”:

 The Development Layer


 Communication Layer
 Device Layer

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
24
Introduction

1.7.1 Development Layer

In the development layer find the following components:

 IEC 61131-3 Software Tool (programming environment)


 Configuration of the hardware (I/O) and bus system
 Modules for Softmotion
 Modules for Visualization

1.7.2 Communication Layer

Interaction with PLC via Gateway, Scada, NET and C++ applications.

1.7.3 Device Layer

The runtime system manages:

 The cycle with the I/O update


 The communication with the development environment
 Starting the application after power-on

1.8 Structure of a Project

 In general, a project comprises four blocks.

Project

POU‘s Data types HMI Resources

Global
Declaration variables

Code Libraries

1.8.1 POU (Program Organisation Unit)

Functions, function blocks and programs are POUs . Actions can be


added to these. Every POU consists of a declaration section and a
body. The body is written in one of the IEC programming languages: IL,
ST, SFC, FBD and LD; or CFC.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
25
Introduction

CoDeSys supports all standard POUs. If you wish to use these in a


project, then you must include the library standard.lib in your project.
POUs may call other POUs, however recursive calling (calling itself) is
prohibited.

1.8.2 Data Types

Data types: this entails defining the various data types that are used in
the project. The user can define their own data types, structures and
enumerations (self-defined data types).

1.8.3 Visualisation

HMI: this is where the various visualization screens are composed and
saved.

1.8.4 Resources

The resources include the Global variables and the available libraries.
Likewise tools for monitoring and graphical display of variables.

We encounter these four “blocks” in the CoDeSys main window. They


can be selected in the tabs at the bottom of the left panel.

POUs
Data types
Visualization
Resources

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
26
Introduction

1.9 Main Program PLC_PRG

The POU PLC_PRG is automatically activated every 10 ms after the


controller is powered on. This program must be present in every
project. Whenever a new project is created, this program is created
first by default.

All other programs, functions and function blocks are called from
PLC_PRG.

1.10 The 5 Programming Languages

CoDeSys supports all languages described in IEC 61131-3.

Textual languages:
 Instruction List (IL)
 Structured Text (ST)

Graphical languages:
 Sequential Function Chart (SFC)
 Function Block Diagram (FBD)
 Ladder diagram (LD)

An extra programming language, based on the Function block


Diagram, is the graphical language Continuous Function Chart (CFC).

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
27
First Steps

Chapter 2

First Steps

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
28
First Steps

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
29
First Steps

2.0 First Steps


In this chapter, we will be going through the steps from starting up the
software to downloading the program. We will do a basic logic
problem where there are three inputs and one output.

When either one of two inputs and together with the third input is
activated, the output is switched on.

2.1 Starting Up the CoDeSys Software


Start up the CoDeSys software and you will get the following screen:

 It is recommended to run the program at 96 dpi. You can choose to


ignore this if you wish.
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
30
First Steps

2.2 Setting the Options

 Before starting the new project, you might want to change some
settings.
 The following are just recommended settings; you need not set
them if you wish.
 Go to “Tools”, “Options”

 Choose “international Settings” in the left panel.


 Here, you can choose the language that you want.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
31
First Steps

 Choose “Features” and click on the “Predefined feature sets”


button.

 Choose “Professional” and click “OK”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
32
First Steps

 Choose “Load and Save”.


 Click on “At startup” and choose “Show empty environment”

 Click “OK”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
33
First Steps

 Choose “FBD, LD and IL editor”.


 Choose the items you want to view in your program and click “OK”

 Choose “CoDeSys 2.3 converter” and click on “(Enter a new


mapping here..)”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
34
First Steps

 Choose “Festo CPX-CEC/CPX-CEC-C1” and click “OK”

2.3 Adding in New Packages for the Hardware


In order to include the hardware into the program, you need to install
the package.

 Click “Tools’; “Package Manager”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
35
First Steps

 Click the icon “Install new packages”


 Click the button “CD or disk...”

 Locate where you place the package file.


 Click on the file and click “Open”.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
36
First Steps

 Click the button “Start installation’

 Choose “Typical setup”


 Click “Next”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
37
First Steps

 Click “Next”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
38
First Steps

 Click “Finish”

 The package with the hardware should be installed.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
39
First Steps

2.4 Getting Started


We will now start creating a new program.

On the “File” menu, click “New Project…”.


Another option would be to click directly on the “Create a new project”
icon.

 Choose “empty project”


Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
40
First Steps

 Give your project a name. In this case; “First Project”


 Click “OK”

 Right-click on “First Project”


 Click on “Add Device”

 Alternatively, you can go to “Project”, “Add Device”


IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
41
First Steps

 Choose the PLC you want to program to.


 If it is not there, you need to install the hardware package as
previously described.
 Give a name to the device and click “Add Device”
 Click “Close”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
42
First Steps

 Your device should appear under the project heading.

2.5 The IOs


Before we start, it is recommended that we configure our inputs and
outputs for the program.

 Click “Digital inputs” under the heading of “Onboard”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
43
First Steps

 A new window will appear on the right; you would also be able to
see a tab with the “Digital Inputs” heading.

 Here you can declare the input variables.

 You can give it a symbolic name and also the description.


 The absolute address can also be seen.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
44
First Steps

 You can do the same for the digital outputs.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
45
First Steps

2.5.1 IEC 61131-3 Addressing

This is how the different hardware variables are called.

Example: %IX100.0
Byte. Bit

X: Bit
B: Byte
W: Word

I: Input
Q: Output
M: Memory

IEC Address Register


% IEC address identifier
I Input module
Q Output module
M Internal memory
X Data type BOOL (1 bit)
W Data type BOOL (16 bits)
D Data type DOUBLE WORD (32 bits)
a) For I and Q:
No_1 = Word number
b) For M:
No_1 = Reference for the internal memory
Relay, special internal relay R/RW/DWR  0
Timer T  1
Counter C  2
Set value counter/timer SV/DSV  3
No_1 Elapsed value counter/timer EV/DEV  4
Data register,
Special data register DT/DDT  5
Index register IX,IY  6
Link relay L/LW/DWL  7
Link data register Ld/DLd  8
File register FL/DFL  9
Alarm relay E  10
Impulse relay P  11
. Separator
a) For I and Q:
No_2  bit position in the word
No_2 b) For M:
No_2 = 0..9, 11  word number
No_2 = 10  relay number
. Separator
No_3 Bit position in the word at R, L or P (No_1 = 0, 7 or 11)
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
46
First Steps

2.6 The Library


Before writing the program, you might want to add in the relevant
libraries. This need not be done at this early stage. You can add in the
library at any time when needed.

 Click on “Library Manager” on the left window.


 Click on “Add library” in the right window.

 Choose the library “Standard” under “Application”; “Common”.


 Click “OK”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
47
First Steps

 The new library will be added and you should be able to see what it
contains at the bottom left window.

2.7 The Program


 We can now start writing the program.

 Right-click on “Application”
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
48
First Steps

 Click on “Add Object”


 Click on “POU”

 Similarly, you can add it in from the main menu.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
49
First Steps

 Type in the name.


 Choose “Program”
 Click “Open”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
50
First Steps

 The programming window will appear on the right.


 You can start writing your program by adding in the required
symbols for LD.
 You can click the relevant elements from the toolbar at the top.

 At the symbols you will see question marks; you are required to
allocate the relevant variables.

 Click on the box next to the “???”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
51
First Steps

 You can choose the variables from the “Input Assistant”

 Choose the variable and click “OK”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
52
First Steps

 Complete the program.

2.7.1 Program in Different Languages

Anytime during the writing of the program, you can change to another
language.

The three languages are:

Ladder Diagram
Function Block Diagram
Instruction List

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
53
First Steps

Click “FBD/LD/IL”
Click “View”
Choose the language you want to view your program.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
54
First Steps

2.8 Compiling the Program


After writing the program, we need to compile or build the program.
This is to check for any errors.

Click on the “Build” in the menu bar and then click “Build” or press
“F11”.

Make sure that there are no errors.


 Errors and warnings will be displayed in the bottom window.
 You need not clear the warnings but all errors must be solved or
you will not be able to download the program into your hardware.

2.9 Controlling the Communication Settings


Before downloading the program we must be sure that the
communication settings between the PLC and the PC are correct.

 Connect the network cable to the PLC as well as to the PC either


directly or with a hub.
 Check the address of the PLC.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
55
First Steps

Click “Online”
Click “Scan Festo devices”

After scanning, you would be able to see the PLC with its IP address.
Take note of the address.
If you wish, you can change the IP address of the PLC.
This is to make sure that if there are more than one PLC in the
network, they have the same first three numbers and a different last
number.
Click “OK”

Now, you must change the computer network IP address for your PC
from your control panel, network settings.
This is done in your PC depending on the version of Windows you are
using.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
56
First Steps

 Click “Change Adapter Settings”

 Right-click on “Local Area Connection”, then click on “Properties”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
57
First Steps

Click on “Internet Protocol Version 4 (TCP/IPv4)”


 Click “OK”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
58
First Steps

 Key in the IP address for the PC based on the one from the PLC,
remember that the last number should be different.
 Click “OK”
 Then close all windows.

2.10 Task Configuration

Before downloading the program, we need to set the task. This will tell
the software which program to download to the hardware.

Right-click on “Application”
Click “Add Object”
Click “Task Configuration”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
59
First Steps

Click “Open”

Click “Add”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
60
First Steps

Choose the POU


Click “OK”

The POU should appear in the box below.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
61
First Steps

You should see the “Task” in the left window.

2.11 Setting the Communication Parameter


A next step is to set the communication parameter.

Double-click on the device.


A new window will open on the right panel.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
62
First Steps

Choose the hardware and click “Set active path”

2.12 Downloading the Program


Once we have communication, we can download the program.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
63
First Steps

 Click “Online”
 Click “Login”
 If there is an existing program in the PLC, click “Yes” to overwrite
the existing program.

The program is downloaded.

2.13 Running the Program

 After getting “Online” click “Start” to run the program.


 You can also click the  icon.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
64
First Steps

The program is now running.

2.13.1 Monitoring the Program

When the program is running, you might want to monitor the IOs.

Click on “Debug”
Click “Write values”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
65
First Steps

Click “Run”

When you run the program, you would be able to see the contacts
change colour.

2.13.2 Logging Out

To logout, go to “Online”
Click “Logout”

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
66
First Steps

2.14 Saving the Program/Project


 To save the program, click “File”
 Click “Save Project” or the “Save” icon.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
67
Structured Text

Chapter 3

Structured Text

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
68
Structured Text

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
69
Structured Text

3.0 Structured Text (ST)

To illustrate the use of the CoDeSys software, we will program the PLC
using the “Garage Door” example. For this first part, we will be using
the “Structured text” programming language.

3.1 Structured Text Programming


A Structured Text program is a list of ST instructions. Each instruction
is terminated by a semicolon (;). Names of variables, constants and
reserved words are separated by a passive separator.

Permitted passive separators are spaces, tabs and end of line.


Comments can be placed anywhere. Comments must start with (* and
end with *).

When writing ST programs, we must heed the following basic rules:

 Write no more than one instruction on one line.


 Ensure that complex instructions are made clearer by the use of
tabs.
 Use comments to clarify the program.

3.2 The Garage Door Example


In this example, the garage door opens or close in the same logic
sequence, it opens when it fulfils either one of the following options:

Pressing the “Button Up” inside the garage with the “Limit Switch Up”
is disengaged.
Pressing the “Button Up” outside the garage with the “Limit Switch
Up” disengaged and the “Key Operator Switch” on.

The inputs:

Limit switches:
Input0_0 Limit switch up NC
Input0_1 Limit switch down NC
Buttons inside
Input0_2 Button up
Input0_3 Button down
Buttons outside
Input0_4 Key-operator switch
Input0_5 Button up
Input0_6 Button down

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
70
Structured Text

The outputs:

Ouput0_0 Door goes up


Ouput0_1 Door goes down

3.3 Changing the “Global Variables” List

The name “Input0_0, Input0_1” does not explain what the input
means. Therefore I would like to change the name in the global
variables list.

Limit switches:
I0_0_LimitUp
I0_1_LimitDown
Buttons inside
I0_2_InButtonUp
I0_3_InButtonDown
Buttons outside
I0_4_SwitchEnableOut
I0_5_OutButtonUp
I0_6_OutButtonDown
Outputs
O0_0_DoorUp
O0_1_DoorDown
 Add in the symbolic name in the variables list.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
71
Structured Text

3.4 Starting to Write the Program Code and


Implementing the Variables Automatically
Which program code? The door of the garage must open when:

The limit switch is not pressed


AND
(The indoor button up is pressed)
OR
(The outdoor button up is pressed)
AND
The outdoor switch has enabled the outdoor buttons.

The program in Structured Text:

O0_0_DoorUP := I0_0_LimitUp AND (I0_2_InButtonUP OR


(I0_4_SwitchEnableOut AND I0_5_OutButtonUP));

O0_1_DoorDown := I0_1_LimitDown AND (I0_3_InButtonDown


OR (I0_4_SwitchEnableOut AND I0_6_OutButtonDown));

3.5 Building the Project


Build the project and check for errors. Look in the message window.
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
72
Structured Text

If there is an error, you will not be allowed to download the program.


 Click on the “Error” tab on the bottom.
 The error(s) will be listed.
 Double-click on the error and you will be brought to where the
error occurs.

3.6 Download and Run the Program


Establish communication with the PLC, download and run the
program.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
73
The Ladder Diagram
Language (LD)

Chapter 4

The Ladder Diagram Language

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
74
The Ladder Diagram
Language (LD)

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
75
The Ladder Diagram
Language (LD)

4.0 The Ladder Diagram Language (LD)


After we tested the program with the “ST” language, we continue with
the “LD” (Ladder) language. We will be using the same project.

4.1 The Ladder Diagram Language


The ladder diagram is no more than an electrical diagram drawn on its
side. In a normal diagram, we have a line, usually the plus, at the top
of the page and a line, usually the minus, at the bottom of the page.
The components (switches and other contacts) are placed between
these lines, from top down. Below these are the controlled
components such as coils, lamps, etc. In a ladder diagram we have a
line down the left side (the plus) and another down the right side (the
minus). Between these lines (also referred to as power rails) are
networks on which we place the components. From left to right the
symbols for inputs followed by the symbols for the outputs.

In a ladder diagram, rather than drawing normal switches, coils and


lamps, instead we draw symbols that indicate whether or not there is a
signal.

As far as the controller is concerned, it does not matter where the


signals originate. Whether it's a switch, a button or a foot pedal, the
only thing that matters to the controller is that the signal has just two
states: a logical 1 (TRUE) for signal (voltage present) and a logical 0
(FALSE) for no signal (voltage not present). This also applies to the
output: it makes no difference to the controller whether a coil or a
lamp is controlled. The controller ensures that a logical 1 (TRUE) signal
(voltage) or a logical 0 (FALSE) signal (no voltage) is on the output.
The ladder diagram is usually created on the computer.

The following icons are used in the development of the program.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
76
The Ladder Diagram
Language (LD)

4.2 Making a New Program

To add in a new program:

Right-click on the “Application”; click on “Add Object”; and click


“POU”

An “New POU” window will pop-up and you can enter the required
details.
Give your new POU a name.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
77
The Ladder Diagram
Language (LD)

 Choose the programming language, in this case it is “Ladder”


 Click “OK”

Your new program will be added into the project.


 You can see the new program in the “Project Tree” window.

4.3 The Program Itself

We will now start writing the program in ladder diagram language.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
78
The Ladder Diagram
Language (LD)

Start by making the ladder structure shown.


Identify the contacts.
Choose the relevant variables.
 If you need to have another network, click on the icon to add in
one.

4.4 Download and Run the Program


Establish communication with the PLC, download and run the
program.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
79
The Function Block
Diagram (FBD)

Chapter 5

The Function Block Diagram

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
80
The Function Block
Diagram (FBD)

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
81
The Function Block
Diagram (FBD)

5.0 The Function Block Diagram (FBD)


5.1 Making a New Program

If you want to insert a new program, follow the same procedure as


stated earlier in the Ladder Diagram

5.2 The Function Block Diagram (FBD)


A function block diagram is built up from a collection of functions, also
referred to as networks. The relationship between input variables and
output variables is described in a function. A function is composed
from standard function blocks.

Function

Inputs

A function is processed from the top down and from left to right. Each
successive function is only processed after the preceding function has
been processed to completion.

An output is linked to an input by a combination of standard functions.


Outputs may never be linked to one another. A function block is
represented by a rectangle with the inputs on the left hand side and
the output on the-right hand side. The function is indicated by a
symbol inside the rectangle.
Name of the function

Inputs & Output

The variables on the inputs of a function block must be of equivalent


type, the output may be of another type.

Outputs may be internal variables or output variables; it is also


possible for the output to be the name of a subroutine which is called.
Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
82
The Function Block
Diagram (FBD)

When attaching variables in the editor, you can position the cursor on
a “link” and enter a variable name. Question marks do not always
appear behind the link.

When using the FBD, we will be working with a different set of editor
functions from the ones used for Ladder Diagram programming.

This is how the editing window appears when FBD is chosen as


programming language.

 To insert a block, you can click on the icon to open the “Input
Assistant”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
83
The Function Block
Diagram (FBD)

Choose the category and then choose the function block you want.
Click “OK”

5.3 Writing the Program


Similar to the Ladder Diagram and Structured Text program, write the
same program in Function Block Diagram.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
84
The Function Block
Diagram (FBD)

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
85
Using Memories

Chapter 6

The Instruction List Program

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
86
Using Memories

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
87
Using Memories

6.0 The Instruction List Program (IL)


The last computer language in which we are going to make with the
door example is the IL language. You can do this on your own.

6.1 The Instruction List Program (IL)


An Instruction List program comprises a series of instructions. Each
instruction begins on a new line. Each instruction contains an
Operator, if necessary a Modifier, and one or more Operands
separated by commas.

The Operator can be preceded by a Label. This Label consists of a


word ending with a colon (Label:).

A comment may also be present on a line as the last element, enclosed


entirely between brackets and asterisks (* like this *). Empty lines
between instructions are permitted.

NOTE!

IL is compiled line by line, and does not comply with the Boolean order
of operations BNAO (Brackets, Not, And, Or).

6.2 Writing the Program


The program is as follows:

(*Sending the Door Up*)


LD I0_0_LimitUp
AND ( I0_2_InButtonUp
OR ( I0_4_SwitchEnableOut
AND I0_5_OutButtonUp
)
)
ST O0_0_DoorUp

(*Sending the Door Down*)


LD I0_1_LimitDown
AND ( I0_3_InButtonDown
OR ( I0_4_SwitchEnableOut
AND I0_6_OutButtonDown
)
)
ST O0_1_DoorDown

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
88
Using Memories

6.3 Toggling Between LD/FBD/IL


Once you have written your program in either Ladder Diagram,
Function Block Diagram or Instruction List; you can use the software to
toggle between them.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
89
Using Memories

Chapter 7

Using Memories

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
90
Using Memories

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
91
Using Memories

7.0 Using Memories


So far, we wrote programs for the door example in 4 languages, but
we used only the inputs and outputs from the PLC. Now it is time to
use memories. In other PLC languages they are called flags, markers,
internal relays, etc. In this case (IEC61131-3) we speak about
memories.

7.1 The Idea


Let’s change the door example in the ladder program. The idea is that
even after a short button push the door opens totally. This we will do
in the ladder example.

We will add an additional parallel rung to the ladder diagram.

7.2 Implementing the Memory Variable Automatically


 Click on the question marks and type in a name you wish to give
the memory.
 As this is a new name the “Declare Variable” box will appear.
 Click “OK” as it is a memory. The software will automatically issue
it an address.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
92
Using Memories

 However, you may give it a specific address if you wish.


 It has to follow the IEC convention and be declared for example as
“%MX10.0”

7.3 Implementing the Memory Variable Manually


 You can also declare the memory manually.
 Move your cursor to the top window and type in the variable.

7.4 Implementing the New Variable a Second Time

 Insert a new parallel coil.


 Declare the coil as “Mem_Up”
 Go to the contact and also declare it as “Mem_Up”, the “Declare
Variable” box will not appear as it has already been declared.

7.5 Download and Run the Program


Establish communication with the PLC, download and run the
program.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
93
Using Timers

Chapter 8

Using Timers

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
94
Using Timers

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
95
Using Timers

8. Using Timers
8.1 Description of the Example
In the following example we are going to use a timer. For this we use
the “Function Block Diagram” computer language.

What’s the idea behind the door example program: after pushing the
button “UP”, we will have to wait some time before the door opens.

8.2 Three Types of Timers


Let’s start the “program” under “FBD01”. Which timer functions exist
in “IEC61131-3”?

There are three types of timers:

8.2.1 Function Block TP, Pulse Timer

This function delivers a pulse to the output.

 PT is the preset time, and may also be a variable of type TIME.


 Q is the output of the timer, and is of type BOOL.
 IN is the input of the timer, and is of type BOOL
 ET is an output of type Time, and is used to display the elapsed
time.

On a rising edge of input IN, the timer output Q goes True and the
timer starts. After the preset time, the timer output Q goes False.

Once input IN has become False and the time has elapsed, the pulse
can be used again.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
96
Using Timers

8.2.2 Function Block TON, Switch-on Signal Delay

This function provides for a delayed turn-on.

 PT is the preset time, and may also be a variable of type TIME.


 Q is the output of the timer, and is of type BOOL.
 IN is the input of the timer, and is of type BOOL
 ET is an output of type Time, and is used to display the elapsed
time.

The timer starts on a rising edge of input IN. After the preset time, the
timer output Q goes True. If input IN goes False during the timer
duration, then the timer stops. If input IN goes True again, then the
timer restarts.

8.2.3 Function Block TOF, Switch-off Signal Delay

This function provides for a delayed turn-off.

 PT is the preset time, and may also be a variable of type TIME.


 Q is the output of the timer, and is of type BOOL.
 IN is the input of the timer, and is of type BOOL
 ET is an output of type Time, and is used to display the elapsed
time.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
97
Using Timers

On a rising edge of input IN, the timer output Q goes True; once the
input is again False, the timer starts to run. If input IN becomes True
again before the preset time has elapsed, then output Q stays True.
The timer will restart only when the input IN has again become False.
After the preset time, the timer output Q goes False.

8.3 Inserting the Timer in Our Example

We learnt how the TON timer is working. Now we are going to use it in
our example.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
98
Using Timers

 Click at the point just after the last “AND” function block before the
output.

 Click the block icon to insert a new function block.

 Name the block “T1” or any other name you wish and give the
preset time to “T#3s”. This is based on the IEC 61131-3
convention.
 The preset time (PT) of the timer = the execution time of the timer.
 The time interval (ET); this is an option. When does the timer run?

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
99
Using Timers

 Your timer has been inserted.


 After a 3 second delay then the output will be switched on.

Festo Didactic • IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1
101
Using Counters

Chapter 9

Using Counters

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


102
Using Counters

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
103
Using Counters

9. Using Counters
9.1 Description of the Example
We are going to make a new program that uses a counter. There is a
lamp, which indicates that the garage is full. We also have an input,
which resets the counter.

For counting up we use the inputs “I0_5_OutButtonUp” AND


“I0_4_SwitchEnableOut”.

For counting down we use input “I0_2_InButtonUp”. This is not 100%


correct but never mind, it is only an exercise.

9.2 Types of Counters


Which help functions exist for the different counters? Let’s see. Open
the help file the same way you opened when checking on the timers.

9.2.1 Function Block CTU, Incremental Counter

This counter counts up from 0 to the specified value

 The inputs CU and RESET and the output Q are of type BOOL. Input
PV and output CV are of type WORD
 When the input RESET becomes True (1), the value 0 will be placed
in CV.
 When a rising edge is observed on CU, CV is incremented by 1
(one). As soon as CV is greater than or equal to the value of PV,
output Q will become True (1).

9.2.2 Function Block CTD, Decremental Counter

This counter counts down from a specified value to 0.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


104
Using Counters

 The inputs CD and LOAD and the output Q are of type BOOL. Input
PV and output CV are of type WORD
 When the input LOAD becomes True (1), the value of PV will be
placed in CV.
 When a rising edge is observed on CD, CV is decremented by 1
(one). As soon as CV reaches the value 0, output Q will become
True (1).
 The value of CV cannot become less than 0.

9.2.3 Function Block CTUD, Combined Incremental/Decremental


Counter

This is a combination of an incrementer and a decrementer.

CU is the input that increments the counter by 1, and is edge-sensitive.


RESET is the input that resets the counter. The counter will be
continuously reset for as long as this input is True. This will keep the
output CV (the number of counted pulses) to 0. In order to use the
counter, the RESET input must therefore be False.

CD is the input that decrements the counter by 1, and is edge-


sensitive.

LOAD is the input with that causes the counter to be loaded with the
specified value. The counter will be continuously kept at the specified
value for as long as this input is True. This forces the output CV (the
number of pulses still to be counted) to be kept equal to the preset
value PV. In order to use the counter, the LOAD input must therefore
be False.

So, to use this counter, both the input RESET and the input LOAD must
be False.

 PV is the number of pulses to be counted, specified as an integer.


 QU is the output from the counter which is True when the number
of counted pulses (CV) is greater than or equal to the number of
pulses to be counted (PV).
 QD is the output from the counter which is True when the number
of pulses still to be counted (CV) is equal to 0.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
105
Using Counters

 CV is the number of counted or still to be counted pulses as


integer, depending on use as incrementer or decrementer.

For our exercise, we will be using the “CTU” function block.

9.3 Implementing Two New Variables

We need two new physical I/Os and one memory variable.

 One input “Reset”, we will use %IX0.7


 One output “Lamp” (garage full), we will use %QX0.2
 One memory (integer) - IGarageActual

9.4 The Program


We will add in another network in the FBD01 program.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


106
Using Counters

The program would look like this:

9.5 Download and Run the Program


Establish communication with the PLC, download and run the
program.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
107
The Sequential Function
Chart (SFC)

Chapter 10

The Sequential Function Chart

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


108
The Sequential Function
Chart (SFC)

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
109
The Sequential Function
Chart (SFC)

10. The Sequential Function Chart (SFC)


10.1 Introduction
The last programming language used in IEC 61131-3 is the Sequential
Function Chart (SFC). This programming language makes the
programming of sequences easier, ie. events which take place step by
step. Typical sentences include gripper motion : left  lower  close
 lift  right, or the movement for an automated packaging system :
feed  lift  set  insert  close, or similar events.

Our example with the garage door, which was used for the other four
languages, is of purely logical nature and does not include any
sequences. Thus a new task should be selected for the first SFC
example: The Bedside Table Lamp Controller.

Have you seen such a lamp? The lamp can be switched on and back off
again with a single button. Button depressed: Lamp on  Button
released: Lamp remains on  Button depressed: Lamp off  Button
released: Lamp remains off.

This example could certainly be programmed in several different ways,


but our example is also well suited for sequential language.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


110
The Sequential Function
Chart (SFC)

10.2 The Sequential Function Chart Program


Sequential Function Chart is included in IEC61131-3 as a convenient
tool for writing step-based programs. Depending on the function, you
can make use of any of the other IEC programming languages to define
the steps.

The SFC elements enable you to split control programs up into sets of
actions and conditions (transitions).

When a Program Organization Unit (POU) is specified as Sequential


Function Chart (SFC), then it must be programmed entirely as
Sequential Function Chart (SFC). The steps are processed in
chronological order.

Step 3 Open inlet Action

Transition Vessel full Transition condition

Close inlet
4
Heating on

65°C

Heating off
5
Stir 8 min

Stir time elapsed

Stop stirring
6
Open outlet

Vessel empty

This is part of a SFC; the steps are processed from the top down. An
action lasts until the transition condition becomes “True” (1). Then the
next action is executed.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
111
The Sequential Function
Chart (SFC)

10.2.1 Step

A POU written in SFC consists of a series of steps, linked together by


transitions (conditions for linking).

You can use a simple step to carry out an assigned action.

You can assign one or more actions to an IEC step. Each action can
consist of a number of Boolean variables.

The action “Clamp” is linked to this step. If you link multiple actions to
a single step, then they will be processed in the order in which they
are linked.

You may link a maximum of nine (9) actions to a single IEC step.

Although an action is linked to a program, it is not automatically


processed. You must always link the action to a step, as shown here.

10.2.2 IEC-Extending Step Actions

These are actions extending the IEC standard, (in CoDeSys V3


previously known as "3S-Smart Software Solutions GmbH step
actions": The actions must be available as objects below the SFC
object. The action names must be unique.

a. “Step entry” action (Step activated)

This type of step action will be processed as soon as the step has
become active and before the "step active" action.

The action is associated to a step via an entry in the "step entry" field
of the step. It is represented by an "E" in the lower left corner of the
respective step box.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


112
The Sequential Function
Chart (SFC)

b. “Step active” action (Step action)

This type of step action will be processed when the step has become
active and after a possible "step entry" action of this step has been
processed. However in contrast to an IEC step action it is not executed
once more when it gets deactivated and it cannot get assigned
qualifiers.

The action is associated to a step via an entry in the "step active" field
of the step. It is represented by a small triangle in the upper right
corner of the respective step box.

c. “Step exit” action (Step deactivated)

An exit action will be executed once when the step has got
deactivated. Notice however that this execution will not be done in the
same, but at the beginning of the subsequent cycle.

The action is associated to a step via an entry in the "step exit" field of
the step. It is represented by an "X" in the lower right corner of the
respective step box.

A combination of both “entry action” and “exit action” is also


permitted.

These actions may be programmed in any IEC language.

The action can be modified by clicking on the E or the X.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
113
The Sequential Function
Chart (SFC)

10.2.3 Transition or Condition

A transition must provide the condition on which the subsequent step


shall get active as soon as the condition value is TRUE.

So a transition condition must have the value TRUE or FALSE. It can be


defined in the following two ways:

 Directly ("inline condition"): Replace the default transition name


by either a boolean variable, a boolean address, a boolean
constant or an instruction having a boolean result (e.g. (i<100) AND
 It is not possible here to specifiy programs, function blocks or
assignments!

Using a separate transition or property object ("multi-use condition"):


Replace the default transition name by the name of a transition or
property object available in the project (this allows multiple uses of
transitions; see for example "condition_xy" in the figures below).

The object like an inline transition can contain a boolean variable,


address, constant or instruction, but also multiple statements with
arbitrary code.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


114
The Sequential Function
Chart (SFC)

"Multiple use conditions" (transitions or properties) are indicated by a triangle

A transition condition is handled like a method call It will be entered


according to the following

Syntax:

<transition name>:=<transition condition>;


(for example" trans1:= a=100")

or just

<transition condition>;
(for example a=100)

See also an example (condition_xy) in the figure above, "Examples of


transitions".

10.2.4 Active Step

Once a POU has been called, the action with the double frame, the Init
step, is executed first. A step in which the action is being executed is
referred to as an active step. In online mode, active steps are
displayed in blue.

The next step becomes active as soon as the condition preceding this
step becomes “True” (1). The actions linked to this step are executed
in the following cycle.

10.2.5 IEC Step

An IEC step action is represented by a bipartite box, connected to the


right of a step via a connection line. In the left part it shows the action
qualifier, in the right part the action name. Both can be edited inline.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
115
The Sequential Function
Chart (SFC)

IEC step actions get associated to a step via the “Insert action
association” command. One or multiple actions can be associated to a
step. The position of the new action depends on the current cursor
position and the command. The actions must be available in the
project and be inserted with a unique action name

No more than nine (9) actions may be linked to an IEC step! IEC actions
are not linked to a specific step, but are stored with the program as
separate modules. This enables them to be re-used without restriction
throughout all steps in the program.

However, these actions do need to be linked to each step by means of


the function Associate Action.

Successive actions can now be added using the function Associate


Action. The right column shows the name of the called action. The left
column shows the qualifier of the Action.

When an action is deactivated, it is executed one last time. This means


that an action that gets deactivated still gets executed at least twice.

When called, the deactivation is first executed, and then the active
actions are processed each time in alphabetic order.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


116
The Sequential Function
Chart (SFC)

There are different action qualifiers available which are shown in the
following table:

Action
Description Functionality
Qualifier
N Non-stored The action is active as long as the step is active

RO Overriding Reset The action gets deactivated.


The action will be started when the step becomes
SO Set (Stored) active and will be continued after the step is
deactivated, until the action gets reset
The action will be started when the step becomes
L Time Limited active and it will continue until the step goes
inactive or a set time has passed.
A delay timer will be started when the step
becomes active. If the step is still active after the
D Time Delayed
time delay, the action will start and continue until it
gets deactivated.
The action will be started when the step becomes
P Pulse active/deactive and will be executed once.
Stored and Time The action will be started after the set time delay
SD
Delayed and it will continue until it gets reset.
The action becomes active after a preset delay, and
Delayed and
DS remains so for the duration of the step and until a
Stored Reset.
The action will be started when the step becomes
Stored and Time
SL active and it will continue for the specified time or
Limited until a reset.

10.3 Branches
Steps are processed from top down and from left to right.

10.3.1 Alternative Branches (OR Function)

SFC permits the use of an OR function, in which only one of the


branches can be active at a time. In the SFC diagram segment below,
processing can continue in two directions: to step 6 or to step 16. Step
6 becomes active when step 5 is active and condition B is “True”. Or
step 16 becomes active when step 5 is active and condition D is
“True”.

It is possible to define multiple branches alongside one another. The


horizontal connecting line is a single line, indicating OR functionality.

The programmer must ensure that condition B and the condition D are
mutually exclusive (totally different). The first condition from left that
is “True” is used.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
117
The Sequential Function
Chart (SFC)

Condition_A

Condition_B Condition_D

6 16

Condition_C Condition_E

7 17

Condition_F Condition_G

10

When step 7 is active and condition F is “True” then step 7 becomes


inactive and step 10 active or when step 17 active is and condition G is
“True” then step 17 becomes inactive and step 10 active.

10.3.2 Parallel Branches (AND Function)

SFC permits the use of an AND function, in which multiple branches


can be active simultaneously. When step 5 in the SFC diagram
segment below is active, processing continues simultaneously in two
directions, to step 6 and to step 16. Step 6 and step 16 becomes
active when step 5 is active and condition B is True.

The horizontal connecting line is a double line, indicating AND


functionality. It is possible to define multiple branches alongside one
another. In this situation, multiple steps may be active in the SFC.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


118
The Sequential Function
Chart (SFC)

Condition_A

Condition_B

6 16

Condition_C Condition_E

7 17

Condition_G

10

When step 7 and step 17 are active and condition G is “True”, step 7
and step 17 become inactive, and step 10 becomes active.

10.3.3 Jumps

Jumps can be made using the JUMP function. The name of the
destination step is placed below the JUMP. You can jump either
forwards or backwards.

Every SFC ends with a jump. Normally the SFC ends with a jump back
to “Init”.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
119
The Sequential Function
Chart (SFC)

10.4 SFC Editor

10.5 Start a New Project


Add in a new SFC program; follow the same procedure described
earlier for adding in the ladder diagram program.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


120
The Sequential Function
Chart (SFC)

 You will get the SFC left window in the main page.

 We can now start writing the program.

10.6 Writing the Program


As mentioned earlier in the chapter, we will write the program where
there are one input and one output variable.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
121
The Sequential Function
Chart (SFC)

Once the push button input is pressed, the lamp output will switch on
and then the push button is pressed again, the lamp will switch off
and this will toggle.

10.7 Making Steps and Transitions


 We will add in some more steps and transitions for this program.

 Highlight the transition where you wish to add in another step and
transition.
 Click on the icon “Step Transition (after)” or right-click and choose
the same option.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


122
The Sequential Function
Chart (SFC)

 A new step and transition will appear.

 Right-click on the “Step” and click “Insert action association”


IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
123
The Sequential Function
Chart (SFC)

 You can also click on the icon to insert the action association.

 You can now change the actions for the steps.

 Click in the identifier box and type in the command; a drop down
menu will appear with the choices available.
 Choose the one you want.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


124
The Sequential Function
Chart (SFC)

 Click on the box for the variable.

 Choose the variable you want and click “OK”

 For transitions which do not have a simple input, you have to


create a sub-task.
 Double click on the transition and a dialog box will appear

 Choose the programming language and click “OK”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
125
The Sequential Function
Chart (SFC)

 You will see the sub-program under “Trans0”


 Type in the program

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


126
The Sequential Function
Chart (SFC)

 Complete the program as shown.

10.8 Download and Run the Program


Establish communication with the PLC, download and run the
program.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
127
Running Multiple
Programs

Chapter 11

Running Multiple Programs

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


128
Running Multiple
Programs

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
129
Running Multiple
Programs

11. Running Multiple Programs


There are instances where you cannot write the whole control task in
one program. You will need to have several programs running for
different functions.

The following are the steps we need to do to create multiple programs.

11.1 Creating a Global Variable List


Create your programs as you would normally do. However, if there is a
variable which will be used across programs, it has to be created
under the “Global Variable List”

 Right-click on “Application”, click on “Add Object” and click on


“Global Variable List”.

 A new pop-up window will appear.


 Give a name for the list.
 Click “Open”

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


130
Running Multiple
Programs

11.1.1 Adding in the Global Variable

 Click on the “GVL” tab.


 You can add in the global variable here.

 For our example, I will add in “Start_Flag:BOOL;”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
131
Running Multiple
Programs

11.2 Create a New Program


 Next, we need to create a new program.
 Right-click on “Application”, click on “Add Object” and click on
“POU”.

 Give a name to the program and choose the programming


language.
 Click “Open”

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


132
Running Multiple
Programs

 Write your program.


 When you choose the variable, make sure you choose the one in
the Global Variable List if is it to be used in one or more programs.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
133
Running Multiple
Programs

11.2.1 Adding Programs in the Main Task

 After writing the program, you will need to add in this program in
the “Main Task”

 Click on “Main Task”


 Click on “Add POU”

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


134
Running Multiple
Programs

 Choose the program,


 Click “OK”
 The program will appear in the list.

 When you start the software, both programs will run concurrently.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
135
Using the Simulator

Chapter 12

Simulation and Visualisation

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


136
Using the Simulator

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
137
Using the Simulator

12. Simulation and Visualisation


The program allows you to simulate what you have written. It is an
option which you can use if required. You are also able to create a
screen to see you inputs and outputs visually.

12.1 Sample Program for Simulation


To see the function of the simulation and visualisation, we will write a
new program with the following conditions.

There would be three inputs and one output. Only when two of the
three inputs are activated will the output be switched on. When all
three inputs are activated, the output should not switch on.

12.2 Starting the Simulator


This command (category “Online”) is available in Online Mode to
switch on and off the simulation mode of the programming system. In
simulation mode the application can be run and debugged on a
"simulation target" which is always available within the programming
system. So no real target device is needed to test the online behaviour
of an application.

If the command is called from the “Online” menu, the currently active
application will be running.

If the command is called from the context menu when an application


object is selected in the device tree, then the selected application will
be affected, no matter whether it is set active.

When command “Simulation” is activated ( ), the device


entry in the device tree will be displayed in italic letters and at the first
“login” with the currently active application you will be asked whether
application "Sim.<devicename>.<applicationname>" should be created
and loaded to the simulation target. No communication settings have
to be done.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


138
Using the Simulator

 Choose “Simulation”; make sure that the tick is there.

 Click “Login”

 Choose “Yes”
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
139
Using the Simulator

After successful login the icon beside the device icon indicates the
simulation mode. You can use the respective online commands to test
the application.

To switch off the simulation mode, first log out and then again perform
command "Simulation". The checkmark in front of the command will
disappear, the target entry in the device tree again will be displayed in
normal letters and you can log in to a real device.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


140
Using the Simulator

12.3 Simulation Device


In the “Devices window” ("device tree") you can map the hardware on
which the application is to run.

Each device object represents a specific (target) hardware object.


Examples: controller, field bus node, bus coupler, drive, I/O-module,
monitor.

Each device is defined by a device description and must be installed


on the local system in order to be available for inserting in the devices
tree. The device description file defines the properties of a device
concerning configurability, programmability and possible connections
to other devices.

However not only device objects are managed in the device tree, but
all objects which are needed to run an “Application” on a device
(controller, PLC); thus also the “Application” objects as well as “Task
Configuration” and “Task” objects.

But also pure programming objects like particular POUs, Global


Variable lists and Library Manager can - instead of being managed as
project-globally instantiable units in the POUs window - be managed
ONLY in the device tree and in this case are only available for exactly
“their” application or “child applications” of their application.

There is the possibility to run the active application on a “simulation


device” which is per default automatically available within the
programming system. So no real target device is needed to test the
online behaviour of an application. When you switch to simulation
mode, the CoDeSys Control Win V3 entry in the device tree will be
displayed in italic letters and you can login with the application.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
141
Using the Simulator

Regard also the possibility to connect to the device in “online


configuration mode” without the need of first having loaded a real
application there. This is useful for the initial start-up of an I/O
system, because you can access and test the I/Os in the PLC
configuration before you build and load a real application program.

The root node of the tree always is a symbolic node entry:


<Projectname>.

The trees of the "PLC Configuration" and "Task Configuration", which


in CoDeSys are completely handled in separate windows, are
integrated in the devices tree. The PLC configuration is defined by the
topological arrangement of the devices in the device tree. Just the
configuration of the particular device resp. task parameters is done in
corresponding editor dialogs.

Thus the hardware structure will be mapped and represented within


the devices tree by the corresponding arrangement of device objects,
allowing setting up a complex heterogeneous system of multiple
networked controllers and underlying field busses.

See below for the rules for inserting objects in the device tree.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


142
Using the Simulator

Each entry in the device tree shows the symbol, the symbolic name
(editable) and the device type (=device name as provided by the device
description)

A device is programmable or just parameterizable. The type of the


device determines the possible position within the resources tree and
also which further resources can be inserted below the device.

Programmable devices are indicated by an additional "Plc Logic"


node inserted automatically below the device entry. Below this node the
objects needed for programming the device (applications, text lists
etc.), as well as functional objects like e.g. a Parameter Manager, can be
inserted. Pure parameterizable devices cannot get assigned such
programming objects; however the values of the device parameters
might be edited in the parameter dialog of the device editor. Regard
that the programmability of a device is a property which can change
(device description) without the need of reinserting the device.

Within a single “Project” one or several programmable devices -


regardless of manufacturer or type - can be configured (“Multi-
Resource”, “Multi-Device”, Networking).

The configuration of a device concerning communication, parameters,


I/O Mapping is done in the Device dialog (Device Editor), which can be
opened by a double-click on the device entry.

In online mode an icon at the beginning of a device entry indicates the


status of the device:

: Device is running
: Device is not running / Device is not configured / Device has an
error/ Device is in “simulation mode”.
: Device is running, diagnostic information available
: Device is configured but not running
: PLC is in 'stop' and 'Update IO while in stop' is not set

12.4 Simulating Your Program


You can test your program by forcing the inputs and outputs from the
IO list.

To do this, you need to open:

 The program you wrote


 The input screen
 The output screen

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
143
Using the Simulator

When you open the respective components, you will see the last
opened screen. To see your program running it would be good to have
all three screens visible.

 To do this, click on the tab and drag it down to the main screen, a
navigation button will appear.

 Choose the location you want to place the screen; by clicking on


the top, left, bottom or right button.
 Do this for all three screens.

 If you are unable to see the whole program, you can click on the
zoom icon at the bottom right.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


144
Using the Simulator

 Run the simulation program.


 When it is online, you should be able to see the status of the IOs;
either “True’ or “False”.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
145
Using the Simulator

 Move your cursor to the “Prepared Value” box and double-click.


 It will toggle between “True” and “False”

 Once you have decided, you can choose one of the following
options from the “Debug” menu or by pressing the shortcut key:
o Write values – [Ctrl] + [F7]
o Force values – [F7]
o Unforce values – [Alt] + [F7]

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


146
Using the Simulator

 You can try the different options.


 Take note that the program will run according to the status of the
inputs.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
147
Using the Simulator

12.5 Using the Visualisation Function


Using the visualisation function in CoDeSys, we are able to make the
project variables dynamic. We will create a visualisation screen for the
same 2 out of 3 project.

 Right-click on “Application”; click on “Add Object”; then click on


“Visualisation”

 Create a visualisation object by giving it a name and click “Open”

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


148
Using the Simulator

 Your device screen should have two more additions.

 Double-click on the visualisation object; in this case, “Monitor”.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
149
Using the Simulator

 A new window will open with the visualisation toolbox on the right.

 If the toolbox is not present, go to “View” and click “Toolbox”


 We will now create the push buttons as well as the lamp output.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


150
Using the Simulator

 For the push buttons, we will use the “round rectangle” feature.
 Create 3 buttons; you can both create from the toolbox or copy and
paste from the first button.

 Use the “Ellipse” form for the lamp output.


 Once, the figures are created, we now need to configure them.
 Click on the first button.
 The “Properties” window will appear with the main headings.

 We will first change the colour; click on the [+] next to the word
“Colors” and it will expand.
 The two options are; “Normalstate” and “Alarmstate’.
 Click on the box next to “Fillcolor”

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
151
Using the Simulator

 A small icon will appear next to it.

 Click on the icon to open the colour palette.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


152
Using the Simulator

 Choose the colour you want and click “OK”

 The colours you choose will highlight when the input is activated
or not.
 Next, we choose the “Text” property.
 Type in the name you want to see on the drawn element.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
153
Using the Simulator

 Next, click on “text properties”; here you can set the font,
alignment, colour of the text.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


154
Using the Simulator

 Choose “Color variables”; click in the box “Toggle color” and


choose the variable.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
155
Using the Simulator

 Choose “Inputconfiguration”; choose “Toggle” and enter the


variable.

 Do the same for the remaining objects.


 This will complete the design of the visual screen.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


156
Using the Simulator

12.6 Running the Simulation

 Build the program again.


 Click “Online” from the menu bar.
 Click “Simulation Mode”
 Make sure that a tick appears on the left.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
157
Using the Simulator

 Click “Online” from the menu bar.


 Click “Login”
 Or you can click the “Login” icon

 Click “Online” from the menu bar.


 Click “Run”
 Or you can click the “Run” icon

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


158
Using the Simulator

 Click on the visual screen to see the effect.

 When S1 and S2 are pressed, the Lamp will activated.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
159
Functions and Function
Blocks

Chapter 13

Functions and Function Blocks

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


160
Structured Text
Instructions

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
161
Functions and Function
Blocks

13. Functions and Function Blocks


13.1 Introduction
When programming using the IEC 61131-3 languages, it is useful to
use the Functions and Function Blocks in the software.

13.2 Types of Functions


Functions are POUs with multiple input parameters and exactly one
output parameter. They do not have any internal memory.
Consequently, calling a function with the same values always the
same return values result. The return values can be either single or
multi-element variables such as arrays or structures. The abbreviation
for functions is FU.

IEC 61131-3 describes standard functions which can be used while


editing your PLC program. According to your hardware and PLC type it
is possible that not all standard functions are available or that
firmware functions have been added.

Note: Please refer to your hardware documentation or to the specific


help for detailed information. IEC 61131-3 lists different types of
standard functions:

Standard Functions Functions Remarks

*_TO_** Type conversion


Type Conversion *_TO_**_BCD Type conversion to BCD code
Functions *_BCD_TO_** Type conversion from BCD code
TRUNC_* Truncation
ABS Absolute value
ACOS Arc cosine
ASIN Arc sine
ATAN Arc tangent
COS Cosine
Numerical
EXP Exponent
Functions
LN Natural logarithm
LOG Logarithm to the base of 10
SIN Sine
SQRT Square root
TAN Tangent

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


162
Structured Text
Instructions

Standard Functions Functions Remarks

ADD Additor for ANY_NUM


ADD_T_T Additor for TIME
DIV Divisor for ANY_NUM
DIV_T_AN Divisor for TIME
EXPT Exponentiation
Arithmetic MOD Modulo divisor
Functions MOVE Assignment
MUL Multiplier for ANY_NUM
MUL_T_AN Multiplier for TIME
NEG Double complement
SUB Subtractor for ANY_NUM
SUB_T_T Subtractor for TIME
AND AND connection
Bitwise Boolean NOT Complement
functions OR OR connection
XOR XOR connection
ROL Rotate left
ROL_* Rotate left typed
ROR Rotate right
ROR_* Rotate right typed
Bit-string Functions
SHL Shift left
SHL_* Shift left typed
SHR Shift right
SHR_* Shift right typed
LIMIT Limitation
LIMIT_* Limitation typed
MAX Maximum
MAX_* Maximum typed
Selection Functions
MIN Minimum
MIN_* Minimum typed
SEL Binary selection
SEL_* Binary selection typed

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
163
Functions and Function
Blocks

Standard Functions Functions Remarks

EQ Comparison: =

GE Comparison: =

Comparison GT Comparison: 
Functions LE Comparison: =
LT Comparison: 
NE Comparison: 
*_TO_STRING Type conversion to STRING

CONCAT Extensible concatenation

DELETE Delete substring


EQ_STRING Comparison: = for STRING
FIND Find characters

GE_STRING Comparison: = for STRING


GT_STRING Comparison:  for STRING

Character String INSERT Insert characters


Functions LE_STRING Comparison: = for STRING
LEFT Leftmost characters

LEN String length


LT_STRING Comparison:  for STRING
MID Middle characters
NE_STRING Comparison:  for STRING
REPLACE Replace characters

RIGHT Rightmost characters


·
13.3 Function Blocks
Function blocks are POUs with multiple input and output parameters.
They do have an internal memory. The value that a function block
returns depends on the value of its internal memory. The abbreviation
for function blocks is FB.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


164
Structured Text
Instructions

IEC 61131-3 describes standard function blocks which can be used


while editing your PLC program. According to your hardware and PLC
type it is possible that not all standard function blocks are available or
that firmware function blocks have been added.

Note: Please refer to your hardware documentation or to the specific


help for detailed information.

IEC 61131-3 lists different types of standard function blocks:

Standard Function Blocks Function Blocks Remarks

SR Set Dominant
Bistable Function Blocks
RS Reset Dominant

F_TRIG Falling Edge Detector


Edge Detection Function
Blocks
R_TRIG Rising Edge Detector

CTU Up Counter

Counter Function Blocks CTD Down Counter

CTUD Up-Down Counter

TP Pulse

Timer Function Blocks TON On-Delay Timer

TOF Off-Delay Timer

13.4 Common Functions and Function Blocks


13.4.1 AND Function

The AND function results in a logical 1 at the output when all inputs
are logical 1.

Inputs Output

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
165
Functions and Function
Blocks

13.4.2 OR Function

The OR function results in a logical 1 at the output when one or more


inputs are logical 1.

Inputs Output

13.4.3 EXOR Function

The exclusive OR function results in a logical 1 at the output when only


one of the inputs is logical 1.

Output
Inputs

13.4.4 Inversion / Negation

This function enables you to invert the status of a bit. If the bit is a
logical 1, then it becomes a logical 0, and vice versa.

Input Output

13.4.5 RS Function Block (Reset Dominant)

The RS function block

If input SET to this function block goes True, then output Q1 will also
go True.

If input RESET1 to this function block goes True when input SET is
False, then output Q1 will go False.

When input SET and input RESET1 are both True, then output Q1 is
False. Output Q1 can be made True using input SET only when input
RESET1 is False.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


166
Structured Text
Instructions

13.4.6 SEMA (Software SEMAfore)

Sema is a special function block that cannot be interrupted once it has


started executing. This function block is used to execute operating
system functions.

Input Claim is used to indicate that the function must be executed.


Output Busy will then be True. Output Busy stays True until input
Release goes True. This is used in programs to execute a Print
command, for example.

This function cannot be interrupted until the output Busy is again


False.

13.4.7 SR Function Block (Set Dominant)

The SR function block:

If input SET to this function block goes True, then output Q1 will also
go True.

If input RESET1 to this function block goes True when input SET is
False, then output Q1 will go False.

When input SET1 and input RESET are both True, then output Q1 is
True. Output Q1 can be made False using input RESET only when input
SET1 is False.

13.4.8 CTD Function Block (Decrementer, Count Down)

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
167
Functions and Function
Blocks

This counter counts down from a specified value to 0.

The inputs CD and LOAD and the output Q are of type BOOL. Input PV
and output CV are of type WORD

When the input LOAD becomes True (1), the value of PV will be placed
in CV. When a rising edge is observed on CD, CV is decremented by 1
(one). As soon as CV reaches the value 0, output Q will become True
(1).

The value of CV cannot become less than 0.

13.4.9 CTU Function Block (Increment, Count Up)

This counter counts up from 0 to the specified value.

The inputs CU and RESET and the output Q are of type BOOL. Input PV
and output CV are of type WORD

When the input RESET becomes True (1), the value 0 will be placed in
CV. When a rising edge is observed on CU, CV is incremented by 1
(one). As soon as CV is greater than or equal to the value of PV, output
Q will become True (1).

13.4.10 CTUD Function Block (Combined Incrementer/Decrementer,


Count Up & Down)

This is a combination of an incrementer and a decrementer.

CU is the input that increments the counter by 1, and is edge-sensitive.

RESET is the input that resets the counter. The counter will be
continuously reset for as long as this input is True. This will keep the
output CV (the number of counted pulses) to 0. In order to use the
counter, the RESET input must therefore be False.
Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1
168
Structured Text
Instructions

CD is the input that decrements the counter by 1, and is edge-


sensitive.

LOAD is the input with that causes the counter to be loaded with the
specified value. The counter will be continuously kept at the specified
value for as long as this input is True. This forces the output CV (the
number of pulses still to be counted) to be kept equal to the preset
value PV. In order to use the counter, the LOAD input must therefore
be False.

So, to use this counter, both the input RESET and the input LOAD must
be False. PV is the number of pulses to be counted, specified as an
integer.

QU is the output from the counter which is True when the number of
counted pulses (CV) is greater than or equal to the number of pulses
to be counted (PV).

QD is the output from the counter which is True when the number of
pulses still to be counted (CV) is equal to 0.

CV is the number of counted or still to be counted pulses as integer,


depending on use as incrementer or decrementer.

13.4.11 RTC Function Block (Runtime Clock)

This function is used to set and read the Runtime Clock.

Input Preset Date and Time (PDT) must be loaded on a rising edge of
the Boolean input EN. The Boolean output Q is a copy of the Boolean
input EN.

When EN becomes True (1), the date and time are taken over from PDT
and the clock starts to run. The clock continues to run as long as EN is
True (1). The moment EN becomes False (0, the current date and time
CDT becomes the default value of 1970-01-01-00:00:00

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
169
Functions and Function
Blocks

If no value is supplied for PDT and for as long as EN is True (1), the
function block will return the current date and time in CDT.

13.4.12 TOF Function Block (Delayed Turn-off)

This function provides for a delayed turn-off.

PT is the preset time, and may also be a variable of type TIME.


Q is the output of the timer, and is of type BOOL.
IN is the input of the timer, and is of type BOOL
ET is an output of type Time, and is used to display the elapsed time.

On a rising edge of input IN, the timer output Q goes True; once the
input is again False, the timer starts to run. If input IN becomes True
again before the preset time has elapsed, then output Q stays True.

The timer will restart only when the input IN has again become False.
After the preset time, the timer output Q goes False.

13.4.13 TON Function Block (Timed Turn-on)

This function provides for a delayed turn-on.

PT is the preset time, and may also be a variable of type TIME.


Q is the output of the timer, and is of type BOOL.
IN is the input of the timer, and is of type BOOL
ET is an output of type Time, and is used to display the elapsed time.

The timer starts on a rising edge of input IN. After the preset time, the
timer output Q goes True. If input IN goes False during the timer
duration, then the timer stops. If input IN goes True again, then the
timer restarts.

13.4.14 TP Function Block (Pulse Timer)

This function delivers a pulse to the output.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


170
Structured Text
Instructions

PT is the preset time, and may also be a variable of type TIME.


Q is the output of the timer, and is of type BOOL.
IN is the input of the timer, and is of type BOOL
ET is an output of type Time, and is used to display the elapsed time.

On a rising edge of input IN, the timer output Q goes True and the
timer starts. After the preset time, the timer output Q goes False.

Once input IN has become False and the time has elapsed, the pulse
can be used again.

13.4.15 F_trig Function Block (Falling Edge Trigger)

This function detects the falling edge of a variable.

When the input CLK goes from True to False, output Q gives a short
pulse. To visualize this, a delayed turn-off is placed here immediately
behind the F_TRIG function block.

13.4.16 R_trig Function Block (Rising Edge Trigger)

When the input CLK goes from False to True, output Q gives a short
pulse. To visualize this, a delayed turn-off is placed here immediately
behind the R_TRIG function block.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
171
Structured Text
Instructions

Chapter 14

Structured Text Instructions

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


172
Structured Text
Instructions

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
173
Structured Text
Instructions

14. Structured Text Instructions


The following are some of the commonly used structured text
instructions.

14.1 Instructions
The table below shows the operations in order of priority

Operation Symbol Priority

Parentheses ( expression) Highest priority

Use of functions LN (A), MAX (A,B) etc.

Exponent A**B
EXPT(A,B) AB

Negation -
Complement NOT
Multiply *
Divide /
Modulo MOD
Add +
Subtract -

Compare <, >, <=, >=

Equality =
Inequality <>

Boolean AND &


Boolean AND AND

Boolean exclusive OR XOR

Boolean OR OR Lowest priority

14.1.1 assignment, :=

The assignment is a colon followed by an equals sign := and it enables


us to assign the value of one variable to another variable, or to assign
the outcome of an operation to a variable. The assignment is also to
define the type of the variable, for example:

A := B; Give variable A the value of B


B := C * D; Multiply variable C by D and assign the outcome to B.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


174
Structured Text
Instructions

F := COS(H); Determine the cosine of an angle and assign this value


to F
A := BOOL; Variable A is of type Boolean
C := REAL; Variable C is of type REAL

14.1.2 Using Subprograms

We can call a subprogram. To do this, we use the name of the called


subprogram, followed by the transferred parameters within
parentheses.

Example:

Calling the IL subprogram Calc.

(* Main ST program *)
(* Calculates an analog value from three binary signals *)

Value := Calc (bi0,bi1,bi2);

Subprogram Calc.

In the Calc subprogram, the variables in0, in1 and in2 are defined as
input parameters.
And Temporary is a variable used to store an intermediate result.

Calcprog:
LD in2 (* Input 2 *)
BOOL_TO_INT (* result := 1 or 0 of in2 *)
MUL 4 (* result := 4 x INT of in2 *)
ST Temporary (* store the intermediate result *)

LD in1 (* Input 1 *)
BOOL_TO_INT (* result := 1 or 0 of in1 *)
MUL 2 (* result := 2 x INT of in1 *)
ADD Temporary (* result := 4 x INT of in2 + 2 x INT of in1 *)
ST Temporary (* store the intermediate result *)

LD in0 (* Input 0 *)
BOOL_TO_INT (* result := 1 or 0 of in0 *)
ADD Temporary (* result := 4 x ana (in2) +2 x ana (in1) + ana (in0) *)
ST Calc (* send result back to calling program *)

14.1.3 Using Function Blocks

We can call a function block. To do this, we use the name of the called
function block, followed by the transferred parameters within
parentheses
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
175
Structured Text
Instructions

The result is returned with the function block name followed by a full
stop and the returned parameter.

Example:

Calling the function R_trig in ST in order to detect a rising edge.


We will use input in1
(* trig_in1:= R_trig *)
(* calling the function block from ST *)
trig_in1(in1);

(* Using the result of the rising edge detection to increment a counter


*)
IF trig_in1.Q THEN
count: := count +1;
END_IF;

Obviously, depending on the number of parameters belonging to the


function block in question, the same number of parameters must be
specified.

If not all parameters are specified, then the function block will use the
“old” value or the default value of the unspecified parameter.

Example 1:

(* Outcome: = Value from ADD function block *)


(* The function block adds two values together *)
(* sum: = ADD *)
(* in1:= 10, in 2: = 5 *)
sum(in1,in2);
(* outcome here is 10 + 5 = 15 *)
outcome: = sum.Q;

Example 2:

(* Outcome: = Value from ADD function block *)


(* The function block adds two values together *)
(* sum: = ADD *)
(* in1: = 11, in2 is not specified *)
sum (in1,);
(* because this time only the first parameter is specified, *)
(* the “old” parameter is used as second parameter*)
(* outcome this time is 11 + 5 = 16 *)
outcome: = sum.Q;

However, if this example was executed first of all, then the result
would be 11 + 0 = 11. This is because the default value (0) then gets
used for the unspecified parameter.
Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1
176
Structured Text
Instructions

14.1.4 RETURN Instruction

The Return instruction halts program processing, and takes us back to


the point here the call was issued, for example:

(* Count: = is the input for the counter, Q_Count is the counter output
*)
IF NOT Count THEN
RETURN;
ELSE Q_Count := Q_Count + 1;
END_IF

14.1.5 IF … THEN … ELSIF … THEN … ELSE … END_IF Instructions

In this instruction, the THEN clause is executed when the result of the
IF is True. When the result is False, then the ELSIF is executed, with the
THEN clause executed when the result of the ELSIF is True. When the
ELSIF is False, then the ELSE is executed.

Whenever the ELSIF clause is not included, then a False result from the
IF will cause immediate execution of the ELSE.

IF hand AND NOT (alarm) THEN


level: = hand_level;
ELSIF full THEN
level := max_level;
ELSE level := pulse * 100;
END_IF

(* only an IF – THEN – ELSE *)


IF NOT (max_level) THEN
Valve: = True;
ELSE Valve: = False;
END_IF

(* only an IF – THEN *)
IF max_level THEN
Alarm: = True;
END_IF

14.1.6 CASE … or … ELSE … END_CASE Instructions

The CASE instruction is based on an integer variable. The


corresponding value is looked up in a list with integer variables, and
the corresponding instructions are executed. If no corresponding
integer value is found, then the ELSE instruction, if present, is
executed.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
177
Structured Text
Instructions

In all other cases, no instructions are executed and the CASE


instruction is terminated. Multiple integer values may be combined,
provided they are separated by a comma.

CASE error_code OR
255: error_message := ‘Divide by zero’;
system_stop: = True;
1: error_message: = ‘level too high’;
2.3: error_message: = ‘wrong option’;
ELSE error_message: = ‘unknown error’;
END_CASE

14.1.7 FOR … TO … BY … DO … END_FOR Instruction

The FOR instruction enables us to run through a loop a specified


number of times. Because the FOR loop itself monitors the number of
times the loop is processed, we may not influence this variable. The
variable type used in the FOR and BY must be the same (SINT, INT or
DIN).

The value of the FOR loop variable is tested at the start of the loop. By
default, the variable is incremented by 1 each time the loop is
processed. We can influence this by supplying a BY value.

(* the following codes searches for the word “key” in a list of 100
words *)
(* the variable “position” stores the value of i when the word is found
*)
(* the loop ends when the value of i reaches 101 before the word
“key” is found *)

FOR i: = 1 TO 100 DO
IF word[i]: = ‘key’ THEN
Position: = i;
EXIT;
END_IF
END_FOR

Another possibility is to specify the step size with BY, for example
when we only want to test the even entries in the table. The number
after BY may be any value provided it is the same type as the variable
after the FOR instruction.

FOR i: = 0 TO 100 BY 2 DO
IF word[i]: = ‘key’ THEN
Position: = i;
EXIT;
END_IF
END_FOR
Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1
178
Structured Text
Instructions

14.1.8 WHILE … DO … END_WHILE Instructions

With the While instruction we create a loop which is processed


whenever the condition after the WHILE instruction is True. Is this
condition is False, then the loop is not executed.

In the following example code, the variable Edge is set to False. Each
time the door is opened, a counter is incremented. The program code
ensures that the variable Edge is reset to False each time the code is
processed.

Example:
Edge: = False;

WHILE Door AND NOT (Edge) DO


open: = open + 1;
Edge: = True;
END_WHILE

In the WHILE instruction, the condition is evaluated before execution


of the loop. If it is True, then the WHILE is executed. If the condition is
False, then the WHILE is not executed. Even if the condition becomes
False during execution of the instructions between the WHILE and
END_WHILE, the loop is still processed to the end.

14.1.9 REPEAT … UNTIL … END_REPEAT Instructions

The Repeat instruction creates a loop that must be processed at least


once, because the condition is tested as True or False only at the end
of the loop.

Example:
A: = 0;
REPEAT
A: = A +3;
UNTIL A: = >= 39;
END_REPEAT

If the condition evaluates to True at the end of the loop, then the loop
is processed again. If the condition is False then the program leaves
the loop and continues with the next instruction.

14.1.10 EXIT Instruction

The EXIT instruction is used to halt a FOR, WHILE or REPEAT instruction


before all steps have been processed.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
179
Structured Text
Instructions

The EXIT is mainly used in combination with an IF.

(* with the FOR instruction *)


FOR i: = 1 TO 100 DO
IF word[i] := ‘key’ THEN
Position: = i;
EXIT;
END_IF
END_FOR

(* with the WHILE instruction *)


Edge: = False;

WHILE Door AND NOT (Edge) DO


open: = open + 1;
Edge: = True;
IF Stop THEN
EXIT;
END_IF
END_WHILE

(* with the REPEAT instruction *)


A: = 0;
REPEAT
A: = A +3;
IF B: = 11 THEN
EXIT;
END_IF
UNTIL A: = >= 39;
END_REPEAT

14.1.11 AND, & Instruction

The AND or & instruction executes a logical AND function. With


variables with more bits, a bitwise logical AND function is executed.
IN1 AND IN2, or IN1 & IN2;

(* IN1 AND IN2*)


result: = IN1 AND IN2 AND … AND INn; or result :=
AND(IN1,IN2,...,INn);

(* IN1 = 21845 AND 255 *)


We can use this to extract the first 8 bits from IN1.
result: = IN1 AND 255; or result := AND(IN10.255);

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


180
Structured Text
Instructions

14.1.12 XOR Instruction

The XOR instruction executes an exclusive OR function. The result is


True when only one of the operands is True. With variables with more
bits, a bitwise logical XOR function is executed.
IN1 XOR IN2;

(* IN1 XOR IN2*)


result: = IN1 XOR IN2 XOR … XOR INn;
or
result: = XOR(IN1,IN2,...INn);

(* IN1 = 21845 XOR 255 *)


We can use this to perform a logical XOR on 16 bits.
result: = IN1 XOR 255;
or
result: = XOR(IN10.255);

14.1.13 OR Instruction

The OR instruction executes a logical OR function. With variables with


more bits, a bitwise logical OR function is executed.
IN1 OR IN2;

(* IN1 OR IN2*)
result: = IN1 OR IN2 OR … OR INn;
or
result: = OR(IN1,IN2,...INn);

(* IN1 = 21845 OR 255 *)


We can use this to perform a logical OR on 16 bits.
result: = IN1 OR 255;
or
result: = OR(IN10.255);

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
181
Instruction List
Instructions

Chapter 15

Instruction List Instructions

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


182
Instruction List
Instructions

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
183
Instruction List
Instructions

15. Instruction List Instructions


The following are some of the commonly used instruction list
instructions.

15.1 Instructions
15.1.1 Labels

An instruction may be preceded by a label terminated with a colon (:) .


With the JMP instruction we can jump to a label.

 A label may be no longer than 16 characters.


 The first character must be a letter.
 All other characters may be numerals, letters or the underline _
symbol.

A label may occur only once in an IL program. However, a label may


have the same name as a variable. The difference is that the label
terminates with a colon.

Example:

Begin:
LD Start (*Start button*)
AND Stop (*Stop button*)
JMP Program (*Label Program*)

Program:
LD %IX5 (*Clamp in*)
ST %QX2 (*Clamp cylinder*)

15.1.2 Modifiers

The following Modifiers may be used. Modifiers are used by adding


them to operators without a space.

N negation of a Boolean operand,


( deferred operation,
) execution of the operation (is actually an operator),
C conditional operation.

The N modifier indicates that the next Boolean operand must be


negated.
ANDN %IX4 is interpreted as AND NOT Input 4.

The ( modifier indicates that the result of the operations must be


evaluated up to the ) modifier.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


184
Instruction List
Instructions

AND( %IX5 OR %IX6 ) is interpreted as


AND ( Input 5 or Input 6) with the OR function evaluated before the
AND function is executed.

The C modifier indicates that the operation may only be executed


when the result of the preceding part is True.

LD %IX2 AND %IX7 JMPC Ready is interpreted as jump to label Ready


only if the result of the AND function is True (1) . If this is not the case,
then the jump function is not executed.

15.1.3 The IL Operators and Modifiers

Operator Modifier Operand Description


LD N Variable, constant Load the operand into memory
Store the current result into
ST N Variable
memory
S Boolean variable Set to True (1)
R Boolean variable Reset to False (0)
AND N ( Boolean Logical AND
& N ( Boolean Logical AND
OR N ( Boolean Logical or
XOR N ( Boolean Logical exclusive OR
ADD ( Variable, constant Add
SUB ( Variable, constant Subtract
MUL ( Variable, constant Multiply
DIV ( Variable, constant Divide
GT ( Variable, constant Greater than >
GE ( Variable, constant Greater than or equal >=
EQ ( Variable, constant Equal =
NE ( Variable, constant Not equal <>
LE ( Variable, constant Less than or equal <=
LT ( Variable, constant Less than <
JMP C N LABEL Jump to LABEL
Program or Function block call
CAL C N Name
followed by parameters
Exit POU and return to the calling
RET C N
program….

15.1.4 Load (LD) Instruction

The Load (LD) instruction loads a value into the current result.

The only modifier applicable to the Load (LD) instruction is N (Not).


The operand may be a constant or an internal input or output variable.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
185
Instruction List
Instructions

Examples of the contents of the variable “result” after several LD


operations:

LD False (* result: = False Boolean constant *)


LD True (* result: = True Boolean constant *)
LD 135 (* result: = integer constant 135 *)
LD 116.3 (* result: = real constant 116.3 *)

(* NOTE! Imperial decimalization, i.e. dot, not comma *)

LD t#6ms (* result: = time constant of 6 milliseconds *)


LD Boo_var7 (* result: = Boolean variable *)
LD Ana_var9 (* result: = Analogue variable *)
LD Tmr_var2 (* result: = Timer variable *)
LDN Boo_var3 (* result: = NOT Boolean variable *)
(* In this case, the negated value of Boo_var3 is loaded into the
variable result *)

15.1.5 Store (ST) Instruction

The Store (ST) instruction stores the result of an operation in a


variable. The result is not affected by this operation.

The only modifier applicable to the Store (ST) instruction is N (Not).


The operand may be an internal input or output variable

Examples of several Store (ST) operations are:

Store_Boo:
LD False (* Boolean constant False (0) *)
ST Boo_var7 (* Boo_var7: = False (0) *)
STN Boo_var4 (* Boo_var4: = True (1) *)

Store_ana:
LD 156 (* Constant integer 156 *)
ST ana_var8 (* ana_var8: = 156 *)

Store_tmr:
LD t#9s (* Constant time of 9 seconds *)
ST tmr_var1 (* tmr_var1: = t#9s *)

15.1.6 Set (S) Instruction

The Set instruction sets the Boolean value True in a Boolean variable
when the result of the preceding operation is True.

If the previous operation evaluates to False, then nothing changes.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


186
Instruction List
Instructions

Examples:
LD True (* Boolean constant True (1) *)
S Boo_var2 (* Boo_var2: = True (1) *)

LD False (* Boolean constant True (1) *)


S Boo_var2 (* Boo_var2: = remains unchanged *)

15.1.7 Reset (R) Instruction

The Reset (R) instruction sets the Boolean value False in a Boolean
variable when the result of the preceding operation is False. If the
previous operation evaluates to False, then nothing changes.

Examples:
(* Boo_var1:= True (1) *)
(* Boo_var2:= False (0) *)
LD True (* Boolean constant True (1) *)
R Boo_var1 (* Boo_var1 becomes False (0) *)
R Boo_var2 (* Boo_var2 remains False (0) *)
(* Boolean False not changed by R*)

LD False (* Boolean constant True (1) *)


R Boo_var3 (* Boo_var3 := remains unchanged *)

15.1.8 AND Instruction

The AND instruction enables a logical AND function to be executed.


This operation can only be executed with Boolean variables.

Examples:
LD Boo_var1 (* Boolean variable 1*)
AND Boo_var2 (* Boolean variable 2 *)
ST Boo_var6 (* Boolean variable 6 *)

15.1.9 OR Instruction

The OR instruction enables a logical OR function to be executed. This


operation can only be executed with Boolean variables.

Example:
LD Boo_var1 (* Boolean variable 1*)
OR Boo_var2 (* Boolean variable 2 *)
ST Boo_var6 (* Boolean variable 6 *)

15.1.10 XOR Instruction

The XOR instruction enables an exclusive OR function to be executed.


This operation can only be executed with Boolean variables.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
187
Instruction List
Instructions

Example:
LD Boo_var1 (* Boolean variable 1*)
XOR Boo_var2 (* Boolean variable 2 *)
ST Boo_var6 (* Boolean variable 6 *)

As opposed to the regular OR function, the result of the exclusive OR


is only a logical 1 when only _one_ of the inputs is logical 1.

15.1.11 JuMP, JMP Instruction

The jump (JMP) instruction enables us to jump through the program,


with or without conditions.

The jump instruction should be used with appropriate reticence,


because it lets us leap back and forth throughout the program.

Excessive and unregulated use of the jump instruction will certainly


earn no beauty prizes for legibility and service-friendliness.

In the following example, depending on the value in the variable


“Option”, an output is switched on, and processing jumps to a certain
program block.

At the end of each program block, processing jumps back to label


Option. The variable “option” can contain the value 0, 1 or 2. The
outputs are defined as Option_0, Option_1 and Option_2.

Option:
LD Option (* Option can have the value 0, 1 or 2 *)
EQ 1 (* If the variable Option has the value 1 *)
JMPC Option1 (* If True, jump to label Option1 *)

LD Option (* Option can have the value 0, 1 or 2 *)


EQ 1 (* If the variable Option has the value 1 *)
JMPC Option2 (* If True, jump to label Option1 *)

LD True (* Load the value True into memory *)


ST Option_0 (* Set output 0 on *)
JMP Block_0 (* Jump to program block 0 *)

Option1:
LD True (* Load the value True into memory *)
ST Option_1 (* Set output 1 on *)
JMP Block_1 (* Jump to program block 0 *)

Option2:
LD True (* Load the value True into memory *)
ST Option_2 (* Set output 2 on *)
JMP Block_2 (* Jump to program block 2 *)
Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1
188
Instruction List
Instructions

Block_0:
LD xxx (* Start of program block 0 *)

LD False (* End of program block 0 *)


ST Option_0 (* Reset output 0 *)
JMP Option (* Jump back to label Option *)

Block_1:
LD xxx (* Start of program block 1 *)

LD False (* End of program block 1 *)


ST Option_1 (* Reset output 1 *)
JMP Option (* Jump back to label Option *)

Block_2:
LD xxx (* Start of program block 2 *)

LD False (* End of program block 2 *)


ST Option_2 (* Reset output 2 *)
JMP Option (* Jump back to label Option *)

15.1.12 CAL Instruction

The CAL instruction calls a function block or program. Any parameters


that must be transferred to the function block must be stated between
brackets after the call.

CAL CTUInst(CU := VarBOOL1, RESET := VarBOOL2, PV := VarINT1)

The results must be processed with the aid of LD and ST.


LD CTUInst.Q
ST VarBOOL3
LD CTUInst.CV
ST VarINT2

The meaning of the various inputs and outputs of this CounTUp


(increment) function block:

CTUInst the name of the function.


CU the counter input, the value of PV will be incremented by 1 on the
rising edge of this input.
RESET the reset input, PV will be set to 0 on the rising edge.
IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
189
Instruction List
Instructions

PV the counted pulses, The current counter value.


Q output, this output goes high (1) when the counted value is greater
than or equal to CV.
CV counter preset, The counter value that will set Q high (1).

The identifiers VarBool1, VarBool2 are inputs. VarBool3 is an output.


These VarBools (1.2 and 3) are of type BOOL.

The identifiers VarINT1 and VarINT2 are variables of Type INTeger.


CTUInst is the name of the counter and is of Type CTU.

In order to use output Q in the IL program, it has to be read as follows:

LD CTUInst.Q (* Read the Status of CTU output Q *)


ST VarBool3 (* Store the status in VarBool3 *)

The same applies to the counted pulses; these must be read as


follows:

LD CTUInst.PV (*Read the current value of CTU counter output


PV *)
ST VarINT1 (* Store this value in VarINT1 *)

The program then looks like this:

15.1.13 CAL Program

As long as the program has still to be executed, it must be called via


the CAL instruction.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


190
Instruction List
Instructions

Mainprog:

LD On_Off (* Load the status of On_Off *)


JMPC On (* If True, jump to label On *)
JMP Off (* If False lump to label Off *)

On: (* Label On *)
CAL Control (* Start subprogram Control *)
JMP Off (* Jump to label Off *)

Off: (* Label Off *)

Subprogram Control:

LD Button (* Load the status of button *)


ST Lamp: (* Place the status in Lamp *)
(* Button 1, Lamp 1, Button 0, Lamp 0 *)

15.1.14 RET Instruction

The RET instruction is used to jump out of a called subprogram and


return to the program that called the subprogram. Any lines after the
RET in the subprogram will no longer be executed.

Mainprog:
….
LD Option (* Load the contents of the variable option *)
CAL Decide (* Call subprogram Decide *)
ST Outcome (* Outcome: = result from Decide *)

Subprogram “decide”

Option:
LD Option (* Option can have the value 0 False or 1 True *)
JMPC Option1 (* If True, jump to label Option1 *)
JMP Option0 (* If False Jump to label Option0 *)

Option0:
LD True (* Load the value True *)
ST Option_0 (* Set output 0 on *)
RET (* End of the subprogram *)

Option1:
LD True (* Load the value True into memory *)
ST Option_1 (* Set output 1 on *)
RET (* End of the subprogram *)

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
191
Instruction List
Instructions

15.1.15 Working With “( )”

Parentheses can be used to influence the order of processing.

The example program below demonstrates the following evaluation:

Result: = a1 + ( a2 * (a3 –a4) * a5) + a6

LD a1 (* result: = a1 *)
ADD( a2 (* deferred addition, result := a2 *)
MUL( a3 (* deferred multiplication, result := a3 *)
SUB a4 (* result: = a3 – a4 *)
) (* execute multiplication, *)
(* result: = a2 * (a3-a4) *)
MUL a5 (* result: = a2 * (a3-a4) * a5 *)
) (* execute deferred addition *)
(* result: = a1 + (a2 * (a3 – a4) * a5) *)
ADD a6 (* result: = a1 + (a2 * (a3 – a4) * a5) + a6 *)
ST Result (* Store the calculated result in Result *)

You need to use the parentheses to force IL to behave according to the


generally accepted Boolean rules.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


192
Instruction List
Instructions

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
193
Ladder Diagram Symbols

Chapter 16

Ladder Diagram Symbols

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


194
Ladder Diagram Symbols

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
195
Ladder Diagram Symbols

16. Ladder Diagram Symbols


The following are some of the commonly used ladder diagram
symbols.

16.1 Symbols

16.1.1 Normally Open Contact

This symbol indicates that the corresponding input signal is tested for
a logical 1 (TRUE). If this is the case, then this condition is “true”.

16.1.2 Normally Closed Contact

This symbol indicates that the corresponding input signal is tested for
a logical 0 (FALSE). If this is the case, then this condition is “true”.

16.1.3 Coil

The coil here signifies not only the coil of a relay or solenoid switch,
but also a variable of 1 bit.

This symbol indicates that the corresponding output signal is equal to


the result of the conditions included in this network.

16.1.4 Negated Coil

This symbol indicates that the corresponding output signal is the


inverse of the result of the conditions included in this network.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1


196
Ladder Diagram Symbols

This means that the result of the conditions is negated. If the logical
result is a 1 (TRUE) then the output is a logical 0 (FALSE), and vice
versa.

16.1.5 Set Coil

This symbol indicates that the corresponding output signal is logical 1


(TRUE) if the condition included in this network result in “true”. Even if
the conditions subsequently change, the output remains logical 1
(TRUE). The output can only be made logical 0 (FALSE) by a Reset Coil,
otherwise it stays logical 1 (TRUE) for as long as the control program
continues to run.

16.1.6 Reset Coil

This symbol indicates that the corresponding output signal is logical 0


(FALSE) if the conditions included in this network result in “true”. Even
if the conditions subsequently change, the output remains logical 0
(FALSE).

16.1.7 Rising Edge

Function block R_TRIG is used to detect a rising edge.

16.1.8 Falling Edge

Function block F_TRIG is used to detect a falling edge.

IEC 61131-3 CoDeSys version 3.5 Textbook Version 1.1 • Festo Didactic
197
Ladder Diagram Symbols

16.1.9 Box with AND Function

This AND function box with ENable function is executed whenever the
input EN (enable) is TRUE. The previous result persists when the EN is
FALSE. If the output of the AND function is TRUE, and the EN
subsequently becomes FALSE, then the output of the AND function
remains TRUE. If the EN (enable) again becomes TRUE and one of the
inputs of the AND function is FALSE, then the output of the AND
function will also become FALSE.

Select AND and hit F2 to open the Input assistant, then you can select
from all other available functions. They are all equipped with an
ENable capability.

Festo Didactic • IEC 61131-3 CoDeSys Textbook Version 1.1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy