Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions and Function Blocks

Similar presentations


Presentation on theme: "Functions and Function Blocks"— Presentation transcript:

1 Functions and Function Blocks
FC FB DB Contents Page Introduction Temporary Variables Local Data Stack Size … Byte Requirement of a Block in the Local Data Stack Total Occupation in the Local Data Stack Exercise: Use of Temporary Variables ………… Example of a Message Display Indicating a Problem in the Process Parameter-assignable Blocks Declaration of the Formal Parameters Editing a Parameter-assignable Block Calling a Parameter-assignable Block Using the EN/ENO Parameters with Block Calls ……… Exercise: Creating a Parameter-assignable FC Block Exercise: Calling a Parameter-assignable FC Block Function Blocks (FBs) Function Blocks for Message Display …… Generating Instance Data Blocks … The Multiple Instance Model Inserting/Deleting Block Parameters Later On …………… Corrections when Calling Modified Blocks …… Exercise: Editing a Function Block Calling a Function Block and Testing It Converting an FC to an FB using a Source Program (1) Converting an FC to an FB using a Source Program (2) Exercise: Recognizing Types of Variables Summary: Block Calls …………………

2 Introduction Local Variables / Data (only valid in one block)
Global Variables / Data (valid in the entire program) • PII / PIQ • I/ O • M / T / C • DB areas Temporary Variables • are deleted after the associated block is executed • temporary storage in L stack • useable in OBs / FCs / FBs Static Variables • are retained even after the block is executed • permanent storage in DBs • can only be used in FBs absolute Access symbolic General Up until now, the inputs and outputs in the bottling plant were addressed with their actual parameters. You could not assign parameters to the blocks. You would choose this procedure, for example, for the creation of a program that is only used once with a special machine. For frequently recurring functions in larger systems, you create universally useable, parameter-assignable blocks (FC, FB). These have formal input and output parameters, that are assigned actual parameters when the block is called. The adjustment of block functionality to the hardware takes place with the parameter assignment when the block is called; the “inner life" of the block does not change. Local Variables Up until now you have used global variables (bit memories and data blocks) to store production data, for example. In this chapter you will find out more about data storage in local variables. Temporary Variables Temporary variables are variables that are only stored while the block is being executed. They can be used in all blocks (OB, FC, FB). Static Variables If the data are to be retained even after the block is executed, they must be stored in static variables. Static variables can only be used in function blocks.

3 Temporary Variables General Temporary variables can be used in all blocks (OB, FC, FB). They are used to temporarily store information while the block is being executed. The data are lost when the block is exited. The data are stored in the L stack (local data stack). It is a separate memory area in the CPU. Declaration You define the variables in the declaration table of the block. In the line "temp" you enter a variable name and the associated data type. You cannot predefine a start value here. After you have saved the block, the memory location in the L stack is displayed in the "Address" column. Access In Network 1, you see an example of the symbolic access to a temporary variable. The result of subtracting is stored in the temporary variable “result". You can also make an absolute access (T LW0). You should, however, try to avoid this since the program is difficult to read. Note Variable names that begin with the special character # are local variables which # are only valid within the block in which they are declared in the declaration table. The Program Editor automatically enters the special character.

4 Entire size: 1.5 Kbyte (CPU 313..316)
Local Data Stack Size Entire size: 1.5 Kbyte (CPU ) Execution For S7-300: Priority class L stack size Startup (one-time execution) 27 256 bytes Cyclic execution 1 256 bytes 12 3 2 Time-controlled execution Time-of-Day Interrupt Time-Delay Interrupt Cyclic Interrupt Local Data Stack The local data stack (L stack) is a memory area that contains the temporary variables (replacement for scratchpad memories in SIMATIC S5) of the blocks. Local Data Stack When the operating system calls an OB, an L stack area of 256 bytes is opened Size up while the OB and the blocks called in it are executed. Every priority class is assigned 256 bytes. The L stack of the CPUs has a total of 1536 bytes (1.5kByte). Priority Classes There are a total of eight priority classes with the S However, no more than 6 priority classes can be active at the same time. If, for example, OB 100 is active (with priority class 27), then OB1 (priority class 1) can never be active. Furthermore, the error OBs 80 to 87 for asynchronous errors can only then have priority class 28, if the fault occurs in the startup program. In other words, when they interrupt OB100. More information can be found in the chapter "Organization Blocks". S7-400 With the S7-400 CPUs, you can decide what the size of the local data stack is for the individual priority classes (Tool: HW Config.). You can deselect the priority classes which you do not need. That way, you can make more local data available to the other priority classes. Error handling in scan cycle 256 bytes 16 28 26 Event-driven execution Hardware Interrupt Error handling in startup

