Siemens Industry Online Support
Siemens AG
Entry type: Application example, Entry ID: 16772536, Entry date: 08/29/2003

Search for minimum / maximum values (slave pointer) and averaging.

  • Entry
  • Associated product(s)

How can I determine the minimum/maximum values from the values in a data block?

The search for minimum / maximum values within a data block (slave pointer) can be done by means of loop programming. The attached sample program facilitates the search for minimum / maximum values in a data block and calculates the average.

Description of the function:
The search for the minimum/maximum values, including averaging, is created as a function. An IN parameter is used to define the data block in which the search is to be performed. The data type ID defines the data type of the values in the data block.

Browsing takes place within a loop. The number of loop runs is calculated from the number of bytes in the DB and the data type ID. The number of bytes in the DB is ascertained via the system function SFC 24.

The function presupposes that there are only values of one data type in the DB. The DB is tested from address 0 on in data type increments. The values are totaled at the same time and are divided by the number of values at the end (averaged).

If the length of the DB is incompatible with the data type, the amount remaining (e.g. 2 bytes in the case of the REAL data type) is ignored.
If the DB is smaller than the length of the data type, a parameter assignment error is output. This also happens if the DB doesn't exist (info supplied by SFC 24).

The average is output as a REAL value. The min./max. values are saved for all the data types in DWORD variables. The actual data type is maintained. This means that the OUT parameters for the function can be allocated to variables with the corresponding data types (without conversion functions) (refer to the example).

CALL "MinMax"
DBNumber :=3
DatatypeID :=1 // Identifier for INT
MinValue :=MD20
AverageValue:= #Average
MaxValue :=MD24
PAFE :=#Error
// from DWORD to INT
L MD 20
T #IntegerVariable_Min
L MD 24
T #IntegerVariable_Max

Interface description of the function FC 1 "MinMax":








Number of the DB being checked




Data type identifier: 1=INT; 2=DINT; 3=REAL




Minimum value arising in the DB




Average value calculated over the number of values




Maximum value arising in the DB




Parameter assignment error

Address registers are not changed by the function.

Description of the sample program:
In the programming example, "MinMax" is called up in OB 1 via FC 1. In the current example a search is performed for the minimum/maximum values in DB 3. DB 3 contains "INT"-type values.
In order to facilitate further tests, DB 4 is prepared with DINT-type values and DB 5 is prepared with "REAL"-type values, and the requisite FC 1 call is available in OB 1 as a source code.
You can use the variable declaration table to monitor the OUT parameters of the function and to change the values in the DBs.

The attached download contains the STEP 7 project with the function block for determining the minimum/maximum values and for averaging, as well as the prepared data blocks and variable declaration tables.


Copy the "Min_Max.exe" file to a separate folder and then double-click on the file to start it. The STEP 7 project is now automatically extracted along with all its sub-folders. You can now open and process the extracted project using the SIMATIC Manager.

Runnability and test environment:
The example can be used on all the CPU variants of SIMATIC S7-300, S7-400 and WinAC.
The following table lists the components which have been used to create this entry and verify the functions described:

Test environment

Version / Release

PC platform

Pentium III 800 MHz, 264 MB

Operating system

Windows XP




CPU 315-2DP

Security information
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 constitute one element of such a concept. For more information about industrial security, please visit