• No results found

Designing SCL Programs

2.3 Using SCL Blocks to Perform the Tasks

The task defined above is best performed by means of a structured SCL program. This involves using a modular design; that is, the program is subdivided into a number of blocks, each of which performs a specific subtask. In SCL, as with the other programming languages in STEP 7, you have a number of block types available. For more information on these types, see Chapters 1, 7 and 8.

You can adopt the following procedure:

1. Define the subtasks

2. Select and assign the available block types 3. Define the interfaces between the blocks 4. Define the input/output interface 5. Program the blocks

2.3.1 Defining the Subtasks

The subtasks are shown as boxes in Figure 2-3. The rectangular shaded areas represent the blocks. The order of the code blocks from left to right

corresponds to the order in which they are called.

Organization Block

Steps in the Task

Overview

Designing SCL Programs

2.3.2 Selecting and Assigning the Available Block Types

The individual blocks were selected according to the following criteria:

User programs can only be called by an OB. Since the measured data are to be received cyclically, an OB for a cyclic operation call (OB1) is required.

Part of the processing – Data Input and Data Output – is programmed in the OB.

The subtask Record Measured Data requires a block with a memory; that is, a function block (FB), since certain block-specific data (for example, the cyclic buffer) must be retained from one program cycle to the next. The location for the task Store Data (memory) is the instance data block RECORD_DATA.

The same FB can also perform the subtask Access and Select Output Data, since this is where the required data is kept.

When selecting the type of block for performing the subtasks Sort Measured Data and Calculate Results you must remember that an output buffer has to be set up which contains the calculated results Square Root and Square for each measured value.

For that reason, this block can only be an FB. Since this FB is called by a higher-level FB it does not require its own DB. Its instance data can be stored in the instance data block of the calling FB.

The type of block best suited to performing the subtasks Calculate Square Root and Square is a function (FC) since the the result can be returned as a function value. In addition, no data which has to be stored for more than one program cycle is required for the calculation.

The standard SCL function SQRT can be used for calculating the square root.

A special function SQUARE is to be created for calculating the square and will also check that the value is within the permissible range.

Overview

CYCLE

RECORD

ANALYZE

SQRT (Square Root) and SQUARE

2.3.3 Defining the Interfaces Between the Blocks

The interface between two blocks is defined by declaring the formal parameters. SCL offers the following possibilities:

S Input parameters: declared by means of VAR_INPUT S Output parameters: declared by means of VAR_OUTPUT S In/out parameters: declared by means of VAR_IN_OUT

When a block is called, input data is passed to it as actual parameters. After the program returns to the calling block, the output data is prepared for copying. An FC can transfer its result as a function value (for details, refer to Chapter 16).

The OB CYCLE has no formal parameters itself. It calls the FB RECORD and passes to it the measured value and the control data for its formal parameters (Table 2-1):

Table 2-1 Formal Parameters of RECORD

Parameter Name Data Type Declaration Type Description measval_in INT VAR_INPUT Measured value

newval BOOL VAR_INPUT Switch for copying measured value to cyclic buffer resort BOOL VAR_INPUT Switch for sorting and

analyzing measured data select

function

BOOL VAR_INPUT Two-way switch for selecting square root or square selection WORD VAR_INPUT Code for selecting output

value

newselection BOOL VAR_INPUT Switch for copying code result_out DWORD VAR_OUTPUT Output of calculated result measval_out DWORD VAR_OUTPUT Output of corresponding

measured value Overview

RECORD

Designing SCL Programs

The FB RECORD calls the FB ANALYZE. The information they share is the measured value array to be sorted. For that reason, this array is declared as an in/out parameter. A structured array is set up as an output parameter for the calculated results Square Root and Square. For details of formal parameters, see Table 2-2:

Table 2-2 Formal Parameters of ANALYZE Parameter

Name

Data Type Declaration Type

Description

sortbuffer ARRAY[..]

OF REAL

VAR_IN_OUT Measured value array, corresponds to cyclic buffer calcbuffer ARRAY[..]

OF STRUCT

VAR_OUTPUT Array for results:

Structure having components

”Square Root” and ”Square”

of type INT

These functions are called by ANALYZE. They require an input value and return their results as a function value, see Table 2-3.

Table 2-3 Formal Parameters and Function Values of SQRT and SQUARE

Name Data

Type

Declaration Type Description

value REAL VAR_INPUT Input for SQRT

SQRT REAL Function value Square root of input value value INT VAR_INPUT Input for SQUARE SQUARE INT Function value Square of input value ANALYZE

SQRT and SQUARE

2.3.4 Defining the Input/Output Interface

Figure 2-4 shows the input/output interface. Note that in the case of input/output in bytes, the least significant byte is at the top and the most significant byte is at the bottom. In the case of input/output in words on the other hand, the opposite is true.

Power

Channel Description Input Module

0 Copy measured value

1 Initiate sorting and calculation 2 Select result: square root or square 3 Select output: measured value or result

4 Code, Bit 0

5 Code, Bit 1

6 Code, Bit 2

7 Copy code

0 to 7 Input byte: measured value Channel Description

Output Module

0 to 7 Most significant byte of output word (bits 8 to 15):

Required for calculation of square only, otherwise 0

0 to 7 Least significant byte of output word (bits 0 to 7):

Measured value or result:

square root or square

DisplaysSwitches

Figure 2-4 Displays and Controls Overview

Designing SCL Programs

2.3.5 Programming the Blocks

Once the interfaces have been defined, you can create each of the blocks separately from one another. This is best done from the top down; that is, in the order CYCLE, RECORD, ANALYZE and SQUARE. This is the order in which the blocks are described below.

When compiling the blocks, you must remember that a block must exist before you can use it; that is, call it from another block. This dictates that the order of the blocks in the SCL source file must be SQUARE, ANALYZE, RECORD, and CYCLE (for details, refer to Chapter 8).

The comprehensibility of the program will be improved if you use symbolic names for module addresses and blocks. To do this, you must enter

definitions in the symbol table as shown in Figure 2-5 (see Chapter 7). The names must conform to the naming conventions for either IDENTIFIERS or symbols (for example, ”Input 0.0”), see Appendix A.

Figure 2-5 shows the introductory comment of the SCL source file and the symbolic names which are to be declared in the symbol table to permit its error–free compilation.

(*################################################################################

SCL Program for Recording and Processing Measured Data:

A measured value whose signal is present on the input module is copied from input 0.0 (input switch)

Subsequent processing of the measured values can be controlled by various switches

All values are stored in the working section of the function block RECORD, the instance data block RECORD_DATA.

The program is programmed symbolically. In order for it to be compiled, details of the assignment of the symbolic names to the module addresses and the blocks running on the CPU must be specified. This requires the following symbol table:

Input IB1 BYTE // Measured value

Input 0.0 I0.0 BOOL // Input switch for copying measured value Sort switch I0.1 BOOL // Initiates sorting and calculation Function switch I0.2 BOOL // Selects result: square root or square Output switch I0.3 BOOL // Selects output: measured value or result Code IW0 WORD // Code, relevant bits 12,13 and 14

Code switch I0.7 BOOL // Copies code

Output QW4 INT // Measured value or result: square root or square RECORD FB10 FB10 // Records measured values,

// accesses and selects output RECORD_DATA DB10 FB10 // Instance data block for RECORD

Programming

Related documents