5 Byte Requirement of a Block in the Local Data Stack
rechts Displaying the You can see the exact number of bytes a block requires in the local data stack Byte Requirement by going into the block properties. Activate 1. In the SIMATIC Manager, select the block with the right mouse button and then -> Object Properties. or 2. In the SIMATIC Manager, select the block with the left mouse button and then the menu options Edit -> Object Properties. Notes The sum of local data for an execution level (OB) is a maximum of 256 bytes with the S Every OB itself always takes up 20 or 22 bytes. This means that a maximum of 234 bytes can be used in an FC or FB. If more than 256 bytes of local data are defined in a block, the block cannot be downloaded into the CPU. The transmission is interrupted with an error message “The block could not be copied". Within this error message is a "Details" button. If you click on it, a message box appears with an explanation “Incorrect local data length".

6 Total Occupation in the Local Data Stack
256 Bytes Event Occupation in the L stack 1 OB1 OB 1 Operating system FC 1 with temp. variables 2 OB1 FC1 FC 2 with temp. variables OB1 FC2 FC1 3 OB1 FC1 4 FC 3 with temp. variables OB1 FC3 FC1 5 7 OB1 OB1 FC1 6 Total Occupation in You can display the number of bytes an entire program requires in the local Local Data Stack data stack with the "Reference Data" tool. You will become familiar with this tool in the chapter “Troubleshooting". The total occupation of the local data stack and the number of bytes required per call path is displayed on the screen. Activate In the SIMATIC Manager you select the block folder and then the menu options Reference Data Options -> Reference Data -> Display. Note If the maximum number of local data is exceeded during program execution in the CPU, the CPU goes into the Stop mode. “STOP caused by error when allocating local data" is entered as the cause of error in the diagnostics buffer.

7 Exercise: Use of Temporary Variables
Replace by the temporary variable "Packages" Goal You are not to use any bit memories as temporary storage in the FC 19 block of the S7 program "Fill". These are to be replaced by a temporary variable “Packages". If you wrote a program for the block in LAD or FBD, the bit memories were necessary to connect the output of the divider with the input of the code converter. Even if you wrote a program for the block in STL (where no bit memories are necessary) insert the temporary variable “Packages" for storing. What to Do • Open the FC 19 block in the S7 program "FILL" . • Define a temporary variable with the name “Packages" and the data type "Integer“ in the declaration table. • Store the number of packages in the temporary variable. • Download the changed program and test it. Result You now know the use of temporary variables.

8 Example of a Message Display Indicating a Problem in the Process
Task Disturbance Acknowledge Report Memory LED Edge Mem. Bit Disturb. Input Acknowl. Flash Freq. Display RS & = >=1 Q S R P Report Memory Solution Suggestion Description Problems (disturbances) that occur are to be displayed by an LED on the operator console. When the problem (I1.3) occurs, the LED (Q8.3 or Q4.3) is to flash with 2Hz. The problem is detected at the acknowledge input I 1.2. If the problem is corrected, the LED stops flashing. If the problem continues, the LED switches to a steady light until the problem is corrected. Program So that even problems that only exist for a short time are not lost, a dominant set flip flop (M40.0) is used. An RLO edge detection of the message signal is also carried out, since the memory is otherwise immediately reset when an existing problem is acknowledged. If the report memory is set (message has not yet been acknowledged), the upper AND logic operation causes the LED to flash. With this, the bit memory M10.3, that was defined as a clock memory when parameter assignment was made in the CPU, is gated. The lower AND logic operation is used to cause a steady light for a problem that is acknowledged but still exists.

