Programming a logic block in the source file

Một phần của tài liệu Hans berger automating with SIMATIC s7 1200 configuring programming (Trang 322 - 326)

9.7 Working with source files

9.7.2 Programming a logic block in the source file

Table 9.10 shows which keywords you require for block programming and the sequence in which the keywords are used.

Block header and block properties

Programming a logic block commences with the keyword for the block type and with the specification of the block name in quotation marks. On import, the block is assigned the first free number of the block type. This can be changed in the block properties after import.

An organization block has the name of the event class (e.g. “Cyclic interrupt”, see Table 5.5 on page 154). On import, the organization block is then assigned the first free number of the event class (e.g. 30 for event class “Cyclic interrupt”). Only one organization block can be imported to a source file per event class.

In the case of functions, you specify the data type of the function value following the addressing; example: FUNCTION “FC_name” : INT. If the function does not have a function value, the data type is called VOID.

The block title and the block commentary are entered in the block properties under General > Information.

The data for the block properties is optional. You simply omit the surplus data together with the keywords.

You activate the block attribute Optimized block access by specifying {S7_Opti- mized_Access := ’TRUE’}.

The keyword KNOW_HOW_PROTECT protects the block from unwanted access. You can no longer cancel this protection, in contrast to block protection with password in the TIA Portal.

Block interface

The block interface contains the definition of the block parameters and block-local tags. You cannot program every declaration section in every block (see Table 9.10).

If you do not use a declaration section, omit it including the keywords.

The declaration of a tag consists of the name, the data type, possibly a default set- ting, and an optional tag comment. Example:

Quantity : INT := +500; //Units per batch

If a name contains special characters such as a space, it must be given in quotation marks. Not all tags can have default values, e.g. default values are not possible for the temporary local data.

Table 9.10 Keywords for logic blocks

Section Keyword Meaning

Block type ORGANIZATION_BLOCK “OB_name

FUNCTION_BLOCK “FB_name

FUNCTION “FC_name” : Data type

Start of an organization block Start of a function block Start of a function Header TITLE = block title

//Block comment

Block title in the block properties Block comment in the block properties {S7_Optimized_Access := ’TRUE’}

KNOW_HOW_PROTECT

Optimized block access: selected Know-how protection (cannot be canceled) NAME : Block name

FAMILY : Block family AUTHOR : Created by VERSION : Version

Block property: Block name Block property: Block family Block property: Created by Block property: Block version Declaration VAR_INPUT

name : Data type := Default setting; *) END_VAR

Input parameters (for FC, FB, and some OBs)

VAR_OUTPUT

name : Data type := Default setting; *) END_VAR

Output parameters (for FC and FB)

VAR_IN_OUT

name : Data type := Default setting; *) END_VAR

In-out parameters (for FC and FB)

VAR

name : Data type := Default setting; *) END_VAR

Static local data (only with FB)

VAR_TEMP

name : Data type := Default setting; *) END_VAR

Temporary local data

Program BEGIN Beginning of the block program

Program statement; Termination of each statement with semicolon //Line comment Line comment up to end of line, also programmable

following statements

(* Block comment *) Block comment, can extend over several lines Block end END_ORGANIZATION_ BLOCK

END_FUNCTION_BLOCK END_FUNCTION

End of an organization block End of a function block End of a function

*) Superimposing of data types with the keyword AT is additionally possible (see text)

The sequence of individual declaration sections is defined as shown in Table 9.10.

The sequence within a declaration section is optional. If you combine tags with data type BOOL and also combine byte-wide tags with data types BYTE and CHAR, you can minimize the memory requirements for blocks with non-optimized block access.

The superimposition of data types with the keyword AT is programmed directly after the declaration of the tags to be overlaid. The schema is as follows: var_new AT var_old : new_data type. Example:

VAR_INPUT

Date : DTL;

Byte array AT date : ARRAY [1..12] OF BYTE;

END_VAR

You can now address the total tag in the program of the block using #Date or indi- vidual components such as the day using #Byte array[3]. Data type superimposition is possible for blocks with non-optimized block access and is described in Chapter 4.3.3 “Overlaying tags (data type views)” on page 93.

Program section

The program section of a logic block starts with the keyword BEGIN and ends with the keyword for the block end.

No distinction is made between upper and lower case when compiling. The syntax of an SCL statement is described in Chapter 9.1.2 “SCL statements and operators”

on page 286. You can enter one or more spaces or tabulators between operation and operand. To achieve a clearer layout of the source text, for example to increase leg- ibility, you can enter any spaces and/or tabs between the words.

You must conclude every statement by a semicolon. You can also program several statements per line, each separated by a semicolon. Following the semicolon you can specify a statement comment, separated by two slashes; this extends up to the end of the line.

A line comment commences with two slashes at the start of the line. It extends up to the end of the line. A block comment is started by a round left parenthesis and asterisk and finished by an asterisk and round right parenthesis. A block comment can extend over several lines.

If the source file contains blocks which are called in the source file or if data oper- ands are accessed, you should observe a specific sequence in the source file. The blocks or data operands should be located before the position of use in the source file.

When calling a block, you enter the block parameters in round parentheses, each separated by a comma. Make sure that the transferred block parameters are listed in the same order as they have been declared in the called block. The same applies to functions with parameter list from the program elements catalog.

Fig. 9.30 shows an example of an SCL source file for a function block with the asso- ciated instance data block.

FUNCTION_BLOCK FIFO

TITLE=Intermediate memory for 4 values AUTHOR : Berger

FAMILY : Book1200 NAME : FIFO_SCL VERSION : 01.00

//Example of a function block VAR_INPUT

Import : BOOL := FALSE; //Import with positive edge Input value : REAL := 0.0; //Input in data format REAL END_VAR

VAR_OUTPUT

Output value : REAL := 0.0; //Output in data format REAL END_VAR

VAR

Value1 : REAL := 0.0; //First saved REAL value Value2 : REAL := 0.0; //Second value

Value3 : REAL := 0.0; //Third value Value4 : REAL := 0.0; //Fourth value

Edge trigger flag : BOOL := FALSE;//Edge trigger flag for importing END_VAR

BEGIN

//Control is implemented with positive edge on #Import //

IF #Import = TRUE AND #Edge trigger flag = FALSE THEN

#Output value := #Value4;

#Value4 := #Value3; //Transfer starting with the last value

#Value3 := #Value2;

#Value2 := #Value1;

#Value1 := #Input value;

END_IF;

#Edge trigger flag := #Import; //Update edge trigger flag END_FUNCTION_BLOCK

DATA_BLOCK DB_FIFO

TITLE = Instance data block for "FIFO"

//Example of an instance data block AUTHOR : Berger

FAMILY : Book1200 NAME : FIFO_Dat VERSION : 01.00

"FIFO" //Instance for the FB "FIFO"

BEGIN

Value1 := 1.0; //Individual default setting Value2 := 1.0; //of selected values

END_DATA_BLOCK

Fig. 9.30 Example of an SCL source file

Một phần của tài liệu Hans berger automating with SIMATIC s7 1200 configuring programming (Trang 322 - 326)

Tải bản đầy đủ (PDF)

(577 trang)