0% found this document useful (0 votes)
170 views73 pages

Function Block Introduction Guide R133-E1-01

The document describes a function block implementation guide that: - Explains basic operations for using OMRON's function block library and hints for creating programs with function blocks. - Provides an overview of OMRON's function block library, including its benefits, examples of use, and included content. - Details how to customize existing function blocks in the library by adding variables or changing the function block logic.

Uploaded by

laba libi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views73 pages

Function Block Introduction Guide R133-E1-01

The document describes a function block implementation guide that: - Explains basic operations for using OMRON's function block library and hints for creating programs with function blocks. - Provides an overview of OMRON's function block library, including its benefits, examples of use, and included content. - Details how to customize existing function blocks in the library by adding variables or changing the function block logic.

Uploaded by

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

CX-One / CX-Programmer CD-ROM contains an operation manual PDF file.

Before using this product, you must read “Introduction”, “Safety Cautions”, and “Precautions for
Safe Use”.
“Function Block Implementation Guide” describes basic operations to use OMRON FB Library and hints to create user
program with Function Blocks.
Cautions and detailed explanation are available on Help and PDF Manual.
* Acrobat Reader 4.0 or later is required to read the PDF file.
Contents
Chapter 1 OMRON FB Library
1. What is a Function Block?                                                             1-1
2. An Example of a Function Block                                                        1-2
3. Overview of the OMRON FB Library                                                    1-3
3-1. Benefits of the OMRON FB Library                                                   1-3
3-2. Example of using the OMRON FB Library                                             1-4
3-3. Content of the OMRON FB Library                                                   1-6
3-4. File Catalog and Where to Access the OMRON FB Library                                1-7
Chapter 2 How to use the OMRON FB Library
1. Explanation of the target program                                                      2-1
1-1. Application Specifications                                                          2-1
1-2. Specifications of the OMRON FB Part file                                             2-1
1-3. Input program                                                                   2-2
2. Opening a new project and setting the Device Type                                        2-3
3. Main Window functions                                                               2-4
4. Import the OMRON FB Part file                                                        2-5
5. Program Creation                                                                   2-6
5-1. Enter a Normally Open Contact                                                      2-6
5-2. Entering an Instance                                                              2-7
5-3. Entering Parameters                                                              2-7
6. Program Error Check (Compile)                                                       2-9
7. Going Online                                                                       2-10
8. Monitoring - 1                                                                      2-11
9. Monitoring - 2 Change Parameter Current Value                                          2-12
10. Online Editing                                                                     2-13

Chapter 3 Customize the OMRON FB Part file


1. Explanation of target program                                                         3-1
1-1. Changing File Specifications                                                        3-1
1-2. Changing the contents of the OMRON FB Part file                                       3-1
2. Copy the OMRON FB Part file                                                         3-2
3. Add a variable to the Function Block                                                    3-3
4. Changing the Function Block Ladder                                                    3-4
4-1. Entering a Contact  3-4
4-2. Checking Usage Status of Variables                                                  3-5

Chapter 4 How to use the ST (Structured Text) language


1. What is the ST Language?                                                           4-1
2. Explanation of the target program                                                      4-1
3. Create a Function Block using ST                                                      4-2
4. Entering Variables into Function Blocks                                                  4-3
5. Entry of ST program                                                                 4-4
6. Entering the FB to the Ladder Program and error checking                                  4-5
7. Program Transfer                                                                   4-6
8. Monitoring the Function Block execution                                                 4-7
Reference: Example of an ST program using IF-THEN-ELSE-END_IF                           4-8

Chapter 5 Advanced (Componentizing a Program Using FB)


1. Overview                                                                          5-1
2. How to Proceed Program Development                                                 5-1
3. Application Example                                                                 5-1
4. How to Proceed Program Development                                                  5-2
5. Entering FB Definition                                                                5-9
6. Creating FB Definition Library                                                          5-14
7. Entering Main Program                                                              5-15
8. Debugging Main Program                                                            5-16

Supplemental Information
How to delete unused Function Block definitions
Memory allocation for Function Blocks
Useful Functions
Appendix. Examples of ST (Structured Text)                                                 Appendix
Introduction
This document summarizes tips for using OMRON FB Library and creating function block (FB), which are available for
Omron’s SYSMAC CS1/CJ1-H/CJ1M series CPU unit (unit version 3.0 or later) and CX-Programmer Ver.5.0 or later.

New functions available on CX-Programmer Ver. 6.0


Nesting of FB
Nesting of FBs is now available for easier use of FB as a method to structure and reuse user programs. FB can be called
from a ladder program structured text (ST) program that is converted to FB. Following functions are also supported for this.

Easy understanding of program structure . . . FB Instance Viewer


Management as components including called FBs . . . Saving and loading of files including called FB
Easy jump to called FB . . . Mouse double-click operation on FB Instance (calling instruction)

Monitoring of FB ladder
As with main ladder program, state of FB ladder program can be monitored.

Cross reference pop-up in FB ladder


As with main ladder program, cross reference pop-up is now available in FB ladder program.
Also supported now is jump to output coil from contact via space key.

Jump to ST help
You can jump Structured Text editor through pop-up menu to a help topic to easily check syntax for ST programming.

Jump to library reference of OMRON FB Library


You can easily view a PDF file of library cross reference that describes specifications from a OMRON FB Library
registered in a project file.

Caution:
Although you can use a program that contains nested FB for a CS1/CJ1-H/CJ1M series CPU unit (unit version 3.0 or
later), if you try to upload a program that contains nested FB by CX-Programmer (Ver.5.0 or older) that does not support
nesting, either you will fail to do so or you will just get one under incomplete state.
If you save the file as it is, you will not be able to tell difference between the incomplete and correct programs.

[In case of CX-Programmer Ver.5.0]


Following messages will be shown after upload is finished:
The PLC properties which are not supported by this version of CX-Programmer are set in the connecting target PLC.
The PLC properties will not be displayed correctly. Do you wish to continue?

[In case of CX-Programmer Ver.4.0]


Following message will be shown after upload is finished:
Function Block or other data besides Ladder are included in the programs.

[In case of CX-Programmer Ver.3.*]


When upload is finished, a message "Decompile error" is displayed and no program will be shown.
OMRON FB Library

1.
1. What
What is
is aa Function
Function Block?
Block?
“Function Blocks” are predefined programs (or functions) contained within a single program element that may be
used in the ladder diagram. A contact element is required to start the function, but inputs and outputs are editable
through parameters used in the ladder arrangement.
The functions can be reused as the same element (same memory) or occur as a new element with its own memory
assigned.

Partial Ladder program for machine A Defining Inputs and Outputs …

Input
Input Process (algorithm) Output
Output

Control Device 1 Control Device n

A1 B1 X1 An Bn Xn
An
An
Xn
Xn
Bn
Bn

TIM TIM
1 n
#0100 #0100

T1 Y1 Tn Yn
Yn
Yn

Z1 Zn
Zn
Zn

Produce template
Partial Ladder program for machine A
Function Block definition

Control Device 1
Device Control
P_On Device Control
EN ENO EN ENO
A1 A X X1
B1 B Y Y1 Allocate to
Z Z1 Ladder A X
program
Sets input / output B Y
Control Device 2 parameters Function Block
Instance (invocation)
Z
P_On Device Control
EN ENO
A2 A X X2
B2 B Y Y2
Z Z2

Function Block definition … This contains the defined logic (algorithm) and I/O interface. The memory addresses are not allocated in the Function Block Definition
Function Block instance (invocation) … This is the instruction that will call the function block instance when used by the ladder program, using the memory allocated
to the instance
OMRON FB Library

2.
2. An
An Example
Example of
of aa Function
Function Block
Block

The following figures describe an example of a function block for a time limit circuit, to be used in the ladder. It is
possible to edit the set point of the TIM instruction to reallocate the set time for turning off the output in the ladder
rung. Using the function block as shown below, it is possible to make the time limit of the circuit arbitrary by only
changing one specific parameter.

By enabling the input parameter to


Ladder diagram be editable, it is possible to allow
an arbitrary time limit circuit.

000.00 TIM0000 TIM


0000 P_On PULSE
#0020 EN ENO

001.00 Start Q 001.00


000.00
001.00 Time
#0020

Timing chart

Start T_FB
000.00 TIM
T_FB
Time
001.00 Q

2.0 sec Q
OMRON FB Library

3.
3. Overview
Overview of
of the
the OMRON
OMRON FB
FB Library
Library
The OMRON FB Library is a collection of predefined Function Block files provided by Omron. These files are
intended to be used as an aid to simplify programs, containing standard functionality for programming PLCs
and Omron FA component functions.

3-1. Benefits of the OMRON FB Library


The OMRON FB Library is a collection of function block examples that aim to improve the connectivity of the
units for PLCs and FA components made by Omron. Here is a list of the benefits to be gained from using the
OMRON FB Library:

(1) No need to create ladder diagrams using basic functions of the PLC units and FA components
More time can be spent on bespoke programs for the external devices, rather than creating basic
ladder diagrams, as these are already available.

(2) Easy to use


A functioning program is achieved by loading the function block file to perform the target functionality,
then by inputting an instance (function block call instruction: invocation) to the ladder diagram
program and setting addresses (parameters) for the inputs and outputs.

(3) Testing of program operation is unnecessary


Omron has tested the Function Block library. Debugging the programs for operating the unit and FA
components for the PLCs is unnecessary for the user.

(4) Easy to understand


The function block has a clearly displayed name for its body and instances. A fixed name can be
applied to the process.
The instance (function block call instruction: invocation) has input and output parameters. As the
temporary relay and processing data is not displayed, the values of the inputs and outputs are more
visible. Furthermore, as the modification of the parameters is localised, fine control during debugging
etc. is easier.
Finally, as the internal processing of the function block is not displayed when the instance is used in
the ladder diagram, the ladder diagram program looks simpler to the end user.

(5) Extendibility in the future


Omron will not change the interface between the ladder diagram and the function blocks. Units will
operate by replacing the function block to the corresponding FB for the new unit in the event of PLC
and the FA component upgrades, for higher performance or enhancements, in the future.

A fixed name can be named to the processes.

It is not necessary to create the basic


communications program.

INPUT/OUTPUT data is clear.


Parameters are easy to understand and edit.
OMRON FB Library

3-2-1. Example of using the OMRON FB Library - 1

Controlling the predefined components made by Omron can be easily achieved from the PLC ladder diagram.

- Ability to configure low-cost communications (RS-232C/485)

FB Access by Function Block


