Functions and FBWord格式.docx
《Functions and FBWord格式.docx》由会员分享,可在线阅读,更多相关《Functions and FBWord格式.docx(10页珍藏版)》请在冰点文库上搜索。
Lesson1-UsingFunctionsandFunctionBlocks
11.2.1Description
ThislessondescribeshowtouseFunctionsandFunctionBlocksinthedifferentlanguages.
11.2.2Objectives
Oncompletionofthislessonyouwillbeableto:
∙UseFunctionsandFunctionBlocksinthedifferentlanguages.
11.2.3ThedifferencebetweenFunctionsandFunctionBlocks
11.2.3.1Function
∙Haveavalue.
∙Canbeusedinexpressions.
∙Donotretaintheiroldvaluesanddoalwaysgivethesamevaluewhentheinputparametershavethesamevalue.
11.2.3.2Functionblock
∙Havebothinputandoutputparameters.
∙Cangiveseveraloutputvaluesusingparameters.
∙Retaintheirvalues,fromthelastcall,whencalledthesecondtime.Cangivedifferentoutputvalueseveniftheinputvaluesarethesame.
∙Havetobemadeintoaninstance.
11.2.4Insertinglibraries
RightclickontheLibrariesiconandselectInsertLibrarytoinsertalibrarytoyourproject.
11.2.5Somestandardfunctionsandfunctionblocks
ThelibrarySystemLibcontainsanumberofpredefinedFunctionsandFunctionBlocks.
11.2.5.1Datatypeconversion
TheareanumberofpredefineddataconvertersinthelibrarySystemLib.Forexamplebool_to_dint,andreal_to_dint.
11.2.5.2Mathematicalfunctions
TheareanumberofpredefinedmathematicalfunctionsinthelibrarySystemLib.Forexamplesin,cos,add,ln,sqrt.
11.2.5.3Countersandtimers
TheareanumberofpredefinedcountersandtimersinthelibrarySystemLib.ForexampleCTU(CounterUp),CTD(CounterDown),TOn(Timerondelay),Tof(Timeroffdelay).
11.2.6UsingFunctionsindifferentlanguages
11.2.6.1STandSFC
AfunctioncallinSTisdonebywritingthenameofthefunctionintheeditororbyusingthe
button,pressing<
CtrlL>
orselecting(Insert>
Function…).AnexamplewiththefunctionAddinST:
11.2.6.2FBD
Usethetoolbarbutton
orselect(Insert>
Function/Functionblock...)orpress<
Ctrl+K>
todisplaythedialogboxcontainingtheavailablefunctionsandfunctionblocks.Ifthefunctionorfunctionblockisextensible,i.e.canbecalledwithavaryingnumberofinputs,youcanenterthenumberinthePropertiesSizefield.
AnexamplewiththefunctionAddinFBD:
11.2.6.3
IL
AfunctioncallinILisdonebywritingthenameofthefunctionintheinstructionfieldorusingthe
button,press<
CtrlJ>
orselect(Insert>
Function…).
AnexamplewiththefunctionAddinIL:
11.2.6.4LD
Useoneofthetoolbarbuttons
Left,Right,ParallelFunction/Functionblock...)todisplaythedialogboxcontainingtheavailablefunctionsandfunctionblocks.Ifthefunctionorfunctionblockisextensible,i.e.canbecalledwithavaryingnumberofinputs,youcanenterthenumberinthePropertiesSizefield.
AnexamplewiththefunctionAddinLD:
11.2.7Instantiating/UsingFBInstancesindifferentlanguages
11.2.7.1STandSFC
YoumustcreateaninstanceofthefunctionblockbeforeitisusedintheSTorSFCeditor.Theinstanceiscreatedlikethis:
∙ClickontheFunctionblockstaboftheProgram,ControlModuleTypeorFunctionblocktype.
∙WritethenameofthefunctionblockyouintendtouseintheFunctionBlockTypefieldorpress<
CtrlJ>
.
∙GivethefunctionblockasuitablenameintheNamefield.
∙WriteadescriptionintheDescriptionfield.
∙Callforthefunctionblockbywritinglikethisinthecodeblockwereyouintendtouseit:
TimerCloseDoor(
∙Anewwindowliketheonebelowwillbevisible.WritesuitablevariablesintheActualParameterfield.
11.2.7.2FBD
SamasusingfunctionsinFBD.ItisnotnecessarytodeclareafunctionorfunctionblockbeforeusingitintheFBDeditor.
11.2.7.3IL
YoumustcreateaninstanceofthefunctionblockbeforeitisusedintheILeditor.Theinstanceiscreatedlikethis:
CtrlJ>
∙Callforthefunctionblockbywritinglikethisinthecodeblock.
11.2.7.4LD
ItisnotnecessarytodeclareafunctionorfunctionblockbeforeusingitintheFBDeditor.
∙Usethetoolbarbuttons
Left,Right,ParallelFunction/Functionblock...),todisplaythedialogboxcontainingtheavailablefunctionsandfunctionblocks.TheInsertFunction/FunctionBlockcommanddisplaysadialogboxshowingfunctionsandfunctionblocksavailableforinsertion.
∙ThedialogboxisthesameastheoneintheFBDeditor(above).
∙Declaresuitablevariableswithcorrectdatatypestotheparametersofthefunctionblock.
∙Connectthepinsofthefunctionblockwiththevariables.
11.3Exercise11.1–UsingFunctionBlocks
Pleasedoexercise11.1
11.4
Lesson2-CreatingFunctionBlocks
11.4.1Description
ThislessondescribeshowtodeclareuserdefinedFunctionBlocksinlibraries.
11.4.2Objectives
11.4.3Creatingauserlibrary
RightclickontheLibrariesiconandselectNewLibrarytoinsertalibrarytoyourproject.
11.4.3.1Whyusinglibraries?
Itisrecommendedtocreateownlibrarieswhenyouworkinalargerproject,becausethenyougetabetterstructureoftheproject.AnothermajoradvantagesofcreatingownlibrariesarethatitispossibletoreusetheDatatypes,FunctionBlocktypesandControlModuletypesinotherprojects.
11.4.4CreatingaFunctionblocktype
RightclickontheFunctionBlockTypesiconandselectNewFunctionBlockType…tocreateanewFunctionBlockType.Thiscouldbedoneinanapplicationoralibrary.
11.4.4.1WhycreatingFunctionBlockTypes?
ItisrecommendedtocreateownFunctionblocktypes,becausethenyoumakeitpossibletoreusethecodelater.
11.4.4.2
FunctionBlockTypeEditor
Thecontentofthefunctionblocktypeisdisplayedinaneditor.Youcandefinefunctionblocktypes,whichcanbeusedinotherPOU:
s.Parameters,variablesandfunctionblocksthatareusedmustbedeclaredinthedeclarationpane.Codeisenteredinthecodepane.Afunctionblocktypetypicallyusesthefollowingelements,whicharedeclaredinseparatetabsinthedeclarationpane:
∙ParametersVariablesthatpassesvaluesinoroutfromtheprogram.Theoptionsare:
in,outandin_out.Itisnotallowedtosetaninitialvalueonaparameter
∙LocalVariablesusedinthefunctionblock.
∙ExternalvariablesGloballydefinedvariablesintheapplicationeditormustbespecifiedasexternalvariablestobeaccessedbythefunctionblock.
∙Functionblocks.Youmustexplicitlydeclarefunctionblockstousethem(notnecessaryintheFBDandLDeditors).
11.4.5MoveFBbetweenlibraries
Itispossibletomovefunctionsorfunctionblocksbetweenlibrariesorapplications,byusingthecopyandpastefunction.
11.5Exercise11.2–CreatingFunctionBlocks
Pleasedoexercise11.2
Dukantagbortdennaosynligatextmenlå
t
nedanstå
endePageBreakliggakvar!
Lå
tä
vendenefterfö
ljandesidanliggakvar!
Thispageisintentionallyleftblank
Osynligtext:
Nä
stasidamå
stebö
rjapå
UDDAsidnummer
Lä
mnaENellerTVÅ
tommasidorhä
rOVANFÖ
R.
Nedanstå
endeSectionBreakfå
rINTEtasbort!
Kompletteratomsidavidbehovmednedanstå
endetext
TABLEOFCONTENTS
Chapter11FunctionandFunctionBlocks1
11.1GeneralInformation1
11.1.1Description1
11.1.2Objectives1
11.1.3ReferenceDocumentation1
11.2Lesson1-UsingFunctionsandFunctionBlocks2
11.2.1Description2
11.2.2Objectives2
11.2.3ThedifferencebetweenFunctionsandFunctionBlocks2
11.2.3.1Function2
11.2.3.2Functionblock2
11.2.4Insertinglibraries2
11.2.5Somestandardfunctionsandfunctionblocks3
11.2.5.1Datatypeconversion3
11.2.5.2Mathematicalfunctions3
11.2.5.3Countersandtimers3
11.2.6UsingFunctionsindifferentlanguages3
11.2.6.1STandSFC3
11.2.6.2FBD3
11.2.6.3IL4
11.2.6.4LD4
11.2.7Instantiating/UsingFBInstancesindifferentlanguages4
11.2.7.1STandSFC4
11.2.7.2FBD5
11.2.7.3IL5
11.2.7.4LD6
11.3Exercise11.1–UsingFunctionBlocks6
11.4Lesson2-CreatingFunctionBlocks7
11.4.1Description7
11.4.2Objectives7
11.4.3Creatingauserlibrary7
7
11.4.4CreatingaFunctionblocktype7
11.4.4.2FunctionBlockTypeEditor8
11.4.5MoveFB