《老外写的ST语言教程-很详细.ppt》由会员分享,可在线阅读,更多相关《老外写的ST语言教程-很详细.ppt(53页珍藏版)》请在三一办公上搜索。
1、1,ST Language,Basic Course,2,ST Language,Key wordIf there is any unclear or uncorrected,please feel free to speak out,Basic Course,3,Requirements,HardwareThe programming language ST is only available for following programmable controllers:,pCO3 Supernode pCO5 pCO compact,BIOSTo support ST language,b
2、ios version must greater than 5.0,Software,4,Creation of a solution ST Language,Choosing:-FBD,the application will be mixed,it will consist of a main with Atoms,Macroblocks.but you can use routines(functions and function blocks)written in the ST-ST,the application will be developed entirely using ST
3、 language,5,Basic concepts,When programming in ST,the application can be organised using three fundamental programming elements generically called Program Organization Units(POU):PROGRAM FUNCTION FUNCTION BLOCK,POU,Note:we can define more functions and function blocks,but only one program main,6,The
4、 basic types,The following table lists all the properties of basic types(IECType):,7,Declaration of Global variables,There are two ways to declare Global variables:Variable ListTextual mode of ST language,Note:if you want to see the variable in the pGD,please declare the variable in the Variable Lis
5、t,8,Assignment operator:=,Syntax,Assign to a variable:,a constant value the value of another variable the result of an expression.,Varaible_name:=expression;,Note:there is a;in the end of statement,9,Textual declaration,To declare variables in ST you have to use the following declaration block:,Note
6、:1.there is no;in the end of END_VAR;2.we can give the default value in the declaration.,10,Rules of the identifiers(variables name),a.z,A.Z 0.9 _,The name of an identifier(variable or constant)can be any combination(see Example)of the characters listed below:,ExampleRules to create identifiers.,11,
7、The qualifier CONSTANT,Textual declaration of Constants,The keyword CONSTANT can declare a constant.The constants do not occupy space in memory as the variables During compilation instead of the constant is replaced a numeric value.,12,Comments,Comment using keyword REGION ENDREGIONYou can hide seve
8、ral lines of code using the keyword REGION ENDREGION.So increase the readability of the code on the screen.The code is replaced with a comment.Clicking on comment it will appear again the code.,The code commentsYou can use two ways to make a commentTo comment a single code line use“”To comment more
9、code lines use“(*”and“*)”,There is no _ in between END and REGION,13,The qualifier RETAIN,Textual declaration of global variables REATAIN(retentive),Example,Declaration of retentive Global variables.,RETAIN keyword declares a variable in T-permanent memory.,Organization of the variables.,Note:pay at
10、tention of the default value,14,Cast Functions,Syntax,Variable_name:=*_TO_*(value);,The conversion functions are used to convert any of the elementary data types to any other.They must be used to make the data type uniform before a function,where this is required.,15,ST operators,For the evaluation
11、of expressions,always use brackets,this avoids errors in the evaluation of the expressions.,Example,Table of the operators priority,An expression is executed only after the evaluation of the operators priority.The execution order of an expression and its result are influenced by the operators priori
12、ty.,Suggestion,A:=NOT X AND Y OR NOT J;Increases readability:A:=NOT(X)AND(Y OR(NOT J);,If Z=0 the other variables have not been evaluated.A:=Z AND Y AND(NOT J);,16,Conditional statements:IF,IF.THEN.ELSE statement Conditional construct:executes a statement block depending on whether or not certain co
13、nditions are satisfied.,Syntax,Exercize,If Expression=TRUE,statement_block is executed Otherwise the conditions in the ELSIF blocks are evaluated Finally,if these are all false,the statements in the ELSE block are executed.,Boolean(AND,OR,NOT,XOR)Comparison(=,=,=).,Features,IF expression THEN statem
14、ent_block;ELSIF expression THEN statement_block;ELSE statement_block;END_IF;,Note expression contains the operators:,Management of a range-550,950 C with NTC probe.,17,Conditional statements:CASE,CASE.OF.statement,Syntax,Conditional construct:selects the statements to be executed depending on the va
15、lue of an integer expression.,CASE expression OF selector_1:statement_block;selector_2,selector_3:statemnt_block;selector_4.selector_5:statemnt_block;selector_6,selector_7.selector_8:statemnt_block;ELSE statemnt_block;END_CASE;,Example,Speed management of a fan.,18,The Function,It can be comparable
16、to a Black box:it works on a Input parameters configuration,and it always produces in Output the same result for the same Inputs.It lets not repeat many times the same code within the project,as instead it is for macroblocks in FBD environment.At each invocation of the function,the same code will be
17、 always executed.,What does a Function do?,It read Inputs,no outputs,but can have a return valueIt executes the code of the functionIt writes the result in a Return ValueA function doesnt have a memory stateIt always produces the same result for the same inputs.,The function Add_3,What does a Functi
18、on contain?,The list of parameters that are passed as inputsProbable Local variables,only for its internal useThe code of the function,What is a Function?,19,FUNCTION statement,The table below lists all properties of a function.,Summary,20,Pass by value(VAR_INPUT),In the pass by value the Actual Par
19、ameters are copied in Formal Parameters.,Actual Parameters,Formal Parameters,Modifications dont have effect on Actual Parameters.,21,Pass by Reference(VAR_IN_OUT),Pass by Reference,Actual Parameters,Formal Parameters,Modification,Sometimes it is necessary to modify the variable value passed to the f
20、unction,and render such modification outside immediately visible.This modality is called Pass by Reference and it is realized using keyword VAR_IN_OUT.In Pass by Reference the Formal Parameter point to the same cell of memory that contains the value of Actual Parameter.,22,Variables scope,23,RETURN
21、statement,Syntax,The RETURN statement is used to exit from a Function,Function Block or the Program before the end.,FUNCTION My_functionlist_statements_1;RETURN;list_statements_2;END_FUNCTION,1st Example,2sd Example,In the case of a Function or Function Block,the statement interrupts execution after
22、 list_statements_1,and passes control to the calling POU without executing list_statements_2.In the case of the Program,the statement closes the Program.,24,ARRAY statement,Syntax,Exercize,Array_name:ARRAYelem_1.elem_n OF Type;where:,Simulation,elem_1 and elem_n are INT numbers or constants(they can
23、not be variables)and represent the limits of the arrayelem_n=elem_1,the difference+1 indicates the number of elements in the arrayType identifies the elementary data type elements inside the array or Structure type,25,Array Breakthrough(exceeded limits),Example,Run-Time error on Simulator,Run-Time e
24、rror on pCO Place 2 integer variables on a Mask related to library functions:The position of error in the Strategy Editor ST:go in simulation and then press insert High part and Low part,When a variable is used to access the elements of an array,care must be paid to ensure the size of the array is n
25、ot exceeded.If run-time the index value exceeds the size of the array,this will entail the re-boot of the pCO.,NoteTo reset error variables use this library functions:,26,Iteration statements:FOR,FOR.DO.Statement,FOR variable:=initial_expression TO final_expression BY increase_expression DO statemen
26、t_block;END_FOR;,FOR loop executes a statement block from an initial value to a final value of the control variable.,Syntax,Example,Counter=10,27,Iteration statements:WHILE,WHILE.DO.Statement,WHILE statement executes a statement block while the expression is true.,Syntax,WHILE expression DO statemen
27、t_block;END_WHILE;,Example,To search for an element within an array.,28,Iteration statements:REPEAT,REPEAT.UNTIL.Statement,REPEAT statement_block;UNTIL expressionEND_REPEAT;,The statements contained in the loop are executed at least once.,REPEAT statement executes a statement block while the express
28、ion is true.The loop is executed at least once.,Syntax,29,Iteration statements:EXIT,Syntax,1sh Example,EXIT statement,To exit by for loop.,EXIT;,The EXIT statement is used exclusively inside WHILE,REPEAT,and FOR statements to interrupt execution of the loop and go to the statement after the end of t
29、he loop(END_FOR,END_WHILE,END_REPEAT).,2sd Example,To exit by nested for loop.,30,Iteration statements,Note:Compare statement While Do and Repeat Until;Compare statement Return and Exit.,31,n-dimensional Array,Syntax,Exercize,Simulation,Note:,Array_name:ARRAY el_1.el_n,el_1.el_m OF Type;,The rules s
30、een for one-dimensional arrays are valid The syntax is used to declare a matrix To access array elements,nested FOR loops can be used(see exercize),32,The fields of a structure can be of elementary type,of type structure,Array,n-dim Array,not Array of structuresA structure must be instanced,before t
31、o be used,in such way comes reserved in memory the necessary space to its data.The space occupied by a structure is given by the sum of the space occupied by each single fields.In order to access/to modify single fields of a structure the.is used(e.g.:Room_Temp.Temperature).It is possible to assign
32、a structure to another,thus realizing the copy of the data.Remember the two instances must be of the same type(such as the Arrays).,The data type Structure,The structures are used in order to create more complex user types,of the normal elementary types.A structure is a data type that contains a pre
33、defined number of fields,usually of different types.,33,TYPE STRUCT statement,Allows to define the data type structure.,Definition syntaxTYPEstructure_type:STRUCTfield_1_name:data_type;field_n_name:data_type:=value;END_STRUCT;END_TYPE,Instance delaration sysntaxVAR_GLOBALmy_struct1:structure_type;my
34、_struct2:structure_type:=(field_1_name:=value,fiel_2_name:=value);END_VAR,Example,In simulation it is possible to visualize the instances,Build two structure Cmpr_Info.,34,FUNCTION BLOCK,It uses the potentialities of a function and those of a structure.It can be thought as a Black Box endowed of:Inp
35、ut Parameters(pass by Value)or Input_Output Parameters(pass by Reference)Output ParametersLocal Variables(accessible only within the FB)It is a object and has a internal memory stateNote:Functions dont have memoryA FB must be instanced before use,in such way the FB comes statically allocated by the
36、controller in Ram Memory,in order to reserve the space for its dataIt is possible to define more instances of the same FB,but the code contained in the application is unique.,What is a Function Block?,Example,Use TON timer in order to activate relay n1 after 2 second.,Use.to access to FB elements:,U
37、se=to write on a variable the FB outputs.,Use:=to assign value to the input.,35,FUNCTION BLOCK,Function Block instanceLocal variables(VAR),are allocate/deallocate inside Stack at every calling of:Program Function A Function Block instance should be declared Global(VAR_GLOBAL)and not Local(VAR).A ins
38、tance of FB that is declared Local(VAR)could have a different behaviour from that expected.,ExampleIn this example a Globale and Local instance of TON are declared.The Local instance will never succeed to finish the count,because of the internal management of the Local variables.,36,FUNCTION_BLOCK i
39、nstance,FUNCTION_BLOCK functionblock_name(*Input,In_Out,and Output variables declaration*)(*Local variable declaration*)(*Implementation code*)END_FUNCTION_BLOCK,Definition synstax,A Function Block:,can be called more than once inside a Program,a Function or a Function Blockreceives one or more inpu
40、t values(VAR_INPUT or VAR_IN_OUT)and unlike Functions,gives one or more output values(VAR_OUTPUT).the values of the Local variables(VAR)and the input and output parameters in Function Blocks are saved in the memory between calls of the function block,and from one program cycle to the next;in the lat
41、ter case as long as the function block instance has been declared as Global(VAR_GLOBAL).,In the calling POU the instance of the function block must be declared FIRST,so as to allocate the memory required to store the values of the internal variables(e.g.:use VAR_GLOBAL;take care if using VAR).If one
42、 of the input parameters is omitted(VAR_INPUT),the function block uses the default value(0 or FALSE in the first program cycle)or the last value used inside the function block.Parameters declared as VAR_IN_OUT cannot be omitted.Recursion is not allowed.,37,FUNCTION_BLOCK instance,Recursion,Recursion
43、 is not allowed,that is a Function Block cannot:,refer directly refer indirectly call a Function or Function Block instance that refer it.,Direct recursion,Indirect recursion,38,FUNCTION_BLOCK instance,Variables scope:,The qualifier RETAIN,If a Global instance has the qualifier RETAIN,data are saved
44、 in T-permanent memory.,39,The properties of a block are listed below.,FUNCTION_BLOCK:Summary,40,ST event is a function with the following restrictions:Has a unique nameHas no Return valueHas no Input parametersHas no Local variablesCan only modify Global variables,you can not call Functions or Func
45、tion BlocksEvent differs from other functions,because the name is preceded by the reserved word Event_(e.g.:Event_incA)There is no limit to the number of events that you can define.Syntax FUNCTION Event_NameEvent/CODE END_FUNCTIONFrom Mask Editor is possible to execute a ST event,specifing in the Ke
46、y Function Editor Do_ST_Event statement.ExampleWhen the PRG button is pressed,increase a variable.3How?In the Startegy Editor ST define the eventIn the Key Function Editor with DO_ST_Event statement call the event,ST Event,41,How to use the Add_3 in FBD?In the Solution Explorer add a page to Subrout
47、ine ST nodeEdit Add_3 function code,paying attention to types of Input parameters and Return value.In the Dependencies node of Solution Explorer add Core_ST library.Inside FBD environmentFrom Core_ST library select ST_Call atom to call Add_3 function inside FDB environment.Finally connect variables
48、to pins.,FBD and ST environment,Reminders on FBD environmentFBD Environment uses only the basic types(Boolean,Integer/Analog)The ST language is supported by some controllers(pCO3,Suprnodo,),42,FBD and ST environment,How do you change firm of your Function or Function Block in FBD Strategy Editor?In
49、FBD Startegy Editor I have placed My_Add function,but i have to add an other parameterYou have to add the new parameter to My_Add function,and then,you have to select“Update from firm”in FBD Strategy Editor,1st stepAdd In3 to My_Add function,2sd stepIn FBD Strategy Editor“Update from firm”,3rd stepI
50、n FBD Strategy Editor connect _In3 variable,43,System variable,ST Language uses specific library functions,in order to access to system variables of type:BooleanIntegerSyntaxSet_SystemVariableName(Vaule);Variable:=Get_SystemVariableName();1st Example(Boolean)Set_KEY_PRESSED(1);Alarms:=Get_GLOBAL_ALA