CS/C J Series
PLC
Example: Communication between
Temperature controller and PLC

Serial communications (Compoway/F protocol)

Temperature
controller
Smart sensor Vision sensor

Omron Components
OMRON FB Library

3-2-2. Example of using the OMRON FB Library - 2

High performance communications can be made by DeviceNet level.

- Ability to communicate between PLC and DeviceNet slaves easily.

Access by Function block


FB
CS/CJ series
PLC Example: data exchange between
PLC and the slave devices

Communicate between
PLC and the devices

DeviceNet

Temperature
controller
Generic slaves such as IO terminal PLC Wireless

Inverter
OMRON FB Library

3-3. Content of the OMRON FB Library


The OMRON FB Library consist of the following:

3-3-1. OMRON FB Part Files


The OMRON FB Part file is prepared using the ladder diagram function block, for defining each function of
the PLC unit and the FA component.
The files contain a program written in ladder diagram and have the extension .CXF.
The file name of the OMRON FB Part file begins with ‘_’ (under score).
When the OMRON FB Library is installed onto a personal computer, the OMRON FB Part files are
classified in the folder appropriate to each PLC Unit and FA component in the Omron Installation directory.

3-3-2. Library reference

The library reference describes the operation specifications of the OMRON FB Part file, and the specifications
of the input and the output parameters for each. The file format for this is PDF.
When the OMRON FB Library is used, the user should select the OMRON FB Part file, set the input / output
parameters, and test the program operations referring to the library reference.
OMRON FB Library

3-4. File Catalog and Where to Access the OMRON FB Library

3-4-1. Catalog of OMRON FB Library files

Number of OMRON
Type Target components FB Part files (at the
time of February ’05)

FA components Temperature controller, Smart sensor, ID approx. 80


sensor, Vision sensor, 2 dimensions bar
code reader, Wireless terminal

PLC CPU unit, Memory card, Special CPU IO approx. 95


unit (Ethernet, Controller Link, DeviceNet
unit, Temperature control unit)

Motion control Position control unit approx. 70


components Inverter
Servo motor driver

3-4-2. CX-One / CX-Programmer installation CD


CX-One / CX-Programmer Ver.6.0 installation CD contains the OMRON FB Library files.
The user can select to install the OMRON FB Library during the installation of CX-One / CX-Programmer
Ver.6.0.

CX-Programmer Ver.6.0
Install CD

3-4-3. Accessing OMRON FB Library files from Web server


The latest version OMRON FB Library files are provided by Omron on the Web server.
New files will be added to support new or enhanced PLC units and FA components.
The download service of the OMRON FB Library is provided as a menu of Omron Web in each country.

Web server
The internet

The internet
Offline Operation
Explanation
Explanation of
of Opening
Opening aa Import
Import Creating
Creating aa Program
Program Check
Check
target
target Program
Program new
new project
project FB
FB Library
Library program
program

1.
1. Explanation
Explanation of
of the
the target
target program
program
This chapter describes how to use OMRON FB Library using the OMRON FB Part file ‘Make ON Time/OFF
Time Clock Pulse in BCD’.

1-1. Application Specifications


The target application specifications are as follows :-
- Pulse is generated after PLC mode is changed to‘run’ or ‘monitor’ mode.
- Output the pulse to address 1.00.
- On time of generated pulse is set at D100.
- Off time of generated pulse is 2 seconds.

1-2. Specifications of the OMRON FB Part file


The OMRON FB Part file ‘Make ON Time/OFF Time Clock Pulse in BCD’ has the following specifications:-
Offline Operation
Explanation
Explanation of
of Opening
Opening aa Import
Import Creating
Creating aa Program
Program Check
Check
target
target Program
Program new
new project
project FB
FB Library
Library program
program

1-3. Input program

Create the following ladder program:-

[Reference] If created as a straightforward ladder diagram, the program would be as below:-


Offline Operation
Explanation
Explanation of
of Opening
Opening aa Import
Import Creating
Creating aa
Import
Import FB
FB Library
Library Program
Program Check
Check
target
target Program
Program new
new project
project FB
FB Library
Library program
program

2.
2. Opening
Opening aa new
new project
project and
and setting
setting the
the Device
Device Type
Type
Click the toolbar button [New] in CX-Programmer.

Click

Click the left mouse button.

! To use Function Blocks, select the following PLCs:


CS1G-H, CS1H-H, CJ1G-H, CJ1H-H, CJ1M

Click the left mouse button

Click the left mouse button


to select CPU type.

Click [OK] to decide


the selected CPU type.
3.
3. Main
Main Window
Window functions
functions

The main window functionality is explained here.

Title bar

Menus

Tool bar

Project
Project Tree
Tree

Section
Section

Function
Function Block
Block Definition
Definition

Status bar

Project
Project Workspace
Workspace Ladder
LadderWindow
Window

Name Contents / Function

Title Bar Shows the file name of saved data created in CX-Programmer.

Menus Enables you to select menu items.

Toolbars Enables you to select functions by clicking icons. Select [View] -> [Toolbars], display
toolbars. Dragging toolbars enables you to change the display positions.

Section Enables you to divide a program into several blocks. Each can be created and
displayed separately.
Project Workspace Controls programs and data. Enables you to copy element data by executing Drag and
Project Tree Drop between different projects or from within a project.
Ladder Window A screen for creating and editing a ladder program.

Shows Function Block definition.


Function Block Definition By selecting the icons, you can copy or delete the selected Function Block definition.
- is shown if the file is a OMRON FB Part file.
- In the case of a User-defined Function Block, is shown if Ladder, is shown if ST.

Status Bar Shows information such as a PLC name, online/offline state, location of the active cell.
Offline Operation
Explanation
Explanation of
of Opening
Opening aa Import
Import Creating
Creating aa Program
Program Check
Check
target
target Program
Program new
new project
project FB
FB Library
Library program
program

4.
4. Import
Import the
the OMRON
OMRON FB
FB Part
Part file
file
Select Function Block definition icon from the project tree using the mouse cursor, right click.
Select Insert Function Block, then select a Library file using mouse to navigate.

Click mouse right button


→ Insert Function Block
→ Library File

Double click mouse left button.


→ [OmronLib]
Select the necessary OMRON FB
→ [Programmable Controller] Double click mouse left button
→ [CPU]
Part file in the ‘Select Function
Select each of the above in Block Library’ dialog.
series.

! The default path of the OMRON FB Library is


C: /Program Files /Omron /CX-One /Lib /FBL.

Left Click
‘_CPU007_MakeClockPulse
_BCD.cxf’

Left Click the [Open] button

! You can easily check specifications of


OMRON FB part files by selecting
registered OMRON FB part files and [FB
Library Reference] from a pop-up menu
Function Block definition ‘_CPU007_MakeClockPulse_BCD’ and showing a library reference file.
is registered as part of the project file.

Function
Function Block
Block Definition
Definition
Offline Operation
Explanation
Explanation of
of Opening
Opening aa Import
Import Creating
Creating aa Program
Program Check
Check
target
target Program
Program new
new project
project FB
FB Library
Library program
program

5. Program Creation

Confirm cursor position is at the upper left of Ladder Window to start programming.

5-1. Enter a Normally Open Contact

C Press the [C] key on the keyboard to open the [New Contact] dialog. Use the
dropdownbox to select the “P_On” symbol.

P_On

ENT

Deleting instructions
• Move the cursor to the instruction and
then press the DEL key or
• Move the cursor to the right cell of
the instruction and press the BS key.

“P_On” is a system defined symbol. Its state is always ON.


0 of the upper digit of an address is omitted when shown.
[.] (period) is displayed between a channel number and a relay number.
Offline Operation
Explanation
Explanation of
of Opening
Opening aa Import
Import Creating
Creating aa Program
Program Check
Check
target
target Program
Program new
new project
project FB
FB Library
Library program
program

5-2. Entering an Instance

F Press the [F] key on the keyboard to open the [New Function Block Invocation] dialog.

Enter text to create an FB


instance name.
[WorkInputTimingGenerator]

Applies
Applies aa name
name forfor the
the specific
specific
process
process inin the
the diagram.
diagram.

ENT Shows FB instance (invocation) ‘WorkInputTimingGenerator’.

5-3. Entering Parameters

P or ENT Move the cursor to the left of input parameter.

Enter the address.

[d100]

ENT Choose an address for the input


parameter ‘OnTime’.
Offline Operation
Explanation
Explanation of
of Opening
Opening aa Import
Import Creating
Creating aa Program
Program Check
Check
target
target Program
Program new
new project
project FB
FB Library
Library program
program

Enter the remaining parameters in the same way.

P Or ENT

#10

Please add the following prefix for


entering constants as parameters:
ENT “#” (Hexadecimal/BCD)
Or
“&” (Decimal)

1.00

ENT

[Generated Pulse]

ENT
Offline Operation
Explanation
Explanation of
of Opening
Opening aa Import
Import Creating
Creating aa Program
Program Check
Check
target
target Program
Program new
new project
project FB
FB Library
Library program
program

6. Program Error Check (Compile)

Before program transfer, check for errors using the program compile.

Click

Errors
Errorsand
andaddresses
addressesare
are
displayed
displayedininthe
theOutput
OutputWindow.
Window.

Double-click
Double-clickon ondisplayed
displayederrors,
errors,
and theLadder
andthe LadderDiagram
Diagram cursor
cursor
will
willmove
moveto tothe
thecorresponding
corresponding
error
errorlocation,
location,displaying
displayingthe
theerror
error
rung
rungininred.
red.

• Output Window automatically opens at program check.


Modify
Modifythe
theerror.
error.
• The cursor moves to an error location by pressing J or F4 key.
• Output Window closes by pressing the ESC key.
Online Operation
Online
Online Online
Online
Monitoring
Monitoring
to
to transfer
transfer Edit
Edit

7. Going Online

CX-Programmer provides three methods of connecting, depending on usage.

Normal
Normal online.
online. Enables
Enables you
you to
to go
go online
online with
with aa PLC
PLC of
of the
the device
device type
type and
and method
method specified
specified when
when opening
opening aa project.
project.

Auto
Auto online.
online. Automatically
Automaticallyrecognizes
recognizes the
the connected
connected PLC
PLC and
and enables
enables you
you to
to go
go online
online with
with aa PLC
PLC with
with one
one button.
button.

→Uploads
Uploads all
all data,
data, such
such as
as programs,
programs, from
from the
the PLC.
PLC.

Online
Online with
with Simulator.
Simulator. Enables
Enables you
you to
to go
go online
online with
with CX-Simulator
CX-Simulator with
with one
one button
button (CX-Simulator
(CX-Simulator must
must be
be installed.)
installed.)

