Library of General Functions (LGF) For S7-1200 - 1500
Library of General Functions (LGF) For S7-1200 - 1500
https://support.industry.siemens.com/cs/ww/en/view/109479728
Warranty and liability
Note The Application Examples are not binding and do not claim to be complete
regarding the circuits shown, equipping and any eventuality. The Application
Examples do not represent customer-specific solutions. They are only intended
to provide support for typical applications. You are responsible for ensuring that
the described products are used correctly. These Application Examples do not
relieve you of the responsibility to use safe practices in application, installation,
operation and maintenance. When using these Application Examples, you
recognize that we cannot be made liable for any damage/claims beyond the
liability clause described. We reserve the right to make changes to these
Application Examples at any time without prior notice.
If there are any deviations between the recommendations provided in these
Application Examples and other Siemens publications – e.g. Catalogs – the
contents of the other documents have priority.
We do not accept any liability for the information contained in this document.
Any claims against us – based on whatever legal reason – resulting from the use of
the examples, information, programs, engineering and performance data etc.,
described in this Application Example shall be excluded. Such an exclusion shall
not apply in the case of mandatory liability, e.g. under the German Product Liability
Act (“Produkthaftungsgesetz”), in case of intent, gross negligence, or injury of life,
body or health, guarantee for the quality of a product, fraudulent concealment of a
Siemens AG 2017 All rights reserved
Security Siemens provides products and solutions with industrial security functions that
informa- support the secure operation of plants, systems, machines and networks.
tion In order to protect plants, systems, machines and networks against cyber
threats, it is necessary to implement – and continuously maintain – a holistic,
state-of-the-art industrial security concept. Siemens’ products and solutions only
form one element of such a concept.
Customer is responsible to prevent unauthorized access to its plants, systems,
machines and networks. Systems, machines and components should only be
connected to the enterprise network or the internet if and to the extent necessary
and with appropriate security measures (e.g. use of firewalls and network
segmentation) in place.
Additionally, Siemens’ guidance on appropriate security measures should be
taken into account. For more information about industrial security, please visit
http://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them
more secure. Siemens strongly recommends to apply product updates as soon
as available and to always use the latest product versions. Use of product
versions that are no longer supported, and failure to apply latest updates may
increase customer’s exposure to cyber threats.
To stay informed about product updates, subscribe to the Siemens Industrial
Security RSS Feed under http://www.siemens.com/industrialsecurity.
Table of Contents
Warranty and liability ................................................................................................... 2
1 Library Overview ................................................................................................ 4
1.1 General ................................................................................................. 4
1.2 Hardware and software requirements .................................................. 4
1.3 Library resources .................................................................................. 5
2 How to Work with the Library ........................................................................... 6
3 Explanation of the blocks ................................................................................. 7
3.0 Bit logic operations ............................................................................... 8
3.0.1 FB LGF_PulseRelay............................................................................. 8
3.1 Date and timer operations .................................................................. 10
3.1.1 FB LGF_Astro .................................................................................... 10
3.1.2 FB LGF_SetTime ............................................................................... 14
3.1.3 FB LGF_TimerSwitch ......................................................................... 17
3.2 Counter operations ............................................................................. 20
3.2.1 FC LGF_CountFalInDWord ................................................................ 20
3.2.2 FC LGF_CountRisInDWord................................................................ 22
3.3 Comparator operations....................................................................... 24
3.3.1 FC LGF_CompareVariant .................................................................. 24
3.4 Math operations .................................................................................. 26
3.4.1 FC LGF_AverageAndDeviation .......................................................... 26
Siemens AG 2017 All rights reserved
3.4.2 FB LGF_FloatingAverage................................................................... 28
3.4.3 FC LGF_MatrixAddition ...................................................................... 30
3.4.4 FC LGF_MatrixInverse ....................................................................... 32
3.4.5 FC LGF_MatrixMultiplication .............................................................. 34
3.4.6 FC LGF_MatrixSubtraction................................................................. 36
3.4.7 FC LGF_MatrixTranspose .................................................................. 38
3.4.8 FB LGF_MinMaxHistory ..................................................................... 40
3.4.9 FC LGF_RandomINT / LGF_RandomReal ........................................ 41
3.4.10 FC LGF_SearchMinMax..................................................................... 43
3.4.11 FC LGF_XRoot ................................................................................... 45
3.5 Data handling ..................................................................................... 46
3.5.1 FB LGF_FIFO ..................................................................................... 46
3.5.2 FB LGF_ShellSortInt / LGF_ShellSortUInt / LGF_ShellSortReal....... 49
3.6 Converter operations .......................................................................... 51
3.6.1 FC LGF_BinaryToGray ...................................................................... 51
3.6.2 FC LGF_GrayToBinary ...................................................................... 52
3.6.3 FC LGF_DTLtoString ......................................................................... 53
3.6.4 FC LGF_StringToDTL ........................................................................ 55
3.6.5 FC LGF_TemperatureConvert ........................................................... 57
3.7 Signal generators ............................................................................... 58
3.7.1 FB LGF_Frequency ............................................................................ 58
3.7.2 FB LGF_Impulse ................................................................................ 60
3.7.3 FB LGF_SawTooth ............................................................................. 61
3.8 Technology operations ....................................................................... 63
3.8.1 FB LGF_LimRateOfChangeBasic ...................................................... 63
3.8.2 FB LGF_LimRateOfChangeAdvanced ............................................... 66
4 References ....................................................................................................... 71
5 History............................................................................................................... 72
5.1 Library versioning ............................................................................... 72
5.2 Change log ......................................................................................... 73
1 Library Overview
1.1 General
TIA Portal features an extensive number of “ready-to-use” instructions
(mathematical functions, times, timers, etc.). Furthermore, there are additional
useful basic functions.
These functions are provided in the form of a library and can be used freely. The
finished functions are freely adjustable and can thus be used universally.
The library described here is version-numbered and is continually expanded.
Information on the versioning can be found in chapter 5.1 Library versioning.
Hardware
Siemens AG 2017 All rights reserved
All blocks (FB, FC, DB,...) in the library can be used universally with the following
controllers:
S7-1200 and S7-1200 F product family
S7-1500 and S7-1500 F product family
Simulation with S7-PLCSIM (from V13 SP1 and higher)
Software
STEP 7 (TIA Portal) Basic or Professional from V13 SP1 Update 9 or higher
Note It is generally possible to open a library with STEP 7 Basic, even if it contains
STEP 7 Professional elements (e.g. S7-1500 controller). In this case, the user
will be informed by a message upon opening the library.
All elements (types and master copies) can be used if they are supported by the
installed hardware in the TIA portal.
In case one attempts to copy elements from the library with STEP 7 Basic which
are not supported (e.g. S7-1500 controller), an error message is displayed.
Note Information on the general use of libraries can be found in the S7-1200/1500
program guide under the chapter “libraries”.
https://support.industry.siemens.com/cs/ww/en/view/81318674
Note All blocks in the LGF have been created according to the programming style
guide.
https://support.industry.siemens.com/cs/ww/en/view/81318674
Siemens AG 2017 All rights reserved
Automation in less than 10 minutes TIA portal: Time Savers - Global libraries
https://support.industry.siemens.com/cs/ww/en/view/78529894
Which elements of STEP 7 (TIA Portal) can you store in a library as Type or as
Master Copy?
https://support.industry.siemens.com/cs/ww/en/view/109476862
How can you automatically open a global library upon starting up TIA portal
from V13 or higher, and how can you use it, for example, as a company
library?
https://support.industry.siemens.com/cs/ww/en/view/100451450
How can you open a global library with write access rights in STEP 7
(TIA Portal)?
https://support.industry.siemens.com/cs/ww/en/view/37364723
Short description
This block equals a surge relay or a toggle flip-flop including a set and reset input.
Block
Figure 3-2: FB LGF_PulseRelay
FB LGF_PulseRelay
BOOL trigger out BOOL
BOOL set
BOOL reset
Input parameters
Table 3-1: Input parameters
Siemens AG 2017 All rights reserved
Output parameters
Table 3-2: Output parameters
Parameter Data type Description
out BOOL Signal output
Mode of operation
Figure 3-3: LGF_PulseRelay signal diagram
trigger
1 1
set
2
reset
3 4
out
Time
Siemens AG 2017 All rights reserved
1. Every rising edge of the “trigger” input changes the Boolean value of the “out”
output.
2. Every rising edge of the “set” input sets the Boolean value of the “out” output to
“TRUE”.
3. Every rising edge of the “reset” input sets the Boolean value of the “out” output
to “FALSE”.
4. If the “set” and “reset” inputs are set in the same cycle, the “reset” input has
priority.
The block can also be used as frequency distribution. If the “trigger” input is
supplied with a fixed frequency, the “out” output provides half the frequency.
Short description
This block calculates the sunrise and sunset times for a particular location on earth.
The exact position is transmitted to the block in the form of geographic coordinates
(longitude and latitude).
60°
South (-)
30°
60°
South Pole
Block
Figure 3-5: FB LGF_Astro
FB LGF_Astro
REAL latitudeDD sunrise DTL
REAL longitudeDD sunset DTL
BOOL modeDMS daytime BOOL
LGF_typeAstroDMS latitudeDMS actSystemTime DTL
LGF_typeAstroDMS longitudeDMS actLocalTime DTL
TIME offsetSunrise error BOOL
TIME offsetSunset statusID UINT
status WORD
Input parameters
Table 3-3: Input parameters
Parameter Data type Description
latitudeDD REAL Latitude in degrees with decimal values (unit: degrees decimal),
Permissible value range [-90.00000..+90.00000]°
This is a common coordinate format in GPX files (GPS).
longitudeDD REAL Longitude in degrees with decimal values (unit: degrees
1
decimal)
Permissible value range [-180.0000..+180.0000]°
modeDMS BOOL 0 : Transfer format of position in “degrees decimal” via the formal
parameters “latitudeDD” and “longitudeDD”
1 : Transfer format of position in direction, degree, minute and
seconds via the formal parameters “latitudeDMS” and
“longitudeDMS”
latitudeDMS LGF_typeAstroDMS Latitude in compass direction; degree; minutes and seconds in
the PLC data type “LGF_typeAstroDMS”.
Permissible parameter values [N,S]; [0..90]; [0..59]; [0..59]
Permissible value range (sum of parameter values) [N, S, n, s];
[0..90]°
This is a common coordinate format in map navigation.
longitudeDMS LGF_typeAstroDMS Longitude in direction; degree; minutes and seconds in the PLC
data type “LGF_typeAstroDMS“.
Siemens AG 2017 All rights reserved
Output parameters
Table 3-4: Output parameters
Parameter Data Description
type
sunrise DTL Sunrise at specified location considering the “offsetSunrise“
sunset DTL Sunset at specified location considering the “offsetSunset“
daytime BOOL If the controller’s local time is between “sunrise” and “sunset” then
“daytime” returns the value “TRUE”.
actSystemTime DTL Current system time (UTC)
actLocalTime DTL Current local time
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status” returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status. See table below.
status WORD “status” returns the status/error code (see table below).
“RD_SYS_T”.
3 - Error/status of subordinate block -
“RD_LOC_T”.
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
If processes are to be run automatically, depending on the day and night change,
an astronomical clock function is required. Examples for this would be switching
exterior lighting on and off of opening and closing roller shutters.
If these processes are to be executed time-delayed, that is, at a specified time
before or after sunrise or sunset, an offset is required for each of them as well.
Note To execute the function exactly, it needs to be ensured that the system time and
local time of the SIMATIC controller are set correctly.
Based on the system time/local time of the SIMATIC controller and the set
coordinates, the block calculates the sunrise and sunset times. The offset times are
added on to the sunrise and sunset and returned at the “sunrise” and “sunset”
outputs. If the system time of the SIMATIC controller is in-between these values,
the “daytime” output is set to the value “TRUE”.
Note Because sunrise and sunset times change daily, it is possible that the “daytime”
output constantly “remains” on “TRUE” or “FALSE” for a longer period of time:
The coordinates can be entered in the “DMS” format (with PLC data type
“LGF_typeAstroDMS”), or in “Degree.Decimal”.
Which format is active can be set with the “modeDMS” formal parameter (see
Table 3-3).
The entry of the coordinate values is checked for valid values. In case of invalid
values, a corresponding error code is returned at “status” (see Table 3-3).
If there is an invalid coordinate value at a formal parameter and if this formal
parameter was activated via “modeDMS”, the “sunrise” and “sunset” outputs are
set to the value DTL#1970-01-01-00:00:00.
Example
The following example describes the block’s mode of operation.
Table 3-6: Geographic coordinates for Nuremberg-Moorenbrunn, date and system time
longitude: + 11.07675° or E 11° 4' 36''
Siemens AG 2017 All rights reserved
Figure 3-6: FB LGF_Astro, online monitoring of the block with the parameters as well as the
actual parameters via the monitoring table
3.1.2 FB LGF_SetTime
Short description
This block summarizes the functions system time, local time and setting time zone.
Block
Figure 3-7: FB LGF_SetTime
FB LGF_SetTime
BOOL setSystemTimeUTC actSystemTime DTL
DTL systemTimeUTC actLocalTime DTL
BOOL setLocalTime lastSetTimeZone STRING
DTL localTime error BOOL
BOOL setTimeZone statusID UINT
INT timeZone status WORD
BOOL daylightSavingTime
Siemens AG 2017 All rights reserved
Input parameters
Table 3-7: Input parameters
Parameter Data type Description
setSystemTimeUTC BOOL Rising edge sets the specified system time at the
“systemTimeUTC” input
systemTimeUTC DTL Specified system time, corresponds to UTC (Coordinated
Universal Time)
setLocalTime BOOL Rising edge sets the specified local time at the “localTime”
input
localTime DTL Defined local time
setTimeZone BOOL Rising edge sets
the specified time zone at the “timeZone” input
the specified daylight saving time at the
“daylightSavingTime” input
timeZone INT Defined local time (format [+-HHMM]
Examples:
UTC -12:00 [-1200]
UTC -03:30 [-330]
UTC [0]
UTC +13:00 [1300]
daylightSavingTime BOOL TRUE: switch from daylight saving time to standard time
(Local time + 60 min)
– from last Sunday in March at 02:00 am
– until last Sunday in October at 3:00 AM
FALSE: no switch from daylight saving time to standard time
Output parameters
Table 3-8: Output parameters
Parameter Data type Description
systemTime DTL Current system time (UTC)
localTime DTL Current local time
lastSetTimeZone STRING Time zone that has last been set by the block
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See table below.
status WORD “status” returns the status/error code (see table below).
operation).
2 - Error/status of subordinate block -
“SET_TIMEZONE“
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
This block summarizes the functions system time, local time and setting time zone.
The following time zones are possible at the “timeZone” input.
Input
Time zone
“timeZone”
Input
Time zone
“timeZone”
-330 (UTC -03:30) Newfoundland
-300 (UTC -03:00) Brasilia, Buenos Aires
-200 (UTC -02:00) Mid-Atlantic
-100 (UTC -01:00) Azores, Cape Verde Is.
0 (UTC) Dublin, Edinburgh, Lisbon, London
100 (UTC +01:00) Berlin, Bern, Brussels, Rome, Stockholm, Vienna
200 (UTC +02:00) Athens, Istanbul, Minsk, Bucharest
300 (UTC +03:00) Baghdad, Kuwait, Riyadh
330 (UTC +03:00) Iran
400 (UTC +04:00) Moscow, St. Petersburg, Volgograd, Abu Dhabi, Muscat
430 (UTC +04:30) Afghanistan
500 (UTC +05:00) Islamabad, Karachi, Tashkent
530 (UTC +05:30) India, Sri Lanka
545 (UTC +05:45) India, Sri Lanka
600 (UTC +06:00) Astana, Almaty, Dhaka, Colombo
630 (UTC +06:30) Coco Island, Myanmar
Siemens AG 2017 All rights reserved
3.1.3 FB LGF_TimerSwitch
Short description
This block is a timer. It is possible to set daily, weekly, monthly and annual
scheduler points, as well as scheduler points for weekdays or weekend days.
Block
Figure 3-8: FB LGF_TimerSwitch
FB LGF_TimerSwitch
USINT onMonth signal BOOL
USINT onDay actLocalTime DTL
USINT onWeekday error BOOL
USINT onWeekOfMonth statusID UINT
USINT onHour status WORD
USINT onMinute
USINT offMonth
USINT offDay
USINT offWeekday
Siemens AG 2017 All rights reserved
USINT offWeekOfMonth
USINT offHour
USINT offMinute
USINT mode
Input parameters
Table 3-10: Input parameters
Parameter Data type Description
onMonth USINT Month, in which the signal is to be set.
onDay USINT Day, on which the signal is to be set.
onWeekday USINT Weekday, on which the signal is to be set.
(Sunday = 1)
onHour USINT Hour, at which the signal is to be set.
onMinute USINT Minute, at which the signal is to be set.
offMonth USINT Month, in which the signal is to be reset.
offDay USINT Day, on which the signal is to be reset.
offWeekday USINT Weekday, on which the signal is to be reset.
(Sunday = 1)
offHour USINT Hour, at which the signal is to be reset.
offMinute USINT Minute, at which the signal is to be reset.
mode USINT Specification of module (see operating notes).
Output parameters
Table 3-11: Output parameters
Parameter Data type Description
signal BOOL Output signal
actLocalTime DTL Current local time
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See the table below.
status WORD “status” returns the status/error code (see table below).
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
The block provides different kinds of timers which are specified in the “mode”
parameter:
Daily timer (mode = 1)
Weekly timer (mode = 2)
Monthly timer (mode = 3)
Annual timer (mode = 4)
On weekdays, from Monday till Friday (mode = 5)
On weekend, Saturday and Sunday (mode = 6)
If the set start time corresponds to the controller’s actual local time, the “signal”
output is set to “TRUE”. If the set switch-off time corresponds to the controller’s
actual local time, the “signal” output is reset again.
Short description
This block analyses a tag of the DWORD type and returns, how often a 1-0
sequence (falling edge) occurs in the tag.
Application Example
Extract from the manual of the TM Timer DIDQ 16x24V technology module
With the oversampling function, the technology module captures the status of the
respective digital input per application cycle (e.g. OB61) at 32 times and in even
chronological intervals. These 32 states are collectively returned at the check-back
interface in the form of 32 bit values.
Figure 3-9: Oversampling example of DI0 at the DIDQ 16x24V TM Timer.
Siemens AG 2017 All rights reserved
Block
Figure 3-10: FC LGF_CountFalInDWord
FC LGF_CountFalInDWord
DWORD dWordActCycle Ret_Val INT
DWORD dWordPrevCycle
Input parameters
Table 3-14: Input parameters
Parameter Data type Description
dWordActCycle DWORD Double word, in which the falling edges are counted
dWordPrevCycle DWORD Double word from the previous cycle
Output parameters
Table 3-15: Output parameters
Parameter Data type Description
Ret_Val INT Number of falling edges in the double word
Mode of operation
The block counts the number of falling edges (1-0 transitions) in a tag of the
DWORD data type from left to right. In this, the “Ret_Val” output returns the
number of falling edges.
For falling edges to be detected at tag limits, the “dWordPrevCycle” input needs to
be interconnected with the tag of the previous cycle.
Example
The following example describes the block’s mode of operation. In this case, it is
assumed that a signal of unknown length is continually scanned per cycle in the
form of double words (DWORD).
Within this signal, 1-0 sequences (falling edges) are to be continually counted and
returned. For falling edges to be detected at tag limits as well in this example, the
“dWordPrevCycle” input needs to be interconnected with the tag of the previous
cycle.
Siemens AG 2017 All rights reserved
3.2.2 FC LGF_CountRisInDWord
Short description
This block analyzes a tag of the DWORD type and returns, how often a 0-1
sequence (rising edge) occurs in the tag.
Application example:
Extract from the manual of the TM Timer DIDQ 16x24V technology module:
With the oversampling function, the technology module captures the status of the
respective digital input per application cycle (e.g. OB61) at 32 times and in even
chronological intervals. These 32 states are collectively returned at the check-back
interface in the form of 32 bit values.
Figure 3-11: Oversampling example of DI0 at the DIDQ 16x24V TM Timer.
Siemens AG 2017 All rights reserved
In this case, the LGF_CountRisInDWord block is used to count the number of rising
edges.
Block
Figure 3-12: FC LGF_CountRisInDWord
FC LGF_CountRisInDWord
DWORD dWordActCycle Ret_Val INT
DWORD dWordPrevCycle
Input parameters
Table 3-17: Input parameters
Parameter Data type Description
dWordActCycle DWORD Double word, in which the rising edges are counted
dWordPrevCycle DWORD Double word from the previous cycle
Output parameters
Table 3-18: Output parameters
Parameter Data type Description
Ret_Val INT Number of rising edges in the double word
Mode of operation
The block counts the number of rising edges (0-1 transitions) in a tag of the
DWORD data type from left to right. In this, the “Ret_Val” output returns the
number of rising edges.
For rising edges to be detected at tag limits as well, the “dWordPrevCycle” input
needs to be interconnected with the tag of the previous cycle.
Example
The following example describes the block’s mode of operation. In this case, it is
assumed that a signal of unknown length is continually scanned per cycle in the
form of double words (DWORD).
Within this signal, 0-1 sequences (rising edges) are to be continually counted and
returned. For rising edges to be detected at tag limits as well in this example, the
“dWordPrevCycle” input needs to be interconnected with the double word of the
previous cycle.
Siemens AG 2017 All rights reserved
Short description
This block compares two structured actual parameters (array, PLC data type) and
returns, whether they correspond to the same type and have the same values.
Block
Figure 3-13: FC LGF_CompareVariant
FC LGF_CompareVariant
VARIANT variable1 (InOut) Ret_Val BOOL
VARIANT variable2 (InOut) error BOOL
statusID UINT
status WORD
Output parameters
Table 3-21: Output parameters
Parameter Data type Description
Ret_Val BOOL 0: Values of comparison tags or PLC data types differ.
1: Values of comparison tags are the same and PLC data
types are identical.
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See table below.
status WORD “status” returns the status/error code (see table below).
Mode of operation
This block compares two (structured) actual parameters and returns, whether they
correspond to the same value.
Note The following differences cannot be detected with the comparison method (byte
level):
Tags of the “Struct” data type cannot be compared.
With strings, differences may occur in the range between actual length and maximum
length.
A disparity can also be displayed with “same” tags, if the structure contains REAL
numbers.
Tags of the “ARRAY of BOOL“ type cannot be checked for equality with the function,
Siemens AG 2017 All rights reserved
because the used “CountOfElements” instruction also counts the filling elements (e.g.
with an ARRAY[0..1] of BOOL, 8 is returned).
Short description
This block determines the arithmetic average and the standard deviation from a
series of numbers.
Block
Figure 3-14: FC LGF_AverageAndDeviation
FC LGF_AverageAndDeviation
VARIANT variableArray arithmeticAverage REAL
standardDeviation REAL
error BOOL
statusID UINT
status WORD
Siemens AG 2017 All rights reserved
Input parameters
Table 3-23: Input parameters
Parameter Data type Description
variableArray VARIANT Series of numbers, which are used for calculation
Output parameters
Table 3-24: Output parameters
Parameter Data type Description
arithmeticAverage REAL Arithmetic mean
standardDeviation REAL Standard deviation
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See the table below.
status WORD “status” returns the status/error code (see table below).
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
Via the “variableArray” input, an array of any size is connected. After a data type
Siemens AG 2017 All rights reserved
query in the block, the arithmetic average and the standard deviation are
calculated from the values and returned.
Note An array with too many elements may lead to the scan cycle monitoring time
being exceeded.
Note Note: Only the data types Int, UInt, DInt, UDInt, USInt, SInt and Real are
supported.
3.4.2 FB LGF_FloatingAverage
Short description
This block calculates a moving arithmetic average from REAL values. This method
can be used to smooth a data series. The values can be read in cyclically or
triggered.
Block
Figure 3-15: LGF_FloatingAverage
FB LGF_FloatingAverage
REAL value average REAL
INT mode error BOOL
BOOL trigger status WORD
BOOL reset
Input parameters
Table 3-26: Input parameters
Siemens AG 2017 All rights reserved
Note The “LGF_FloatingAverage” block does not perform a data type query for the
“value” input parameter. With data types other than REAL, either an implicit
conversion is performed automatically, or an error is generated during
translation.
Further information can be found in the chapter “Overview of data type
conversion” in the TIA Portal online help or under:
https://support.industry.siemens.com/cs/ww/en/view/109011420/58427923211
Output parameters
Table 3-27: Output parameters
Parameter Data type Description
average REAL Moving average
error BOOL 0: no errors
1: Block errors
status WORD Status/ error code (see table below)
Mode of operation
The block calculates the (moving) average of the last 100 values that have been
read in. After reading in 100 values, the current value that has been read in always
replaces the oldest value (FIFO principle).
To read in the values, two modes are available which are determined via the
“mode” parameter:
mode = 1: Read in with each impulse at the “trigger” input
mode = 2: Cyclic read in
The block is activated as soon as a mode has been selected. It is possible to select
between the modes during operation.
Note In this block, a range of 100 values has been determined for the moving average
Siemens AG 2017 All rights reserved
calculation. You can adapt the range to your requirements by adjusting the
default value of “WINDOW_SIZE” in the block’s block interface under “Constant”.
3.4.3 FC LGF_MatrixAddition
Short description
This block adds up two matrices of the same size of the “REAL” type.
Block
Figure 3-16: FC LGF_MatrixAddition
FC LGF_MatrixAddition
VARIANT matrix1 matrixResult VARIANT
VARIANT matrix2 error BOOL
statusID UINT
status WORD
Input parameters
Table 3-29: Input parameters
Parameter Data type Description
Siemens AG 2017 All rights reserved
Output parameters
Table 3-30: Output parameters
Parameter Data type Description
matrixResult VARIANT Sum (Matrix)
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See the table below.
status WORD “status” returns the status/error code (see table below).
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
The block adds up two matrices of the same size. The individual fields of the two
incoming matrices are read, added up and then returned in the “matrixResults”
matrix.
Note Please note that all input and output matrices need to have the same number of
columns and lines.
Siemens AG 2017 All rights reserved
3.4.4 FC LGF_MatrixInverse
Block
Figure 3-17: FC LGF_MatrixInverse
FC LGF_MatrixInverse
VARIANT matrix matrixResult VARIANT
error BOOL
statusID UINT
status WORD
Input parameters
Table 3-32: Input parameters
Parameter Data type Description
matrix VARIANT inverting matrix
Siemens AG 2017 All rights reserved
Output parameters
Table 3-33: Output parameters
Parameter Data type Description
matrixResult VARIANT Resulting matrix.
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See the table below.
status WORD “status” returns the status/error code (see table below).
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
The block inverts a matrix of any size with the Shipley-Coleman procedure.
Note Note that the matrix needs to be quadratic. This means that the number of lines
needs to be equal to the number of columns.
The output matrix needs to be dimensioned as large as the input matrix.
Siemens AG 2017 All rights reserved
3.4.5 FC LGF_MatrixMultiplication
Short description
This block adds up two matrices of the “REAL” type.
Block
Figure 3-18: FC LGF_MatrixMultiplication
FC LGF_MatrixMultiplication
VARIANT matrix1 matrixResult VARIANT
INT rows1 error BOOL
INT columns1 statusID UINT
VARIANT matrix2 status WORD
INT rows2
INT columns2
Input parameters
Table 3-35: Input parameters
Siemens AG 2017 All rights reserved
Output parameters
Table 3-36: Output parameters
Parameter Data type Description
matrixResult VARIANT Product: The resulting matrix
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See table below.
status WORD “status” returns the status/error code (see table below).
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
The block adds up two matrices of varying sizes. The individual elements of the
two incoming matrices are read, added up and then returned in the “matrixResults”
Siemens AG 2017 All rights reserved
matrix.
Note Note that the numbers of rows of the first matrix needs to be equal to the number
of columns of the second matrix.
The size of the output matrix (m * n) results from the number of rows (m) of
“matrix1” and the number of columns (n) from “matrix2”.
3.4.6 FC LGF_MatrixSubtraction
Block
Figure 3-19: FC LGF_MatrixSubtraction
FC LGF_MatrixSubtraction
VARIANT matrix1 matrixResult VARIANT
VARIANT matrix2 error BOOL
statusID UINT
status WORD
Input parameters
Table 3-38: Input parameters
Parameter Data type Description
matrix1 VARIANT Minuend: From this matrix, “matrix2” is subtracted.
Subtrahend: This matrix is subtracted from “matrix1”.
Siemens AG 2017 All rights reserved
matrix2 VARIANT
Output parameters
Table 3-39: Output parameters
Parameter Data type Description
matrixResult VARIANT Difference: The resulting matrix
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See table below.
status WORD “status” returns the status/error code (see table below).
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
The block subtracts two matrices of varying sizes. The individual fields of the two
matrices are read, subtracted and then returned in the “matrixResults” matrix.
Note Please note that all input and output matrices need to have the same number of
columns and rows.
Siemens AG 2017 All rights reserved
3.4.7 FC LGF_MatrixTranspose
Block
Figure 3-20: FC LGF_MatrixTranspose
FC LGF_MatrixTranspose
VARIANT matrix matrixResult VARIANT
INT rows error BOOL
INT columns statusID UINT
status WORD
Input parameters
Table 3-41: Input parameters
Parameter Data type Description
matrix VARIANT Matrix to be transposed.
Siemens AG 2017 All rights reserved
Output parameters
Table 3-42: Output parameters
Parameter Data type Description
matrixResult VARIANT The resulting matrix.
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See table below.
status WORD “status” returns the status/error code (see table below).
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
The block transposes a matrix of any size. During this, the rows and the columns
are reversed (mirrored).
Note Note that the numbers of columns of the output matrix needs to be equal to the
number of rows of the input matrix. The numbers of rows of the output matrix
needs to be equal to the number of columns of the input matrix.
Siemens AG 2017 All rights reserved
3.4.8 FB LGF_MinMaxHistory
Short description
This block reads in a tag’s value each time it is called up and returns the maximum
and minimum value that has been read in since the first call up.
The evaluation can be reset, if required. The block supports the LREAL data type.
Block
Figure 3-21: FB LGF_MinMaxHistory
FB LGF_MinMaxHistory
LREAL Variable minValue LREAL
BOOL reset maxValue LREAL
Input parameters
Table 3-44: Input parameters
Parameter Data type Description
Siemens AG 2017 All rights reserved
Variable LREAL Tag whose value is checked for minimum and maximum.
reset BOOL The block is reset and the evaluation starts over.
Output parameters
Table 3-45: Output parameters
Parameter Data type Description
minValue LREAL Minimum value since the first call up or since the activation
of the “reset” input.
maxValue LREAL Maximum value since the first call up or since the activation
of the “reset” input.
Short description
This block generates a “random” value between a defined maximum and minimum
value per call up. The random number has the INT /REAL data type.
Block
Figure 3-22: FC LGF_RandomINT / FC LGF_RandomReal
FC LGF_Random…
INT / REAL maxValue Ret_Val INT / REAL
INT / REAL minValue error BOOL
statusID UINT
status WORD
Input parameters
Table 3-46: Input parameters
Parameter Data type Description
Siemens AG 2017 All rights reserved
maxValue INT / REAL Defines the random number’s upper limit value.
minValue INT / REAL Defines the random number’s lower limit value.
Output parameters
Table 3-47: Output parameters
Parameter Data type Description
Ret_Val INT / REAL Random number
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See table below.
status WORD “status” returns the status/error code (see table below).
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
This block generates random values that are between the specified “minValue”
value and “maxValue” value. This random value is returned via “Ret-Val”.
Background information
For the random value to be as random as possible, two important program steps
are performed. Every time the block is called up, a new star value is used. As start
value, the nanoseconds of the current daytime are used. In order to ensure that the
output value is not constantly increasing, the ten thousands, thousands, hundreds,
tens and units are transposed and partly exchanged. This process depends on the
current second of the controller’s system time.
Note If no maximum and minimum value (= 0) is determined, the block returns random
values from the entire range of values of INT / REAL.
Siemens AG 2017 All rights reserved
3.4.10 FC LGF_SearchMinMax
Short description
This block searches for the maximum and minimum value as well as the respective
index in an array.
The following data types of the array elements are supported:
Int, DInt, UInt, UDInt, USInt, SInt and Real.
Block
Figure 3-23: FC LGF_SearchMinMax
FC LGF_SearchMinMax
VARIANT variableArray min VARIANT
minArrayIndex INT
max VARIANT
maxArrayIndex INT
error BOOL
statusID UINT
status WORD
Siemens AG 2017 All rights reserved
Input parameters
Table 3-49: Input parameters
Parameter Data type Description
variableArray VARIANT Array in whose fields the maximum and minimum is
searched for.
Output parameters
Table 3-50: Output parameters
Parameter Data type Description
minValue VARIANT Smallest value found.
minArrayIndex INT Start index of the array plus minArrayIndex results in the
array index of the smallest value. The index starts with 0.
maxValue VARIANT Largest value found.
maxArrayIndex INT Start index of the array plus maxArrayIndex results in the
array index of the largest value. The index starts with 0.
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See table below.
status WORD “status” returns the status/error code (see table below).
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Siemens AG 2017 All rights reserved
Mode of operation
Via the “variableArray” input, an array of any size is connected. After a data type
query in the block, the elements are one after another copied into a tag of the
respective type and compared. The smallest and the largest value are returned as
well as their respective index in the array.
Note With multiple identical minimum and maximum values, the index of the first
minimum or maximum value is returned.
3.4.11 FC LGF_XRoot
Short description
This block calculates the xth root of a numerical tag.
Block
Figure 3-24: FC LGF_Random
FC LGF_XRoot
REAL Variable Ret_Val REAL
REAL root
Input parameters
Table 3-52: Input parameters
Parameter Data type Description
Variable REAL Tag, of which the root is to be calculated.
root REAL Root (e.g. 3 as third root)
Siemens AG 2017 All rights reserved
Output parameters
Table 3-53: Output parameters
Parameter Data type Description
Ret_Val REAL Output of result
Mode of operation
The block calculates the nth root of a number. To perform this function, the
following formula is extended.
(…)
In STEP 7 (TIA Portal) the function equals “EXP“ e and the function “LN“ ln(…).
This results in the following formula:
𝑅𝑒𝑡_𝑉𝑎𝑙 = 𝐸𝑋𝑃((1⁄𝑟𝑜𝑜𝑡 ) ∗ LN(𝑛𝑢𝑚𝑏𝑒𝑟))
Short description
This block stores incoming jobs/data and returns the oldest job that has not yet
been processed.
Block
Figure 3-25: FB LGF_FIFO
FB LGF_FIFO
BOOL execute done BOOL
BOOL mode error BOOL
VARIANT initialValue statusID UINT
BOOL resetBuffer status WORD
Input parameters
Table 3-54: Input parameters
Parameter Data type Description
execute BOOL Requirement of a pass.
mode BOOL Selecting the mode.
TRUE: Writing of “item” value into “buffer”
FALSE: Reading the value from “buffer” and output at “item”
initialValue VARIANT Value for initialization of ring buffer (usually: 0 )
resetBuffer BOOL Clearing out and initializing the ring buffer.
Output parameters
Table 3-56: Output parameters
Parameter Data type Description
done BOOL 1: Pass completed.
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See table below.
status WORD “status” returns the status/error code (see table below).
input.
1 16#8201 The data type of the InOut parameter -
“item” does not correspond to the data
type of the array elements at the
“buffer” input.
1 16#8202 The data type of the input “initialValue” -
does not correspond to the data type
of the InOut parameter “item”.
1 16#8601 The tag “nextEmptyItemIndex“ is not -
within the array-limits.
1 16#8602 The tag “firstItemIndex“ is not within -
the array-limits.
2 - Error/status of subordinate block -
“MOVE_BLK_VARIANT“.
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
For the block to be started up, a positive edge needs to be detected at the
“execute” input.
To call up the next read-task to be processed, the value “FALSE” must be activated
at the “mode” input. If this is the case, the next task/data to be processed is
returned at the “item” InOut parameter and this field is replaced in the ring buffer
with the value at the “initialValue” parameter.
To save a new write task in the ring buffer, the “TRUE” value needs to be activated
at the “mode” input. If this is the case, the value at the “item” InOut parameter or
the task in the ring buffer is saved at the next free location.
If during a pass, the “TRUE” value is activated at the “resetBuffer” input, all fields in
the ring buffer are reset to the value specified at the “InitialValue” input. After this,
the ring buffer can once more be filled with tasks/data.
Siemens AG 2017 All rights reserved
Short description
This block sorts an array with any number of elements (1000 max.) in ascending or
descending order. The following data types are supported:
Array of “Int“ type: LGF_ShellSortInt
Array of “UInt“ type: LGF_ShellSortUInt
Array of “Real“ type: LGF_ShellSortReal
Block
Figure 3-26: FB LGF_ShellSort…
FB LGF_ShellSort…
BOOL mode done BOOL
error BOOL
VARIANT variantArray (InOut) statusID UINT
status WORD
Siemens AG 2017 All rights reserved
Input parameters
Table 3-58: Input parameters
Parameter Data type Description
mode BOOL 0: sort in ascending order (default)
1: sort in descending order
Output parameters
Table 3-60: Output parameters
Parameter Data type Description
done BOOL 1: Sorting completed.
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code.
statusID UINT “statusID” returns the ID of the block reporting the status.
See table below.
status WORD “status” returns the status/error code (see table below).
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
The block sorts according to the shell sorting method. Note that the block’s
execution time strongly depends on how many elements the array to be sorted has.
The following overview shows a few of the block’s measured values which are
subject to the number of array elements.
Table 3-62: Execution times of the “LGF_ShellSort…” block
Number of
S7-1212C DC/DC/DC S7-1516-3 PN/DP
array elements
100 Approx. 11-16 ms Approx. 1-2 ms
1000 Approx. 185-205 ms Approx. 10-12 ms
Note The block is executed synchronously and is not distributed over several SPS
cycles. Therefore, the execution time directly affects the SPS cycle time. Keep
this behavior in mind with regards to your project and the controller used and
adjust the controller’s monitoring time, if necessary.
Short description
This block converts a binary coded value into a gray coded value.
Block
Figure 3-27: FC LGF_BinaryToGray
FC LGF_BinaryToGray
DWORD variableBinary Ret_Val DWORD
Input parameters
Table 3-63: Input parameters
Parameter Data type Description
variableBinary DWORD Binary coded value
Siemens AG 2017 All rights reserved
Output parameters
Table 3-64: Output parameters
Parameter Data type Description
Ret_Val DWORD Gray coded value
3.6.2 FC LGF_GrayToBinary
Short description
This block converts a gray coded value into a binary coded value.
Block
Figure 3-28: FC LGF_GrayToBinary
FC LGF_GrayToBinary
DWORD variableGray Ret_Val DWORD
Input parameters
Table 3-65: Input parameters
Parameter Data type Description
variableGray DWORD Gray coded value
Output parameters
Siemens AG 2017 All rights reserved
3.6.3 FC LGF_DTLtoString
Short description
This block converts a date of the DTL data type into a character string of the
STRING data type.
Block
Figure 3-29: FC LGF_DTLtoString
FC DTLtoString
DTL inDTL Ret_Val STRING
BOOL format
CHAR separatorDate
Input parameters
Table 3-67: Input parameters
Parameter Data type Description
inDTL DTL Date
Siemens AG 2017 All rights reserved
Output parameters
Table 3-68: Output parameters
Parameter Data type Description
Ret_Val STRING Displayed character string
Mode of operation
The block reads a date of the DTL data type, converts the date’s individual
components (year, month, day, hour...) into a character string and outputs it. The
individual components are placed at the correct position in the character string
according to the format selection. The separator between the date’s components is
variable.
Format selection
Via the “format” input parameter, you can select between the international
(ISO 8601) and the traditional (DE) date format for the returned character string.
1. International format (ISO 8601)
If the “format” input parameter has not been set (format = FALSE), the date is
returned as character string in the international format.
Example: 2016-03-16 13:34:12.123456789
The following figure shows the position of the individual characters in the
character string.
Figure 3-4: Position of the individual characters
The following figure shows the position of the individual characters in the
character string.
Figure 3-30: Position of the individual characters
Separator
At the “separatorDate” input parameter, you have to specify the separator between
Siemens AG 2017 All rights reserved
separatorDate = ‘-‘
outString = ‘2016-03-16 …’
3.6.4 FC LGF_StringToDTL
Short description
This block converts a character string of the String format with date components
into the DTL data type.
Block
Figure 3-31: FC LGF_StringToDTL
FC LGF_StringToDTL
STRING inString Ret_Val DTL
BOOL format error Bool
status WORD
Input parameters
Table 3-7: Input parameters
Parameter Data type Description
inString STRING Date as character string
Siemens AG 2017 All rights reserved
Output parameters
Table 3-8: Output parameters
Parameter Data type Description
Ret_Val DTL Returns the read in date
error BOOL 0: no error
1: Block error, “status” returns error code
status WORD “status” returns the status/error code (see table below)
Mode of operation
This block reads in a date as character string and converts it into the DTL data
type. The individual date components in the character string are separated
according to the format selection (positioning of data components in the character
string). In this, the separator between the components in the character string is
irrelevant.
Format selection
Via the “format” input parameter, you select, whether the read in character string is
specified in the international (ISO 8601) or traditional (DE) data format.
Siemens AG 2017 All rights reserved
Figure 2-10 shows the position of the individual characters in the character
string.
Figure 3-10: Position of the individual characters
The following figure shows the position of the individual characters in the
character string.
Figure 3-11: Position of the individual characters
3.6.5 FC LGF_TemperatureConvert
Short description
This block converts °Celsius to °Fahrenheit or Kelvin, as well as °Fahrenheit to
Kelvin and vice versa.
Block
Figure 3-32: FC LGF_TemperatureConvert
FC LGF_TemperatureConvert
INT mode Ret_Val REAL
REAL temperature error BOOL
Status WORD
Input parameters
Table 3-69: Input parameters
Parameter Data type Description
mode INT Mode
Siemens AG 2017 All rights reserved
1. °Celsius to °Fahrenheit
2. °Fahrenheit to °Celsius
3. °Celsius to Kelvin
4. Kelvin to °Celsius
5. °Fahrenheit to Kelvin
6. Kelvin to °Fahrenheit
temperature REAL Temperature to be converted
Output parameters
Table 3-70: Output parameters
Parameter Data type Description
Ret_Val REAL Converted temperature
error BOOL 0: no errors
1: Block errors
status WORD “status” returns the status/error code (see table below).
Short description
The block generates a signal which, depending on a defined frequency and a
pulse-to-pause ratio, switches between the values “0” and “1”.
Block
Figure 3-33: FB LGF_Frequency
FB LGF_Frequency
REAL frequency clock BOOL
REAL pulsePauseRatio countdown TIME
Input parameters
Table 3-72: Input parameters
Parameter Data type Description
Siemens AG 2017 All rights reserved
Output parameters
Table 3-73: Output parameters
Parameter Data type Description
clock BOOL Output switches with defined frequency
countdown TIME Remaining time of current “clock” status
Mode of operation
The “clock” output is a Boolean value that toggles in the desired frequency. Via the
“pulsePauseRatio“ input, the pulse-pause ratio is set.
The “Countdown” output outputs the remaining time of the current state of “clock”.
If the desired frequency or pulse-pause ratio is smaller than or equal to 0.0, then
the “clock” output = FALSE and “countdown” = “0 s”.
Example
Figure 3-34: FB LGF_Frequency
FB LGF_Frequency
𝑡𝑜
𝑢𝑙 𝑒𝑃𝑎𝑢 𝑒𝑅𝑎𝑡 𝑜 = 𝑡𝑜
= 1:3
T
ton toff T =2s
ton = 1.5 s
clock toff = 0.5 s
Siemens AG 2017 All rights reserved
3.7.2 FB LGF_Impulse
Short description
This block generates impulses in a predefined frequency. The impulse is always
available for a (controller) cycle.
Block
Figure 3-35: FB LGF_Impulse
FB LGF_Impulse
REAL frequency impulse BOOL
countdown TIME
Input parameters
Table 3-74: Input parameters
Parameter Data type Description
frequency REAL Clock speed
Siemens AG 2017 All rights reserved
Output parameters
Table 3-75: Output parameters
Parameter Data type Description
impulse BOOL Signal with impulses
countdown TIME Time until next impulse
Mode of operation
The block generates impulses at the “impulse” output with the frequency
“frequency”.
The frequency is implemented via the “LGF_Frequency“ block. If there is a
negative edge at the “clock” output of the “LGF_Frequency” block, an impulse is
generated at the “impulse” output.
Example
Figure 3-36
FB LGF_Impulse
impulse TRUE
0.5 frequency Period duration: 2 seconds
countdown T#0S_703MS
Note For the block to work, the “LGF_Frequency“ block additionally needs to be
included into the STEP7 project.
3.7.3 FB LGF_SawTooth
Short description
This block generates a sawtooth-shaped signal path. Every sawtooth consists of a
defined number of steps (increments).
Block
Figure 3-37: FB LGF_SawTooth
FB LGF_SawTooth
INT startValue value INT
TIME timeRange
INT incrementRange
INT numberSteps
BOOL endlessSteps
BOOL restart
Input parameters
Siemens AG 2017 All rights reserved
Note Note that changes to the input parameters only become effective upon “restart”.
Output parameters
Table 3-77: Output parameters
Parameter Data type Description
value INT Current value of the sawtooth-signal.
Mode of operation
The block calculates the values for a sawtooth-shaped signal path which are output
at the “value” output parameter. The signal starts with the start value “startValue”
and is added up with the “increment” value every time the “timeRange” time interval
expires. The value can also be negative.
If the “endlessSteps” tag is set to “FALSE”, the number of summation steps is
counted. If it exceeds the “numberSteps” value, then the output parameter “value”
is reset to the start value. A new sawtooth starts.
If the “endlessSteps” tag is set to “TRUE”, then - once beginning at “startValue” -
the “increment” value is continuously added up. If the maximum positive INT value
range (32767) of the “value” output parameter is exceeded, “value” switches to the
maximum negative INT value range (-32768) and is continuously added up.
value
endless Steps= true
Siemens AG 2017 All rights reserved
incrementRange
startValue
time
restart = false
Short description
This block limits the rate of change of an input value. A jump function turns into a
ramp function.
Block
Figure 3-39: FB LGF_LimRateOfChangeBasic
FB LGF_LimRateOfChangeBasic
REAL inputValue outputValue REAL
REAL setRateLim error BOOL
REAL defaultOutValue statusID UINT
BOOL onDefaultOutValue status WORD
OB_CYCLIC callOB
Siemens AG 2017 All rights reserved
Input parameters
Table 3-78: Input parameters
Parameter Data type Description
inputValue REAL Input value (jump function)
setRateLim REAL Rate of change of ramp function (1/second)
onDefaultOutValue BOOL Preset output value
dafaultOutValue REAL Value for the preset output value
callOB OB_CYCLIC Calling cyclic interrupt OB
Output parameters
Table 3-79: Output parameters
Parameter Data type Description
outputValue REAL Output value
error BOOL 0: no errors
1: Block error, “statusID” returns error source, “status”
returns error code
statusID UINT “statusID” returns the ID of the block reporting the status.
See table below.
status WORD “status” returns the status/error code (see table below)
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Mode of operation
Siemens AG 2017 All rights reserved
The ramp is a limitation line and refers to a change of rate per second; if
“setRateLim = 10.0“ is parameterized, for example, then at a sampling time of
1s/100ms/10ms and if “inputValue > outputValue“ is set, 10.0/1.0/0.1 are added to
“outputValue“ during each block call, until “inputValue” has been reached.
The limitation of the change of rate is valid for the increase and the decrease in
both, the positive and the negative value range.
The “outputValue” output can be preset or initialized.
The time interval of the calling cycle interrupt OB is detected by connecting the
calling cycle interrupt OB at the “callOB” input parameter.
Function growth
Figure 3-41: Ramp function growth
onDefaultOutValue
outputValue
inputValue (t)
defaultOutValue
t
Siemens AG 2017 All rights reserved
3.8.2 FB LGF_LimRateOfChangeAdvanced
Short description
The LGF_LimRateOfChangeAdvanced block limits the rate of change of an input
value. Jump functions turn to ramp functions. Additionally, the block has several
operation modes.
Block
Figure 3-42: FB LGF_LimRateOfChangeAdvanced
FB LGF_LimRateOfChangeAdvanced
REAL inputValue outputValue REAL
REAL setPosUpRateLim posUpRateLim BOOL
REAL setPosDownRateLim posDownRateLim BOOL
REAL setNegUpRateLim negUpRateLim BOOL
REAL setNegDownRateLim negDownRateLim BOOL
REAL setHighLim highLim BOOL
REAL setLowLim lowLim BOOL
REAL processValue error BOOL
Siemens AG 2017 All rights reserved
Input parameters
Table 3-81: Input parameters
Parameter Data type Description
inputValue REAL Input value (jump function)
Rate of change per second for up ramp in positive value
setPosUpRateLim REAL
range
Rate of change per second for down ramp in positive value
setPosDownRateLim REAL
range
Rate of change per second for up ramp in negative value
setNegUpRateLim REAL
range
Rate of change per second for down ramp in negative
setNegDownRateLim REAL
value range
setHighLim REAL Upper limit
setLowLim REAL Lower limit
processValue REAL Process value
defaultOutValue REAL Value for preset of output value
onDefaultOutValue BOOL Preset output value
track BOOL Switching input value (tracking)
manOp BOOL Switching process value
reset BOOL Restart
Siemens AG 2017 All rights reserved
Output parameters
Table 3-82: Output parameters
Parameter Data Description
type
outputValue REAL output quantity
posUpRateLim BOOL Up rate limitation in positive area switched on
posDownRateLim BOOL Down rate limitation in positive area switched on
negUpRateLim BOOL Up rate limitation in negative area switched on
negDownRateLim BOOL Down rate limitation in negative area switched on
highLim BOOL Upper limit switched on
lowLim BOOL Lower limit switched on
0: no errors
error BOOL 1: Block error, “statusID” returns error source, “status” returns error
code
“statusID” returns the ID of the block reporting the status. See table
statusID UINT
below
status WORD “status” returns the status/error code (see table below)
Note If “statusID” is > 1, all values of the “status” output came directly from called up
instructions (see table on output parameters). In this case, get the information on
the respective instructions from the TIA Portal Online Help.
Siemens AG 2017 All rights reserved
Mode of operation
For the positive/negative value range, two rates of change are parametrizable for
the ramp (rising and falling values). Via controller inputs, the following operation
modes can be selected:
Restart
Presetting the output
Normal operation (automatic)
Tracking
Switching process value (manual)
The output value can be limited via two parametrizable limits. An active limitation of
the rate of change of a ramp as well as an active limitation of the output value are
reported via the outputs.
The time interval of the calling cycle interrupt OB is detected by connecting the
calling cycle interrupt OB at the “callOB” input parameter.
Restart
Upon restart “reset = TRUE”, the output “outputValue” is reset to 0.0. If
“onDefaultOutValue = TRUE“ is set, “defaultOutValue” is output. All signal outputs
are set to FALSE.
Normal operation
The ramps are a limitation lines and refer to a change of rate per second; if
“setPosUpRateLim = 10.0“ is parameterized, for example, then at a sampling time
of 1s/100ms/10ms and if “inputValue > outputValue“ is set, 10.0/1.0/0.1 are added
to “outputValue“ during each block call, until “inputValue” has been reached.
The limitation of the change of rate is parametrizable for the rise and the fall in
both, the positive and the negative value range.
Siemens AG 2017 All rights reserved
Tracking
If the input “track = TRUE“ is set, the input value “inputValue” is switched directly to
the output value “outputValue”. Thus, jumps of the input value are also output.
Function growth
Figure 3-44: Ramp function
manOp
track
onDefaultOutValue
outputValue
setHighLim
inputValue (t)
defaultOutValue
processValue (t)
setLowLim
Siemens AG 2017 All rights reserved
highLim
lowLim
posUpRateLim
posNegRateLim
negDownRateLim
negUpRateLim
4 References
Table 4-1: References
Topic Title
\1\ Siemens Industry Online http://support.automation.siemens.com
Support
\2\ Download page of the entry https://support.industry.siemens.com/cs/ww/en/
view/109479728
5 History
5.1 Library versioning
The library and library elements are maintained on basis of the following table:
Table 1: Definition of version
V 1. 2. 3
Non-compatible changes Compatible changes Error correction
Reduction of interfaces Extension of Bug fix
Modification of interfaces
interfaces Compatible
Incompatible extension extension of
of functionality functionality
Versioning example
Table 5-2: Example for changing the version
Library FB1 FB2 FC1 FC2 Comment
Error correction
LGF_FIFO V1.0.1
Error correction
V2.0 07/2016 New:
Chapter 1.3 Library resources
FB LGF_PulseRelay V1.0.0
FB LGF_SetTime V1.0.0
FB LGF_FloatingAverage V1.0.0
FC LGF_DTLtoString V1.0.0
FC LGF_StringToDTL V1.0.0
FB LGF_LimRateOfChangeBasic V1.0.0
FB LGF_LimRateOfChangeAdvanced V1.0.0
Revised:
LGF_Astro V1.1.1
systemTime and localTime outputs added
FB LGF_TimerSwitch V1.1.0
Two new modes: Weekday, weekend
FB LGF_ShallSort… V1.1.0
New mode: Sort in descending order
FB LGF_Frequency V1.1.0
New function: Pulse-pause ratio adjustable
FB LGF_Impulse V1.1.0
Calls new LGF_Frequency V1.1.0.
V2.0.1 01/2017 Revised:
LGF_Astro V1.1.2
Error correction at calculation of sunrise and sunset