Module 3 - Programming-Preliminary
Module 3 - Programming-Preliminary
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
Table of Contents
Introduction ........................................................................................................ 5
IEC and Programming for PLCs .................................................................................................. 5
IEC ....................................................................................................................... 6
IEC 61131 ............................................................................................................ 6
IEC 61131-3 ......................................................................................................... 7
Successful Project Process .............................................................................. 8
Basic System Design ................................................................................................................... 9
Lab 1: Design Basic System Operations ................................................................................... 10
Program Components...................................................................................... 14
POU ........................................................................................................................................... 14
Function Block ........................................................................................................................... 14
Function ..................................................................................................................................... 14
Program ..................................................................................................................................... 14
Data Types ................................................................................................................................ 14
Array .......................................................................................................................................... 15
Enumeration .............................................................................................................................. 15
Range ........................................................................................................................................ 15
Structure .................................................................................................................................... 15
Resources.................................................................................................................................. 16
Libraries ..................................................................................................................................... 16
Glossary ............................................................................................................ 32
Index .................................................................................................................. 33
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
Introduction
IEC and Programming for PLCs
Software plays an ever increasing factor in the quality of any product. Software errors at minimum
will cause loses of time and waste of salary but in the extreme, can mean destruction of the
system it controls.
The increasing complexity of industrial control have complicated software code by a hundredfold.
Where in the past a machine might require 100 lines of code, it is not uncommon for major
installations to require 10,000 lines now. As one can imagine, more code means more possibility
for errors and makes 100% testing impossible. With tightening deadlines and increased
programmer turnover, expecting a single programmer to work on a project is a fallacy.
Modern programming tools combined with time-tested methods provide the environment for
higher quality software, i.e. reliability, robustness, integrity, persistence, and safety. Beckhoff s
PLC Control with the international standard IEC 61131-3 creates the perfect environment to
achieve the highest quality in software.
Following IEC recommendations will increase efficiency. More time will be available to tweak logic
because time will not be eaten-up by bug hunting, making code compatible from other
programmers and rewriting instead of simply reusing existing code blocks.
Everyone will benefit from the adoption of IEC Standards. To recap, here are the advantages
mentioned above plus more:
Code is immediately understandable by multiple programmers
Code can be worked on by programmers worldwide
Code will be usable far into the future
Program can be broken into smaller, reusable modules
Access to 3rd party modules and tools
Strong datatyping prohibits programming errors
Data structures for easy exchange of data elements
Flexible language selection, matching different backgrounds
Vendor independent software development possible
Supplier independent courses possible
Product independent courses possible
Better matching of theory and praxis, esp. at schools / educational level
Based upon up-to-date programming techniques and structural thinking
NOTE: This manual assumes basic to intermediate knowledge of Windows based computers and
basic programming knowledge. Familiarity with Module 1 of this series may also be helpful.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
IEC
The International Electrotechnical Commission (IEC) is an international standards organization
dealing with electrical, electronic and related technologies. Some of its standards are developed
jointly with ISO.
The IEC started on 26 June 1906 in London, the commission moved to its current headquarters in
Geneva in 1948. Today, the IEC is the world's leading international organization in its field, and
its standards are adopted as national standards by its members. The work is done by some
10,000 electrical and electronics experts from industry, government, academia, test labs and
others with an interest in the subject.
The IEC was instrumental in developing and distributing standards for units of measurement,
particularly the gauss, hertz, and weber. They also first proposed a system of standards, the
Giorgi System, which ultimately became the SI, or Systme International d units (the
International System of Units).
IEC standards have numbers in the range 60000 79999 and their titles take a form such as IEC
60417: Graphical Symbols for use on Equipment. The numbers of older IEC standards were
converted in 1997 by adding 60000, for example IEC 27 became IEC 60027.
Standards developed jointly with ISO such as ISO/IEC 26300, Open Document Format for Office
Applications (OpenDocument) v1.0 carry the acronym of both organizations. The use of the
ISO/IEC prefix is limited to publications from ISO/IEC Joint Technical Committee 1 on Information
Technology, as well as some ISO/IEC guides. ISO/IEC JTC1 is described in more detail under
ISO.
IEC 61131
IEC 61131 deals with PLCs. Development for the 61131 standard started in 1979 and Parts 1 and
2 were made standard in 1990 with part 3 being standardized in 1993.
Note: The standard for programmable logic controllers (PLCs) used to be 1131 but
because of the IEC s change in the numbering scheme starting January 1, 1997, this was
changed to 61131.
The standard is broken up into eight parts:
Part 1: General information
Part 2: Equipment requirements and tests
Part 3: Programming languages
Part 4: User guidelines
Part 5: Messaging service specification
Part 6: Communications via fieldbus (Awaiting completion of fieldbus standards.)
Part 7: Fuzzy control programming
Part 8: Guidelines for the application and implementation of programming languages
Therefore, IEC 61131-3 is the designator for standards concerning programming languages.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
IEC 61131-3
The purpose of any standardization is to achieve consistency. With PLC programming this means
that the languages, methods and structure of existing programs should be easily understood by
any PLC programmer with IEC 61131-3 techniques experience.
The 61131-3 Standards committee has specified the following requirements and
recommendations for programming:
1. Well Structured: Top down or bottom up programming development. Top down describes
starting with the complete requirements of the project then breaking those needs into individual
tasks. Bottom up starts with the independent tasks and builds up to the complete project.
Beckhoff recommends a top down approach as the project needs should define the tasks
required.
2. Strong Data Typing: Program should detect when the wrong type of data is written to a
variable.
3. Full Execution Control: Different parts of a program can run at different times, at different
rates and in parallel.
4. Complex Sequential Behavior: The steps, actions and transition of program sequences can
be described graphically. Also sequences can initiate other sequences and can run in parallel.
5. Data Structures: Program elements that are related can be shared among different POUs as
a complete unit. E.g. once a pump is completely described with all its I/O, that collection of code
becomes a virtual object that can be passed and reused.
6. Flexible Language Selection: The programmer is free to choose among IEC sanctioned
graphical and textual programming languages that best fit the task, and different parts of the
program can utilize different languages.
7. Vendor-Independent Software: PLC code that adheres to IEC 61131-3 standards should run
on any PLC from any vendor with minimal rewriting.
One final note is that the IEC 61131-3 standard is constantly growing and changing to meet
current challenges and new advances in technology and programming. Though the core of the
standard is not likely to change drastically, expect changes to the programming languages, file
formats and other aspects that guarantee interoperability among PLC vendors in the future.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
7
6
In thinking about the overall necessity of the machine, as with any project the safety of the users
is paramount. Second to that, it needs to always dispense properly made product.
Starting with the product, the system needs to maintain a proper amount of properly cooled and
mixed product. This involves activating the auto fill feature, maintaining a temperature threshold
and mixing when appropriate.
It should also restrict dispensing when the product is not ready and restrict internal access when
the system is operating. This means shutting off the spout (and lighting the Not Ready light) if the
product is not optimum and locking the lid when the stirrer is operational.
Design Approach
We can describe the overall project by breaking it down into smaller components. We will look at
the following pieces through the next three sections:
1. Interface this includes the sensors as well as the user control.
2. Signals messages that are passed between the interfaces and the rest of the system (or
plant in larger systems).
3. User Interactions all operator interactions (larger systems may also include overrides and
supervisory data).
4. Control Analysis the macro systems that can be described in terms of POUs.
5. Function Blocks low level control
6. Scan Cycles any time requirements for the different programs and function blocks.
7. Detailed Design Specifics of the program and function blocks.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
10
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
2. Signals describe the messages that need to be passed by each of the interface elements
above. E.g. if there is a pressure gauge, the message might be, Amount of pressure in PSI.
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
11
3. User Interactions describe every way that a human will interact with the system. This can be
as basic as, Switch system on or complex such as, Place material in crimper and pull handle.
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
12
Data Type
Language Recommendation
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
13
Plain Language
Data Type
Language Recommendation
Boolean
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
14
Program Components
Now that we have a basic overview of the project we can begin to think about it in terms of code.
As has been mentioned, smaller components make for better code. Here we will begin to
familiarize ourselves with these components and the standard IEC nomenclature.
A complete TwinCAT program is called a Project. A Project can contain three kinds of objects:
Program Organizational Units (POUs), data types and resources, plus libraries which are
collections of the afore mentioned objects which can speed the writing of new code and give the
Project added power such as connecting to third party hardware.
A POU is a Program, Function Block or Function. The important factor is that the POU can be
used repeatedly in different parts of a Project.
POU
POUs are complete packages of code that can be reused throughout a Project or with separate
Projects. Unlike some other (high-level) languages, POU names are always global, that is, they
are not like local subroutines.
Function Block
Function Blocks are the most common POU. Function Blocks are encapsulated code describing a
reusable function all the way up to a virtual device. They can be written in any programming
language and their interoperability makes them compatible with programs from any IEC compliant
vendor program.
Function Blocks can be assigned to specific tasks. By default, a Function Block runs in the same
task as its parent Program. To assign a Function Block to a specific task, use the keyword WITH
(described later).
Function
Functions are the most restricted of the POUs because they can only contain local and input
variables.
Program
A program has all the features of a Function Block plus the ability to access the physical I/O
addresses of the PLC. Programs can contain declarations of global variables (VAR_GLOBAL),
which can then be referenced with Function Blocks. Programs can contain access variables
(VAR_ACCESS) which allow variables to be to be read and written to remote devices using
communication services. One limitation is that a Program can not contain an instance of another
Program.
Data Types
The data type determines what form information is saved in or passed out of variables. For
example, if the variable receives a number with a decimal (e.g. 3.14159) and the variable is set to
INT, then the value retained in the variable will lose the decimal (i.e. 3). To retain a decimal, the
variable would need to be set to REAL.
Data types can also limit how large a saved value will be. INT can only be 16 bits, so if the
received value is expected to be greater than +/-32767 than DINT or LINT should be used. Keep
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
15
in mind that enough memory will be reserved to hold the maximum variable size, so if the
expected values will be small, use smaller Data Types to limit memory waste.
Boolean/
Bit String
Signed
Integer
Unsigned
Integer
Floatingpoint (Real)
BOOL
Bits
Time, Duration,
Date & Character String
TIME
BYTE
SINT
USINT
DATE
WORD
INT
UINT
16
TIME_OF_DAY
DWORD
DINT
UDINT
REAL
32
DATE_AND_TIME
LWORD
LINT
ULINT
LREAL
64
STRING
Besides the common data types above, the IEC standard allows for customized data types.
Called Derived Data Types, these data types are user definable, that is, the programmer can
create a data model that is customized for the specific application.
One caveat is that the IEC standard currently does not make mention of graphical representation
of derived data types. This means that defined data types must be created using textual
representation.
Use TYPE
END_TYPE to define a derived data type. Within the derived statement, varieties
of derived data types can be used, specifically: arrays, enumerations, ranges and structures.
Array
An array (AKA multi-element variable) is a group of data elements that all have the same data
type. Another restriction is that IEC 61131-3 states that an array cannot contain FB instances.
Enumeration
An enumeration is a list of names which can be applied to a variable. One possible use is to have
a list of machine states (ready, active, paused, etc.) that a variable can assume. One benefit is
that enumerations tend to be self commenting as the state is more easily determined.
Range
A range is the difference between the highest and lowest value a variable can be. A standard
data type, such as INT or REAL, can be specified to control the type of data allowed.
Structure
A structure is a composite data type defined using existing (basic or derived) data types. They
utilizes a STRUCT
END_STRUCT form and can be nested within the TYPE
END_TYPE.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
16
(* enumeration *)
(* range *)
(* 1-dimensional array *)
(* 2-dimensional array *)
(* structure *)
(* basic IEC data type *)
(* enumerated data type with initial value *)
(* array type *)
(* array type *)
Resources
Resources provide support for all the features needed to execute a program. You can think of a
resource as a virtual machine, that is, it embodies all the capabilities of the hardware for which
you are programming. So, not only does a resource allow for the possibility of a simulated or
virtual PLC control, but in fact, one of the main functions is to provide an interface between a
program and the physical I/O.
Libraries
Libraries are collections of code that programmers can access to facilitate programming. Often
these libraries support specific hardware. Beckhoff offers libraries for all their bus couplers and
other manufacturers generally have libraries that compliment their hardware offerings.
A library can be a complete POU or simply a snippet of code. In addition to hardware
communication, Beckhoff provides libraries to help with specific tasks such as motion or building
automation.
In TwinCAT PLC Control, the
Library Manager is employed
to access libraries. Two ways
to access the Library
Manager in PLC Control are
the Window menu and the
Resources tab.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
17
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
18
*)
*)
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
END_VAR
VAR_OUT
END_VAR
VAR_OUT
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
END_VAR
END_VAR
*)
*)
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
END_VAR
VAR_OUT
END_VAR
VAR_OUT
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
END_VAR
END_VAR
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
19
*)
*)
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
END_VAR
VAR_OUT
END_VAR
VAR_OUT
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
END_VAR
END_VAR
*)
*)
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
END_VAR
VAR_OUT
END_VAR
VAR_OUT
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
____________________________
END_VAR
END_VAR
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
20
5. Function Blocks
Here are some possible Function Blocks. Variable naming is completely up to the programmer,
but having readable names goes far to make the code immediately readable.
Functionality
Function Block
FB_LevelCheck
VAR_IN
ProductLevel : INT;
MaxLevel : INT;
END_VAR
VAR_OUT
LockSpout : BOOL;
NotReady : BOOL;
FillState : BOOL;
END_VAR
FB_TempCheck
VAR_IN
CurrentTemp : INT;
MaxTemp : INT;
OpTemp : INT;
END_VAR
VAR_OUT
RunFan : BOOL;
LockSpout : BOOL;
NotReady : BOOL;
END_VAR
FB_Dispense
VAR_IN
SpoutState : BOOL;
END_VAR
VAR_OUT
SetStir : BOOL;
LockLid : BOOL;
END_VAR
Dispense product
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
21
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
22
Language breakdown
Language
Type
Pros
Cons
LD
Ladder Description
Graphical
Familiar to anyone
practiced in RLL.
Decent translation to
and from FBD
ST
Structured Text
Textual
Graphical
IL
Instruction List
Textual
Least documented
language.
Can be directly
downloaded to some
PLC without compiling.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
23
Language
Type
Pros
SFC
Sequential Function
Chart
Textual
and
Graphical
Cons
Graphical
Notes: German programmers prefer IL and ST. US seems to prefer a combination of SFC and
Ladder. France s PLC experts tend to use SFC.
Conventions
Conventions are protocols that help to standardize written code. This makes it is easier to work
with code that someone else started or even dig into code you may have written long ago. IEC
only gives recommendations on this topic. Your company may have specific conventions to use
when writing code. Following are Beckhoff suggested conventions which if followed will make
your code readable by a vast majority of PLC programmers and will make working with Beckhoff
even easier.
Comments
The best habit you can adopt is to comment you code. Commenting is often seen as the last step
in programming but when the coding is completed, commenting is usually forgotten. It is far better
to comment as you code as it will then be done when the code is done and won t be neglected.
Plus, if you were to leave the project before completion, it will reduce the time it takes for the
programmer assuming responsibility of the project to get up to speed.
Comments in graphic programming languages is usually in the form of added lines to the element
name. In textural languages, the comment is in-line with the code being commented. Also, in the
Declaration part of the PLC Control window, the comments are in-line with the code being
commented. In both of the latter situations, comments are marked with parentheses and
asterisks, i.e. (* this is a comment and will be ignored when the code is run *).
Naming Conventions
In addition to commenting, naming conventions also help to clue a programmer in as to what the
code is doing as any given point. Naming conventions should be used in both naming POUs and
also variables within POU objects.
Starting with POUs or Object Names, the following is a good beginning guide:
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
24
Object
Prefix
Description
Example
FUNCTION_BLOCK
FB_
Function Block
FB_GetData
STRUCT
ST_
Structure
ST_BufferEntry
ENUM
E_
Type of Enumeration
E_SignalStates
TYPE
T_
Reference
T_Nibble
PROGRAM
P_
Program
P_Axis
FUNCTION
F_
Function
F_Convert
There really isn t a reason to add a code to identify the language in which
the POU is written since PLC Control marks each POU with a
representative icon; FBD, ST and LD respectively in the example shown.
Furthermore, as has been stated many times, the language choice is only
important in writing the POU not using it. You might think that identifying
the type of POU is not necessary as it too is marked by PLC Control, but changing the prefix
allows for similar named variables that have different functionality. Plus, it will be helpful when
trying to understand the function of a particular instance in the code when printed out or viewed in
raw text.
Instance names should also follow a convention. The markers are similar to the POU conventions
but lowercase is used to provide on-sight identification that the item is an instance and not the
original POU. Compare to the POU convention table above.
Object
Prefix
Description
Example
Function block
fb
fbGetData
Struct
st
Instance of a Structure
stBufferEntry
Enum
Instance of Enumeration
eSignalStates
Alias type
<none>
Nibble
Finally, conventions can optionally be used for variables. Some programmers like to mark the
variable with the type of data it can contain. Below are the most common data types:
Data Type
Prefix
Example
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
25
Data Type
Prefix
Example
BOOL
bDoorOpen
INT
iMaxSpeed
REAL
rMaxTemp
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
26
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
27
Here is a possible way to program the system. The structure can be different and the languages
chosen to complete the components can be different, but this shows how to make code very
efficient, intuitive and safety conscience as well as making the system function.
[ Finished program]
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
28
Documentation
A number of sources of information are available when learning about, using specific Beckhoff
tools, Beckhoff products and services and company information. As a customer of Beckhoff, all of
this information will be at your disposal.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
29
There are three main ways to find information in the Information System:
Contents shows a tree of topics that can be browsed.
Index takes a keyword and shows items specified as being related to that keyword.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
30
Search returns every page that contains the entered search term.
If you find something close to what you are looking for in a search, try clicking on the Contents
tab to see where the selected page falls in the tree. You can then browse from there to pinpoint
the information you require.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
31
Other resources
www.beckhoff.com
This is the official website of Beckhoff Automation. At this site you will have access to company
information, including news, training locations, contact information, products and services.
PC Control
This printed, monthly magazine, published by Beckhoff offers stories about companies that are
benefiting from the incorporation of Beckhoff products plus tips, new product announcements and
general news. It can also be read online at www.pc-control.net/english.
Main Catalog
This printed document contains all of Beckhoff s products and services and also includes some
company information. See the I/O section in this manual for a description of model numbering
and color scheme.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
32
Glossary
CFC (Continuous Function Chart), a version
of FBD that is not yet an IEC standard PLC
programming language.
12150 Nicollet Avenue South / Burnsville, MN 55337 / 877 TWIN-CAT (877 894-6228)
33
Index
data type, 14
DLL, 32
error, 28
I/O, 31, 32
Information System, 28, 29
library, 14, 32
OCX, 32
PLC, 28, 32
POU, 14, 32
Scan, 10, 18, 26