Online/debug
Online/debug functions
functions when
when working
working online
online with
with CX-Simulator
CX-Simulator are
are
explained
explained in
in this
this guide
guide (Install
(Install CX-Simulator
CX-Simulatorseparately).
separately).

Click

Click [OK]
Program transfer starts.

Click [OK]

The
The background
background color
color of
of
The
The CX-Simulator
CX-Simulator the
the Ladder
LadderWindow
Window
Console
Console box
boxisis shown.
shown. changes
changes to
to gray.
gray.

The
The operating
operating mode
mode ofof
the
the active
active PLC
PLCisis shown.
shown.

Scan
Scan time
time isis displayed
displayed
(except
(except during
during Program
Program Mode).
Mode).
Online Operation
Online
Online Online
Online
Monitoring
Monitoring
to
to transfer
transfer Edit
Edit

8. Monitoring - 1
Change
Change the
the PLC
PLC (Simulator)
(Simulator)
to
to Monitor
Monitormode.
mode. The on/off status of contacts and coils can be monitored.

Click

If your program has a large volume of data,


the scroll speed of the screen may become
slow when monitoring.
Click [Yes].
To resolve this, click the icon below to cancel
monitoring, scroll to the address you want to
monitor, then restart the monitor mode.

xxx toggles PLC monitoring on/off

The
The monitored
monitored area
area isis
displayed
displayed in
in aa specified
specified
color.
color.

The
The current
current values
values of
of
parameters
parameters are
are shown.
shown.
Online Operation
Online
Online Online
Online
Monitoring
Monitoring
to
to transfer
transfer Edit
Edit

9. Monitoring - 2 Change Parameter Current Value

Change the current value of contact/coils or word data in the Ladder Window.

Move the cursor to the input


parameter ‘D100’.

Click mouse right button and


select the menu item
[Set/Reset(S)]
→ [Setting Value (V)]

Or

Double click mouse left button. Change


Change the
the current
current value
value of
of Input
Input
parameter.
parameter.

Click
Click [Set]
[Set]
Or

ENT Please add the following prefix for


entering constants as parameters:
“#” (Hexadecimal/BCD)
Or
“&” (Decimal)
Online Operation
Online
Online Online
Online
Monitoring
Monitoring
to
to transfer
transfer Edit
Edit

10. Online Editing

Move the cursor to the rung


requiring modification.

You can also select multiple


rungs by using the Drag & Drop
facility with the mouse.

Select [Program] → [Online Edit]


→ [Begin]

Shortcut: [Ctrl]+[E]

Move the cursor to a instruction


you want to modify. Double
click the left mouse button.

Double click

Edit the address to the required bit number (4.11 in the example)

Select [Program] → [Online Edit]


→ [Send Change]

Shortcut: [Ctrl]+{Shift]+[E]

End
Explanation
Explanation of
of Change
Change ofof
Copy
Copy of
of FB
FB part
part
target
target Program
Program FB
FB Definition
Definition

1. Explanation of target program

This chapter describes how to customize the OMRON FB Library using the OMRON FB Part file ‘Make ON
Time/OFF Time Clock Pulse in BCD’.

1-1. Changing File Specifications

The OMRON FB Part file ‘Make ON Time/OFF Time Clock Pulse in BCD’ is designed to repeatedly turn off
the ENO for the specified OffTime (unit: 100 msec) and on for the specified OnTime (unit: 100 msec). In this
example, the OMRON FB Part file will be changed to output an invert signal by adding the output parameter
‘INV_ENO’.

EN ON
OFF

OnTime(*100ms)

ENO ON
OFF

OffTime(*100ms)

INV_ENO ON
OFF

1-2. Changing the contents of the OMRON FB Part file

To satisfy the requirement described above, the following changes must be made to OMRON FB Part file
‘Make ON Time/OFF Time Clock Pulse in BCD’
1. Add an output parameter ‘INV_ENO’.
2. Add ladder program to output the ENO for inverting the signal.

Caution
Caution
OMRON
OMRON cannot
cannot guarantee
guarantee the
the operation
operation of
of aa customized
customized OMRON
OMRON FB FB parts.
parts. Please
Please be
be sure
sure to
to check
check the
the process
process of
of
your
your FB
FB part
part sufficiently
sufficiently before
before customization
customization and
and confirm
confirm the
the operation
operation of
of each
each FB
FB parts
parts thoroughly
thoroughly after
after that.
that.
Explanation
Explanation of
of Change
Change ofof
Copy
Copy of
of FB
FB part
part
target
target Program
Program FB
FB Definition
Definition

2. Copy the OMRON FB Part file


Import the ‘Make ON Time/OFF Time Clock Pulse in BCD’ Function Block Part file as explained
in Chapter 1 (FB definition name: _CPU007_MakeClockPulse_BCD)

Select the OMRON FB Part icon


then right click the mouse. 
→ Copy

Select Function Block Definition


icon and right click the
mouse.
→ Paste

The OMRON FB Part file is


pasted.

Change the FB definition name.

Select pasted Function Block Note:


Note:
icon and click mouse right The
The user
user can’t
can’t create
create Function
Function Block
Block Definitions
Definitions
button. With
With name
name starting
starting ‘_’
‘_’ (underscore).
(underscore).
→ Rename Please
Please use
use names
names not not starting
starting with
with ‘_’.
‘_’.
[MakeClockPulse_BCD_INV]

Enable editing of the internal FB Program code.

Select pasted Function Block


icon and right click the mouse
button.
→ Property

     Or

ALT + ENT
Tick
Tick the
the check
check box
box using
using the
the left
left mouse
mouse click.
click.
Explanation
Explanation of
of Change
Change ofof
Copy
Copy of
of FB
FB part
part
target
target Program
Program FB
FB Definition
Definition

3. Add a variable to the Function Block


Variable
Variable Table
Table
Open the Function Block Ladder Editor.

Opens
Opens the
the Function
Function
Block
Block Ladder
Ladder Editor.
Editor.

Select the Function Block icon


using the mouse cursor and
double click the left mouse button.

The
The original
original OMRON
OMRON FB
FB Part
Part file
file is
is also
also able
able to
to display
display
its ladder program, but cannot be edited.
its ladder program, but cannot be edited.

Ladder
Ladder Editor
Editor
Variable table

Select Output tab in Variable


Table using the mouse cursor
And click the left mouse button.

Enter
Enter aa new
new variable
variable name.
name.

Click the left mouse button and


select Insert Variable(I).

Select
Select BOOL
BOOL for
for bit
bit data.
data.

Confirm the entered variable is


correct.
Explanation
Explanation of
of Change
Change ofof
Copy
Copy of
of FB
FB part
part
target
target Program
Program FB
FB Definition
Definition

4. Changing the Function Block Ladder

Add the required ladder diagram on Function Block Ladder edit field.
Move the cursor to the left column of the next rung.

4-1. Entering a Contact

ENO

ENT

INV_ENO

ENT
Explanation
Explanation of
of Change
Change ofof
Copy
Copy of
of FB
FB part
part
target
target Program
Program FB
FB Definition
Definition

4-2. Checking Usage Status of Variables

As with main ladder program, you can use cross reference pop-up to check usage conditions of
variables.

Display cross reference


pop-up.

Alt + 4
Move
Move the
the cursor.
cursor.

Select LDNOT from cross


reference pop-up by the
mouse cursor.

You can see that variable ENO is used in an output coil in the step No.20 as well.

The cursor in the FB Ladder Editor moves to the output coil in the step No.20.
Offline Operation

Explanation
Explanation of
of Create
Create new
new Entering
Entering Creating
Creating Creating
Creating Ladder
Ladder
target
target Program
Program FB
FB Definition
Definition Variables
Variables ST
ST Program
Program Program
Program and
and check
check

1. What is the ST Language?

The ST (Structured Text) language is a


high-level language code for industrial
controls (mainly PLCs) defined by the IEC
61131-3 standard.
It has many control statements, including
IF-THEN-ELSE-END_IF, FOR / WHILE loop,
and many mathematical functions such as
SIN / LOG. it is suitable for mathematical
processing.
The ST language supported by CX-
Programmer is in conformance with IEC
61131-3 standard.
The arithmetic functions in CX-Programmer
Ver.5/6 are as follows:
sine (SIN), cosine (COS), tangent
(TAN), arc-sine (ASIN), arc-cosine
(ACOS), arc-tangent (ATAN),
square root (SQRT), absolute
value (ABS), logarithm (LOG),
natural-logarithm (LN), natural-
exponential (EXP), exponentiation
(EXPT)

Reference: The IEC 61131 standard is an international standard for programming Programmable Logic
Controllers (PLC), defined by the International Electro-technical Commission (IEC).
The standard consists of 7 parts, with part 3 defining the programming of PLCs.

2. Explanation of the target program


This example describes how to create an ST program in a Function Block to calculate the average value of a
measured thickness.

The
The data
data type
type should
should bebe set
set to
to REAL
REAL to to store
store the
the data.
data.
REAL
REAL type
type allows
allows values
values with
with 3232 bits
bits of
of length,
length, see
see range
range below:-
below:-
38 -38
-3.402823
-3.402823 xx 10
1038 ~~ -1.175494
-1.175494 xx 10 10-3 ,0,0,
-38 38
+1.175494
+1.175494 xx 10
10-38 ~~ +3.402823
+3.402823 xx 10 1038

FB
FB definition
definition name
name AverageCalc_3Value
AverageCalc_3Value
Input
Input symbols
symbols xx(REAL type), y
(REAL type), y(REAL type), z
(REAL type), z(REAL
(REAL type)
type)
Output
Output symbol
symbol score
score(REAL(REAL type)
type)
ST
ST Program
Program definition
definition score
score := := ((xx ++ yy ++ zz)) // 3.0;
3.0;

Substitute
Substitute aa value
value to
to aa symbol
symbol is
is expressed
expressed by
by ““ :=
:= ”.”. Enter
Enter““ ;; ””(semicolon)
(semicolon) to
to
complete
complete the the code.
code.
Offline Operation

Explanation
Explanation of
of Create
Create new
new Entering
Entering Creating
Creating Creating
Creating Ladder
Ladder
target
target Program
Program FB
FB Definition
Definition Variables
Variables ST
ST Program
Program Program
Program and
and check
check

3. Create a Function Block using ST

Create a Function Block using


Structured Text.

Select the Function Block icon


using a mouse cursor, and
click the right mouse button.
→ Insert Function Block(I)
→ Structured Text(S)

