Sunteți pe pagina 1din 56

Training Documents: Programming the WAGO Controller

based on IEC 61131-3 using CoDeSys 2.3

Supplement to WAGO Seminar 404: Basics of


programming IEC 61131 – 3 using CoDeSys 2.3 CAA
Also go to www.wago.de
Glossary

Glossary

Controller A controller is a component of the WAGO-I/O-SYSTEM. It


contains the CPU and the fieldbus link to the system. Similar to a
PLC or DDC, a controller can be programmed.

WAGO-I/O-SYSTEM The 750/753 Series WAGO-I/O-SYSTEM is a PLC system of


modular design from WAGO.

Nodes Nodes, as used in this document, are combinations of WAGO-


I/O-SYSTEM input and output components with a controller.

K-Bus The K bus (also called internal data bus) is a data link within the
PLC. The task of the K bus is to link the CPU (bus coupler) with
the input and output components (nodes) of the PLC.

CoDeSys CoDeSys corresponds to the WAGO-I/O-PRO CAA software and


is the programming tool for WAGO controllers. A demo version of
CoDeSys is available at www.wago.com. You can order the full
version, including USB programming cable, under item number
759-333/000-923.

II
Table of contents

Table of contents

Glossary ............................................................................................................................ II
Table of contents .............................................................................................................. III
List of Figures ................................................................................................................... V
1 Introduction ................................................................................................................ 1
1.1 Goal ................................................................................................................... 1
1.2 Document Structure ........................................................................................... 1
2 The Theory ................................................................................................................ 2
2.1 Initial Commissioning of a WAGO Node ............................................................. 2
2.1.1 Creating a Project in WAGO-I/O-PRO(CoDeSys) ....................................... 2
2.1.2 Defining Communication Parameters ......................................................... 4
2.1.3 Creating the PLC Configuration .................................................................. 6
2.1.4 The First Program..................................................................................... 10
2.2 Writing/Forcing values ...................................................................................... 13
2.3 Variable Declaration ......................................................................................... 14
2.3.1 Variable Name/Designator ........................................................................ 15
2.3.2 Variable Classes....................................................................................... 16
2.3.3 Variable Type ........................................................................................... 17
2.3.4 Initial Value ............................................................................................... 18
2.3.5 Address .................................................................................................... 19
2.3.6 Comment .................................................................................................. 20
2.3.7 Constant, Retain and Persistent Variables................................................ 20
2.4 Relational Operators ........................................................................................ 22
2.5 Functions, Function Blocks and Programs........................................................ 24
2.6 Set/Reset Command ........................................................................................ 27
2.7 Timers in IEC 61131-3...................................................................................... 29
2.7.1 Timer Off-Delay TOF ................................................................................ 29
2.7.2 Timer On Delay TON ................................................................................ 29
2.7.3 Pulse Timer (TP) ...................................................................................... 30
2.7.4 Time Declaration ...................................................................................... 30
2.8 Computational Operators ................................................................................. 31
2.9 Counters in IEC 61131-3 .................................................................................. 32
2.9.1 CTU Counter ............................................................................................ 32
2.9.2 CTD Counter ............................................................................................ 32
2.9.3 CTUD Counter .......................................................................................... 33

III
Table of contents

3 Program Examples .................................................................................................. 35


3.1 The Dimmer Function Block ............................................................................. 35
3.2 The Latching Function Block ............................................................................ 36
4 Exercises ................................................................................................................. 38
4.1 Exercise 1: Vehicle Traffic Tunnel .................................................................... 38
4.2 Exercise 2: Service Water Solar Installation ..................................................... 39
4.3 Fan Control ...................................................................................................... 40
4.4 Parking Lot Monitoring ..................................................................................... 42
4.5 Room Automation............................................................................................. 43
5 Solutions .................................................................................................................. 45
5.1 Exercise 1: Vehicle Traffic Tunnel .................................................................... 45
5.2 Exercise 2: Service Water Solar Installation ..................................................... 47
5.3 Exercise 3: Fan Control .................................................................................... 47
5.4 Exercise 4: Parking Lot Monitoring ................................................................... 48
5.5 Exercise 5: Room Automation .......................................................................... 49

IV
List of Figures

List of Figures

Figure 2.1: Creating a new project ..................................................................................... 2


Figure 2.2: Selecting the target system .............................................................................. 2
Figure 2.3: Input mask for selecting the programming language ........................................ 3
Figure 2.4: Programming window ...................................................................................... 3
Figure 2.5: Communication parameters window ................................................................ 4
Figure 2.6: Communication parameters ............................................................................. 5
Figure 2.7: Modifying the values for the communication parameters.................................. 5
Figure 2.8: Opening PLC Configuration ............................................................................. 6
Figure 2.9: Append subelement ......................................................................................... 6
Figure 2.10: PLC configuration .......................................................................................... 7
Figure 2.11: Terminal selection .......................................................................................... 8
Figure 2.12: Designation for digital inputs .......................................................................... 9
Figure 2.13: Adding a module to the project..................................................................... 10
Figure 2.14: Selecting the module type ............................................................................ 10
Figure 2.15: Adding an input ............................................................................................ 11
Figure 2.16: Labeling the input/output .............................................................................. 11
Figure 2.17: Login ............................................................................................................ 12
Figure 2.18: Status .......................................................................................................... 12
Figure 2.19: Calling up Declare Variable.......................................................................... 14
Figure 2.20: Alternative call-up of Input Assistant for variable declaration........................ 15
Figure 2.21: Defining variable names............................................................................... 16
Figure 2.22: Setting the variable class ............................................................................. 16
Figure 2.23: Variable type selection ................................................................................. 17
Figure 2.24: Initial value ................................................................................................... 19
Figure 2.25: Address ....................................................................................................... 19
Figure 2.26: Comment ..................................................................................................... 20
Figure 2.27: Retain and Persistent................................................................................... 20
Figure 2.28: Variable declaration ..................................................................................... 21
Figure 2.29: Input Assistant for operators ........................................................................ 22
Figure 2.30: Overwriting the operator value ..................................................................... 23
Figure 2.31: Accepting operands that have been overwritten........................................... 23
Figure 2.32: Input Assistant ............................................................................................. 24
Figure 2.33: Instantiation of a function block .................................................................... 24
Figure 2.34: Illustrating an entity ...................................................................................... 25

