资源描述
,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,1,ST Language,Basic Course,2,ST Language,Key word,If there is any unclear or uncorrected,please feel free to speak out,Basic Course,3,Requirements,Hardware,The programming language ST is only available for following programmable controllers:,pCO,3,Supernode,pCO5,pCO compact,BIOS,To support ST language,bios version must greater than 5.0,Software,Use a version of 1tool =2.1.x,pCO,3,pCO,5,Supernode,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 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 type,Keyword,Description,Program,PROGRAM,main,END_PROGRAM,Main program that includes:,read/write I/O,definition of local variables,calls to Functions or Function Blocks,Function,FUNCTION,END_FUNCTION,ST statements block with input variables,local variables,and if specified,a,return value.This can be called at different points of the program.Recursion is not allowed.,Function block,FUNCTION_BLOCK,END_FUNCTION_BLOCK,ST statements block with input variables,local variables,and,output variables,.This can be called at different points of the program.Recursion is not allowed.,POU,Note:we can define more functions and function blocks,but only one program main,6,The basic types,Keyword,Data type,Lower limit,Upper limit,Memory Space,BOOL,Boolean,FALSE,(0),TRUE,(1),8 Bit,BYTE,Bit string,0,255,8 Bit,WORD,Bit string,0,65535,16 Bit,DWORD,Bit string,0,4.294.967.295,32 Bit,SINT,Short Integer,-128,127,8 Bit,USINT,Unsigned Short Integer,0,255,8 Bit,INT,Integer,-32768,32767,16 Bit,UINT,Unsigned Integer,0,65535,16 Bit,DINT,Double Integer,-231,(231)-1,32 Bit,UDINT,Unsigned Double Integer,0,232-1,32 Bit,REAL,Real Number,-1038,1038,32 Bit,Bitstring,Integers,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 List,Textual mode,of ST language,Button,User,Button,Extend,IECType,BOOL,INT,REAL,IECType,BOOL,INT,REAL,Variable List,Textual mode,CarelType,Boolean,Integer/Analog,Global variables declaration,Usable in any environment,Usable only in the Strategy Editor ST,(no:,MASK,FBD Strategy Editor,),You dont see,these variables in,Variable List,Note:if you want to see the variable in the pGD,please declare the variable in the Variable List,8,Assignment operator:=,Syntax,Assign to a variable,:,In assignment operations“,:=,”,the variables involved must all be the,same type,each statement must use“;”as the statement terminator.,In assignment operations with,BOOL,values,1,or,TRUE,or,0,or,FALSE,can be used indifferently.,a constant value,the value of another variable,the result of an expression.,Varaible_name,:=,expression,;,Assignment operator,Statement terminator,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:,Global variables,declared in text mode will,not,be visible in the,Variable List,.,Type,Syntax,Descrition,Gloabal,VAR_GLOBAL,END_VAR,Declares a group of global variables,shared by program,functions or function blocks.,Local,VAR,END_VAR,Declares a group of local variables to a program,function or function block.,Note: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,_,Valid identifiers,Inv,alid identifiers,Note,Mn2,2Mn,The 1,st,character must not be numeric.,_3exit,_3exit.1,“.”is not included in the set of characters.,Enrg_off,Enrg off,Spaces are not allowed.,_Drill,_Drill,Only single underscores“_”are allowed.,Valve,VALVE,Not case-sensitive.,The variable names are not case-sensitive,Valve,VaLVe,e,VALVE,are the same variable.,The name of an identifier(variable or constant)can be any combination(see Example)of the characters listed below:,Example,Rules to create identifiers.,11,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,.,Type,Syntax,Description,Costanti,Locali,VAR CONSTANT,END_VAR,Declares a group of local constants to a program,function or function block.,Costanti,Globali,VAR_GLOBAL CONSTANT,END_VAR,Declares a group of global constants,shared by program,functions or function blocks.,12,Comments,Comment using keyword REGION ENDREGION,You can hide several 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 comments,You can use two ways to make a comment,To comment a single code line use“,”,To comment more 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.,Type,Syntax,Description,Global Retentive,VAR_GLOBAL RETAIN,END_VAR,Declares a group of retain global variables,shared by program,functions or function blocks.,It is not possible to initialize a retain variable to a default value at the time of declaration.,The initialisation of a variable in T-memory must be performed using the,Variable Wizard,which implies creation of the,.DEV,file.,Organization of the variables.,Note:pay attention of the default value,14,Cast Functions,Syntax,Parameter,Description,Variable_name,Variable that holds the result of the conversion.,*,_TO_*,Conversion function.,*,indicates the data type of the variable being to convert,.,*,indicates the converted data type(e.g.:,INT_TO_REAL,),value,Value or variable to be converted,.,Symbolo,Description,*,_TO_BOOL,conversion from type*to type BOOL,*,_TO_BYTE,conversion from type*to type BYTE,*,_TO_WORD,conversion from type*to type WORD,*,_TO_DWORD,conversion from type*to type DWORD,*,_TO_SINT,conversion from type*to type SINT,*,_TO_USINT,conversion from type*to type USINT,*,_TO_INT,conversion from type*to type INT,*,_TO_UINT,conversion from type*to type UINT,*,_TO_DINT,conversion from type*to type DINT,*,_TO_UDINT,conversion from type*to type UDINT,*,_TO_REAL,conversion from type*to type REAL,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 of expressions,always use brackets,this avoids errors in the evaluation of the expressions.,Example,Table of the operators priority,Priorit,Operatore,Descrizione,1,(.),Expression in brackets.,2,nome_POU(.);,Call a function or function block.,3,*,Raise to the power.,4,-,NOT,Negation,Boolean complement,5,*,/,MOD,Multiplication,Division,Module,6,+,-,Addition,Subtraction,7,=,Comparison operators,8,=,Equality,Inequality,9,AND,Boolean,AND,10,XOR,Boolean,XOR,11,OR,Boolean,OR,In the case of Boolean,AND,OR,boolean(not bitwise),if the result of the operation can already be determined after having evaluated the first operand,the second is not evaluated(Short-Circuit Boolean Expressions).,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 priority.,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 conditions are satisfied.,Syntax,Exercize,Short-Circuit Boolean Expressions,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,(=,=,=,elem_1,the,difference+1,indicates the number of elements in the array,Type,identifies the,elementary data type,elements,inside the array or,Structure type,25,Array Breakthrough(exceeded limits),This type of error is not detectable at compile time by the compiler.The error can be detected only at run-time.,Example,Run-Time error on Simulator,Run-Time error 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 not exceeded.,If run-time the index value exceeds the size of the array,this will entail the re-boot of the pCO.,Exceeds the size of the array.,Note,To 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,statement_block,;,END_FOR,;,FOR loop executes a statement block from an initial value to a final value of the control variable.,Syntax,Parameter,Type,Description,Variable,SINT,INT,DINT,Control variable that is increased/decreased by,increase_expression.,initial_expression,SINT,INT,DINT,Expression that initialises the control variable.,final_expression,SINT,INT,DINT,Final value of the control variable.,increase_expression,SINT,INT,DINT,Expression processed each cycle that increases(if result 0)or decreases(if result to write on a variable the FB outputs.,IN,PT,Q,ET,Local Var,Memory,My_Timer_1,Use,:=,to assign value to the input.,35,FUNCTION BLOCK,Function Block instance,Local 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 instance of FB that is declared Local(,VAR,)could have a different behaviour from that expected.,It doesnt count correctly,Example,In 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.,Local instance allocated in the Stack,Global instance allocated in Ram,36,FUNCTION_BLOCK instance,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 Block,receives one or more input 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 latter case as long as the function block instance has been declared as Global(,VAR_GLOBAL,).,11,20,15,25,Offset,My_FB1,In1,In2,Out_1,Out_2,Visible only inside FB.,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 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 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,Type of variable,Access by calling POU,Access inside the function,Note,Memory,VAR,No,RW,Visible,only,to the function where it was declared.,Yes,VAR_INPUT,RW,R,An input variable is visible to the calling POU and is written by it.Inside the function where it is defined,its value is not modified.,Yes,VAR_OUTPUT,R,W,An output variable is visible to the calling POU and can only be read by this.It can be read and written inside the Function Block,Yes,VAR_IN_OUT,RW,RW,An input/output variable combines the characteristics of the,VAR_INPUT,and,VAR_OUTPUT,variables:both visible and can be read and modified inside or outside of the function where it is defined.,Yes,VAR_GLOBAL,RW,RW,Global variables are accessible from all POUs.,Yes,It is possible(but not recommended)to declare a Local variable in a program with the same identifier as a Global variable.Inside the program the processor will consider the Local variable(whose value is initialised between one program cycle and the next)and not the Global variable.,Variables scope:,The qualifier RETAIN,If a Global instance has the qualifier,RETAIN,data are saved in T-permanent memory.,Care must be paid when,RETAIN,qualifier is used,in fact at each call of the Function Block a,possible change of data,will produce a writing in the,Flash memory,.,39,Function Block,Declaration,The declaration must be made in two way:,using“,Subroutine ST,”node,before the,PROGRAM,statement,FUNCTION_BLOCK,FB_name,.,END_FUNCTION_BLOCK,Instance,Function blocks must be instanced before being used in a POU.A function block can be instanced before the,PROGRAM,statement,if it is a global instance,or within any POU,if it is a local instance.,Call,A function block can be called one or more times in a program,function or in another function block.Recursion is not allowed.,Memory,For each instance of a function block declared as,Local variable,is allocated space in Stack.The data are stored for just one program cycle(e.g.:within the PROGRAM or within a FUNCTION).,For each instance of a function block declared as,Global variable,is allocated space in RAM.The data are stored for all program cycles.,If the Global variable has the,RETAIN,qualifier,the data are also stored after the controller is switched off.,Use,Control functions that remain over time(e.g.:PID,counters).,Input,parameters,The parameters are sent to the function block with their identifier.This allows some,VAR_INPUT,and,VAR_OUTPUT,parameters to be omitted.,The position of the parameters in the call does not need to correspond to the position of the parameters in the declaration.,VAR_IN_OUT,parameters cannot be omitted.,The parameters are used as elements in a structure(function_block_instance.parameter_name).,Output,parameters,Function blocks can return one or more output values.The output parameters may be,elementary,data types,arrays,or,structures,.,The properties of a block are listed bel
展开阅读全文