18.1 Working with source files
18.1.2 Programming a logic block in the source file
Table 18.1 shows which keywords you require for block programming and the se- quence in which the keywords are used.
Table 18.1 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 CODE_VERSION1
KNOW_HOW_PROTECT
Only with FB (“not capable of multi-instance”), only with STL
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 parameter (not with OB)
VAR_OUTPUT
name : Data type := Default setting; *) END_VAR
Output parameter (not with OB)
VAR_IN_OUT
name : Data type := Default setting; *) END_VAR
In/out parameter (not with OB)
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 Start of block program,
can be omitted with SCL
NETWORK Network start, only with STL
TITLE = Network title Network title, only with STL
//Network comment Network comment; line comment with SCL 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, only with SCL
NETWORK Start of next network, only with STL
… … etc.
Block end END_ORGANIZATION_ BLOCK END_FUNCTION_BLOCK END_FUNCTION
End of an organization block End of a function block End of a function
*) Overlaying of data types with the keyword AT is additionally possible with SCL (see text)
Block header and block properties
A logic block commences with the keyword for the block type and with the speci- fication of the block name. With symbolic addressing (e.g. FUNCTION_ BLOCK
“FB_name”), the first vacant number of the block type is assigned when import- ing for absolute addressing. When specifying an absolute address (e.g. FUNC- TION_BLOCK %FB102), the operand with the number is imported as the symbolic address.
With symbolic addressing, an organization block should have the standard name.
The correct block number is then assigned to it when importing from the external source. If the organization block has any name, it is assigned 0 (zero) as the num- ber. The envisaged block number must then be manually assigned to this OB 0, for example using the Properties command in the shortcut menu of the organization block. The standard names of the organization blocks are listed in Table 5.4
“Startup program” on page 171.
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 data for the block properties is optional. You simply omit the surplus data to- gether with the keywords.
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.
The keyword CODE_VERSION1 is permissible for function blocks with STL program.
Following importing into STEP 7, the block attribute Multiple instance capability can be enabled or disabled for these function blocks.
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 18.1).
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
Not all tags can have default values, e.g. default values are not possible for the tem- porary local data. Chapter 5.2.5 “Block interface” on page 162 describes the data types permissible for block parameters.
The sequence of individual declaration sections is defined as shown in the table.
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.
The programming language SCL permits the overlaying of data types with the key- word AT in the declaration section of logic blocks. You program the overlaying di- rectly 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 : DT;
Byte_array AT date : ARRAY [1..8] 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].
Overlaying a data type is described in Chapter 4.4 “Elementary data types” in sec- tion “Overlaying tags (data type views with SCL)” on page 120.
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, except for jump labels. Refer to Chapter 9.1.2 “Structure of an STL statement” on page 349 for the syntax of an STL statement and to Chapter 10.1.2 “SCL statements and operators” on page 398 for that of an SCL statement. You can enter one or more spaces or tabulators between operation and operand. To achieve a clearer layout of the source text, you can enter any spaces and/or tabulators between the words.
You must conclude every statement 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. You can also program several statements per line, each separated by a semicolon.
A line comment commences with two slashes at the start of the line. A line comment can have up to 160 characters, but no tabulators or non-printable characters.
A block comment with SCL 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.
You can also program networks to structure the block program better in STL.
Networks commence with the keyword NETWORK. You can assign a title to every network using the keyword TITLE, which is present in the next line. The line com- ments which directly follow the network title are the network comment. No net- works are possible with SCL.
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.
Fig. 18.1 shows an example of an STL source file for a function block with the asso- ciated instance data block.
Fig. 18.2 shows an example of an SCL source file for a function block with the asso- ciated instance data block.
FUNCTION_BLOCK FIFO_STL
TITLE = Intermediate memory for 4 values //Example of a function block in STL AUTHOR : Berger
FAMILY : Book_400 NAME : Memory VERSION : 01.00 VAR_INPUT
Transfer : BOOL := FALSE; //Transfer on positive edge Input_value : REAL := 0.0; //In data format REAL END_VAR
VAR_OUTPUT
Output_value : REAL := 0.0; //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_memory_bit : BOOL := FALSE; //Edge memory bit for the transfer END_VAR
BEGIN NETWORK
TITLE = Program for transfer and output
//Transfer and output take place with a positive edge at Transfer A Transfer; //If Transfer changes to "1", FP Edge_memory_bit; //the RLO = "1" following FP
JCN end; //Jump if no positive edge is present //Transfer of values starting with the last value
L Value4;
T Output_value; //Output of last value L Value3;
T Value4;
L Value2;
T Value3;
L Value1;
T Value2;
L Input_value; //Transfer of input value T Value1;
End: BE;
END_FUNCTION_BLOCK DATA_BLOCK DB_FIFO_STL
TITLE = Instance data block for "FIFO_STL"
//Example of an instance data block AUTHOR : Berger
FAMILY : Book_400 NAME : FIFO_Dat VERSION : 01.00
FIFO_STL //Instance for the FB "FIFO_STL"
BEGIN
Value1 := 1.0; //Individual default setting Value2 := 1.0; //of selected values
END_DATA_BLOCK
Fig. 18.1 Example of an STL source file
FUNCTION_BLOCK FIFO_SCL
TITLE = Intermediate memory for 4 values
//Example of a function block with static local data in SCL AUTHOR : Berger
FAMILY : Book_400 NAME : Memory VERSION : 01.00 VAR_INPUT
Transfer : BOOL := FALSE; //Transfer with positive edge Input_value : REAL := 0.0; //In data format REAL
END_VAR VAR_OUTPUT
Output_value : REAL := 0.0; //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_memory_bit : BOOL := FALSE; //Edge memory bit for the transfer END_VAR
BEGIN
//Transfer and output take place with a positive edge at Transfer IF Transfer = TRUE AND Edge_memory_bit = FALSE
THEN Output_value := Value4;
//Transfer of values starting with the last value Value4 := Value3;
Value3 := Value2;
Value2 := Value1;
Value1 := Input_value;
END_IF;
Edge_memory_bit := Transfer; //Update edge memory bit END_FUNCTION_BLOCK
DATA_BLOCK DB_FIFO_SCL
TITLE = Instance data block for "FIFO_SCL"
//Example of an instance data block AUTHOR : Berger
FAMILY : Book_400 NAME : FIFO_Dat VERSION : 01.00
FIFO_SCL //Instance for the FB "FIFO_SCL"
BEGIN
Value1 := 1.0; //Individual default setting Value2 := 1.0; //of selected values
END_DATA_BLOCK
Fig. 18.2 Example of an SCL source file