9 Parameter-Assignable Blocks
A #Acknowledge R #Report Memory U #Disturb. input FP #Edge mem. bit S #Report memory A #Report memory A #Flashing freq. O AN #Reportmemory A #Disturb. input = #Display Parameter-assignable block Program Non-parameter- assignable block Disturb. input Report memory Acknowledge Edge mem.bit Display Flash freq. FC 20 Call A I 1.2 R M 40.0 A I 1.3 FP M 40.1 S M 40.0 A M 40.0 A M 10.3 O ANM 40.0 = Q 8.3 M 40.0 M 40.1 I 1.3 I 1.2 A 8.3 M 10.3 Actual parameters Formal parameters Introduction You can use parameter-assignable blocks for frequently recurring program sections. This has the following advantages: • the program only has to be created once • the block is only stored in the user memory once and you can call it as often as you like • the block can be programmed with formal parameters (input, output or in/out parameters) and only when the block is called is it given the “real“ addresses (actual parameters). Example When the block is executed, the statement “A Disturbance input" is checked to see which actual parameters are assigned to the formal parameter “Disturbance input". If, when the block is called, I 1.4 is lined up as the actual parameter, then the statement “A I 1.4" is carried out. FC / FB Parameter-assignable blocks can either be FCs or FBs. Parameter- In the example, the message display is required ten times in the system. assignable FC20 It is created as a parameter-assignable FC 20 block and is then called ten times with different actual parameters.

10 Declaration of the Formal Parameters
Type of parameter Declaration Use Graphic Display Input parameter in Read only To the left of the block Output parameter out Write only To the right of the block In/out parameter In_out Read / write To the left of the block Declaration table of the FC 20 block Formal Addresses Before you can create the program for the parameter-assignable block, you have to define the formal parameters in the declaration table. Type of Parameter In the table in the slide, you can see three possible types of parameters and their uses. Please make sure that when you have a reading and writing access to a formal address that you use an in/out parameter. Example of the FC20 In the lower section of the slide, you can see the declaration table for the message display (see previous page). Since the report memory is to be accessed reading (set/reset) as well as writing (query), it has to be defined as an in/out parameter. Notes There is only one row for every type of parameter in the declaration table. Should you need several input parameters, you have to use the "Return" key when you end your inputs in the first row. An additional row for this type of parameter is then opened up. After you have selected a declaration row, you can also use the menu options Insert -> Declaration Row -> Before Selection / After Selection to insert an additional row. Attention! If you want to insert or delete declaration rows later - after the block has already been called - , you have to update the block calls !

11 Editing a Parameter-assignable Block
Here was, e.g., with a non-parameter-assignable FC: • with absolute addressing: I1.3 • with symbolic addressing: “End_left" Notes It doesn‘t matter whether the names of the formal parameters are written with capital or small letters. The "#" character in front of the name is automatically inserted by the PG. This is to indicate to you that it is a local variable that was defined in the variable declaration table of this block. It is possible, that when you write the program in LAD / FBD, that the name is not completely displayed in one row. This depends on how you customized the settings in the Program Editor (Options -> Customize -> "LAD/FBD" tab -> Width of address field). Symbols 1. If you use a symbolic name when you edit a block, the Editor searches the variable declaration table. If it is there, the symbol with the # in front of it is accepted in the program as a local variable. 2. If it cannot be found as a local variable, the Editor searches the symbol table for global symbols. If it is found there, the symbol is placed in quotation marks and is accepted in the program. 3. If you specified the same symbolic name as global (in the symbol table) as well as local (in the variable declaration table), the Editor will always insert the local variable. If, however, you want to work with the global symbol, you must place the symbol name in quotation marks when you make the entry.

12 Calling a Parameter-assignable Block
Network 3: First Call FC20 .... EN absolute Addressing symbolic “End left" Disturb.input Edge mem. bit M40.1 “Acknow.button" Acknowledge “Position error" Display M10.3 Flash freq. M40.0 Report memory ENO Network 4: Second Call FC20 A8.1 EN Symbols Local Symbols --> Formal parameters “End right" Disturb.input Edge mem. bit M40.3 Symbol- table Global Symbols “Acknow.button" Acknowledge Display Q9.4 M10.3 Flash freq. Call In LAD/FBD, you can select the call from the "Program Elements" browser. Question marks (??.?) are displayed at the input, output and in/out parameters of the block. Here you insert the actual parameters you want. Note When you call a parameter-assignable FC block, you must assign all block parameters (with the exception of EN and ENO). ENO M40.2 Report memory