V
List of Figures

Figure 2.35: Simulation .................................................................................................... 25


Figure 2.36: Declaration through instantiation .................................................................. 25
Figure 2.37: Using variables declared through instantiation by pressing F2 ..................... 26
Figure 2.38: Marking a point ............................................................................................ 27
Figure 2.39: Generating a Set command ......................................................................... 27
Figure 2.40: Generating a Reset command ..................................................................... 28
Figure 2.41: SR-RS priority .............................................................................................. 28
Figure 2.42: Basic calculations such as addition, subtraction, multiplication, division....... 31
Figure 2.43: Computing rules........................................................................................... 31
Figure 3.1: Principle behind dimming ............................................................................... 35
Figure 3.2: Circuit diagram for the Dimmer function block ................................................ 35
Figure 3.3: Connection of the function block .................................................................... 36
Figure 3.4: Diagram of a latching relay ............................................................................ 36
Figure 3.5: Circuit diagram of the Fb_LatchingRelay function block ................................. 37
Figure 3.6: Calling up the function block .......................................................................... 37
Figure 4.1: System setup in the vehicle traffic tunnel ....................................................... 38
Figure 4.2: System layout of a service water solar installation ......................................... 39
Figure 4.3: Global variables for the service water solar installation .................................. 39
Figure 4.4: Layout of a parking lot.................................................................................... 42
Figure 4.5: Layout diagram, room automation.................................................................. 43
Figure 4.6: Creating a function block................................................................................ 44
Figure 5.1: Solution to vehicle tunnel exercise ................................................................. 45
Figure 5.2: Display of execution sequence numbers in CFC ............................................ 46
Figure 5.3: Solution for service water solar installation exercise ...................................... 47
Figure 5.4: Solution for fan control exercise ..................................................................... 47
Figure 5.5: Solution for parking lot exercise ..................................................................... 48
Figure 5.6: Solution for room automation, exercise 5a ..................................................... 49
Figure 5.7: Solution for room automation, exercise 5b ..................................................... 50

VI
Introduction

1 Introduction
This document is intended for all user who wish to acquire skills in programming
the 750 Series controller. The procedure for programming the controller using
WAGO-I/O-Pro CAA (CoDeSys) is explained step by step in the individual
sections.

1.1 Goal
The goal of this document is to help you take your first steps and gain confidence
in programming based on IEC 61131. The following pages contain a description of
how to develop a small program and load this to the control system.

This seminar is therefore a supplement to the WAGO Seminar 404 "Basics of


Programming IEC 61131-3 Using CoDeSys 2.3 CAA".

The sections of this document are set up such that you can use it as a reference
"book" to look up special topics. This can help you refresh your memory long after
the seminar. This seminar is not, however, a substitute for the WAGO Seminar
404 "Basics of Programming IEC 61131-3 Using CoDeSys 2.3 CAA.". And in the
same sense, reading through this document does not qualify you to take part in
Seminar 404. This document is merely an additional, optional offer in addition to
the seminar to help reinforce the learning effect of the course.

We would be glad to hear any comments or suggestions for improvement that you
may have. Send these to training@wago.com.

1.2 Document Structure


This document is divided into three parts.The first part describes the theory that is
required for programming, meaning that instructions are given in this section that
explain the individual steps involved in programming a WAGO controller.
Examples with screenshots are given to illustrate the instructions.
The second section contains exercises that you can do to apply your newly
acquired knowledge. The exercises become more difficult in the ascending order
of the section numbers.
The solutions to the exercises are given in the third and last part of this document.
The user can check his/her results against the solutions for the sample exercises.
You should always try to complete your solution before you check the solutions for
the exercises.

1
Program Examples

2 The Theory
This section uses examples to describe the theoretical concepts required for
programming the WAGO controller.

2.1 Initial Commissioning of a WAGO Node


After installation the WAGO-I/O-PRO CAA (CoDeSys) software is stored with the
WAGO target files at "C:\Program Files\WAGO Software\CoDeSys
V2.3\Codesys.exe". You can start the program by double-clicking on Codesys.exe.
You can create a shortcut link on your desktop to start the program more quickly
by simply double-clicking on the shortcut icon.

2.1.1 Creating a Project in WAGO-I/O-PRO(CoDeSys)

After starting, you must first create a new project by clicking on the symbol
(or: File/New).

Figure 2.1: Creating a new project


After this, select the target system. Here, you select the controller to be used, e.g.,
750-881.

Figure 2.2: Selecting the target system

2
Program Examples

When you click on OK the following input mask appears:

Figure 2.3: Input mask for selecting the programming language