A New Function Block


definition is created.

Change
Change the
the Function
Function Block
Block definition
definition name
name

Note:
Note:
The
The user
user can’t
can’t create
create Function
Function Block
Block Definitions
Definitions
with
with names
names starting
starting ‘_’
‘_’ (underscore).
(underscore).
Select the Function Block Please
Please use
use names
names notnot starting
starting with
with ‘_’.
‘_’.
definition icon using
the mouse cursor and right
click the mouse button.
Select Paste. Variable
Variable Table
Table
→ Rename
Enter
[AverageCalc_3value]

Open
Open Function
Function Block
Block ST
ST Editor
Editor

Select Function Block definition ST


ST Edit
Edit Field
Field
Icon by mouse cursor and
double click the left mouse
button.
Offline Operation

Explanation
Explanation of
of Create
Create new
new Entering
Entering Creating
Creating Creating
Creating Ladder
Ladder
target
target Program
Program FB
FB Definition
Definition Variables
Variables ST
ST Program
Program Program
Program and
and check
check

4. Entering Variables into Function Blocks


Select Variable Table.

Select the Input tab using


the mouse cursor.

Select Insert from the Enter


Enter aa variable
variable name
name
Pop-up menu.

Select
Select REAL
REAL
Enter data for the following.
Name
Data type
Comment

Enter
Enter and
and applicable
applicable
comment
comment

Enter
Enter input
input symbol
symbol x,
x, output
output symbols
symbols y,z
y,z by
by repeating
repeating the
the process
process above.
above.

Input
Input Variables
Variables

Output
Output Variables
Variables

Reference:
Reference: The
The copy
copy and
and paste
paste operation
operation is
is available
available in
in FB
FB Header.
Header.

Reference:
Reference: The
The order
order of
of the
the variables
variables in in the
the FB
FB table
table becomes
becomes thethe order
order of
of parameters
parameters on on FB
FB
instance
instance (invocation)
(invocation) inin the the normal
normal ladder
ladder view.
view.
To
To change
change the
the order,
order, itit is
is possible
possible toto drag
drag && drop
drop variables
variables within
within the
the table.
table.

Output Variables

Input Variables

FB instance (invocation)
Offline Operation

Explanation
Explanation of
of Create
Create new
new Entering
Entering Creating
Creating Creating
Creating Ladder
Ladder
target
target Program
Program FB
FB Definition
Definition Variables
Variables ST
ST Program
Program Program
Program and
and check
check

5. Entry of ST program
Select the ST Editor text field in the Function Block ST Editor window.

Enter
Enter text
text into
into the
the field:
field: “score
“score :=
:= (x
(x ++ yy ++ z)
z) // 3.0;”.
3.0;”.

When the input expression is a real type


calculation, please enter the constant
value with decimal point and zero for
single decimal places, e.g. ‘3.0’.

Reference:
Reference: User
User may
may type
type Comments
Comments in in the
the ST
ST program.
program.
Enter
Enter ‘(*’
‘(*’ and
and ‘*)’
‘*)’ both
both ends
ends of
of comment
comment strings,
strings, see
see below.
below.
This
This is
is useful
useful for
for recording
recording change
change history,
history, process
process expressions,
expressions, etc.
etc.

Note:
Note: You
You can
can jump
jump to
to aa help
help topic
topic that
that shows
shows
ST
ST control syntax by selecting [ST
control syntax by selecting [ST Help]
Help]
from
from aa pop-up
pop-up menu
menu in in the
the ST
ST Editor.
Editor.
Offline Operation

Explanation
Explanation of
of Create
Create new
new Entering
Entering Creating
Creating Creating
Creating Ladder
Ladder
target
target Program
Program FB
FB Definition
Definition Variables
Variables ST
ST Program
Program Program
Program and
and check
check

6. Entering the FB to the Ladder Program and error checking

Enter the following FB into the ladder program.


Instance name: ThicknessAvarage
Input parameters: D0, D2, D4
Output parameter: D6 ItIt is
is able
able to
to jump
jump the
the referred
referred function
function
block
block definition
definition by
by entering
entering [Shift]+[F]
[Shift]+[F]
key
key whenwhen the
the cursor
cursor isis in
in the
the function
function
block instance.
block instance.

Refer
Refer page
page 2-7
2-7 for
for entering
entering FB
FB instances.
instances.
Entering
Entering ST
ST FB
FB instances
instances is
is the
the same
same
as
as entering
entering FB
FB Ladder
Ladder instances.
instances.

Perform a programs check before transferring the program.

Refer
Refer page
page 2-9
2-9 for
for program
program checking.
checking.
The
The functionality
functionality is
is the
the same
same as
as for
for Function
Function Block
Block Ladder
Ladder instances.
instances.

It is possible to change or add variables in the Function Block after inputting FB instance
into the ladder editor. If modified, the Ladder editor changes the color of the left bus-bar
of the rung containing the changed Function Block.
When this occurs, please select the instance in the Ladder Editor using the mouse
cursor, and select Update Function Block Instance (U) from the pop-up menu.
Online Operation

Transfer
Transfer Program
Program Monitoring
Monitoring

7. Program Transfer

Go online to the PLC with CX-Simulator and transfer the program.

Refer
Refer to
to page
page 2-10
2-10 for
for steps
steps to
to go
go online
online and
and
transfer
transfer the
the program.
program.
Change
Change the
the PLC
PLC
(Simulator)
(Simulator) to
to Monitor
Monitor
mode. The on/off status of contacts and coils can be monitored.
mode.

Click

Click [Yes]

Confirm that the PLC is


Monitor mode.
Online Operation

Transfer
Transfer Program
Program Monitoring
Monitoring

8. Monitoring the Function Block execution


Monitors the present value of parameters in the FB instance using the Watch Window.

Display the Watch Window.

Alt + 3

Open the Edit dialog.

ENT

Click Browse… button using


the mouse left button. Select
Select REAL(32bit
REAL(32bit floating
floating point)
point)

Click the button using the


left mouse button, then select
the following:
[Symbols of type]
[Name or address]

Select
Select ThicknessAvarage.x
ThicknessAvarage.x

When monitoring internal variables at debug phase,


collective registration is available in addition to the
individual registration on the Watch Window
through the operation shown here. For the details,
refer “5-8 Batch Registration to Watch Window”.
Click [OK] button using the
When the function block is a ladder, conducting
left mouse button. monitoring is available. For the details, refer “5-5
Operation Check- 1”
Reference: Example of an ST program using IF-THEN-ELSE-END_IF

The following ST program checks the average value calculated by the example of page 4-7 against a range (upper
limit or lower limit).

FB Definition: OutputOfDecisionResult
Input symbols: score(REAL type), setover(REAL type), setunder(REAL type)
Output symbols: OK(BOOL type), overNG(BOOL type), underNG(BOOL type)

ST program:

IF score > setover THEN (* If score > setover, *)


underNG := FALSE; (* Turn off underNG *)
OK := FALSE; (* Turn off OK *)
overNG := TRUE; (* Turn on overNG *)

ELSIF score < setunder THEN (* if score =< setover and score < setunder then *)
overNG := FALSE; (* Turn on overNG *)
OK := FALSE; (* Turn off OK *)
underNG := TRUE; (* Turn on underNG *)

ELSE (* if setover > score > setunder then*)


underNG := FALSE; (* Turn off underNG *)
overNG := FALSE; (* Turn off overNG *)
OK := TRUE; (* Turn off OK *)

END_IF; (* end of IF section*)

Example of an FB instance (the instance name is ‘ThicknessDecision’)


Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

1. Overview

This chapter describes how to componentize a user program with an example using function blocks.

2. How to Proceed Program Development

Generally shown below is a workflow to create a user program with componentization in the case of
the application example below. Deliberate consideration is required especially in program design
process.

(1) Program Design


(2) Creating Components
(2-1) Entering FB Component
(2-2) Debugging FB Component
(2-3) Creating FB Component Library (File Save)
(3) Using Components in Application
(3-1) Importing Components
(3-2) Using Components for Program
(3-3) Debugging Program
(4) Start-Up

3. Application Example

Shown here is a DVD inspection machine as an example for application.


Process can be primarily categorized into inspection, packing, and assortment.

Inspection Packing
OK

Measuring NG
DVD thickness
Assortment
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

4. How to Proceed Program Development

Application can be materialized by using hardware and software (program) through combination
of requirements.
Following sections describe how to proceed program design using an application example
described before.

4-1 Overview of Design Process


Detailed
Specifications should be repeatedly General Specifications

detailed and integrated to divide and Input from Client Specifications

classify them as shown in the right. Detailed


Specifications
Requirement
Specifications General
Specifications Detailed
for Device Specifications

Detailed
General
Specifications
Specifications

Detailed
Specifications

Detailing

Integrating

4-2 Extracting Requirement Specifications

Shown below are the extracted requirement specifications for this application.

Overview of DVD Inspection Machine (Requirement Specifications)


Req. 1. DVD should be inserted from a loader.
Req. 2. Thickness of DVD should be measured at 3 points. Average thickness of measurements
should be calculated. If it is within its threshold range, DVD should be assorted into a
stocker for good products, or a stocker for bad products if not.
Req. 3. Good DVDs should be packed into the case.
Req. 4. Packed DVDs should be packed into the paper box.
Req. 5. Paper boxes should be classified into 2 types. Switching frequency should be counted to
evaluate a life of limit switch adjacent to actuator of selection part.
Req. 6. Other requirements
* To simplify the description, this document focuses on a part of device (underscored).
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

4-3 Detailing Specifications and Extracting Similar Processes

By detailing the specifications, there you will find similar processes or ones that can be used universally.

Actuator control (Example of similar process)


In this example, you can regard cylinder control for assortment of good and bad products and actuator
control for paper box assortment as the same. Shown below are extracted requirements for these
processes.

• The process has 2 actuators for bilateral movement which operate under input condition for
each.
• Operation of each direction must be interlocked.
• The process has an input signal to reset its operation.

Average_Threshold Check (Example of universal process)


A process should be extracted that will be used universally even if the process itself is used only once
for this application. In this example, a process is extracted that calculates average of measured 3
thickness data of DVD and checks if it is within the threshold. Shown below are extracted requirements
for this process.

• Average of 3 measurements must be calculated.


• Average value must be checked if it is within upper and lower limits of the threshold.

These requirements are used as the base for components. Names of components are defined as
“ActuatorContro” FB and “AvgValue_ThresholdCheck” FB.

4-3-1 Creating Specifications for Components