13 Using the EN/ENO Parameters with Block Calls
LAD/FBD STL FC 1 Unconditional call ?? . ? EN ENO CALL FC NOP 0 A I JNB _ CALL FC _001: A BR = Q 9.0 FC 1 EN ENO I 0.1 = Q 9.0 Conditional call Standard FCs The following rules exist for the execution of standard FCs: • If EN=0, the block is not executed and ENO is also =0. • If EN=1, the block is exeucted and if it is executed without errors ENO is also =1. If an error occurs while the block is being executed, ENO becomes =0. User FCs It doesn‘t matter whether a user block was written in LAD, FBD or STL, when it is called in LAD/FBD, the parameters EN and ENO are added as well. Thus it is possible to pass on the RLO. EN/ENO do not exist in STL. You can, however, emulate them. You must program -irregardless of the programming language- an error evaluation. Interconnection In LAD/FBD, several boxes can be grouped together one after the other and linked logically with EN / ENO. Example ?? . ? FC 1 EN ENO FC 2 FC 3 =

14 Exercise: Creating a Parameter-assignable FC Block
1. Declaration table of the FC 20 block Non-parameter- assignable block Parameter-assign- able FC 20 block A I 1.2 R M 40.0 A I 1.3 FP M 40.1 S M 40.0 A M 40.0 A M 10.3 O AN M 40.0 = Q 9.3(Q5.3) A #Acknowledge R #Report memory A #Disturb. ... : 2. Task Write the program for a message display as a parameter-assignable block. In the slide you can see the declaration table with the input and output parameters and the beginning of the program. What to Do • Insert an FC 20 block in the S7 program "My Program" . • Write the program in FC 20. • Save FC 20.

15 Exercise: Calling a Parameter-assignable FC Block
Disturb. input M 40.1 Edge mem. bit I 1.2 Acknowledge Display Q 9.3 (Q 5.3) Parameter-assignment of FC20 in the 1st. call M 10.3 Flash freq. M 40.0 Report memory Disturb. input Report memory Acknowledge Edge mem. bit Display Flash freq. M 40.2 M 40.3 I 1.4 I 1.2 Q 9.4 (Q 5.4) M 10.3 FC 20 Parameter-assignment of FC20 in the 2nd. call Task Call FC 20 in OB 1 twice (with different absolute addresses). Test the functionality of the program. What to Do • Insert two networks into the OB 1 from the S7 program "My Program". • Create the two calls to FC 20, as given in the slide, • Download the FC20 and OB 1 blocks, • Test the function. Note: During HW Config, you used MB10 for parameter-assignment of the clock memory byte. If you have performed a memory reset in the meantime, you must download the HW configuration once again to cause the M10.3 to flash.

16 Function Blocks (FBs) OB 1 DB 2 FB 2 EN Disturb. input
Acknowledge Display Flash freq. ENO Declaration table of the function block Special Features Unlike functions (FCs), function blocks (FBs) have a (recall) memory. That of FBs means that a local data block is assigned to the function block, the so-called instance data block. When you call an FB, you also have to specify the number of the instance DB, which is automatically opened. An instance DB is used to save static variables. These local variables can only be used in the FB, in whose declaration table they are declared. When the block is exited, they are retained. Parameters When the function block is called, the values of the actual parameter are stored in the instance data block. If no actual parameters are assigned to a formal parameter in a block call, then the last value stored in the instance DB for this parameter is used in the program execution. You can specify different actual parameters with every FB call. When the function block is exited, the data in the data block is retained. FB Advantages • When you write a program for an FC, you must search for empty bit memory address areas or data areas and you must maintain them yourself. The static variables of an FB, on the other hand, are maintained by the STEP 7 software. • When you use static variables you avoid the risk of assigning bit memory address areas or data areas twice. • Instead of the formal parameters “Report memory" and “Edge memory marker" of the FC20, you use the static variables “Report memory" and “Edge memory marker" in the FB. This makes the block call simpler since the two formal parameters are dropped.