In this window you can select the module type and the programming language for
the main program. The language CFC (Continuous Function Chart) is used in this
course. The name of the main program must be PLC_PRG. PLC_PRG is the
designation for the starting module, from which you can then create other links.
The programming window is displayed when click on the OK button.
Inputs and outputs

Operators (AND, OR,


…)

Space for local variables

Programming window

Figure 2.4: Programming window


Before you begin with programming you should first save the project under a name

of your choice by clicking and define further settings for communication


between CoDeSys and the controller. This includes configuring of communication
and PLC configuration.

3
Program Examples

An option exists for testing a program without the hardware. You must activate the
Simulation mode for this via Online Simulation. You can change the value for
the variables in the program that is logged in by double-clicking on the value. The
new value is accepted after selecting Write online values.

2.1.2 Defining Communication Parameters


You do not need to define communication parameters if the program is only to be
used in simulation. If this is the case, you can skip this item.
There are basically two options for communication with the hardware. For a serial
link you must have the 750-923 programming cable. As an alternative you can
also set up a link via the fieldbus. With the 750-881 controller this would be, for
example, the Ethernet TCP/IP.

Select Online Communication Parameters… to open the configuration


window.

Figure 2.5: Communication parameters window

To create a new communication channel click New. Here, you can select the
device drive and a name.
If you specify a serial link here (Serial RS232), you must set the correct COM port
in the next step. COM1 is normally set as the default. The WAGO USB
communication cable takes a virtual COM port. You can find the number used for
this link using the device manager.

4
Program Examples

Figure 2.6: Communication parameters

If you want to set a different COM port, use the up and down arrows to move up
and down in the list.

Figure 2.7: Modifying the values for the communication parameters

The method for creating an Ethernet link is described in the manual. The Ethernet
link is also presented and explained in the course "Basics for Programming IEC
61131-3 using CoDeSys 2.3 CAA."

5
Program Examples

2.1.3 Creating the PLC Configuration


To create the control system configuration, first click on PLC Configuration under
the fourth tab (Resources).

Figure 2.8: Opening PLC Configuration


The PLC configuration window then opens, in which you can expand the hardware
configuration by clicking on the plus sign next to Hardware configuration. The
following dialog window opens when you right-click on K-Bus:

Figure 2.9: Append subelement

6
Program Examples

Here, click on Append subelement to open an input mask for selecting hardware.
Add

Figure 2.10: PLC configuration

Two different situations must be considered at this point:

Situation 1, offline: Hardware is not yet available. In this case, the modules must
be configured individually. To do this, click on the plus symbol to add I/O system
components to the PLC configuration. These components are available in a folder,
sorted by their function. Once you have marked the required hardware you can
add it to the PLC configuration by clicking on the double right arrow (see Figure
2.11). Note here that the quantity and order of the components for the PLC
configuration must correspond to the actual setup.

7
Program Examples

Subelement main groups

Figure 2.11: Terminal selection

Situation 2, online: The hardware is already available and ready for operation
(power supply applied). In this case, you can export the PLC configuration from
the node. The WAGO-I/O-CHECK software must be installed for this and the
communication parameters defined properly as described in Section 2.1.2. To
import the hardware open the Configuration tab and click on the magnifying glass
. This initiates export of the node in the background by WAGO-I/O-CHECK. This
does away with the need for manual configuration and, thus, rules out any
potential for errors. You can order WAGO-I/O-CHECK under item number 759-
302.

If the hardware cannot be imported, either the communication parameters and/or


the link are not correct. If this happens, repeat the steps described in Section 2.1.2
and then re-check the link.

After you configure the hardware you must name the hardware inputs and outputs
to be used in the program. To do this, click on the tab Inputs/Outputs. The
following allocation is offered:

Type of signal Name


Digital input Digital_Input_1, Digital_Input_2 …
Digital output Digital_Output_1, Digital_Output_2 …
Analog Input Thermocouple Rawvalue_Temperature_1 …
Analog input Analog_Input_1 …
Analog output Analog_Output_1 …

8
Program Examples

Below is an example of how the digital inputs can be designated:

Figure 2.12: Designation for digital inputs


After assigning names to the inputs/outputs, close PLC configuration by clicking on
OK. Assigning of addresses to the inputs/outputs is conducted automatically and
is adapted as and if required by the software.

Note: The project should be stored by this time at the latest.

9
Program Examples

2.1.4 The First Program


You can begin programming once you have finished with PLC configuration. For
this, click on the first tab PDUs at the bottom left in CoDeSys and open the main
program by double-clicking on PLC_PRG. You can select a module by clicking the
corresponding system in the toolbar and then place it in the programming window.

Click, release and drag the pointer to the


programming window and place it at the desired
position by clicking

Figure 2.13: Adding a module to the project

An AND operator is intially indicated. If the text in the operator symbol is


highlighted, the function (of the operator) can be changed to match one of the
selection options by simply changing the text, or using key F2 and the Input
Assistant:

Marking and changing


the text, or calling up
Input Assistant by

Figure 2.14: Selecting the module type

10
Program Examples

In this case, an OR operator is selected instead of the AND operator. The operator
must be connected to variables at the inputs and outputs. To do this, position the
inputs and outpus as required and link them to the operator. Use the mouse
pointer for this to "draw" a link to the input and output connection.

Click, release and drag the pointer to


the programming window and position
the item

Holding down the mouse key, move the


pointer from the operator connection to the
input connection (or vice versa) to make a link

Figure 2.15: Adding an input

You must assign names to the inputs and outputs after you have connected them.
To do this, mark the three question marks indicated for the input/output and assign
a name by pressing F2 to obtain the Input Assistant. The following program is then
displayed:

Figure 2.16: Labeling the input/output

11
Program Examples

The program is now complete and can be translated and then tested in the
simulation. You must activate the simulation mode for this via Online
Simulation. The program can be translated via Online login.

Login

Figure 2.17: Login


Now, you must start the program. This is done via Online Start. The bottom
status bar indicates whether the program is running.

Status
indication

Figure 2.18: Status

You can now change the value for the variables in the program that is logged in by
double-clicking on the value. The new value will not be accepted, however, until
you click Write online values.
If the project is to be loaded to a controller, the simulation mode must be
deactivated (no check-mark next to Online Simulation). As in simulation, you
must now log in and start the program. If the program is in the controller and the
has been started, you can test the program by setting the input signals for the
node.

12
Program Examples

2.2 Writing/Forcing values


The processing cycle for a PLC is illustrated below:

Read Inputs Code processing Write Outputs

One or more variables are set one time to user-defined values using the command
Online/ Write values. This is done to change the values of variables.

Procedure:
Login and testing via
Online/ Login and
Online/ Start

Change the displayed


status by double-clicking
on the variable status

Write values via Online/


Write values A change in color to blue indicates that the
variable value is TRUE.

13
Program Examples

Forcing of values is used to permanently change variable values when a program


is running on the controller.
One or more variables are set to user-defined values using the command
Online/Force values. In the runtime system, the values are set both at the
beginning and at the end of the cycle:

Read Force Code Force Write


inputs values processin values outputs

Forced values are listed in a table. You can open this list using Online
Write/Force dialog. You can click on a forced value to cancel forcing and restore
the value to its original setting.

2.3 Variable Declaration


Variables represent memory space in the controller and can be read or written.
Variables are also required to save values and results.
It is recommended to use the CoDeSys Input Assistant for variable declaration. To
do this, enter the name of the variable in the CFC display mode after marking the
question marks. The Input Assistant for variable declaration then opens when you
press Enter.

Enter the text and confirm


the name by pressing
Enter.

Figure 2.19: Calling up Declare Variable

Using Declare Variable you can define (declare) the name, class, type, initial
value, address, retain and a comment. The information for class, name and type is
required. All other information is optional, or is yielded from project requirements.

As an alternative, you can call up the Input Assistant under Edit, or by pressing
Shift + F2.

14
Program Examples

Figure 2.20: Alternative call-up of Input Assistant for variable declaration

2.3.1 Variable Name/Designator

Please note the following for the designator, i.e., the variable name:

• The variable name must begin with a letter (character). Digits can be
entered after this first letter.
• The variable name may not contain any spaces, German Umlauts (ä, ö, ü)
or special characters (ß, #, @, Ω, €, etc.).
• There may be no double declarations or identical key words. Key words are
all names and designations used by the programming system, such as
AND, OR, END_VAR. A complete list of these is given in CoDeSys Help
under "Key words".
• No distinction is made between upper and lower case spelling of variable
names, meaning that EIN1, Ein1 and ein1 are all the same variable.
• Underscores are taken as a valid character, for example "A_BCD" and
"AB_CD" are interpreted as different designators.
• Consecutive underscores are prohibited.
• There is no limit on the number of characters (length) in the variable name.

15
Program Examples

Enter the text and confirm


the name by pressing
Enter.
Figure 2.21: Defining variable names

2.3.2 Variable Classes


Classes may be local, input, output, input/output and global variables.

Figure 2.22: Setting the variable class

Local Variables (VAR)


If a variable is declared in a module, e.g., PLC_PRG, it is a local variable, meaning
it is valid only in this module.

Input Variables (VAR_INPUT)


All variables used as input variables for a module are declared as VAR_INPUT,
meaning the value of the variable can also be indicated on call-up at the call-up
point. An input variable is only valid locally.

Output Variables (VAR_OUTPUT)


All variables used as output variables for a module are declared as
VAR_OUTPUT, meaning these values are fed back to the calling module, where
they can be queried and used further. An output variable is only valid locally.

16
Program Examples

Input/Output Variables (VAR_IN_OUT)


VAR_IN_OUT is used to declare all variables used as input and output variables
for a module. These variables are only valid locally.

Global Variables (VAR_GLOBAL)


A global variable is valid in every module of
a project. This variable is therefore not
declared in the header of a program
module, but at a central location. This
location is the window for global variables.
This window can be accessed via the tab
"Resources.".

Resources
Resources tab

Fig. 5: Global variables window

2.3.3 Variable Type

Call up selection

2
Mark and accept by
double-clicking

Figure 2.23: Variable type selection

17
Program Examples

Different variable types are available, based on the application. The IEC 61131-3
standard contains elementary data types whose possible value range and number
of bits are described below:

Type Description Length(Bit) Range


BOOL single bit 1 0, 1 (FALSE,
TRUE)
BYTE Bit sequence with length 8 8 0 - 255
WORD Bit sequence with length 16 16 0 - 65535
DWORD Bit sequence with length 32 32 0 - 4294987295
SINT Short integer 8 -127 - 127
INT Integer 16 -32768 - 32767
DINT Double integer 32 -2147483648 -
2147483647
USINT Short integer without sign 8 0 - 255
UINT Integer without sign 16 0 - 65535
UDINT Double integer without sign 32 0 - 4294987295
REAL Real number 32
LREAL Long real number 64
TIME Duration (time)
DATE Date Explanation given at
TIME_OF_DATE Time of day corresponding point
DATE_AND_TIME Date and time
STRING String

2.3.4 Initial Value


Variables have the value 0, or FALSE, when a project is started. It may be
meaningful to start with a different variable value, however.

Example BOOL: TRUE or FALSE


Example Integer: value between -32768 and 32767

This makes the EMERGENCY OFF button a break contact, for example. If the
break contact is assigned to a variable with an initial value of TRUE, the hardware
signal and variable value concur when the project is started.

18
Program Examples

Figure 2.24: Initial value


The required value of 12.9 is entered in the bar Initial value.

2.3.5 Address
A variable can also be assigned to a hardware address. This creates a link
between the variable and an I/O point of the hardware, or to a memory location
(marker).

Figure 2.25: Address

Direct assignment of addresses to variable is meant to prevent the possibility of


declaring identical addresses. Assigning of addresses should therefore be left to
the software by leaving the "Address" bar blank.

19
Program Examples

2.3.6 Comment
You can add a comment text to help describe or explain the variable in this bar.
Comments are very valuable for project documentation.

Figure 2.26: Comment

2.3.7 Constant, Retain and Persistent Variables

Figure 2.27: Retain and Persistent

Retain variables keep their values after an abnormal termination as well as after
switching the control on or off regularly. Work is continued with the saved value
when the program is restarted. An example of an application for this is an item
counter in a production line, which continues counting where it left off after a
power failure. All other variables are re-initialized, either with their initialized
values, or with the standard initialization values. Retain variables are, however, re-
initialized for Reset Cold, Reset Original and, in contrast to persistent variables,
on a new program download.

Persistent variables keep their values only after a renewed download (Online
Load) but not, in contrast to retain variables, after Online Reset, as they are not
stored in the retain sector. If persistent variables should also keep their previous
value after a control system failure, they must also be declared as VAR RETAIN.
An application example of a persistent retain variable would be an operating hours
counter, which should resume counting after a power failure.

20
Program Examples

x = value retained - = value is re-initialized

Online command VAR VAR VAR VAR RETAIN


RETAIN PERSISTENT PERSISTENT
Reset - x - x
Reset Cold - - x x
Reset original - - - -
Load (=Download) - - x x
Online change x x x x
see: CoDeSys Help

Constant variables are variables that cannot be changed in the ongoing program.
The number π, for example, could be declared as a constant to use it to perform
calculations in the program.

Variable declaration of the local variables is provided for in the declaration window
for each module.

Variable declaration

Figure 2.28: Variable declaration

21
Program Examples

2.4 Relational Operators


Various operators are available in the IEC61131-3 standard and, thus, also in the
CoDeSys programming system.

After marking,
press F2 for the
Input Assistant

Operands 2

Figure 2.29: Input Assistant for operators

GT (Greater Than)
Result1 = 1, when Operand1 > Operand2, otherwise 0

GE (Greater Equal)
Result2 = 1, when Operand1 >= Operand2, otherwise 0

EQ (Equal)
Result3 = 1, when Operand1 = Operand2, otherwise 0

LE (Lower Equal)
Result4 = 1, when Operand1 <= Operand2, otherwise 0

LT (Lower Than)
Result5 = 1, when Operand1 < Operand2, otherwise 0

NE (Not Equal)
Result6 = 1, when Operand1 is not equal to Operand2,

22
Program Examples

You must overwrite the values for the operands in the logged-in program in order
to test for proper functioning of the operators. To do this, call up an editor.

Double-click here to open the editor

Enter the
new value

Figure 2.30: Overwriting the operator value

The new value is then entered as input. The value is not accepted, however, until
the menu item Online/Write values has been called up.

Accepting the (new)

Figure 2.31: Accepting operands that have been overwritten

23
Program Examples

2.5 Functions, Function Blocks and Programs


To add a function block, you must first add a module to the project (Fig. 2.12).
After this, call up the Input Assistant by pressing F2.

A list of items, including available modules


(function blocks) and other elements is
displayed. The standard function blocks are
provided with the standard library
(Standard.lib).

Figure 2.32: Input Assistant

Function blocks and functions are not integrated into the program module in the
same manner as operators. The procedure for function blocks and functions is
designated as instantiation. This involves creating a copy of the module from the
library that is then used under a different name in the project (program module).
This is illustrated below:

Chosen name of the entity

Figure 2.33: Instantiation of a function block

24
Program Examples

A copy of the standard function


block RS is created with the name
"myRS_FB".

The input and output variables are also


recognized through instantiation in the
format: Entity name. Name of input or
output and can be used in this form.

Figure 2.34: Illustrating an entity

Figure 2.35: Simulation

VAR_INPUT and VAR_OUTPUT for the entity can be used as follows.

Declarations yielded from


instantiation.

Figure 2.36: Declaration through instantiation

The declarations can be used the same as other variables when written the same.

25
Program Examples

Figure 2.37: Using variables declared through instantiation by pressing F2

This procedure saves both time and storage space.

26
Program Examples

2.6 Set/Reset Command


You can perform permanent setting(S) or resetting(R) of an output either using an
SR or an RS function block (see Standard Function Blocks in IEC61131-3). An
output can also be permanently set using the S command and permanently reset
using the R command.

First, set up a project in the manner shown here. Then, mark the given point.

Click the point to mark it


Figure 2.38: Marking a point

Using the menu item Set/Reset you can modify a simple allocation as an allocation
with a stored value.

Generate a Set command


using the menu item
Set/Reset

Figure 2.39: Generating a Set command

27
Program Examples

A corresponding procedure is used for Reset, except that you must double-click
the menu item Set/Reset after marking the point.

Click twice to
generate a Reset
command

Figure 2.40: Generating a Reset command


Clicking on the S/R symbol again will generate a simple command again.

Note:
As with the RS(SR) flip-flop function block, a priority response occurs in the state
when S and R are simultaneously TRUE, which is actually prohibited. The
command that is subordinate, relative to the same output, will prevail.

Execution order

Figure 2.41: SR-RS priority


A simultaneous TRUE as Set and Reset should be avoided when
possible.

28
Resetting Setting
Program Examples

2.7 Timers in IEC 61131-3

2.7.1 Timer Off-Delay TOF

TOF is added by marking an item and then pressing F2 (see Standard Function
Blocks in IEC61131-3).

Input Output

Preset Time
Elapsed Time

TOF(IN, PT, Q, ET), definition:


The timer starts when input IN changes to FALSE (logical 0). When the time
defined at input PT (Preset Time) expires, output Q of the timer changes from 1 to
0. Output ET is the elapsed time for the timer once it has started.

see: CoDeSys Help

2.7.2 Timer On Delay TON

TON(IN, PT, Q, ET), definition:


The timer starts when input IN changes to TRUE (logical 1). When the time
defined at input PT (Preset Time) expires, output Q of the time changes from 0 to
1. Output ET is the elapsed time for the timer once it has started.

see: CoDeSys Help

29
Program Examples

2.7.3 Pulse Timer (TP)


Pulse timers are used to generate a constant pulse length, regardless of the
duration of the input pulse.

TP(IN, PT, Q, ET), definition:


The timer starts when input IN is TRUE (logical 1). Output Q is also TRUE (logical
1) for the time defined for input PT (Preset Time). Output Q will remain set for the
specified time, even if input IN drops (assumes a value FALSE = logical 0). Output
ET is the elapsed time for the timer once it has started. Q is TRUE when IN
changes to TRUE and ET is less than PT, otherwise it is FALSE.
To sum up: TP issues TRUE (logical 1) as soon as it is started, but only for the
pulse duration specified by PT.
see: CoDeSys
Help

2.7.4 Time Declaration


The actual time declaration can consist of days (indicated by "d"), hours ("h"),
minutes ("m"), seconds ("s") and milliseconds ("ms"). Note here that time data
must be indicated in the order of magnitude (d before h before m before s before
m before ms), but not all denominations have to be used.
see: CoDeSys Help

30
Program Examples

2.8 Computational Operators


Various computational operators are available in the IEC61131-3 standard and,
thus, also in the CoDeSys programming system. Illustrated below are the basic
computational operators and their possible usage:

Figure 2.42: Basic calculations such as addition, subtraction, multiplication, division

Inputs can be linked with variables, or values can be input directly. It goes without
saying that for implementation the calculation rules of algebra must always be
followed.

Figure 2.43: Computing rules

31
Program Examples

2.9 Counters in IEC 61131-3

2.9.1 CTU Counter


The CTU function block is an incremental
(upwards) counter.

RESET
If the input parameter RESET = TRUE (logical 1), the counter is initialized with the
value 0 and counting is performed upward from there.

PV (Preset Value)
PV is a signed 16-bit INTeger value, i.e., between -32768 and +32767. This value
is used to define the upper limit for the counter, at which Q changes to TRUE.

CU
CU (Count Up) is the edge-triggered counting input, i.e., the count is increased by
1 when a rising edge changes from FALSE to TRUE (as long as 0 is not reached).

CV
CV (Current Value) is the current value (count) for the counter. This count is
increased by 1 when a positive edge is present at input CU.

Q
Q is TRUE (logical 1) when the current value (count) CV is the same as PV.

The variables CU, RESET and Q are BOOL types, while PV and CV are of type
INT.
see: CoDeSys Help

2.9.2 CTD Counter


The CTD function block is a decrement
(downwards) counter.

LOAD
If the input parameter LOAD = TRUE (logical 1), the counter is initialized with the
value PV and counting is performed downward from there.

PV
PV is a signed 16-bit INTeger value, i.e., between -32768 and +32767. The
counter is initialized with this value when the LOAD input = TRUE.
32
Program Examples

CD
CD (Count Down) is the edge-triggered counting input, i.e., the count CV is
decreased by 1 when a rising edge changes from FALSE to TRUE (as long as 0 is
not reached).

CV
CV (Current Value) is the current value (count) for the counter. This count is
decreased by 1 when a positive edge is present at input CD.

Q
Q is TRUE (logical 1) when the current value (count) CV is 0.

The variables CD, LOAD and Q are BOOL types, while PV and CV are of type
INT.
see: CoDeSys Help

2.9.3 CTUD Counter


The CTUD function block is an upwards and
downwards counter. This counter is a combination
of a CTU (upwards) counter and a CTD
(downwards) counter.

RESET
If the input parameter RESET = TRUE (logical 1), the counter is initialized with the
value 0 and counting is performed upward or downward from there.

LOAD
If the input parameter LOAD = TRUE (logical 1), the counter is initialized with the
value PV and counting is performed upward or downward from there.

PV
PV (Preset Value) is a signed 16-bit INTeger value, i.e., between -32768 and
+32767 (only a value greater than 0 is actually meaningful). The counter is
initialized with this value when the LOAD input = TRUE.

CD
CD (Count Down) is the edge-triggered counting input, i.e., the count CV is
decreased by 1 when a rising edge changes from FALSE to TRUE (as long as 0 is
not reached).

CU
CU
CU (Count Up) is the edge-triggered counting input, i.e., the count is increased by
1 when a rising edge changes from FALSE to TRUE (as long as 0 is not reached).

33
Program Examples

CV
CV (Current Value) is the current value (count) for the counter. This count is
decreased by 1 when a positive edge is present at input CD and increased by 1
when a positive edge is present at input CU.

QD
QD is the lower binary output of the counter. This output is TRUE (logical 1) when
the current value CV = 0.

QU
QU is the upper binary output of the counter. This output is TRUE (logical 1) when
the current value CV >= PV (preset value).

The variables CU, CD, RESET, LOAD, QU and QD are BOOL types, while PV and
CV are of type INT.
see: CoDeSys Help

34
Program Examples

3 Program Examples

3.1 The Dimmer Function Block


Building automation requires functions that are also used in standard installations.
These functions must be programmed for automation, however. This can be, for
example, the functions for a dimmer.

On controlled. This is achieved for a fluorescent lamp with the aid of an electronic
ballast (EVG). Using an EVG, the brightness can be set from minimum to
maximum with direct current of 0-10V. An analog output is required to generate
this voltage. A variable value (e.g., integer) is transformed to an analog voltage in
this process. The analog output is not provided in this system. It therefore suffices
to define a variable whose specific value is displayed in the simulation.

D Electrical
Inputs Function block ballast
A

Figure 3.1: Principle behind dimming

Figure 3.2: Circuit diagram for the Dimmer function block

35
Program Examples

This function block must be linked to inputs and outputs and other elements before
it can be properly used.

Figure 3.3: Connection of the function block

3.2 The Latching Function Block

Building automation requires functions that are also used in standard installations.
WAGO offers a number of function blocks for building automation to simplify these
automation tasks. You can download these function block libraries from our
Website at www.wago.de. Here, you should program a latching relay on your own
to illustrate the use of the function blocks.

With this function an output (such as a lamp) is switched by pressing a button.


This requires that when a lamp that has been switched off is switched on, it will
remain on when the button is released. Conversely, it is switched off by pressing a
button and remains off when the button is released. As a further feature, buttons
and lamps can be connected in parallel.

Tactil Switch

Lamp

Figure 3.4: Diagram of a latching relay

This function has been implemented in the bottom representation within the
function block Fb_LatchingRelay:

36
Program Examples

Figure 3.5: Circuit diagram of the Fb_LatchingRelay function block

This function block can be called up from the main program PLC_PRG:

Figure 3.6: Calling up the function block

37
Exercises Exercise 1: Vehicle Traffic Tunnel

4 Exercises

4.1 Exercise 1: Vehicle Traffic Tunnel


The concentration of hazardous gases is frequently a problem in long vehicle
traffic tunnels. In the tunnel depicted here, three fans are installed which are
activated/deactivated according to the situation at hand. A total of three smoke
detectors are installed at different points in the tunnel to monitor the air quality.

Figure 4.1: System setup in the vehicle traffic tunnel

Procedure description:
If one smoke detector issues a signal, fan 1 must start running. If two smoke
detectors issue signals, fan 2 and fan 3 must also be activated. If all smoke
detectors issue signals, all three fans must start running. Your task is to develop a
control system for the fans in the tunnel based on IEC 61131.

Allocations:
VAR_GLOBAL
R1:BOOL; (*Smoke detector1*)
R2:BOOL; (*Smoke detector2*)
R3:BOOL; (*Smoke detector3*)
fan1:BOOL; (*Fan1*)
fan2:BOOL; (*Fan2*)
fan3:BOOL; (*Fan3*)
END_VAR

Note:
To help solve this task you should analyze the functions of the operators AND, OR
and NOT.

38
Exercises Exercise 2: Service Water Solar Installation

4.2 Exercise 2: Service Water Solar Installation


A service water solar installation is used to heat water for households using solar
energy. On the one hand, these systems work as stand-alone units and, on the
other, they can also be integrated into energy supply systems in buildings.

Figure 4.2: System layout of a service water solar installation

Procedure description:
One task of the control system for a service water solar installation involves
switching of the pump. The solar circuit pumps is to be activated when the
collector temperature is 10°C higher than the temperature toward the bottom of the
reservoir.

classification:

Figure 4.3: Global variables for the service water solar installation

39
Exercises Fan Control

The variables for temperatures are declared as type Integer. This is necessary, as
BOOL variables would not be sufficient for digital temperature values (0 or 1 as
state). An INTEGER variable is better suited for this. An INTEGER variable can
possess values between -32768 to 32767.
Your task is to develop a control system for the service water solar installation
based on IEC 61131. The values can be subsequently manipulated to test them.
To do this, you must double-click exactly on the numerical value and input the new
value. The values are not accepted, however, until the menu item
Online/Overwrite values has been called up.
In actual practice, temperature values are linked via analog inputs (e.g., Wago
750-467), or PT100 (e.g., Wago 750-461) interface modules.

Note:
To help solve this task you should analyze the functions of the relational operators
GT, GE, LT, and those of the LE operator.

4.3 Fan Control


A windowless bathroom is fitted with a ventilation duct. An electric fan has been
installed in this ventilation duct to ensure that damp and stale air is removed
quickly. This is necessary to prevent the formation of mold, for example. The fan is
connected in parallel to the light switch to ensure that it also switched on when the
room is in use.

Light

S1

Fan

40
Exercises Fan Control

Function:
The control system for the light and fan is to be an element of the room automation
system. When the light is turned on after entering the room, the fan should cut in
after 5s (30s). The fan remains off if the light is switched on only briefly. When a
person leaves the bathroom and switches off the light, the fan should continue
running for a further 10s (2m).

Task:
Develop and test the project in which the function described above is
implemented.

The following timing diagram is useful for solving this task.

S1
PT-TON

TON.Q

TOF.Q PT-TOF

Fan

41
Exercises Parking Lot Monitoring

4.4 Parking Lot Monitoring


The number of vehicles in a defined parking area is to be registered in a parking
lot.

Figure 4.4: Layout of a parking lot

Procedure description:

Initiators 1 and 2 issue a 1-signal when vehicles enter/leave. The capacity of the
parking lot is limited to 15 vehicles. The number of vehicles present on the lot must
be counted up/down as vehicles arrive/leave. The signal light should be green if
there are open parking spots available. The signal light must be red when the
parking lot is full.

Allocations:

VAR_GLOBAL
Initiator1: BOOL;
Initiator2: BOOL;
green: BOOL;
red: BOOL;
occupiedParkPl: INT;
freeParkPl: INT;
END_VAR

Task:
Develop a control system for this based on IEC61131-3.

42
Exercises Room Automation

4.5 Room Automation


Automation is becoming more and more important in the building sector. Key
aspects here are convenience, safety and energy efficiency. The example here
presents a situation involving a room in which several control functions are to be
installed.

Sliding

Push-button,
simulating a
position of a sliding
door.

Figure 4.5: Layout diagram, room automation

Tasks:

The following tasks are to be fulfilled by a room control system:


a. When the sliding door is closed (using button S18), lamp E3/E4 is to be
regulated in latching operation via S11/S21. When the door is open, E3 and E4
are to be controlled in parallel operation via S11 and S21.
b. When the door is closed, it must be possible to dim E1 and E2 individually via
S15/S16 and S25/26. When the door is open, it must be possible to dim E1 and
E2 together using S15/S16 or S25/S26.

A function module for latching and dimming must be written at the onset of
programming for this task. These function blocks are described in Section 3.

43
Exercises Room Automation

To do this, first create a new project (Section 2.1.1). You must then define the
communication parameters and PLC configuration (Sections 2.1.2 and 2.1.3). You
can then add an object by right-clicking on function blocks in the "Module" tab. The
following window is displayed after you click on "Add object":
Enter the name of
the function block

Change the type of


module to Function
block.

Figure 4.6: Creating a function block

Here, you can define the type and name of the function module. The program for
latching operation is then added to the new function block. Refer to Section 3.2 for
information about the program and for a description of latching operation.
Following the same principle, you must then add a function block for dimming.
Refer to Section 3.1 for information about the program and for a description of
latching operation.
You can begin the exercise once you have completed the two function blocks for
dimming and latching.

44
Solutions

5 Solutions

5.1 Exercise 1: Vehicle Traffic Tunnel


The task for this exercise was:
If one smoke detector issues a signal, fan 1 must start running. If two smoke
detectors issue signals, fan 2 and fan 3 must also be activated. If all smoke
detectors issue signals, all three fans must start running.

The variable declaration was:


VAR_GLOBAL
R1:BOOL; (*Smoke detector1*)
R2:BOOL; (*Smoke detector2*)
R3:BOOL; (*Smoke detector3*)
fan1:BOOL; (*Fan1*)
fan2:BOOL; (*Fan2*)
fan3:BOOL; (*Fan3*)
END_VAR

First you must create a table showing all possible situations:

R1 R2 R3 Fan1 Fan2 Fan3


0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 1 0 0
0 1 1 0 1 1
1 0 0 1 0 0
1 0 1 0 1 1
1 1 0 0 1 1
1 1 1 1 1 1

You can then use this table to develop your solution:

Figure 5.1: Solution to vehicle tunnel exercise

45
Solutions

Note on execution sequence in CFC:

The elements Function block and Output, as well as others, are each assigned an
execution number in the CFC Editor. When the order (sequence) is displayed, the
assigned execution number is displayed at the top, right corner for the elements.

Display sequence

Sequence

Figure 5.2: Display of execution sequence numbers in CFC

The individual elements are calculated for the runtime in this order.
When an element is added, its number is assigned automatically in topological
order (from left to right, top to bottom). If the order has already been changed, the
new element is assigned the number of its successor in the topology and all
numbers above this are increased by one (1). The number is retained when an
elemented is moved. The number sequence affects the result and may have to be
changed is some cases.
It is therefore important that you define the order of the execution sequence of the
function blocks at the end of programming according to the direction of data flow
(see Figure 4.2).

see: CoDeSys Help

46
Solutions

5.2 Exercise 2: Service Water Solar Installation


The solution for this exercise in CFC is illustrated below:

Figure 5.3: Solution for service water solar installation exercise

5.3 Exercise 3: Fan Control


The solution for this exercise in CFC is illustrated below:

Figure 5.4: Solution for fan control exercise

47
Solutions

5.4 Exercise 4: Parking Lot Monitoring


The solution for this exercise in CFC is illustrated below:

Figure 5.5: Solution for parking lot exercise

48
Solutions

5.5 Exercise 5: Room Automation


A possible solution for exercise a in CFC is illustrated below:

Figure 5.6: Solution for room automation, exercise 5a

49
Solutions

A possible solution for exercise b in CFC is illustrated below:

Figure 5.7: Solution for room automation, exercise 5b

The solution for exercise 5b is given in the same program (PLC_PRG) as the solution for
exercise 5a. Figure 5.7 only illustrates the newly initialized variables and only the program
for exercise b.

50

S-ar putea să vă placă și