Reuse of components can improve productivity of program development. To make reuse easily available,
it is important to create specifications and insert comments for easier understanding specifications of
input/output or operation without looking into the component.
It is advisable to describe library reference for OMRON FB Library.
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

4-3-2 Example of FB Component Creation


“ActuatorControl” FB
It should be described in a ladder sequence because it is a process for sequence control.
[Input Variables]

[Output Variables]

[Internal Variables] Line comments for operational


None. overview and input and output
variables allow for easier
understanding.

“AvgValue_ThresholdCheck” FB
It should be described in ST because it is a process for numeric calculation and comparison.
[Input Variables]

[Output Variables]

[Internal Variables]

Note:
Note: Use
Use general
general names
names as
as long
long as
as possible
possible for
for names
names of
of FB
FB and
and variables
variables in
in ladder
ladder diagram
diagram and
and ST,
ST,
instead
instead of
of specific
specific names
names for
for the
the function
function at
at creation.
creation.
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

4-4. Integrating FBs


Detailed process components are extracted by now. Components for application will be created by
combining them in the following sections.

4-4-1. Combining Existing Components - DVD_ThickSelectControl


Req. 2. “Thickness of DVD should be measured at 3 points. Average thickness of measurements
should be calculated. If it is within its threshold range, DVD should be assorted into a stocker for
good products, or a stocker for bad products if not.” can be regarded as a process that combines
“AvgValue_ThresholdCheck” and “ActuatorControl” investigated in the previous section. “Combining”
these components allows creation of integrated component “DVD_ThickSelectControl” FB. Shown
below is an example of an FB to be created.
[Input Variables]

[Output Variables]

[Internal Variables]
This
This FB
FB has
has itsits specific
specific name
name andand variable
variable
names
names that
that include
include “DVD”
“DVD” oror “Cylinder”
“Cylinder”
because
because itit is
is specifically
specifically created
created for
for
application.
application.

Input
Input
Variables
Variables
Internal
Internal
Variables
Variables

Output
Output
Variables
Variables

A
A function
function block
block can
can be
be called
called from
from within
within another
another function
function block.
block. This
This is
is called
called “nesting”.
“nesting”.
To
To nest, declare a variable of FUNCTION BLOCK(FB) type as its internal variable to
nest, declare a variable of FUNCTION BLOCK(FB) type as its internal variable to use
use the
the
variable
variable name
name as
as an
an instance.
instance.
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

4-4-2. Adding Functions to Existing Components - WorkMoveControl_LSONcount

Req. 5. “Paper boxes should be classified into 2 types. Switching frequency should be counted to
evaluate a life of limit switch adjacent to actuator of selection part.” can be materialized by counting
OFF → ON switching of a limit switch as an input for “ActuatorControl”. This component is called
“WorkMoveControl_LSONcount” FB. Shown below is an example of an FB to be created.
[Input Variables]

[Output Variables]

[Internal Variables]

Ladder
Ladder FB
FB is
is called
called from
from ST.
ST.

How
How toto call
call FB
FB (function
(function block)
block) from
from ST
ST
FB
FB to
to be
be called:
called: MyFB
MyFB Instance
Instance of
of MyFB
MyFB declared
declared in
in ST:
ST: MyInstance
MyInstance
I/O
I/O variable
variable ofof FB
FB to
to be
be called:
called: I/O
I/O variable
variable to
to be
be passed
passed to
to FB
FB in
in ST:
ST:
Input: Input1, Input2
Input: Input1, Input2 Input:
Input: STInput1,
STInput1, STInput2
STInput2
Output:
Output: Output1,
Output1, Output2
Output2 Output:
Output: STOutput1,
STOutput1, STOutput2
STOutput2

In
In this
this example,
example, calling
calling of
of FB
FB instance
instance from
from ST
ST must
must be
be described
described as
as
MyInstance(Input1
MyInstance(Input1 := := STInput1,
STInput1, Input2
Input2 :=
:= STInput2,
STInput2, Output1
Output1 =>
=> STOutput1,
STOutput1, Output2
Output2 =>
=> STOutput2);
STOutput2);

When
When all
all input/output
input/output variables
variables are
are described,
described, description
description of
of variables
variables and
and assignment
assignment operators
operators in
in one
one to
to be
be
called
called can
can be be omitted.
omitted.
MyInstance(STInput1,
MyInstance(STInput1, STInput2,
STInput2, STOutput1,
STOutput1, STOutput2);
STOutput2);

By
By describing
describing variables
variables and
and assignment
assignment operators
operators in
in one
one to
to be
be called,
called, you
you can
can describe
describe only
only aa part
part of
of
input/output
input/output variables.
variables.
MyInstance(Input1
MyInstance(Input1 :=:= STInput1,
STInput1, Output2
Output2 =>
=> STOutput2);
STOutput2);
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

4-5. Total Program Description

For components (FB) investigated here to work as a program, a circuit must be created that calls a
component integrated from main ladder program.
* Example here limits to Req.2 and 5.

[Global Variables]

* Other instance variables than those to use FB are omitted.

Either
Either right
right cylinder
cylinder ONON (2.00)
(2.00) oror
left
left cylinder ON (2.01) as an
cylinder ON (2.01) as an
output
output operates
operates byby using
using aa limit
limit
switch
switch (1.00,
(1.00, 1.01)
1.01) at
at cylinder
cylinder
drive
drive to
to assort
assort DVD
DVD andand 33
measurements
measurements (D0-D5)
(D0-D5) of of DVD
DVD
thickness
thickness asas inputs
inputs and
and by
by turning
turning
contact
contact 0.00
0.00 ON.
ON.

Either
Either right
right actuator
actuator ON ON (4.00)
(4.00) oror
left
left actuator
actuator ONON (4.01)
(4.01) turns
turns ONON
based
based on on condition
condition of of aa limit
limit switch
switch
(3.00,
(3.00, 3.01)
3.01) at
at actuator
actuator end end when
when
operation
operation input
input (W0.00)
(W0.00) and and left
left
move
move input
input (W0.01)
(W0.01) to to move
move aa box box
containing
containing DVDDVD to to the
the right
right oror left.
left.
Also,
Also, switching
switching count
count of of the
the limit
limit
switch
switch isis provided
provided to to D10-13.
D10-13.

Why
Why thethe instance
instance name
name is is “StageA***”?
“StageA***”?
Although
Although itit is
is not
not explicitly
explicitly described
described in
in the
the application
application example,
example, aa program
program for
for newly
newly added
added stage
stage BB
can
can bebe created
created only
only by
by describing
describing an
an instance
instance “StageB***”
“StageB***” inin the
the program
program andand setting
setting necessary
necessary
parameters,
parameters, without
without registering
registering aa new
new function
function block.
block.
As
As aa feature
feature ofof Omron’s
Omron’s function
function block,
block, one
one FB
FB can
can have
have more
more than
than one
one instance.
instance. ByBy using
using operation-
operation-
verified
verified FB
FB definition
definition (algorithm),
(algorithm), aa program
program cancan be
be created
created only
only byby assigning
assigning its
its address.
address.
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

4-6-1. Total Program Structure


This section verifies total program structure including components (function blocks) created here.

[Main Program]

Instance names and FB names can be illustrated as follows: (FB name is described in [ ])
StageA_DVDThickSelect DVD ThickJudge
Main program
[DVD_ThickSelectControl] [AvgValue_ThresholdCheck]

WorkMove [ActuatorControl]

StageA_BoxSelect
WorkMove [ActuatorControl]
[WorkMoveControl_LSONcount]

In
In aa structured
structured program,
program, especially
especially to
to change
change aa lower
lower level
level component
component (FB),
(FB), itit is
is important
important to
to understand
understand
parent/children
parent/children relationship
relationship and
and components’
components’ sharing
sharing when
when process
process flow
flow must
must be be cleared
cleared in
in case
case of
of debugging,
debugging, etc.
etc.
ItIt is
is advisable
advisable to
to create
create an
an understandable
understandable diagram
diagram ofof total
total program
program structure
structure as as design
design documentation.
documentation.
CX-Programmer
CX-Programmer Ver.6.0
Ver.6.0 provides
provides “FB
“FB instance
instance viewer”
viewer” when
when [Alt]+[5]
[Alt]+[5] key
key isis pressed
pressed for
for easier
easier understanding
understanding of
of
software
software structure
structure constructed
constructed by
by FBs.
FBs. Also,
Also, address
address can
can be
be checked
checked thatthat is
is assigned
assigned toto FB
FB instance.
instance.
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

5. Entering FB Definition
This section describes how to enter an actually-designed program and debug it.
New project must be created and “ActuatorControl” FB of Page 5-4 must be entered.

5-1. New Project Creation and PLC Model/CPU Type Setting


Refer to page 2-3 and create a new project.
! Select a PC model from the followings to
use function blocks.
CS1G-H, CS1H-H, CJ1G-H, CJ1H-H, CJ1M

5-2. Creating Ladder Definition FB

Create Ladder definition FB.

Move the mouse cursor to a


function block icon  , then
right-click. Select
→ Insert Function Block
→ Ladder

Now new FB is created.


Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

5-3. Entering FB Ladder Program

Change FB definition name.

Caution:
Caution:
AA user
user cannot
cannot create
create function
function block
block definition
definition name
name
starting
starting from
from "_".
"_".
Move the mouse cursor to a The
The name
name must
must start
start from
from aa character
character other
other than
than "_".
"_".
copied function block icon ,
then right-click. Select
→ Rename
Enter [ActuatorControl].
Variables
Variables Table
Table

Open
Open FB
FB ladder
ladder editor.
editor.

Ladder
Ladder Input
Input Screen
Screen
Move the mouse cursor to a
function block icon , then
double-click to open the
function block ST editor.

Select the variables table and register variables in the function block.
All variables of “ActuatorControl” FB of page 5-4 must be registered.

Note:
Note: Order
Order of
of variables
variables must
must be
be the
the same
same as as FB
FB instance
instance order.
order.
To
To change
change order
order of
of variables,
variables, select
select aa variable
variable name
name then
then drag
drag and
and drop
drop it.
it.

Select ladder input screen, then enter a ladder program.


All variables of “ActuatorControl” FB of page 5-4 must be registered.

Note:
Note: Although
Although you
you can
can enter
enter aa circuit
circuit in
in the
the FB
FB ladder
ladder editor
editor similar
similar to
to the
the main
main
ladder
ladder editor,
editor, entering
entering of
of address
address in in the
the FB
FB is
is invalid.
invalid.