17 Function Block for Message Display
Declaration table of the function block Instance data block Message Display In an earlier exercise you created a parameter-assignable FC 20 block for displaying a message (indicating a problem). Instead of bit memories, that were used in the FC20 to save the message signal and its RLO edge detection, you can use so-called static variables in an FB. They are stored in the instance DB referencing the FB. Instance DB When a DB is generated and references an FB, STEP7 creates the data Structure structure of the data block using the structure specified in the local declaration table for the function block. After you save the DB, the data block is created and can then be used as an instance DB.

18 Generating Instance Data Blocks
1. Generate instance DB with FB call 2. Create new instance DB Generating an There are two ways of generating a new instance DB: Instance DB • When you call an FB, you specify with which instance DB the FB is to work. The following message then pops up: "Instance data block DB x does not exist. Do you want to generate it?". • When you create a new DB, you select the option "Data block referencing a function block". Notes One instance DB can only reference one FB. However, one FB can be referenced by a different instance DB every time it is called. If you modify the FB (by adding parameters or static variables), you must then also generate the instance DB again.

19 The Multiple Instance Model
The Instance Model OB 1 DB10 FB 100 DB100 Call FB100, DB100 OB 1 stat Dist_1 FB20 Dist_2 Call FB20, DB10 Disturb._Input:= Acknowledge:= Flash_Freq:= Display:= FB20 DB11 FB20 Call FB20, DB11 Disturb._Input:= Acknowledge:= Flash_Freq:= Display:= Call Dist_1 Disturb._Input:= Acknowledge:= Flash_Freq:= Display:= Parameters and static variables of the 1st. call of FB20 Multiple Instance Up until now, you had to use a different instance data block for every call of a Model function block. The number of data blocks is limited however, and for that reason there is a method that allows you to use a common instance DB for several FB calls. The multiple instance model now enables you to use a single DB for several calls. To do this, you need an additional FB to manage these instances. For every FB call (FB 20), you define a static variable in the higher-level FB (FB 100). With the block call Call Dist_1, you do not then have to specify an instance DB. The higher-level FB (FB 100) is called, for example, in OB1, the common instance DB (DB 100) is only generated once. Note Multiple instances are discussed in an advanced programming course. DB12 FB20 Call FB20, DB12 Disturb._Input:= Acknowledge:= Flash_Freq:= Display:= Call Dist_2 Disturb._Input:= Acknowledge:= Flash_Freq:= Display:= Parameters and static variables of the 2nd. call of FB20

20 Inserting/Deleting Block Parameters Later On
Save The Problem When you add additional block parameters later on to a block already called in the program, you must also update the block call. Otherwise, the CPU would either go into Stop or the block function could not be guaranteed since the additional parameters still have to be supplied with actual parameters in the call. In the example, an additional input parameter “Check_lights" was inserted. Updating the Call When you save the block in which the declaration table was modified, a message pops up warning you of possible problems.

21 Corrections when Calling Modified Blocks
When the calling block is opened: Once with the right Updating the Call When the calling block is opened, the following message pops up “Time stamp conflict with at least one block call" and the call is displayed in red. With the right mouse button, click on the block call and select the menu option “Update Call". The block call is then redisplayed and contains, in our example, the additional input parameter “Check_light“. This parameter can then be assigned. In the case of function blocks, the instance DB is then regenerated.

22 Exercise: Editing a Function Block
1. Declaration table of the FB 20 block Program section of FB 20 A #Acknowledge R #Report memory A #Disturb. ... : 2. Task The program for displaying a message (indicating a problem) is now to be implemented in a function block. For saving the edge memory bit and the report memory you are to use static variables that are stored in the instance DB of the FB. That way, you do not require any bit memories for saving. In the slide you can see the declaration table with the input and output parameters and the beginning of the program. What to Do • Insert an FB 20 block into the S7 program "My Program". • Write the program in the FB 20. • Save the FB 20.

23 Exercise: Calling a Function Block and Testing It
(Q 5.3) What to Do In the S7 program "My Program": • delete both networks by calling the FC20. • write a program for the two calls to the FB 20, as given in the slide. • download the FB20, DB20, DB21 and OB 1 blocks. • test the functionality of the program. FC -> FB In our example, FB20 was rewritten, even though an FC20 with the same contents already existed. It was not hard to do. If an FC with an extensive program is to be converted into an FB, you go about it differently: 1st. possible solution: • insert a new FB • copy the declaration table of the FC block into the FB and adjust it • copy the networks from the FC into the FB • save the FB. 2nd. possible solution: • generate a source file from the FC block • make the adjustments in the source file • generate a new FB from the source file (see next page). (Q 5.3)

