: This variable is of the type BOOL. When it has the value TRUE, the timeouts of the
steps will be registered in SFCError. Other timeouts will be ignored.
: When this boolean variable has the value TRUE the sequential function chart is set back to
the Init step. The other SFC flags are reset too (initialization). The Init step remains active, but is not
executed, for as long as the variable has the value TRUE. It is only when SFCInit is again set to
FALSE that the block can be processed normally.
: This variable, of type BOOL, behaves similarly to SFCInit. Unlike the latter, however,
further processing takes place after the initialization of the Init step. Thus for example the SFCReset
flag could be re-set to FALSE in the Init step.
: As from compiler version 184.108.40.206 SFCReset also can be used to reset boolean actions
associated to IEC steps, which was not possible before
: Provided that the Execution of the SFC diagram is stopped for as long as this boolean
variable has the value TRUE whereby a possible timeout in the variable SFCError is reset. All
previous times in the active steps are reset when the variable again assumes the value FALSE. It is a
pre-condition that the flag SFCError has been defined also, which registers any timeout in the SFC.
: Execution of the SFC diagram is stopped for as long as this boolean variable has the
: This Boolean variable is TRUE when a timeout has occurred in a SFC diagram. If another
timeout occurs in a program after the first one, it will not be registered unless the variable SFCError is
reset first. It is a pre-condition that SFCError is defined, if you want to use the other time-controlling
flags (SFCErrorStep, SFCErrorPOU, SFCQuitError, SFCErrorAnalyzation).
: This boolean variable takes on the value TRUE when a transition is actuated.
: This variable is of the type STRING. If SFCError registers a timeout, in this variable is
stored the name of the step which has caused the timeout. It is a pre-condition that the flag SFCError
has been defined also, which registers any timeout in the SFC.
: This variable of the type STRING contains the name of the block in which a timeout
has occurred. It is a pre-condition that the flag SFCError has been defined also, which registers any
timeout in the SFC.
: This variable is of the type STRING. The name of the step is stored in this variable
which is active, independently of the time monitoring. In the case of simultaneous sequences the step
is stored in the branch on the outer right. No further timeout will be registered if a timeout occurs and
the variable SFCError is not reset again.
: This variable of type ARRAY [0..n] OF ExpressionResult provides the
result of an analyzation of a transition expression. For each component of the expression, which is
contributing to a FALSE of the transition and thereby to a timeout of the preceding step, the following
information is written to the structure ExpressionResult: name, address, comment, current value.
This is possible for maximum 16 components (variables), thus the array range is max. 0..15).
The structure ExpressionResult as well as the implicitly used analyzation modules are provided with
the library AnalyzationNew.lib. The analyzation modules also can be used explicitly in other POUs,
which are not programmed in SFC.
It is a pre-condition for the analyzation of a transition expression, that a timeout is registered in the
preceding step. So a time monitoring must be implemented there and also the variable SFCError (see
above) must be defined in the declaration window.
: This variables of type BOOL allow inching mode of the SFC. When this is
switched on by SFCTipMode=TRUE, it is only possible to skip to the next step if SFCTip is set to
TRUE. As long as SFCTipMode is set to FALSE, it is possible to skip even over transitions.
: Regard also the implicit variables usable for scanning the status and time of steps resp. actions.
2 - What is What in CoDeSys
Two or more branches in SFC can be defined as alternative branches. Each alternative branch must
begin and end with a transition. Alternative branches can contain parallel branches and other
alternative branches. An alternative branch begins at a horizontal line (alternative beginning) and ends
at a horizontal line (alternative end) or with a jump.
If the step which precedes the alternative beginning line is active, then the first transition of each
alternative branch is evaluated from left to right. The first transition from the left whose transition
condition has the value TRUE is opened and the following steps are activated (see active step).
Two or more branches in SFC can be defined as parallel branches. Each parallel branch must begin
and end with a step. Parallel branches can contain alternative branches or other parallel branches. A
parallel branch begins with a double line (parallel beginning) and ends with a double line (parallel end)
or with a jump. It can be provided with a jump label.
If the parallel beginning line of the previous step is active and the transition condition after this step
has the value TRUE, then the first steps of all parallel branches become active (see active step).
These branches are now processed parallel to one another. The step after the parallel end line
becomes active when all previous steps are active and the transition condition before this step
produces the value TRUE.
A jump is a connection to the step whose name is indicated under the jump symbol. Jumps are
required because it is not allowed to create connections which lead upward or cross each other.
2.2.4 Function Block Diagram (FBD)...
The Function Block Diagram is a graphically oriented programming language. It works with a list of
networks whereby each network contains a structure which represents either a logical or arithmetic
expression, the call of a function block, a jump, or a return instruction.
Example of a network in the Function Block Diagram
For further information on the FBD editor see Chapter 5.4.2.
2.2.5 The Continuous Function Chart Editor (CFC)...
The continuous function chart editor does not operate like the function block diagram FBD with
networks, but rather with freely placeable elements. This allows feedback, for example.
For further information on the CFC editor see Chapter 5.4.5
Example of a network in the continuous function chart editor
2.2.6 Ladder Diagram (LD)...
The Ladder Diagram is also a graphics oriented programming language which approaches the
structure of an electric circuit.
On the one hand, the Ladder Diagram is suitable for constructing logical switches, on the other hand
one can also create networks as in FBD. Therefore the LD is very useful for controlling the call of
The Ladder Diagram consists of a series of networks. A network is limited on the left and right sides
by a left and right vertical current line. In the middle is a circuit diagram made up of contacts, coils,
and connecting lines.
Each network consists on the left side of a series of contacts which pass on from left to right the
condition "ON" or "OFF" which correspond to the Boolean values TRUE and FALSE. To each contact
belongs a Boolean variable. If this variable is TRUE, then the condition is passed from left to right
along the connecting line. Otherwise the right connection receives the value OFF.
Example of a network in a Ladder Diagram made up of contacts and coils
For further information on the LD editor see Chapter 5.4.3.
Each network in LD consists on the left side of a network of
(contacts are represented by two
parallel lines: | |) which from left to right show the condition "On" or "Off".
These conditions correspond to the Boolean values TRUE and FALSE. A Boolean variable belongs to
each contact. If this variable is TRUE, then the condition is passed on by the connecting line from left
to right, otherwise the right connection receives the value "Out".
Contacts can be connected in parallel, then one of the parallel branches must transmit the value "On"
so that the parallel branch transmits the value "On"; or the contacts are connected in series, then
contacts must transmit the condition "On" so that the last contact transmits the "On" condition. This
therefore corresponds to an electric parallel or series circuit.
A contact can also be negated, recognizable by the slash in the contact symbol: |/|. Then the value of
the line is transmitted if the variable is FALSE.
On the right side of a network in LD there can be any number of so-called coils which are represented
. They can only be in parallel. A coil transmits the value of the connections from left
to right and copies it in an appropriate Boolean variable. At the entry line the value ON (corresponds
to the Boolean variable TRUE) or the value OFF (corresponding to FALSE) can be present.
Contacts and coils can also be negated (in the example the contact SWITCH1 and the coil %QX3.0 is
negated). If a coil is negated (recognizable by the slash in the coil symbol:
), then it copies the
negated value in the appropriate Boolean variable. If a contact is negated, then it connects through
only if the appropriate Boolean value is FALSE.
2 - What is What in CoDeSys
Function blocks in the Ladder Diagram
Along with contacts and coils you can also enter function blocks and programs In the network they
must have an input and an output with Boolean values and can be used at the same places as
contacts, that is on the left side of the LD network
Coils can also be defined as set or reset coils. One can recognize a set coil by the "S" in the coil
) It never writes over the value TRUE in the appropriate Boolean variable. That is, if the
variable was once set at TRUE, then it remains so.
One can recognize a reset coil by the "R" in the coil symbol:
) It never writes over the value FALSE
in the appropriate Boolean variable: If the variable has been once set on FALSE, then it remains so.
LD as FBD
When working with LD it is very possible that you will want to use the result of the contact switch for
controlling other POUs. On the one hand you can use the coils to put the result in a global variable
which can then be used in another place. You can, however, also insert the possible call directly into
your LD network. For this you introduce a POU with EN input.
Such POUs are completely normal operands, functions, programs, or function blocks which have an
additional input which is labeled with EN. The EN input is always of the BOOL type and its meaning is:
The POU with EN input is evaluated when EN has the value TRUE.
An EN POU is wired parallel to the coils, whereby the EN input is connected to the connecting line
between the contacts and the coils. If the ON information is transmitted through this line, this POU will
be evaluated completely normally.
Starting from such an EN POU, you can create networks similar to FBD.
Example of a LD network with an EN POU
2.3 Debugging, Online Functions...
The Sampling Trace allows you to trace the value sequence of variables, depending upon the so-
called trigger event. This is the rising edge or falling edge of a previously defined Boolean variable
(trigger variable). CoDeSys permits the tracing of up to 20 variables. 500 values can be traced for
The debugging functions of CoDeSys make it easier for you to find errors.
In order to debug, run the command
and in the dialog box that pops up under
Build options select activate option
Debugging, Online Functions...
A breakpoint is a place in the program at which the processing is stopped. Thus it is possible to look
at the values of variables at specific places within the program.
Breakpoints can be set in all editors. In the text editors breakpoints are set at line numbers, in FBD
and LD at network numbers, in CFC at POUs and in SFC at steps. No breakpoints can be set in
function block instances.
: Runtime system CoDeSys SP 32 Bit Full will deactivate the watchdog function of the concerned task
as soon as the execution of the program currently is stopped at a breakpoint.
Single step means:
In IL: Execute the program until the next CAL, LD or JMP command.
In ST: Execute the next instruction.
In FBD, LD: Execute the next network.
In SFC: Continue the action until the next step.
By proceeding step by step you can check the logical correctness of your program.
If Single cycle has been chosen, then the execution is stopped after each cycle.
Change values online
During operations variables can be set once at a certain value (write value) or also described again
with a certain value after each cycle (force value). In online mode one also can change the variable
value by double click on the value. By that boolean variables change from TRUE to FALSE or the
other way round, for each other types of variables one gets the dialog
Write Variable xy
, where the
actual value of the variable can be edited.
In Online mode, all displayable variables are read from the controller and displayed in real time. You
will find this display in the declarations and program editor; you can also read out current values of
variables in the Watch- and Recipe Manager and in a visualization. If variables from instances of
function blocks are to be monitored, the corresponding instance must first be opened.
In monitoring VAR_IN_OUT variables, the de-referenced value is output.
In monitoring pointers, both the pointer and the de-referenced value are output in the declaration
portion. In the program portion, only the pointer is output:
+ --pointervar = '<'pointervalue'>'
POINTERs in the de-referenced value are also displayed accordingly. With a simple click on the cross
or a double-click on the line, the display is either expanded or truncated.
2 - What is What in CoDeSys
Example for Monitoring of Pointers
In the implementations, the value of the pointer is displayed. For de-referencing, however, the de-
referenced value is displayed.
Monitoring of ARRAY components: In addition to array components indexed by a constant,
components are also displayed which are indexed by a variable:
anarray[i] = 1
If the index consists of an expression (e.g. [i+j] or [i+1]), the component can not be displayed.
If the maximum number of variables which can be monitored, has been reached, for each further
variable instead of the current value the string "Too many monitoring variables" will be displayed.
During the simulation the created PLC program is not processed in the PLC, but rather in the
calculator on which
is running. All online functions are available. That allows you to test the
logical correctness of your program without PLC hardware.
POUs of external libraries do not run in simulation mode.
The log chronologically records user actions, internal processes, state changes and exceptions during
Online mode processing. It is used for monitoring and for error tracing (see Online Functions).
2.4 The Standard...
The standard IEC 61131-3 is an international standard for programming languages of Programmable
The programming languages offered in
conform to the requirements of the standard.
According to this standard, a program consists of the following elements:
Structures (see Data Types)
The general language elements are described in the sections Identifier, Addresses, Types,
Comments, and Constants.
The processing of a
program starts with the special POU PLC_PRG. The POU PLC_PRG
can call other POUs.
3 - We Write a Little Program
3 We Write a Little Program
3.1 Controlling a Traffic Signal Unit...
Let us now start to write a small example program. It is for a simple traffic signal unit which is
supposed to control two traffic signals at an intersection. The red/green phases of both traffic signals
alternate and, in order to avoid accidents, we will insert yellow or yellow/red transitional phases. The
latter will be longer than the former.
In this example you will see how time dependent programs can be shown with the language resources
of the IEC1131-3 standard, how one can edit the different languages of the standard with the help of
, and how one can easily connect them while becoming familiar with the simulation of
Starting always is easy: Start
and choose 'File' 'New'.
In the dialog box which appears, the first POU has already been given the default name PLC_PRG.
Keep this name, and the type of POU should definitely be a program. Each project needs a program
with this name. In this case we choose as the language of this POU the Continuous Function Chart
Now create three more objects with the command 'Project' 'Object Add'
with the menu bar or with the
context menu (press right mouse button in the Object Organizer). A program in the language
Sequential Function Chart (SFC) named SEQUENCE, a function block in the language Function Block
Diagram (FBD) named TRAFFICSIGNAL, along with a POU WAIT, also of the type function block,
which we want to program as an Instruction List (IL).
What does TRAFFICSIGNAL do?
In the POU TRAFFICSIGNAL we will assign the individual trafficsignal phases to the lights, i.e. we will
make sure that the red light is lit red in the red phase and in the yellow/red phase, the yellow light in
the yellow and yellow/red phases, etc.
What does WAIT do?
In WAIT we will program a simple timer which as input will receive the length of the phase in
milliseconds, and as output will produce TRUE as soon as the time period is finished.
What does SEQUENCE do?
In SEQUENCE all is combined so that the right light lights up at the right time for the desired time
What does PLC_PRG do?
In PLC_PRG the input start signal is connected to the traffic lights' sequence and the "color
instructions" for each lamp are provided as outputs.
Now test your program in simulation mode. Compile (
) and load (
Start the program by
, then set variable ON to TRUE, e.g. by a double-click on the
entry "ON" in the input box of the CFC editor. This will mark the variable as prepared to be set to
<TRUE>. Then press <Ctrl><F7> or command 'Online' 'Write values', to set the value. Now variable
START in ABLAUF (which we had set to TRUE manually in the first extension level of the program)
gets this value by variable ON, which is used in PLC_PRG. This will make run the traffic light cycles.
PLC_PRG has changed to a monitoring window. Click twice on the plus sign in the declaration editor,
the variable display drops down, and you can see the values of the individual variables.
Controlling a Traffic Signal Unit...
Let us now turn to the POU TRAFFICSIGNAL. In the declaration editor you declare as input variable
(between the keywords VAR_INPUT and END_VAR) a variable named STATUS of the type INT.
STATUS will have four possible conditions, that is one for the TRAFFICSIGNAL phases green, yellow,
Correspondingly our TRAFFICSIGNAL has three outputs, that is RED, YELLOW and GREEN. You
should declare these three variables. Then the declaration part of our function block TRAFFICSIGNAL
will look like this:
Function block TRAFFICSIGNAL, declaration part
Now we determine the values of the output variables depending on the input STATUS of the POU. To
do this go into the body of the POU. Click on the field to the left beside the first network (the gray field
with the number 0001). You have now selected the first network. Choose the menu item 'Insert' 'Box'.
In the first network a box is inserted with the operator AND and two inputs:
Click on the text AND, so that it appears selected and change the text into EQ. Select then for each of
the two inputs the three question marks and overwrite them with "STATUS" respectively "1".
Click now on a place behind the EQ Box. Now the output of the EQ operation is selected. Choose
Change the three question marks ??? to GREEN. You now have created a network
with the following structure:
STATUS is compared with 1, the result is assigned to GREEN. This network thus switches to GREEN
if the preset state value is 1.
Documents you may be interested
Documents you may be interested