Note:
Note: To
To enter
enter variable
variable list
list in
in aa line
line comment,
comment, you
you can can select
select aa variable
variable from
from
variables
variables table
table then
then copy
copy it. it. You
You can
can use
use itit for
for more
more efficient
efficient input.
input.
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

5-4. Transferring Program


Connect to CX-Simulator online, transfer a program, then set PLC (simulator) to monitor mode.

For
For how
how to
to connect
connect online
online and
and transfer
transfer aa
program,
program, see
see page
page 2-10.
2-10.

5-5. Operation Check-1


Change current parameter value of FB invocation on the main ladder, then check the operation of
“ActuatorControl” FB.
Monitor the instance of ActuatorControl FB first.

Move the cursor to FB


invocation, then double-click
or click button.

FB ladder instance (under


condition of address assigned)
is monitored.
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

Display the main ladder and FB instance (FB ladder invoked by the main ladder) at the same
time, then check the operation while changing current parameter value of FB invocation in the
main ladder.

5-6. Operation Check-2


Enter following parameter values of FB invocation and check if expected output should be provided.
In this example only (1) is shown, but all combination of conditions must be verified.

(1) Initial State: Turn 0.03 ON. => 0.04 and 0.05 must be OFF. FB instance ladder monitor screen
must be under state that corresponds to the value.
(2) Actuator forward direction operation-1: Turn 0.00 ON => 0.04 must be turned ON. FB instance
ladder monitor screen must be under state that corresponds to the value.
(3) Actuator forward direction operation-2: Turn 0.03 OFF => 0.04 must be ON and 0.05 must be
OFF. FB instance ladder monitor screen must be under state that corresponds to the value.
(4) Actuator forward direction operation-3: Turn 0.02 ON => 0.04 must be OFF and 0.05 must be
OFF. FB instance ladder monitor screen must be under state that corresponds to the value.

Move the cursor to 0.03 and


press [ENT] key.
11 must
must be
be displayed.
displayed.

Enter
Enter 11 and
and press
press [Set]
[Set] button.
button.
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

5-7. Entering/Debugging Other FB Definition

Thus far, entering and debugging for “ActuatorControl” FB are described. Other FB definition
must be entered and debugged as well.

5-8. Batch Registration to Watch Window

For debugging, you can use batch registration of FB instance address to Watch Window instead
of FB ladder monitor.

Move the cursor to FB


invocation you want to register,
right-click, then select [Register
in Watch Window] in the menu.

Select Usage and Data type


if necessary.

Select
Select aa name
name to
to register,
register,
then
then press [OK] button.
press [OK] button.
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

6. Creating FB Definition Library


To reuse operation-verified FB definition, it must be incorporated into library (file).
Check the hierarchy using project workspace and FB instance viewer, then determine the FB
definition you want to incorporate into library. In this case, it is “DVD_ThickSelectControl” FB.

Select
“DVD_ThickSelectControl” FB,
right-click and select [Save
Function Block to File] from
the context menu.

Select [Save].

/ / / /
Default
Default folder
folder for
for saving
saving isis C:
C: Program
Program Files
Files Omron
Omron CX-One
CX-One FBL. FBL.
ItIt can
can be changed by CX-Programmer option setting “FB library
be changed by CX-Programmer option setting “FB library storage
storage folder”
folder” ..
OMRON
OMRON FB FB Library
Library isis under
under omronlib
omronlib folder.
folder. /
Create
Create aa folder
folder so
so that
that you
you should
should be
be able
able to to classify
classify itit easily,
easily, such
such as
as Userlib
Userlib DVD.
DVD.

When
When savingsaving FB
FB definition
definition that
that calls
calls another
another FB,
FB, both
both FB
FB definition
definition are are saved.
saved.
When retrieving a project, calling relationship is maintained as
When retrieving a project, calling relationship is maintained as saved. saved.
ItIt is
is easier
easier to
to manage
manage FBFB definition
definition because
because saved
saved FB
FB definition
definition isis integrated.
integrated.
Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

7. Entering Main Program

Add the main program to a project file that contains debugged FB definition. Program to be
entered is one that is described in 4-5. Total Program Description in page 5-7.

[Global Variables]

* Other instance variables than those to use FB are omitted.

For how to enter a program, refer to pages from 2-6 to 2-9.


Advanced
Entering/Deb
Entering/Deb Creating
Creating FB
FB
Program
Program Entering
Entering Main
Main Debugging
Debugging
ugging
ugging FBFB Definition
Definition
Design
Design Definition Library Program
Program Main
Main Program
Program
Definition Library

8. Debugging Main Program

Main program must be debugged considering followings:


• Program areas that are irrelevant to FB
• Program areas that are relevant to an input parameter to FB
• Program areas that refer to an output parameter from FB
Main program in this example has no such area, thus explanation is omitted.
Supplemental Information

How to delete unused Function Block definitions


When you delete unused Function Block definitions, it is not enough just to delete the Function Block invocations (instructions).
This is because the Function Block instance definitions are registered in the global symbol table.
At this situation, when the compile (program check) is done, then the unused function block instances will be shown on the
output window. You can identify the unused function block instance definitions and delete them easily.
The Function Block definitions and Function Block instances are a part of user program in the CPU unit even if they are not
called, so it is recommended to delete unused Function Block definitions and instances before transferring the program to
the CPU unit.

Execute Compile F7 key

Result of Compilation

Del key

Double
Double click
click mouse
mouse left
left button
button
Click mouse left button

Function
Function Block
Block definition
definition will
will be
be deleted.
deleted.

Memory allocation for Function Blocks

It is necessary to allocate required memory for each function block instances to execute Function Blocks.
CX-Programmer allocates the memory automatically based on the following setting dialog information.
( PLC menu → Function Block Memory → Function Block Memory Allocation)
There are 4 types of areas, ‘Not retain’, ‘Retain’, ‘Timers’, and ‘Counters’. Please change the settings if requires.

z Notice when changing the settings


If you change the ‘Not retain’ or ‘Retain’ area, please consider the allocated memory areas for the special IO unit
and CPU SIO unit.

z Special memory area for the Function Blocks


CS1/CJ1-H/CJ1M CPUs (unit version: 3.0 or higher) have a special memory area which is extended hold (H)
relay area.
The address of the area is from H512 to H1535. CX-Programmer sets the area as a default.
Please note that the area cannot be used for the operands of ladder instructions.
Useful Functions

It is possible to automatically display a list of symbol names or IO comments when entering the operands of
instructions.
When entering the operand for contact or output (or special instructions), enter a string, and the dropdown list is
automatically updated to display in symbol names or IO Comments using the defined string. Selecting the item
from the list defines the operand information.
This is an efficient way of entering registered symbol information into the ladder.

Example: Enter text “Temperature” to the edit field in the operand dialog.

Click or push [F4] key; all symbols / address having IO comment containing the text
‘temperature are listed. See below:-

For instance, select ‘temp_alarm01, W1.00, Temperature error of upper case of MachineA’, from
the list. The operand is set to be using symbol ‘alarm01’.
Appendix. Examples of ST (Structured Text)

IF Statement Examples

IF expression1 THEN statement-list1


[ ELSIF expression2 THEN statement-list2 ]
[ ELSE statement-list3 ]
END_IF;

The expression1 and expression2 expressions must each evaluate to a boolean value. The statement-list is a list of
several simple statements e.g. a:=a+1; b:=3+c; etc.
The IF keyword executes statement-list1 if expression1 is true; if ELSIF is present and expression1 is false and
expression2 is true, it executes statement-list2; if ELSE is present and expression1 or expression2 is false, it
executes statement-list3. After executing statement-list1, statement-list2 or statement-list3, control passes to the
next statement after the END_IF.
There can be several ELSIF statements within an IF Statement, but only one ELSE statement.
IF statements can be nested within other IF statements (Refer to example 5).

Example 1
IF a > 0 THEN In this example, if the variable "a" is greater than zero, then the
b := 0; variable "b" will be assigned the value of zero.
END_IF; If "a" is not greater than zero, then no action will be performed
upon the variable "b", and control will pass to the program steps
following the END_IF clause.

Example 2 In this example, if the variable "a" is true, then the variable "b" will
IF a THEN be assigned the value of zero.
b := 0; If "a" is false, then no action will be performed upon the variable
"b", and control will pass to the program steps following the
END_IF; END_IF clause.

Example 3 In this example, if the variable "a" is greater than zero, then the
variable "b" will be assigned the value of true (1), and control will
IF a > 0 THEN be passed to the program steps following the END_IF clause.
b := TRUE;
If "a" is not greater than zero, then no action is performed upon
ELSE the variable "b" and control is passed to the statement following
b := FALSE; the ELSE clause, and "b" will be assigned the value of false (0).
END_IF; Control is then passed to the program steps following the END_IF
clause.

Example 4 In this example, if the variable "a" is less than 10, then the
IF a < 10 THEN variable "b" will be assigned the value of true (1), and the variable
"c" will be assigned the value of 100. Control is then passed to the
b := TRUE; program steps following the END_IF clause.
c := 100;
If the variable "a" is equal to or greater than 10 then control is
ELSIF a > 20 THEN passed to the ELSE_IF clause, and if the variable "a" is greater
b := TRUE; than 20, variable "b" will be assigned the value of true (1), and the
c := 200; variable "c" will be assigned the value of 200. Control is then
passed to the program steps following the END_IF clause.
ELSE
If the variable "a" is between the values of 10 and 20 (i.e. both of
b := FALSE;
the previous conditions IF and ELSE_IF were false) then control is
c := 300; passed to the ELSE clause, and the variable "b" will be assigned
END_IF; the value of false (0), and the variable "c" will be assigned the
value of 300. Control is then passed to the program steps
following the END_IF clause.
IF Statement Examples

Example 5 In this example (an example of a nested IF .. THEN


IF a THEN statement), if the variable "a" is true (1), then the variable "b"
b := TRUE; will be assigned the value of true (1), and control will be
passed to the program steps following the associated
ELSE END_IF clause.
IF c>0 THEN If "a" is false (0), then no action is performed upon the
d := 0; variable "b" and control is passed to the statement following
ELSE the ELSE clause (in this example, another IF .. THEN
statement, which is executed as described in Example 3,
d := 100; although it should be noted that any of the supported
END_IF; IEC61131-3 statements may be used).
d := 400; After the described IF .. THEN statement is executed, the
END_IF; variable "d" will be assigned the value of 400.
Control is then passed to the program steps following the
END_IF clause.

WHILE Statement Examples

WHILE expression DO
statement-list;
END_WHILE;