24 Converting an FC to an FB using a Source Program (1)
Introduction Just as with higher-level programming languages, you can also create blocks using a source program (text file). When you compile this source program, executable blocks are generated. By the same token, a source file can be generated from existing blocks. You are to make use of this to create, with the least amount of work (typing), an FB 20 from an FC 20 block. What to Do 1. Open a block. 2. In the LAD/STL/FBD Editor, choose the menu options Options -> Customize, then the "Editor“ tab. In the “View" box select the option "Symbolic representation". 3. In the LAD/STL/FBD Editor select the menu options File -> Generate Source File... 4. In the "New" window, you can enter a name of your choice for the source file to be generated in the "Object name" box. 5. In the “Generate Source File" window that then appears, select the program blocks that are to be compiled one after the other in the “Unselected Blocks“ window. 6. Click "OK" to start the compilation of the blocks into source code. The compiled blocks are then found in the source file which you named previously, in the “Source Files" folder of the S7 program.

25 Converting an FC Block to an FB using a Source Program (2)
Sources, What For? • for automatic rewiring using symbols • for assigning block attributes, for example, block protection • as data protection of the entire program • more freedom in editing and processing blocks: - modifying block type - insert / delete network separations - merging program elements and comments into new blocks - creating program sources in other text editors (WORD, WordPad), without having to install STEP7 in the PC - complete symbolic programming without syntax check, etc. Source Program In the left-hand section of the slide you can see the source program for the FC 20 block. On the right-hand side, the source file is displayed for the program as FB 20. Here, the associated keywords for a function block were entered. In addition to that, static variables for the report memory and the edge memory marker are defined. After a compilation run, an executable FB 20 exists once more. Note If you do not know the key words, you can insert a block template using the menu options Insert -> Block Template -> FB. The subject of “Source Files" is discussed in greater detail in an advanced programming course. : : : :

26 Exercise: Recognizing Types of Variables
L #Number_1 T #Max_value T MW 40 Statement L #Number_2 L #Intermediate_result L “Number_1" T #Number_2 TYPE OF VARIABLE Global Local Absolute Symbolic Temporary Static Parameter Goal You are to recognize the differences between the various types of variables. What to Do In the table, mark the associated data type with an X. Answer the following question: What is not correct in the statement T#Number_2 ? Result You can recognize and use the types of variables.

27 Summary: Block Calls ( CALL ) FC FB • UC FB1 • CC FB1 • CALL FC2
W/o param., w/o inst. DB • UC FB1 • CC FB1 FB1 EN ENO Lan- guage With parameters • CALL FC2 Par1: Par2: ... Par3: ... FC2 EN ENO Par3 Par1 Par2 With param., with inst.DB • CALL FB2, DB3 Par1: Par2: ... Par3: ... FB2 EN ENO Par3 Par1 Par2 DB3 Without parameters • CALL FC1 • UC FC1 • CC FC1 STL FC1 ( CALL ) FC1 EN ENO LAD FC1 CALL The "CALL" instruction is used for calling program blocks (FC, FB, SFC, SFB), regardless of the RLO or any other conditions. If you call an FB or SFB with "CALL“, you must also specify the relevant instance DB. You can use either the absolute or the symbolic name for the program block. For example: "CALL FB2, DB2" or "CALL valve, level". The "CALL" operation saves the return address, deactivates dependence on the MCR and creates the local data area for the block to be called. UC The "UC" instruction is an unconditional call of a block of the type FC or FB without parameter assignment. "UC" is otherwise identical to "CALL". CC The "CC" instruction calls a block of the type FC or FB without parameters if RLO=1. "CC" is otherwise identical to "CALL". Parameters Parameters declared in the declaration table are known as “formal parameters". The addresses or values specified in the call are referred to as “actual parameters". Static and temporary variables are not specified in the call. CALL FC1 EN ENO FBD


Download ppt "Functions and Function Blocks"

Similar presentations


Ads by Google