The WHILE expression must evaluate to a boolean value. The statement-list is a list of several simple statements.
The WHILE keyword repeatedly executes the statement-list while the expression is true. When the expression
becomes false, control passes to the next statement after the END_WHILE.

Example 1 In this example, the WHILE expression will be evaluated and


WHILE a < 10 DO if true (i.e. variable "a" is less than 10) then the statement-list
a := a + 1; (a:=a+1; and b:=b*2.0;) will be executed. After execution of
the statement-list, control will pass back to the start of the
b := b * 2.0; WHILE expression. This process is repeated while variable
END_WHILE; "a" is less than 10. When the variable "a" is greater than or
equal to 10, then the statement-list will not be executed and
control will pass to the program steps following the
END_WHILE clause.
Example 2
WHILE a DO In this example, the WHILE expression will be evaluated and
b := b + 1; if true (i.e. variable "a" is true), then the statement-list (b:=b+1;
and the IF .. THEN statement) will be executed. After
IF b > 10 THEN execution of the statement-list, control will pass back to the
a:= FALSE; start of the WHILE expression. This process is repeated while
END_IF; variable "a" is true. When variable "a" is false, the statement-
list will not be executed and control will pass to the program
END_WHILE; steps following the END_WHILE clause.

Example 3
WHILE (a + 1) >= (b * 2) DO In this example, the WHILE expression will be evaluated and
if true (i.e. variable "a" plus 1 is greater than or equal to
a := a + 1; variable "b" multiplied by 2) then the statement-list (a:=a+1;
b := b / c; and b:=b/c;) will be executed. After execution of the
END_WHILE; statement-list, control will pass back to the start of the WHILE
expression. This process is repeated while the WHILE
expression equates to true. When the WHILE expression is
false, then the statement-list will not be executed and control
will pass to the program steps following the END_WHILE
clause.
WHILE Statement Examples

Example 4
WHILE (a - b) <= (b + c) DO In this example, the WHILE expression will be evaluated and if
true (i.e. variable "a" minus variable "b" is less than or equal to
a := a + 1;
variable "b" plus variable "c") then the statement-list (a:=a+1; and
b := b * a; b:=b*a;) will be executed. After execution of the statement-list,
END_WHILE; control will pass back to the start of the WHILE expression. This
process is repeated while the WHILE expression is true. When
the WHILE expression is false, then the statement-list will not be
executed and control will pass to the program steps following the
END_WHILE clause.
REPEAT Statement Examples

REPEAT
statement-list;
UNTIL expression
END_REPEAT;

The REPEAT expression must evaluate to a boolean value. The statement-list is a list of several simple statements.
The REPEAT keyword repeatedly executes the statement-list while the expression is false. When the expression
becomes true, control passes to the next statement after END_REPEAT.

Example 1 In this example, the statement-list (a:=a+1; and b:=b*2.0;) will be


REPEAT executed. After execution of the statement-list the UNTIL expression
a := a + 1; is evaluated and if false (i.e. variable "a" is less than or equal to 10),
then control will pass back to the start of the REPEAT expression and
b := b * 2.0; the statement-list will be executed again. This process is repeated
UNTIL a > 10 while the UNTIL expression equates to false. When the UNTIL
END_REPEAT; expression equates to true (i.e. variable "a" is greater than 10) then
control will pass to the program steps following the END_REPEAT
clause.
Example 2
REPEAT In this example, the statement-list (b:=b+1; and the IF .. THEN
statement) will be executed. After execution of the statement-list the
b := b + 1; UNTIL expression is evaluated and if false (i.e. variable "a" is false),
IF b > 10 THEN then control will pass back to the start of the REPEAT expression and
a:= FALSE; the statement-list will be executed again. This process is repeated
while the UNTIL expression equates to false. When the UNTIL
END_IF; expression equates to true (i.e. variable "a" is true) then control will
UNTIL a pass to the program steps following the END_REPEAT clause.
END_REPEAT;

In this example, the statement-list (a:=a+1; and b:=b/c;) will be


Example 3
executed. After execution of the statement-list the UNTIL expression
REPEAT is evaluated and if false (i.e. variable "a" plus 1 is less than variable
a := a + 1; "b" multiplied by 2) then control will pass back to the start of the
REPEAT expression and the statement-list will be executed again.
b := b / c; This process is repeated while the UNTIL expression equates to false.
UNTIL (a + 1) >= (b * 2) When the UNTIL expression equates to true (i.e. variable "a" plus 1 is
END_REPEAT; greater than or equal to variable "b" multiplied by 2) then control will
pass to the program steps following the END_REPEAT clause.

Example 4 In this example, the statement-list (a:=a+1; and b:=b*a;) will be


executed. After execution of the statement-list the UNTIL expression
REPEAT is evaluated and if false (i.e. variable "a" minus variable "b" is greater
a := a + 1; than variable "b" plus variable "c"), then control will pass back to the
b := b * a; start of the REPEAT expression and the statement-list will be
executed again. This process is repeated while the UNTIL expression
UNTIL (a - b) <= (b + c) equates to false. When the UNTIL expression equates to true (i.e.
END_REPEAT; variable "a" minus variable "b" is less than or equal to variable "b" plus
variable "c") then control will pass to the program steps following the
END_REPEAT clause.
FOR Statement Examples

FOR control variable := integer expression1 TO integer expression2 [ BY integer expression3 ] DO


statement-list;
END_FOR;

The FOR control variable must be of an integer variable type. The FOR integer expressions must evaluate to the
same integer variable type as the control variable. The statement-list is a list of several simple statements.
The FOR keyword repeatedly executes the statement-list while the control variable is within the range of integer
expression1 to integer expression2. If the BY is present then the control variable will be incremented by integer
expression3 otherwise by default it is incremented by one. The control variable is incremented after every executed
call of the statement-list. When the control variable is no longer in the range integer expression1 to integer
expression2, control passes to the next statement after the END_FOR.
FOR statements can be nested within other FOR statements.

Example 1 In this example, the FOR expression will initially be evaluated and
FOR a := 1 TO 10 DO variable "a" will be initialized with the value 1. The value of
variable "a" will then be compared with the 'TO' value of the FOR
b := b + a; statement and if it is less than or equal to 10 then the statement-
END_FOR; list (i.e. b:=b+a;) will be executed. Variable "a" will then be
incremented by 1 and control will pass back to the start of the
FOR statement. Variable "a" will again be compared with the 'TO'
value and if it is less than or equal to 10 then the statement-list
will be executed again. This process is repeated until the value of
variable "a" is greater than 10, and then control will pass to the
program steps following the END_FOR clause.

Example 2 In this example, the FOR expression will initially be evaluated and
variable "a" will be initialized with the value 1. The value of
FOR a := 1 TO 10 BY 2 DO variable "a" will then be compared with the 'TO' value of the FOR
b := b + a; statement and if it is less than or equal to 10 then the statement-
list (i.e. b:=b+a; and c:=c+1.0;) will be executed. Variable "a" will
c := c + 1.0; then be incremented by 2 and control will pass back to the start of
END_FOR; the FOR statement. Variable "a" will again be compared with the
'TO' value and if it is less than or equal to 10 then the statement-
list will be executed again. This process is repeated until the value
of variable "a" is greater than 10, and then control will pass to the
program steps following the END_FOR clause.
Example 3
In this example, the FOR expression will initially be evaluated and
FOR a := 10 TO 1 BY -1 DO variable "a" will be initialized with the value 10. The value of
b := b + a; variable "a" will then be compared with the 'TO' value of the FOR
c := c + 1.0; statement and if it is greater than or equal to 1 then the statement-
list (i.e. b:=b+a; and c:=c+1.0;) will be executed. Variable "a" will
END_FOR; then be decremented by 1 and control will pass back to the start
of the FOR statement. Variable "a" will again be compared with
the 'TO' value and if it is greater than or equal to 1 then the
statement-list will be executed again. This process is repeated
until the value of variable "a" is less than 1, and then control will
pass to the program steps following the END_FOR clause.
Example 4
FOR a := b + 1 TO c + 2 DO In this example, the FOR expression will initially be evaluated and
variable "a" will be initialized with the value of variable "b" plus 1.
d := d + a;
The 'TO' value of the FOR statement will be evaluated to the
e := e + 1; value of variable "c" plus 2. The value of variable "a" will then be
END_FOR; compared with the 'TO' value and if it is less than or equal to it
then the statement-list (i.e. d:=d+a; and e:=e+1;) will be executed.
Variable "a" will then be incremented by 1 and control will pass
back to the start of the FOR statement. Variable "a" will again be
compared with the 'TO' value and if it is less than or equal to it
then the statement-list will be executed again. This process is
repeated until the value of variable "a" is greater than the 'TO'
value, and then control will pass to the program steps following
the END_FOR clause.
FOR Statement Examples

Example 5 In this example, the FOR expression will initially be evaluated and variable "a"
FOR a := b + c TO d - e BY f DO will be initialized with the value of variable "b" plus variable "c". The 'TO' value
of the FOR statement will be evaluated to the value of variable "d" minus
g := g + a; variable "e". The value of variable "a" will then be compared with the 'TO' value.
h := h + 1.0; If the value of variable "f" is positive and the value of variable "a" is less than or
END_FOR; equal to the 'TO' value then the statement-list (i.e. g:=g+a; and h:=h+1.0;) will
be executed. If the value variable "f" is negative and the value of variable "a" is
greater than or equal to the 'TO' value then the statement-list (i.e. g:=g+a; and
h:=h+1.0;) will also be executed. Variable "a" will then be incremented or
decremented by the value of variable "f" and control will pass back to the start
of the FOR statement. Variable "a" will again be compared with the 'TO' value
and the statement-list executed if appropriate (as described above).
This process is repeated until the value of variable "a" is greater than the 'TO'
value (if the value of variable "f" is positive) or until the value of variable "a" is
less than the 'TO' value (if the value of variable "f" is negative), and then
control will pass to the program steps following the END_FOR clause.

CASE Statement Examples

CASE expression OF
case label1 [ , case label2 ] [ .. case label3 ] : statement-list1;
[ ELSE
statement-list2 ]
END_CASE;

The CASE expression must evaluate to an integer value. The statement-list is a list of several simple statements.
The case labels must be valid literal integer values e.g. 0, 1, +100, -2 etc..
The CASE keyword evaluates the expression and executes the relevant statement-list associated with a case label
whose value matches the initial expression. Control then passes to the next statement after the END_CASE. If no
match occurs within the previous case labels and an ELSE command is present the statement-list associated with
the ELSE keyword is executed. If the ELSE keyword is not present, control passes to the next statement after the
END_CASE.
There can be several different case labels statements (and associated statement-list) within a CASE statement but
only one ELSE statement.
The “,” operator is used to list multiple case labels associated with the same statement-list.
The “..” operator denotes a range case label. If the CASE expression is within that range then the associated
statement-list is executed, e.g. case label of 1..10 : a:=a+1; would execute the a:=a+1 if the CASE expression is
greater or equal to 1 and less than 10.

Example 1 In this example, the CASE statement will be evaluated and then compared with
CASE a OF each of the CASE statement comparison values (i.e. 2 and 5 in this example).
2 : b := 1; If the value of variable "a" is 2 then that statement-list will be executed (i.e. b:=1;).
Control will then pass to the program steps following the END_CASE clause.
5 : c := 1.0;
END_CASE; If the value of variable "a" is 5 then that statement-list will be executed (i.e. c:=1.0;).
Control will then pass to the program steps following the END_CASE clause.
If the value of variable "a" does not match any of the CASE statement comparison
values then control will pass to the program steps following the END_CASE clause.

Example 2 In this example, the CASE statement will be evaluated and then compared with
each of the CASE statement comparison values (i.e. -2 and 5 in this example).
CASE a + 2 OF
If the value of variable "a" plus 2 is -2 then that statement-list will be executed (i.e.
-2 : b := 1; b:=1;). Control will then pass to the program steps following the END_CASE clause.
5 : c := 1.0; If the value of variable "a" plus 2 is 5 then that statement-list will be executed (i.e.
ELSE c:=1.0;). Control will then pass to the program steps following the END_CASE
clause. If the value of variable "a" plus 2 is not -2 or 5, then the statement-list in the
d := 1.0; ELSE condition (i.e. d:=1.0;) will be executed. Control will then pass to the program
END_CASE; steps following the END_CASE clause.
CASE Statement Examples

Example 3 In this example, the CASE statement will be evaluated and then
CASE a + 3 * b OF compared with each of the CASE statement comparison values (i.e. 1
1, 3 : b := 2; or 3 and 7 or 11 in this example).
7, 11 : c := 3.0; If the value of variable "a" plus 3 multiplied by variable "b" is 1 or 3,
ELSE then that statement-list will be executed (i.e. b:=2;). Control will then
d := 4.0; pass to the program steps following the END_CASE clause.
END_CASE;
If the value of variable "a" plus 3 multiplied by variable "b" is 7 or 11,
then that statement-list will be executed (i.e. c:=3.0;). Control will then
pass to the program steps following the END_CASE clause.
If the value of variable "a" plus 3 multiplied by variable "b" is not 1, 3,
7 or 11, then the statement-list in the ELSE condition (i.e. d:=4.0;) will
be executed. Control will then pass to the program steps following the
END_CASE clause.

Example 4
CASE a OF In this example, the CASE statement will be evaluated and then
compared with each of the CASE statement comparison values, i.e. (-
-2, 2, 4 : b := 2; 2, 2 or 4) and (6 to 11 or 13) and (1, 3 or 5) in this example.
c := 1.0;
If the value of variable "a" equals -2, 2 or 4, then that statement-list
6..11, 13 : c := 2.0; will be executed (i.e. b:=2; and c:=1.0;). Control will then pass to the
1, 3, 5 : c := 3.0; program steps following the END_CASE clause.
ELSE
If the value of variable "a" equals 6, 7, 8, 9, 10, 11 or 13 then, that
b := 1; statement-list will be executed (i.e. c:=2.0;). Control will then pass to
c := 4.0; the program steps following the END_CASE clause.
END_CASE; If the value of variable "a" is 1, 3 or 5, then that statement-list will be
executed (i.e. c:=3.0;). Control will then pass to the program steps
following the END_CASE clause.
If the value of variable "a" is none of those above, then the statement-
list in the ELSE condition (i.e. b:=1; and c:=4.0;) will be executed.
Control will then pass to the program steps following the END_CASE
clause.
EXIT Statement Examples

WHILE expression DO
statement-list1;
EXIT;
END_WHILE;
statement-list2;

REPEAT
statement-list1;
EXIT;
UNTIL expression
END_REPEAT;
statement-list2;

FOR control variable := integer expression1 TO integer expression2 [ BY integer expression3 ] DO


statement-list1;
EXIT;
END_FOR;
statement-list2;

The statement-list is a list of several simple statements.


The EXIT keyword discontinues the repetitive loop execution to go to the next statement, and can only be used in
repetitive statements (WHILE, REPEAT, FOR statements). When the EXIT keyword is executed after statement-
list1 in the repetitive loop, the control passes to statement-list2 immediately.

Example 1
WHILE a DO If the first IF expression is true (i.e. variable "c" is true), the
IF c = TRUE THEN statement-list (b:=0; and EXIT;) is executed during the
b:=0;EXIT; execution of the WHILE loop. After the execution of the EXIT
keyword, the WHILE loop is discontinued and the control
END_IF; passes to the next statement (d:=1;) after the END_WHILE
IF b > 10 THEN clause.
a:= FALSE;
END_IF;
END_WHILE;
d:=1;

Example 2
a:=FALSE; If the first IF expression is true (i.e. i>=10 is true) in the inside
FOR i:=1 TO 20 DO FOR loop, the statement-list (n:=i*10+j; and a:=TRUE; and
EXIT;) is executed during the execution of the FOR loop.
FOR j:=0 TO 9 DO After the execution of the EXIT keyword, the inside FOR loop
IF i>=10 THEN is discontinued and the control passes to the next IF
n:=i*10+j; statement after the END_FOR clause. If this IF expression is
true (i.e. the variable "a" is true), EXIT keyword is executed ,
a:=TRUE;EXIT; the outside FOR loop is discontinued after END_FOR clause,
END_IF; and the control passes to the next statement (d:=1;).
END_FOR;
IF a THEN EXIT; END_IF;
END_FOR;
d:=1;
RETURN Statement Examples

statement-list1;
RETURN;
statement-list2;

The statement-list is a list of several simple statements.


The RETURN keyword breaks off the execution of the inside of the Function Block after statement-list1, and then
the control returns to the program which calls the Function Block without executing statement-list2.

Example 1
IF a_1*b>100 THEN If the first or second IF statement is true (i.e. "a_1*b" is larger
c:=TRUE;RETURN; than 100, or "a_2*(b+10)" is larger than 100), the statement
END_IF; (c:=TRUE; and RETURN;) is executed. The execution of the
RETURN keyword breaks off the execution of the inside of
IF a_2*(b+10)>100 THEN the Function Block and the control returns to the program
c:=TRUE;RETURN; which calls the Function Block.
END_IF;
IF a_3*(b+20)>100 THEN
c:=TRUE;
END_IF;

Array Examples

variable name [subscript index]

An array is a collection of like variables. The size of an array can be defined in the Function Block variable table.
An individual variable can be accessed using the array subscript operator [ ].
The subscript index allows a specific variable within an array to be accessed. The subscript index must be either a
positive literal value, an integer expression or an integer variable. The subscript index is zero based. A subscript
index value of zero would access the first variable, a subscript index value of one would access the second variable
and so on.

Warning
If the subscript index is either an integer expression or integer variable, you must ensure that the resulting
subscript index value is within the valid index range of the array. Accessing an array with an invalid index
must be avoided. Refer to Example 5 for details of how to write safer code when using variable array
offsets.

In this example variable "a" is an array of 5 elements and has


Example 1 an INT data type. Variable "b" also has an INT data type.
a[0] := 1; When executed, the first element in the array will be set to the
value 1, the second element will be set to -2, the third
a[1] := -2;
element will be set to 3 (i.e. 1+2), the forth element will be set
a[2] : = 1+2; to the value of variable "b" and the fifth element will be set to
a[3] : = b; the value of variable "b" plus 1.
a[4] : = b+1;

In this example variable "c" is an array of 2 elements and has


Example 2 a BOOL data type. When executed, the first element in the
c[0] := FALSE; array will be set to false and the second element will be set to
c[1] := 2>3; false (i.e. 2 is greater than 3 evaluates to false).
Array Examples

Example 3
d[9]:= 2.0; In this example, variable "d" is an array of 10 elements and
has a REAL data type. When executed, the last element in
the array (the 10th element) will be set to 2.0.

Example 4
a[1] := b[2]; In this example, variable "a" and variable "b" are arrays of the
same data type. When executed, the value of the second
element in variable "a" will be set to the value of the third
element in variable "b".

Example 5
a[b] := 1;
a[b+1] := 1;
a[(b+c) *( d-e)] := 1;

Note: As the integer variables and expressions are being used to access the array, the actual index value
will not be known until run time, so the user must ensure that the index is within the valid range of the
array a. For example, a safer way would be to check the array index is valid:

f := (b+c) *( d-e);
IF (f >0) AND (f<5) THEN
a[f] := 1;
END_IF;
Where variable "f" has an INT data type.

Example 6
a[b[1]]:= c; This example shows how an array element expression can be
a[b[2] + 3]:= c; used within another array element expression.
Numerical Functions and Arithmetic Functions

Function Name Argument data Return value Operation Example


type type
ABS(argument) Absolute value INT, DINT, NT, DINT, | argument | a:=ABS(b)
LINT, UINT, LINT, UINT,
UDINT, UDINT,
ULINT, REAL, ULINT, REAL,
LREAL LREAL
SQRT(argument) Square root REAL, LREAL REAL, LREAL a:=SQRT(b)
LN(argument) Natural REAL, LREAL REAL, LREAL a:=LN(b)
logarithm
LOG(argument) Common REAL, LREAL REAL, LREAL a:=LOG(b)
logarithm
EXP(argument) Natural REAL, LREAL REAL, LREAL a:=EXP(b)
exponential
SIN(argument) Sine REAL, LREAL REAL, LREAL SIN(argument) a:=SIN(b)
COS(argument) Cosine REAL, LREAL REAL, LREAL COS(argument) a:=COS(b)
TAN(argument) Tangent REAL, LREAL REAL, LREAL TAN(argument) a:=TAN(b)
ASIN(argument) Arc sine REAL, LREAL REAL, LREAL a:=ASIN(b)
ACOS(argument) Arc cosine REAL, LREAL REAL, LREAL a:=ACOS(b)
ATAN(argument) Arc tangent REAL, LREAL REAL, LREAL a:=ATAN(b)
EXPT(base, Exponential Base: REAL, REAL, LREAL a:=EXPT(b, c)
exponent) LREAL

Exponent:
INT, DINT,
LINT, UINT,
UDINT, ULINT

You might also like