-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
1/87
MANUAL
SIMATIC
S7-SCL para S7-300
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
2/87
1.3 Quais as funes que o S7-SCL oferta
S7-SCL oferece todas as vantagens de uma linguagem de alto nvel. Alm disso,
S7-SCL oferece recursos que foram projetados especialmente para apoiar aprogramao estruturada!
!i"#iote$a %e "#o$os
"locos do pr-definidos so fornecidos em #i#liotecas, por e$emplo!
%un&es de sistema
%un&es '(C
%un&es de converso
Te&p#ates %e pro'ra&a
) editor S7-SCL oferece diversos modelos para insero que s* precisam serpreenc+idos!
emplates para #locos for e$ample function #locs, data #locs/ and t+eir calls
emplates para coment0rios de #locos, par1metros de #locos e constantes
emplates para estruturas de controle '%, CAS(, %)2, 34'L(, 2(5(A/
(#e&entos %e #in'ua'e& %e pro'ra&a)o %e a#to n*+e#
Loops
2amos alternativos '% ... 4(6 ... (LS(/
Saltos
,ro'ra& eas to un%erstan%
)s recursos a seguir aumentam a legi#ilidade do programa!
5rogramao completamente sim#*lica
Coment0rios
ipos de dados elementares e auto-definidos
($i#io de referncias cru8adas
%ormatao autom0tica de entrada atravs de indentao
Colorao especfica de sinta$e dos elementos de linguagem
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
3/87
epura%or e& n*+e# %e #in'ua'e& %e a#to n*+e#
) depurador permite simples depurao do programa em um nvel de linguagem dealto nvel.Se oferece as seguintes funcionalidades!
) monitoramento contnuo da e$ecuo do programa
9onitoramento passo-a-passo por meio de pontos de parada que podem ser
definidos individualmente.
5ossi#ilidade de saltar para #locos c+amados durante a depurao
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
4/87
/ Usan%o S7-SCL
/. Interfa$e %e usurio
As janelas S7-SCL tm os seguintes componentes padro!
:. !arra %e t*tu#o2Contm os #ot&es de controle dajanela.
;. !arra %e &enu29ostra todos os menusdisponveis para a janela a#erta.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
5/87
5ane#a 6(rrors an% arnin's6
A janela >(rrors and ?arnings > lista os erros e alertas que ocorreram enquanto uma fonteestava sendo compilada.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
6/87
/./ Cria)o e &anipu#a)o %e u& arqui+o fonte S7-SCL
/./.1 Crian%o u& no+o arqui+o fonte S7-SCL
Antes que voc possa escrever um programa S7-SCL, voc deve primeiro criarum novo arquivo fonte S7-SCL @oc cria o arquivo fonte na pasta de fontes emum programa S7.
(strutura %e u& ,ro'ra&a S7 no SIMATIC Mana'er
Arquivos fonte criados no S7-SCL podem ser integrados estrutura de um programaS7 como mostrado a#ai$o!
Si'a os passos %es$ritos a"ai8o2
:. A#ra a cai$a de di0logo >6e?>da seguinte forma!
Clique no #oto >6e?>na #arra de ferramentas ou
Selecione o commando de menu i#e 9 Ne.
;. 6a cai$a de di0logo >6e?>, selecione
Bm projeto
+e filter setting >S7-SCL Source %ile>and
+e source files folder ?it+in t+e S7 program
)E>.
+e source o#ject is created under t+e name ou +ave selected and is t+endisplaed in a ?indo? ?+ere ou can continue ?oring ?it+ it.
Note
Fou can also create an S7-SCL source file ?it+ t+e S'9A'C 9anager # selecting asource file folder and t+e menu command Insert 9 S7 Softare 9 S7-SCL Sour$ei#e.
S7-SCL @=.< for S7-
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
7/87
/././ :penin' !#o$;s
't is not possi#le to open #locs ?it+ t+e S7-SCL application. Fou can al?as onlopen t+e corresponding source. "locs created ?it+ S7-SCL can, +o?ever, #e
opened ?it+ t+e LAJHSLH%"J editor and displaed and edited in t+e SLprogramming language. Jo not mae an modifications to t+e #loc in t+is SLrepresentation for t+e follo?ing reasons!
+e displaed 9C7 commands do not necessaril represent a valid SL #loc.
An error-free compilation ?it+ t+e SL compiler involves modifications t+atrequire t+oroug+ no?ledge of #ot+ SL and S7-SCL.
+e #loc compiled ?it+ SL +as t+e SL language identifier and no longer t+eS7-SCL identifier.
+e S7-SCL source file and t+e 9C7 code are no longer consistent.
%urt+er information is availa#le in t+e S(5 7 online +elp.
Note
't is easier to maintain our C5B programs # maing an c+anges ou require int+e S7-SCL source files, and t+en compiling t+em again.
/./.< Spe$ifin' :"=e$t ,roperties
Fou can specif t+e o#ject properties # assigning attri#utes to t+e #locs. Fou canselect t+e properties for t+e S7-SCL source file for e$ample, t+e aut+or/ in t+e>5roperties>dialog #o$.
o##o t>e steps out#ine% "e#o2
1. Select t+e menu command i#e 9 ,roperties.
. (nter t+e options ou require in t+e >5roperties>dialog #o$.
3. Confirm ?it+ >)E>.
/./.? Creat in' S7-SCL Sour$e i#es it> a Stan%ar% (%itorFou can also use a standard ASC'' editor for editing our S7-SCL source file. 'f ouc+oose t+is met+od, t+e +elpful editing functions and integrated online +elp of S7-SCLare not availa#le.
)nce ou +ave created t+e source file and saved it, ou must t+en import it into t+esource file folder of an S7 program using t+e S'9A'C 9anager see S(5 7documentation/. %ollo?ing t+is, ou can open t+e source file in S7-SCL and continue?oring ?it+ it or compile it.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
8/87
/.< @ui%e#ines for S7-SCL Sour$e i#es
/.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
9/87
/.?.1 Insertin' Te&p#ates
/.?.1.1 Insertin' !#o$; Te&p#ates
)ne S7-SCL editing function allo?s ou to insert #loc templates for )"s, %"s,%Cs, instance J"s, J"s, J"s t+at reference BJs, and BJs. Bsing t+ese #loctemplates maes it easier to program and to eep to t+e required snta$.
o##o t>e steps out#ine% "e#o2
:. 5osition t+e cursor at t+e point at ?+ic+ ou ?ant to insert t+e #loc template.
;. Select t+e menu command 'nsert "loc emplate )"H%"H%CHJ"H'J"HJ"2eferencing BJHBJ.
/.?.1. Insertin' !#o$; Ca##s
S7-SCL supports ou ?+en programming #loc calls. Fou can call t+e follo?ing#locs!
Sstem function #locs S%"/ and sstem functions S%C/ from t+e S'9A'Cli#raries,
%unction #locs and functions created in S7-SCL,
%unction #locs and functions created in ot+er S(5 7 languages.
o##o t>e steps out#ine% "e#o2
:. Select t+e Insert 9 !#o$; Ca## menu command.
;. Select t+e required S%C, S%", %C, or %" in t+e dialog #o$ and confirm ourselection ?it+ >)E>.
S7-SCL copies t+e called #loc automaticall to t+e S7 program and enters t+e#loc call and t+e formal parameters of t+e #loc ?it+ t+e correct snta$ into t+esource file.
e steps out#ine% "e#o2
:. 5osition t+e cursor after t+e #loc +eader of t+e required #loc.
2. Select t+e menu command Insert 9 !#o$; Te&p#ate 9 Co&&ent.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
10/87
/.?.1./ Insertin' ,ara&eter Te&p#ates
)ne S7-SCL editing function allo?s ou to insert templates for t+e declarations of t+eparameters. Bsing t+ese templates maes it easier to tpe in our program and to
eep to t+e required snta$. Fou can declare parameters in function #locs and infunctions.o##o t>e steps out#ine% "e#o2
1. 5osition t+e cursor in t+e declaration section of an %" or %C.
. Select t+e menu command Insert 9 !#o$; Te&p#ate 9 ,ara&eter.
/.?.1.< Insertin' Contro# Stru$tures
+is S7-SCL editing function allo?s ou to insert control structure templates for logic#locs. Bsing t+ese templates maes it easier to input our information and to eep
to t+e required snta$.
o##o t>e steps out#ine% "e#o2
1. 5osition t+e cursor at t+e point at ?+ic+ ou ?ant to insert t+e template.
. Select t+e menu command Insert 9 Contro# Stru$ture 9 IBCAS(B:BDI L(B(,(AT.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
11/87
/.7 Co&pi#in' an S7-SCL ,ro'ra&
/.7.1 >at Eou S>ou#% Fno A"out Co&pi#in'
"efore ou run can run or test our program, ou must first compile it. )nce oustart compilation, t+e compiler is started automaticall. +e compiler +as t+efollo?ing c+aracteristics!
Fou can compile an entire S7-SCL source file in one compilation session orcompile selected individual #locs from t+e source file.
All snta$ errors found # t+e compiler are displaed in a ?indo?.
(ac+ time a function #loc is called, a corresponding instance data #loc iscreated if it does not alread e$ist.
Fou can also compile several S7-SCL source files toget+er # creating anS7-SCL compilation control file.
Bsing t+e :ptions 9 Custo&iGe menu command, ou can set options for t+ecompiler.
)nce ou +ave created a user program t+at is free of errors and +as #eencompiled, ou can assume t+at t+e program is correct. 5ro#lems can,nevert+eless, occur ?+en t+e program is run on t+e 5LC. Bse t+e de#uggingfunctions of S7-SCL to find errors of t+is tpe.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
12/87
/.7. Custo&iGin' t>e Co&pi#er
Fou can adapt t+e compilation to meet our o?n requirements. o##o
t>e steps out#ine% "e#o2
1. Select t+e menu command :ptions 9 Custo&iGe to open t+e >Customi8e>dialog#o$.
. Select t+e >Compiler>ta# or >Create "loc>ta#.
3. (nter t+e options ou require in t+e ta#.
:ptions in t>e 6Co&pi#er6 ta"
Create de#ug info +is option allo?s ou to run a test ?it+ t+e de#ugger after ou +avecompiled t+e program and do?nloaded it to t+e C5B. +e memor
requirements of t+e program and t+e runtimes on t+e AS are, +o?ever,increased # t+is option.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
13/87
/.7.3 Co&pi#in' t>e ,ro'ra&
"efore ou can test a program or run it, it must first #e compiled. o mae sure t+atou al?as compile t+e latest version of our S7-SCL source file, it is advisa#le to
select t+e menu command :ptions 9 Custo&iGe and to select t+e option >Save#efore compiling>in t+e >(ditor>ta#. +e menu command i#e 9 Co&pi#e t+enimplicitl saves t+e S7-SCL source file.
o##o t>e steps out#ine% "e#o2
:. Save t+e S7-SCL source file to #e compiled.
;. o create an e$ecuta#le program, ou must select t+e option >Create o#jectcode>in t+e >Compiler>ta# of t+e >Customi8e>dialog #o$.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
14/87
/.H on#oa%in' t>e Create% ,ro'ra&s
/.H. on#oa%in' User ,ro'ra&s to t>e C,U
equire&ents
3+en ou compile an S7-SCL source file, #locs are created from t+e source fileand are saved in t+e >"locs>folder of t+e S7 program.
"locs t+at are called at t+e first level in S7-SCL #locs are automaticall copied tot+e >"locs>director and entered in t+e load list.
Fou can do?nload furt+er #locs from t+e user program ?it+ t+e S'9A'C9anager from t+e programming device to t+e C5B.
"efore ou can do?nload #locs, a connection must e$ist #et?een t+eprogramming device and C5B. An online user program must #e assigned to t+eC5B module in t+e S'9A'C manager.
,ro$e%ure
)nce ou +ave compiled t+e source file, ou can start t+e do?nload in t+e
follo?ing ?as. +e i#e 9 on#oa% menu command do?nloads all #locs in t+e source file
and all #locs t+at are called at t+e first level.
+e i#e 9 Co&pi#e Se#e$te% !#o$;s menu command opens a dialog #o$ in?+ic+ ou can select individual #locs for compilation.
+e #locs are transferred to t+e C5B. 'f a #loc alread e$ists in t+e 2A9 of t+e C5B ou ?ill #eased to confirm ?+et+er or not ou ?ant to over?rite t+e #loc.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
15/87
< S7-SCL !asi$ Ter&s
ara$ter Set
Letters an% Nu&eri$ C>ara$ters
S7-SCL uses t+e follo?ing c+aracters as a su#set of t+e ASC'' c+aracter set!
+e upper- and lo?ercase/ letters A to M.
+e Ara#ic num#ers G to N.
"lans - t+e #lan itself ASC'' value W X Y Z . ,
Note
'n t+e section entitled >Language Jescription>, ou ?ill find a detailed list of all t+epermitted c+aracters and information on +o? t+e c+aracters are interpreted in S7-SCL.
S7-SCL @=.< for S7-
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
16/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
17/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
18/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
19/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
20/87
? S7-SCL ,ro'ra& Stru$ture
?.1 !#o$;s in S7-SCL Sour$e i#esFou can program an num#er of #locs in an S7-SCL source file. S(5 7 #locsare su#units of a user program distinguis+ed according to t+eir function, t+eirstructure or t+eir intended use.
!#o$; Tpes
+e follo?ing #locs are availa#le!
ea%-&a%e !#o$;s
Fou do not +ave to program ever function ourself. Fou can also mae use ofvarious read-made #locs. +ese are availa#le in t+e C5B operating sstem or
li#raries (S7 l ib ) in t+e S(5 7 Standard 5acage and can #e used, for e$ample, toprogram communication functions.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
21/87
?.3 @enera# Stru$ture of a !#o$;
A #loc consists of t+e follo?ing areas!
"loc start identified # a e?ord and a #loc num#er or a sm#olic #locname, for e$ample, >)2\A6'MA')6["L)CE )":>for an organi8ation #loc.3it+ functions, t+e function tpe is also specified. +is decides t+e data tpe oft+e return value. 'f ou ?ant no value returned, specif t+e e?ord @)'J.
)ptional #loc title preceded # t+e e?ord >'L( Q>.
)ptional #loc comment. +e #loc comment can e$tend over several lines,eac+ line #eginning ?it+ >HH>.
(ntr of t+e #loc attri#utes optional/
(ntr of t+e sstem attri#utes for #locs optional/
Jeclaration section depending on t+e #loc tpe/ Statement section in logic #locs or assignment of
actual values in data #locs optional/
'n logic #locs! Statements
"loc end indicated # (6J[)2\A6'MA')6["L)CE,(6J[%B6C')6["L)CE or (6J[%B6C')6
?./ !#o$; Start an% (n%
Jepending on t+e tpe of #loc, t+e source te$t for a single #loc is introduced # astandard identifier for t+e start of t+e #loc and t+e #loc name. 't is closed ?it+ astandard identifier for t+e end of t+e #loc.
+e snta$ for t+e various tpes of #locs can #e seen in t+e follo?ing ta#le!
I%entifier !#o$; Tpe Snta8
%unction #loc %" %B6C')6["L)CE f#[name. . .(6J[%B6C')6["L)CE
%unction %C %B6C')6 fc[name ! function tpe. . .(6J[%B6C')6
)rgani8ation #loc )" )2\A6'MA')6["L)CE o#[name. . .(6J[)2\A6'MA')6["L)CE
Jata #loc J" JAA["L)CE d#[name. . .(6J[JAA["L)CE
S+ared data tpe BJ F5( udt[name. . .(6J[F5(
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
22/87
?.< Attri"utes for !#o$;s
efinitionA #loc attri#ute is a #loc propert t+at ou can use, for e$ample, to specif t+e #loc tpe,t+e version, t+e #loc protection or t+e aut+or. Fou can displa t+e properties in a S(5 7properties page ?+en ou select #locs for our application.
Fou can assign t+e follo?ing attri#utes!
Feor%BAttri"ute (8p#anation (8a&p#es
'L(QWprinta#le c+aractersW itle of t+e #loc 'L(QWS)2W
@(2S')6 !Wdecimal digit string.decimal digit stringW
@ersion num#er of t+e #locG to :=/
6ote! 3it+ data #locs J"s/, t+e@(2S')6 attri#ute is not specifiedin quotes.
@(2S')6 ! W
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
23/87
?. e$#aration Se$tion
efinition+e declaration section is used for declarations of local varia#les, parameters,constants, and la#els.
+e local varia#les, parameters, constants, and la#els t+at must onl #e valid?it+in a #loc are defined in t+e declaration section of t+e logic #loc.
Fou define t+e data areas ou ?ant to #e accessi#le to an logic #loc in t+edeclaration section of data #locs.
'n t+e declaration section of a BJ, ou specif t+e user-defined data tpe.
Stru$tureA declaration section is divided into different declaration su#sections indicated #t+eir o?n pair of e?ords. (ac+ su#section contains a declaration list for data oft+e same tpe. +ese su#sections can #e positioned in an order. +e follo?ingta#le s+o?s t+e possi#le declaration su#sections!
ata Snta8 ! C :! ! UT
Constants CONST"e#la$a%&n l&(%EN)_CONST
] ] ]
La#elsL!BEL
"e#la$a%&n l&(%EN)_L!BEL
] ] ]
emporarv@aria#lesV!R_TEM "e#la$a%&nl&(%EN)_V!R
] ] ]
Static varia#lesV!R"e#la$a%&n l&(%EN)_V!R
] ] P/ ] PP/ ] PP/
'nput parametersV!R_INUT"e#la$a%&n l&(%EN)_V!R
] ]
)utput parametersV!R_OUTUT"e#la$a%&n l&(%EN)_V!R
] ]
'nHout parametersV!R_IN_OUT"e#la$a%&n l&(%EN)_V!R
] ]
P/ Alt+oug+ t+e declaration of varia#les #et?een t+e e?ord pair @A2 and(6J[@A2 is permitted in functions, t+e declarations are s+ifted to t+etemporar area ?+en t+e source file is compiled.
PP/ 'n J"s and BJs, t+e e?ords @A2 and (6J[@A2 are replaced #
S2BC and (6J[S2BC respectivel.S7-SCL @=.< for S7-
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
24/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
25/87
?.13 Stru$ture of a un$tion CJ
efinitionA function %C/ is a logic #loc t+at is not assigned its o?n memor area. 't does notrequire an instance J". 'n contrast to an %", a function can return a function resultreturn value/ to t+e point from ?+ic+ it ?as called. A function can t+erefore #e usedlie a varia#le in an e$pression. %unctions of t+e tpe @)'J do not +ave a returnvalue.
C I%entifierAfter t+e >%B6C')6>e?ord, enter t+e e?ord %C as t+e %C identifier follo?ed #t+e #loc num#er or t+e sm#olic name of t+e %C. +e #loc num#er can #e a value
from G to I==
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
26/87
(8a&p#e
FUNCTION FC11: RE!LV!R_INUT
RE!L
RE!LRE!LRE!L
EN) V!RV!R_OUTUT
>2: RE!L +EN)_V!RBE/IN
33 Re%u$n alue 4$7 4un#%&nFC11:= S>RT<
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
27/87
?.1/ Stru$ture of an :r'aniGation !#o$; :!J
efinition+e organi8ation #loc just lie an %" or %C is part of t+e user program and iscalled cclicall or as a response to certain events # t+e operating sstem. 'tprovides t+e interface #et?een t+e user program and t+e operating sstem.
:! I%entifierAfter t+e >)2\A6'MA')6["L)CE>e?ord, enter t+e e?ord )" as t+e )"identifier follo?ed # t+e #loc num#er or t+e sm#olic name of t+e )". +e #locnum#er can #e a value from : to I==
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
28/87
7 ata Tpes
7.1 :+er+ie of t>e ata Tpes in S7-SCL
A data tpe is t+e com#ination of value ranges and operations in a single unit.
+e data tpes decide!
t+e tpe and interpretation of t+e data elements,
t+e permitted ranges for t+e data elements,
t+e permitted operations t+at can #e e$ecuted on an address of a data tpe
t+e notation of t+e constants of t+e data tpe.
(#e&entar ata Tpes
(lementar data tpes define t+e structure of data elements t+at cannot #esu#divided into smaller units. +e correspond to t+e definition in t+e
J'6 (6 ::
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
29/87
,ara&eter Tpes
5arameter tpes are special data tpes for timers, counters and #locs t+at can #e usedas formal parameters.
ata Tpe (8p#anation
'9(2 +is is used to declare timer functions as parameters.
C)B6(2 +is is used to declare counter functions as parameters.
"L)CE[$$ +is is used to declare %Cs, %"s, J"s and SJ"s as parameters.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
30/87
7. (#e&entar ata Tpes
7..1 !it ata Tpes
Jata of t+is tpe are #it com#inations occuping eit+er : #it data tpe "))L/, b #its, :I#its or
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
31/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
32/87
7.< ata Tpes for ,ara&eters
o specif t+e formal #loc parameters of %"s and %Cs, ou can use parameter tpes in
addition to t+e data tpes t+at +ave alread #een introduced.
,ara&eter SiGe es$ription
'9(2 ; #tes 'dentifies a specific timer to #e used # t+e program in t+e logic #loccalled.
Actual 5arameter for e$ample, :
C)B6(2 ; #tes 'dentifies a specific counter to #e used # t+e program in t+e logic#loccalled.
"L)CE[%""L)CE[%C"L)CE[J"
"L)CE[SJ"
; #tes'dentifies a specific #loc to #e used # t+e SL program in t+e #loccalled.
Actual 5arameter! for e$ample, %C:G:J"D;
Notes2+e data tpe "L)CE J" can #e accessed a#solutel
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
33/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
34/87
e$#aration of Lo$a# Karia"#es an%,ara&eters
.1 Lo$a# Karia"#es an% !#o$; ,ara&eters
Cate'ories of Karia"#es
+e follo?ing ta#le illustrates t+e categories of local varia#les!
Karia"#e (8p#anation
Static @aria#les Static varia#les are local varia#les ?+ose value is retained t+roug+out all#loc ccles #loc memor/. +e are used to save values of a function#loc and are stored in t+e instance data #loc.
emporar @aria#les emporar varia#les #elong to a logic #loc at local level and do not occupa static memor area, since t+e are stored in t+e C5B stac. +eir value isonl retained ?+ile t+e #loc concerned is running. emporar varia#lescannot #e accessed from outside t+e #loc in ?+ic+ t+e are declared.
Cate'ories of !#o$; ,ara&eters
"loc parameters are place+olders t+at are onl assigned a specific value ?+en t+e#loc is called. +e place+olders in t+e #loc are no?n as formal parameters and t+evalues assigned to t+em ?+en t+e #loc is called are referred to as t+e actualparameters. +e formal parameters of a #loc can #e vie?ed lie local varia#les.
"loc parameters can #e su#divided into t+e categories s+o?n #elo?!
!#o$; ,ara&eter (8p#anation
'nput parameters 'nput parameters accept t+e current input values ?+en t+e #loc is called. +eare read-onl.
)utput parameters )utput parameters transfer t+e current output values to t+e calling #loc. Jatacan #e ?ritten to and read from t+em.
'nHout parameters 'nHout parameters adopt current input values ?+en a #loc is called. Afterprocessing t+e value, t+e receive t+e result and return it to t+e calling #loc.
Dec la ra t ion o f Loca l Va r iab les an Pa ram e te rs
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
35/87
. e$#aration Su"se$tions
..1 :+er+ie of t>e e$#aration Su"se$tions
(ac+ categor of local varia#les or parameters +as its o?n declaration su#sectionidentified # its o?n pair of e?ords. (ac+ su#section contains t+e declarations t+atare permitted for t+at particular declaration su#section. +ese su#sections can #epositioned in an order.
+e follo?ing ta#le s+o?s ?+ic+ varia#les or tpes of parameter ou can declare in t+evarious logic #locs!
ata Snta8 ! C :!
Karia"#e as2Static varia#le
@A2. . .(6J[@A2
] ] P/
emporar varia#le@A2[(95. . .(6J[@A2
] ] ]
!#o$; para&eter as2'nput parameter
@A2['65B. . .(6J[@A2
] ]
)utput parameter
@A2[)B5B. . .(6J[@A2 ] ]
'nHout parameter
@A2['6[)B. . .(6J[@A2
] ]
P/ Alt+oug+ t+e declaration of varia#les #et?een t+e e?ord pair @A2 and(6J[@A2 is permitted in functions, t+e declarations are created in t+etemporar area ?+en t+e source file is compiled.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
36/87
Declaration of Local Variables an Parameters
.. Stati$ Karia"#es
Static varia#les are local varia#les ?+ose value is retained ?+en t+e #locs arerun. +e are used to save t+e values of a function #loc and are contained in acorresponding instance data #loc.
..3 Te&porar Karia"#es
emporar varia#les #elong locall to a logic #loc and do not occup a staticmemor area. +e are located in t+e stac of t+e C5B. +eir value is onlretained ?+ile t+e #loc concerned is running. emporar varia#les cannot #eaccessed from outside t+e #loc in ?+ic+ t+e are declared. 3+en an )", %" or%C is first e$ecuted, t+e value of t+e temporar data +as not #een defined.
'nitiali8ation is not possi#le.
Fou s+ould declare data as temporar data if ou onl require it to record interimresults ?+ile our )", %" or %C e$ecutes.
../ !#o$; ,ara&eters
5arameters are place+olders t+at are onl assigned a value ?+en t+e #loc isactuall called. +e place+olders declared in t+e #loc are no?n as formalparameters t+at are assigned values as actual parameters. 5arameters t+ereforeprovide a mec+anism for t+e e$c+ange of information #et?een t+e #locs.
Tpes of !#o$; ,ara&eters
%ormal input parameters are assigned t+e actual input valuesdata flo? into t+e #loc/.
%ormal output parameters are used to transfer output values
data flo? from t+e #loc to t+e outside/
%ormal inHout parameters +ave #ot+ t+e function of an input and an outputparameter.
Dec la ra t ion o f Loca l Va r iab les an Pa ram e te rs
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
37/87
11 (8pressions :perations an% A%%resses
11.1 :+er+ie of (8pressions :perations an% A%%resses
An e$pression stands for a value t+at is calculated during compilation or duringruntime and consists of addresses for e$ample constants, varia#les or functioncalls/ and operations for e$ample P, H, O or -/.
+e data tpes of t+e addresses and t+e operations used determine t+e tpe ofe$pression. +e follo?ing e$pressions are possi#le in S7-SCL!
Arit+metic e$pressions
Comparison e$pressions
Logical e$pressions
An e$pression is evaluated in a specific order. +is is decided # t+e follo?ing!
t+e precedence of t+e operations involved and
?oring from left to rig+t or
?it+ operations +aving t+e same precedence # t+e parent+eses.
Fou can do t+e follo?ing ?it+ t+e result of an e$pression!
Assign it to a varia#le.
Bse it as a condition for control statement.
Bse it as a parameter for calling a function or a function #loc.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
38/87
11. :perations
($pressions consist of operations and addresses. 9ost S7-SCL operationscom#ine t?o addresses and are t+erefore termed binary operators. +e ot+eroperations involve onl one address and are called unary operators.
"inar operations are ?ritten #et?een t+e addresses for e$ample, A O "/. A unaroperation al?as stands immediatel #efore its address for e$ample, -"/.
+e precedence of t+e operations listed in t+e ta#le #elo? governs t+e order ofevaluation. :W represents t+e +ig+est precedence.
C#ass :peration S&"o# ,re$e%en$e
Assi'n&ent :peration2 Assignment ! Q ::Arit>&eti$ :perations2
5o?er
Unar :perations
PP ;
Bnar plus O &eti$ :perations
- eses 2 5arent+eses / :
S7-SCL @=.< for S7-
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
39/87
11.? Arit>&eti$ (8pressions
An arit+metic e$pression is an e$pression formed ?it+ arit+metic operations. +esee$pressions allo? numeric data tpes to #e processed.
+e follo?ing ta#le s+o?s all t+e possi#le operations and indicates t+e tpe to ?+ic+t+e result #elongs depending on t+e addresses. +e follo?ing a##reviations are used!
:peration I%entifier 1st A%%ress n% A%%ress esu#t ,re$e%en$e
5o?er PP A6F[6B9 A6F[6B9 2(AL ;
Bnar plus O A6F[6B9 - A6F[6B9 J'@>and >H> produce t+e same result see e$ample #elo?/.
'n t+e division operations HW, 9)JWand J'@W/, t+e second address must not #enot equal to 8ero.
'f one num#er is of t+e '6 tpe integer/ and t+e ot+er of t+e 2(AL tpe realnum#er/, t+e result ?ill al?as #e of t+e 2(AL tpe.
3+en data of t+e tpe JA([A6J['9( and '9( are su#tracted, t+e
address of t+e data tpe '9( must al?as #e positioned to t+e rig+t of t+eoperator >->.
(8a&p#es
33 T9e $e(ul%
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
41/87
11.7 Lo'i$a# (8pressions
A logical e$pression is an e$pression formed # logic operations.
!asi$ Lo'i$ :perations
Bsing t+e operations A6J, , ])2 and )2, logical addresses "))L tpe/ orvaria#les of t+e data tpe "F(, 3)2J or J3)2J can #e com#ined to formlogical e$pressions. o negate a logical address, t+e 6) operation is used.
Lo'i$ :perations2
+e result of t+e e$pression is eit+er 2B( or %ALS( follo?ing a logic operation on"oolean addresses or it is a #it pattern after logic operation on t+e #its of t?oaddresses.
+e follo?ing ta#le lists t+e availa#le logical e$pressions and data tpes for t+eresult and addresses. +e follo?ing a##reviations are used!
:peration I%entifier 1stA%%ress
n% A%%ress esu#t ,re$e%en$e
6egation 6) A6F["' - A6F["' if a R # and # R c t+en ...>.
+e operations are evaluated in t+e order of t+eir precedence. +e precedence can #e c+anged# parent+eses.
(8a&p#es
33C75a$e LESS TH!N OR E>U!L TO . T9e $e(ul% &( GTRUEG! := =
33C75a$e * NOT E>U!L TO *. T9e $e(ul%
33&( GF!LSEG* J *
33Ealua%&n 4 a #75a$&(n e?5$e((&n &n an IF33(%a%e7en%
IF COUNTER THEN ....
33C7;&n&n8 4 % #75a$&(n e?5$e((&n(
Value_! J 20 !N) Value_B 20
33C7;&n&n8 4 % #75a$&(n e?5$e((&n( &%9 ;$a#e%&n8
!J
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
43/87
1 State&ents
1.1 Ka#ue Assi'n&ents
3+en a value is assigned, t+e current value of a varia#le is replaced # a ne? valuespecified # an e$pression. +is e$pression can also contain identifiers for functionst+at are activated # t+e statement and t+en return corresponding values returnvalue/.
As s+o?n in t+e diagram #elo?, t+e e$pression on t+e rig+t-+and side of t+eassignment operator is evaluated and t+e value o#tained as t+e result is set in t+evaria#le ?+ose name is on t+e left-+and side of t+e assignment operator. +evaria#les permitted for t+is function are s+o?n in t+e figure.
Snta8 of a Ka#ue Assi'n&ent
+e tpe of an assignment e$pression is t+e tpe of t+e left address. +e simplevaria#le can #e a varia#le of an elementar or comple$ data tpe.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
44/87
1. Contro# State&ents
1..1 :+er+ie of Contro# State&ents
Se#e$ti+e State&ents
A selective statement ena#les ou to direct program e$ecution into alternativesequences of statements.
Tpes of !ran$> un$tion
'% Statement +e '% statement ena#les ou to direct program e$ecution into one of t?oalternative #ranc+es depending on a condition #eing 2B( or %ALS(!
CAS( Statement +e CAS( statement ena#les ou direct program e$ecution into : of nalternative #ranc+es #ased on t+e value of a varia#le.
Loops
Fou can control loop e$ecution using iteration statements. An iteration statementspecifies ?+ic+ parts of a program s+ould #e iterated depending on certain conditions.
Tpes of !ran$> un$tion
%)2 Statement
Bsed to repeat a sequence of statements for as long as t+e control
varia#le
34'L( Statement Bsed to repeat a sequence of statements ?+ile an e$ecution conditioncontinues to #e satisfied
2(5(A Statement Bsed to repeat a sequence of statements until a terminate condition is met
,ro'ra& 5u&p
A program jump means an immediate jump to a specified jump destination andt+erefore to a different statement ?it+in t+e same #loc.
Tpes of !ran$> un$tion
C)6'6B( Statement Bsed to stop e$ecution of t+e current loop iteration.
(]' Statement Bsed to e$it a loop at an point regardless of ?+et+er t+e terminatecondition is satisfied or not
\)) Statement Causes t+e program to jump immediatel to a specified la#el
2(B26 Statement Causes t+e program to e$it t+e #loc currentl #eing e$ecuted
S7-SCL @=.< for S7-
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
45/87
1.. Con%itions
+e condition is eit+er a comparison e$pression, a logical e$pression or an arit+metic
e$pression. 't is of t+e tpe "))L and can +ave t+e values 2B( or %ALS(. Arit+metice$pressions are 2B( if t+e result in a value ot+er t+en G and %ALS( ?+en t+e resultin t+e value G. +e ta#le #elo? s+o?s e$amples of conditions!
Tpe (8a&p#e
Comparison e$pression (95 =GC)B6(2 RQ :GGC4A2: R WSW
Comparison and logical e$pression AL54A R :;/ A6J 6) "(A
"oolean address ' :.:
Arit+metic e$pression AL54A Q = O "(A/
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
46/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
47/87
(8a&p#e
IF I1.1 THENN := 0 +
SUM := 0 +OK := F!LSE + 33 Se% OK 4la8 % F!LSE
ELSIF ST!RT = TRUE THEN N:= N 1 +
SUM := SUM N +ELSE
OK := F!LSE +EN)_IF +
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
48/87
1../ CAS( State&ent
+e CAS( statement is used to select one of several alternative program sections.+is c+oice is #ased on t+e current value of a selection e$pression.
+e CAS( statement is e$ecuted according to t+e follo?ing rules!
+e selection e$pression must return a value of t+e tpe '6(\(2.
3+en a CAS( statement is processed, t+e program c+ecs ?+et+er t+e valueof t+e selection e$pression is contained ?it+in a specified list of values. 'f amatc+ is found, t+e statement component assigned to t+e list is e$ecuted.
'f no matc+ is found, t+e program section follo?ing (LS( is e$ecuted or nostatement is e$ecuted if t+e (LS( #ranc+ does not e$ist.
Ka#ue
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
49/87
1..< : State&ent
A %)2 statement is used to repeat a sequence of statements as long as a controlvaria#le is ?it+in t+e specified range of values. +e control varia#le must #e t+eidentifier of a local varia#le of t+e tpe '6 or J'6. +e definition of a loop ?it+%)2 includes t+e specification of an initial and an end value. "ot+ values must #et+e same tpe as t+e control varia#le.
+e %)2 statement e$ecutes as follo?s!
At t+e start of t+e loop, t+e control varia#le is set to t+e initial value initialassignment/ and eac+ time t+e loop iterates, it is incremented # t+e specifiedincrement positive increment/ or decremented negative increment/ until t+efinal value is reac+ed.
%ollo?ing eac+ run t+roug+ of t+e loop, t+e condition is c+eced final value
reac+ed/ to esta#lis+ ?+et+er or not it is satisfied. 'f t+e condition is satisfied,t+e sequence of statements is e$ecuted, ot+er?ise t+e loop and ?it+ it t+esequence of statements is sipped.
u#es
2ules for formulating %)2 statements
+e control varia#le ma onl #e of t+e data tpe '6 or J'6.
Fou can omit t+e statement "F incrementT. 'f no increment is specified, it isautomaticall assumed to #e O:.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
50/87
Sta tem en ts
1..? DIL( State&ent
+e 34'L( statement allo?s t+e repeated e$ecution of a sequence of statementscontrolled # an e$ecution condition. +e e$ecution condition is formed according tot+e rules for logical e$pressions.
+e 34'L( statement e$ecutes according to t+e follo?ing rules!
5rior to eac+ iteration of t+e loop #od, t+e e$ecution condition is evaluated.
+e loop #od follo?ing J) iterates as long as t+e e$ecution condition +as t+evalue 2B(.
)nce t+e value %ALS( occurs, t+e loop is sipped and t+e statement follo?ingt+e loop is e$ecuted.
1..7 (,(AT State&ent
A 2(5(A statement causes t+e repeated e$ecution of a sequence of statements#et?een 2(5(A and B6'L until a terminate condition occurs. +e terminatecondition is formed according to t+e rules for logical e$pressions.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
51/87
1.. C:NTINU( State&ent
A C)6'6B( statement is used to terminate t+e e$ecution of t+e current iteration ofa loop statement %)2, 34'L( or 2(5(A/.
+e C)6'6B( statement e$ecutes according to t+e follo?ing rules!
+is statement immediatel terminates e$ecution of a loop #od.
Jepending on ?+et+er t+e condition for repeating t+e loop is satisfied or nott+e #od is e$ecuted again or t+e iteration statement is e$ited and t+estatement immediatel follo?ing is e$ecuted.
'n a %)2 statement, t+e control varia#le is incremented # t+e specified
increment immediatel after a C)6'6B( statement.
(8a&p#e
FUNCTION_BLOCK CONTINUE_E,!V!R
IN)E, :INT +!RR! : !RR! D1. .100 OF INT +
EN)_V!R
BE/ININ)E, := 0 +WHILE IN)E, = 100 )O
IN)E, := IN)E, 133 I4 !RR!DIN)E,
+&( eual % IN)E,-
33 %9en !RR! DIN)E, &( n% #9an8e":IF !RR!DIN)E, = IN)E, THENCONTINUE +
EN)_IF +!RR!DIN)E, := 0 +
33 Fu$%9e$ (%a%e7en%(EN)_WHILE +
EN)_FUNCTION_BLOCK
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
52/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
53/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
54/87
(8a&p#e
FUNCTION BLOCK /OTO E,!V!R
IN)E, : INT! : INT +B : INT +C : INT +
I)WOR) : !RR!D1. .1 OF
EN)_V!RL!BEL
L!B1- L!B2- L!B +
EN)_L!BEL
BE/INIF ! J B THEN
/OTO L!B1 +ELSIF ! J C THEN
/OTO L!B2 +
EN)_IF + 33 . . .
L!B1: IN)E, := 1 +/OTO L!B +
L!B2: IN)E, := 2 +
33 . . .L!B:33 . . .
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
55/87
1..11 (TUN State&ent
A 2(B26 statement e$its t+e currentl active #loc )", %", %C/ and returns tot+e calling #loc or to t+e operating sstem, ?+en an )" is e$ited.
Note
A 2(B26 statement at t+e end of t+e code section of a logic #loc or t+edeclaration section of a data #loc is redundant since t+is is automaticalle$ecuted.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
56/87
13 Counters an% Ti&ers
13.1 Counters
13.1.1 Counter un$tions
S(5 7 provides a series of standard counter functions. Fou can use t+ese countersin our S7-SCL program ?it+out needing to declare t+em previousl. Fou must simplsuppl t+em ?it+ t+e required parameters. S(5 7 provides t+e follo?ing counterfunctions!
Counter 3un$tion (8p#anation
S[CB Count Bp
S[CJ Count Jo?n
S[CBJ Count Bp Jo?n
13.1. Ca##in' Counter un$tions
Counter functions are called lie functions. +e function identifier can t+erefore #eused an?+ere instead of an address in an e$pression as long as t+e tpe of t+efunction value is compati#le ?it+ t+at of t+e replaced address.
+e function value return value/ returned to t+e calling #loc is t+e current countvalue "CJ format/ in data tpe 3)2J.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
57/87
(8a&p#es
33E?a75le 4 an a;(lu%e #all:
S_CU) :=a#%Fla8+
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
58/87
13.1.3 Supp#in' ,ara&eters for Counter un$tions
+e follo?ing ta#le provides ou ?it+ an overvie? of t+e parameters for counterfunctions.
,ara&eter ata Tpe es$ription
C[6) C)B6(2'6
Counter num#er C)B6(2 'J(6'%'(2/K t+e area depends on t+eC5B
CJ "))L CJ input! Count do?n
CB "))L CB input! Count up
S "))L 'nput for presetting t+e counter 5@ 3)2J @alue in t+e range #et?een G and NNN for initiali8ing t+e counter
entered as :IVRvalue, ?it+ t+e value in "CJ format/
2 "))L 2eset input
^ "))L )utput! Status of t+e counter C@ 3)2J )utput! Count value #inar
2([@AL 3)2J 2esult in "CJ format
u#es
Since t+e parameter values for e$ample, CJ!Q'G.G/ are stored glo#all, it is notnecessar to specif t+em in certain situations. +e follo?ing general rules s+ould #eo#served ?+en suppling parameters ?it+ values!
+e parameter for t+e counter identifier C[6) must #e supplied ?+en t+e functionis called. 'nstead of t+e a#solute counter num#er for e$ample, C:;/, ou can alsospecif a varia#le or a constant ?it+ t+e '6 data tpe or an input parameter of t+eC)B6(2 data tpe in t+e call.
(it+er t+e parameter CB count up/ or t+e parameter CJ count do?n/ must #e
supplied.
+e parameters 5@ initiali8ation value/ and S set/ can #e omitted as a pair.
+e result value in "CJ format is al?as t+e function value.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
59/87
13.1./ Input an% (+a#uation of t>e Counter Ka#ue
o input t+e initiali8ation value or to evaluate t+e result of t+e function ou require
t+e internal representation of t+e count value. +e count value is of t+e data tpe3)2J in ?+ic+ #its G to :: contain t+e count value in "CJ code. "its :;-:= arenot used.
3+en ou set t+e counter, t+e value ou +ave specified is ?ritten to t+e counter.+e range of values is #et?een G and NNN. Fou can c+ange t+e count value ?it+int+is range # specifing t+e operations count upHdo?n S[CBJ/, count up S[CB/and count do?n S[CJ/.
Input
Jecimal as an integer value! %or e$ample, ;N=, assuming t+at t+is valuecorresponds to a valid "CJ format.
'n "CJ format input as a +e$adecimal constant/! for e$ample :IV:;7
(+a#uation
As a function result tpe 3)2J/! in "CJ format
As t+e output parameter C@ tpe 3)2J/! in #inar code
Coun te rs an T im ers
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
60/87
13.1.< Count Up SOCUJ
3it+ t+e count up S[CB/ function, ou can onl e$ecute incrementing counteroperations. +e ta#le illustrates +o? t+e counter ?ors.
:peration (8p#anation
Count up +e count value is increased # >:> if t+e signal status at input CB c+anges from>G> to >:> and t+e count value is less t+an NNN.
Set counter 3+en t+e signal state at input S c+anges from >G> to >:>, t+e counter is set ?it+t+e value of input ,K. Suc+ a signal c+ange is al?as required to set a counter.
2eset +e counter is reset ?+en input A Q : is set. 2esetting t+e counter sets t+e countvalue to >G>.
^uer counter A signal status quer at output returns >:> if t+e count value is greater t+an >G>.+e quer returns >G> if t+e count value is equal to >G>.
13.1.? Count on SOCJ
3it+ t+e count do?n S[CJ/ function, ou can onl e$ecute decrementing counteroperations. +e ta#le illustrates +o? t+e counter ?ors.
:peration (8p#anation
Count do?n +e value of t+e counter is decremented # >:> ?+en t+e signal state at inputC c+anges from >G> to >:> and t+e count value is greater t+an >G>.
Set counter 3+en t+e signal state at input S c+anges from >G> to >:>, t+e counter is set ?it+
t+e value of input ,K. Suc+ a signal c+ange is al?as required to set a counter.2eset +e counter is reset ?+en input A Q : is set. 2esetting t+e counter sets t+e count
value to >G>.
^uer counter A signal state quer at output Q produces >:> ?+en t+e count value is greatert+an >G>. +e quer returns >G> if t+e count value is equal to >G>.
S7-SCL @=.< for S7-
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
61/87
13.1.7 Count UpBon SOCUJ
3it+ t+e count upHdo?n S[CBJ/ function, ou can e$ecute #ot+ up and do?n counteroperations. 'f up and do?n count pulses are received simultaneousl, #ot+ operations aree$ecuted. +e count value remains unc+anged. +e ta#le illustrates +o? t+e counter ?ors.
:peration (8p#anation
Count up +e value of t+e counter is incremented # >:> ?+en t+e signal state at input CUc+anges from >G> to >:> and t+e count value is less t+an NNN.
Count do?n +e value of t+e counter is decremented # >:> ?+en t+e signal state at inputC c+anges from >G> to >:> and t+e count value is greater t+an >G>.
Set counter 3+en t+e signal state at input S c+anges from >G> to >:>, t+e counter is set ?it+t+e value of input ,K. Suc+ a signal c+ange is al?as required to set a counter.
2eset +e counter is reset ?+en input Q : is set. 2esetting t+e counter sets t+ecount value to >G>.
^uer counter A signal status quer at output ^ returns >:> if t+e count value is greater t+an>G>. +e quer returns >G> if t+e count value is equal to >G>.
13.1. (8a&p#e of Counter un$tions
,ara&eter Assi'n&ent
+e ta#le #elo? illustrates t+e parameter assignment for t+e function S[CJ.
,ara&eter es$ription
C[6) MCOUNTER:
CJ INUT I0.0
S SET
5@ INITI!LV!LUE 100P
2 RESET
^ >0.*
C@ BIN_V!LUE
Coun te rs an T im ers
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
62/87
(8a&p#e
FUNCTION_BLOCK COUNT
VAR_INPUT
MYCOUNTER : COUNTER ;
END_VAR
VAR_OUTPUT
RESULT : INT ;
END_VAR
VAR
SET : BOOL ;
RESET : BOOL ;
BCD_VALUE : WORD ; // Count value BCD coded
BIN_VALUE : WORD ; // Count value binary
INITIALVALUE: WORD ;
END_VAR
BEGINQ0.0 := 1 ;
SET := I0.2 ;
RESET := I0.3 ;
INITIALVALUE := 16#0089 ;
//Count down
BCD_VALUE := S_CD (C_NO := MYCOUNTER,
CD:= I0.0 ,
S := SET ,
PV:= INITIALVALUE,
R := RESET ,
CV:= BIN_VALUE ,
Q := Q0.7) ;//Further processing as output parameter
RESULT := WORD_TO_INT (BIN_VALUE) ;
QW4 := BCD_VALUE ;
END_FUNCTION_BLOCK
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
63/87
13. Ti&ers
13..1 Ti&er un$tions
imers are function elements in our program, t+at e$ecute and monitor time-controlled functions. S(5 7 provides a series of standard timer functions t+at ou canuse in our S7-SCL program.
Ti&er un$tion (8p#anation
S[5BLS( Start timer as pulse timer
S[5(] Start timer as e$tended pulse timer
S[)J Start timer as on-dela timer
S[)JS Start timer as retentive on-dela timer
S[)%%J Start timer as off-dela timer
13.. Ca##in' Ti&er un$tions
imer functions are called lie functions. +e function identifier can t+erefore #eused an?+ere instead of an address in an e$pression as long as t+e tpe of t+efunction result is compati#le ?it+ t+e first replaced address.
+e function value return value/ t+at is returned to t+e calling #loc is a time value oft+e data tpe S='9(.
A"so#ute or na&i$ Ca##
'n t+e call, ou can enter an a#solute value, for e$ample [6)!Q:G/ of t+e '9(2data tpe as t+e num#er of t+e timer function. Suc+ values can, +o?ever, no longer#e modified during runtime.
'nstead of t+e a#solute num#er, ou can also specif a varia#le or constant of t+e '6data tpe. +e advantage of t+is met+od is t+at t+e call can #e made dnamic #assigning t+e varia#le or constant a different num#er in eac+ call.
o ac+ieve a dnamic call, ou can also specif a varia#le of t+e '9(2 data tpe.
Coun te rs an T im ers
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
64/87
(8a&p#es
33E?a75le 4 an a;(lu%e #all:Cu$$T&7e: =S_O)T :=a#%Fla8+
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
65/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
66/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
67/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
68/87
13..< Start Ti&er as ,u#se Ti&er SO,ULS(J
+e ma$imum time for ?+ic+ t+e output signal remains set to >:> is t+e same as t+eprogrammed time value. 'f, during t+e run time of t+e timer, t+e signal state G appears att+e input, t+e timer is set to >G>.+is means a premature termination of t+e timer runtime.
Do t>e Ti&er un$tions+e ta#le s+o?s +o? t+e >pulse timer>function ?ors!
:peration (8p#anation
Start time +e >pulse timer> operation starts t+e specified time ?+en t+e signal state at t+estartinput SJ c+anges from >G> to >:>. o ena#le t+e timer, a signal c+ange is al?as
Specif runtime +e timer runs using t+e value at input TK until t+e programmed time e$pires and t+einput S Q : is set.
A#ort runtime 'f input S c+anges from >:> to >G> #efore t+e time +as e$pired, t+e timer is stopped.
2eset +e time is reset ?+en t+e reset input AJ c+anges from >G> to >:> ?+ile t+e timer is
running. 3it+ t+is c+ange, #ot+ t+e timer reading and t+e time #ase are reset to8ero. +e signal state >:> at input +as no effect if t+e timer is not running.
^uer signal status As long as t+e timer is running, a signal state quer follo?ing a >:> at output Qproduces t+e result >:>. 'f t+e timer is a#orted, a signal state quer at output Qproduces t+e result >G>.
^uer current timerreading
+e current timer reading can #e queried at output "' and using t+e function valueS[5BLS(.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
69/87
13..? Start Ti&er as (8ten%e% ,u#se Ti&er SO,(TJ
+e output signal remains set to >: > for t+e programmed time t/ regardless of +o? long t+einput signal remains set to >:>.riggering t+e start pulse again restarts t+e time so t+at t+eoutput pulse is e$tended retriggering/.
Do t>e Ti&er un$tions
+e ta#le s+o?s +o? t+e >e$tended pulse timer>function ?ors.!
:peration (8p#anation
Start time +e >e$tended pulse timer> S[5(]/ operation starts t+e specified time ?+ent+e signal state at t+e start input SJ c+anges from >G> to >:>. o ena#le t+e timer,a signal c+ange is al?as required.
2estart t+e countertime
'f t+e signal state at input S c+anges to >:> again ?+ile t+e timer is running, t+etimer is restarted ?it+ t+e specified time value.
'nitiali8e runtime +e timer runs ?it+ t+e value at input TK until t+e programmed time +as e$pired.2eset +e time is reset ?+en t+e reset input 2/ c+anges from >G> to >:> ?+ile t+e timer
is running. 3it+ t+is c+ange, #ot+ t+e timer reading and t+e time #ase are reset
to 8ero. +e signal state >:> at input +as no effect if t+e timer is not running.^uer signal status As long as t+e timer is running, a signal state quer follo?ing >:> at output Qproduces t+e result >:> regardless of t+e lengt+ of t+e input signal.
^uer current timerreading
+e current time value can #e queried at output !I and using t+e function valueS[5(].
Coun te rs an T im ers
)utput signale$tended pulsetimer/
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
70/87
13..7 Start Ti&er as :n-e#a Ti&er SO:TJ
+e output signal onl c+anges from >G> to >:> ?+en t+e programmed time +as e$pired andt+e input signal is still >:>.+is means t+at t+e output is activated follo?ing a dela. 'nputsignals t+at remain active for a time t+at is s+orter t+an t+e programmed time do not appearat t+e output.
Do t>e Ti&er un$tions
+e ta#le illustrates +o? t+e >on dela timer>function ?ors.
:peration (8p#anation
Start time +e >on dela timer> starts a specified time ?+en t+e signal state at t+e startinput SJ c+anges from >G> to >:>. o ena#le t+e timer, a signal c+ange is al?asrequired.
Stop timer 'f t+e signal state at input S c+anges from >:> to >G> ?+ile t+e timer is running, it isstopped.
Specif runtime +e timer continues to run ?it+ t+e value at input TK as long as t+e signal stateat input S Q : is set.
2eset +e time is reset ?+en t+e reset input AJ c+anges from >G> to >:> ?+ile t+e timeris running. 3it+ t+is c+ange, #ot+ t+e timer reading and t+e time #ase are resetto 8ero. +e time is also reset ?+en A Q : is set alt+oug+ t+e timer is not running.
^uer signal status A signal state quer follo?ing >:> at output Q produces >:> ?+en t+e time e$pired?it+out an error occurring and input S is still set to >:>.'f t+e timer ?as stopped, a signal status quer follo?ing >:> al?as produces >G>.A signal state quer after >:> at output Q also produces >G> ?+en t+e timer is notrunning and t+e signal state at input S is still >:>.
^uer current timerreading
+e current time value can #e queried at output !I and using t+e function valueS[)J.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
71/87
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
72/87
13..H Start Ti&er as :ff-e#a Ti&er SO:TJ
3it+ a signal state c+ange from >G> to >:> at start input S, a >:> is set at output ^. 'f t+e startinput c+anges from >:> to >G>,t+e timer is started. +e output onl returns to signal status >G>after t+e time +as e$pired. +e output is t+erefore deactivated follo?ing a dela.
Do t>e Ti&er un$tions
+e ta#le s+o?s +o? t+e >off dela timer>function ?ors.
:peration (8p#anation
Start time +e >off dela timer> operation starts t+e specified time ?+en t+e signal state att+e start input SJ c+anges from >:> to >G>. A signal c+ange is al?as required toena#le t+e timer.
2estart timer +e timer is restarted ?+en t+e signal state at input S c+anges from >:> to >G>again for e$ample follo?ing a reset/.
Specif runtime +e timer runs ?it+ t+e value specified at input TK.
2eset 'f t+e reset input A c+anges from >G> to >:> ?+ile t+e timer is running, t+e timer isreset.
^uer signal status A signal state quer follo?ing >:> at output Q produces >:> if t+e signal state atinput S Q : is set or t+e timer is running.
^uer current timerreading
+e current time value can #e queried at output !I and using t+e function valueS[)%%J.
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
73/87
13..10 (8a&p#e of Ti&er un$tions
FUNCTION_BLOCK TIMERVAR_INPUT
mytime : TIMER ;
END_VAR
VAR_OUTPUT
result : S5TIME ;
END_VAR
VAR
set : BOOL ;
reset : BOOL ;
bcdvalue : S5TIME ;//Time base and time remaining
//in BCD
binvalue : WORD ; /Time value in binary
initialvalue : S5TIME ;
END_VAR
BEGIN
Q0.0 := 1;
set := I0.0 ;
reset := I0.1;
initialvalue := T#25S ;
bcdvalue := S_PEXT (T_NO := mytime ,
S := set ,
TV := initialvalue ,
R := reset ,
BI := binvalue ,
Q := Q0.7) ;//Further processing as output parameter
result := bcdvalue ;
//To output for display
QW4 := binvalue ;
END_FUNCTION_BLOCK
Coun te rs an T im ers
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
74/87
1/ Stan%ar% un$tions of S7-SCL
1/.1 ata Tpe Con+ersion un$tions
1/.1.1 Con+ertin' ata Tpes
'f ou use t?o addresses in a logic operation, ou must mae sure t+at t+e datatpes of t+e addresses are compati#le. 'f t+e addresses are of different data tpes, adata tpe conversion is necessar. +e follo?ing data tpe conversions are possi#lein S7-SCL!
'mplicit Jata pe Conversion
+e data tpes are grouped in classes. 'f t+e addresses fall ?it+in t+e sameclass, S7-SCL maes an implicit data tpe conversion. +e functions used #t+e compiler are grouped in >Conversion %unctions Class A>.
($plicit Jata pe Conversion
'f t+e addresses do not #elong to t+e same class, ou must start a conversionfunction ourself. o allo? e$plicit data tpe conversion, S7-SCL providesnumerous standard functions grouped in t+e follo?ing classes!
Conversion %unctions Class "
%unctions for 2ounding and runcating
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
75/87
1/.1. I&p#i$it ata Tpe Con+ersion
3it+in t+e classes of data tpes defined in t+e ta#le, t+e compiler maes an implicit datatpe conversion in t+e order s+o?n. +e common format of t?o addresses is al?as t+e
larger of t+e t?o data tpes defined.- t+e common format of "F( and 3)2J, fore$ample, is '6(\(2 3)2J.
2emem#er t+at a data tpe conversion in t+e A6F["' class results in leading #its #eingset to G.
C#asses Con+ersion :r%er
A6F["' "))L "F( 3)2J J3)2J
A6F[6B9 '6 J'6 2(AL
(8a&p#e of I&p#i$it ata Tpe Con+ersionV!R
I)_CTRLLER_1 : BTE +I)_CTRLLER_2 : WOR) +EN)_V!R
BE/INIF
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
76/87
1/.1.3 Stan%ar% un$tions for (8p#i$it ata Tpe Con+ersion
Fou ?ill find a general description of t+e function call in >Calling %unctions>.
2emem#er t+e follo?ing points ?+en calling t+e conversion functions!
'nput parameters!
(ac+ function for converting a data tpe +as e$actl one input parameter ?it+ t+ename '6. Since it is a function ?it+ onl one parameter, t+is does not need to #especified.
%unction value +e result is al?as t+e
function value.
6ames of t+e functions
+e data tpes of t+e input parameter and t+e function value can clearl #erecogni8ed in t+e function name in t+e overvie? of classes A and ". %ore$ample, for t+e function "))L[)["F(, t+e data tpe of t+e input parameter
is "))L and t+e data tpe of t+e function value "F(.
1/.1 .3.1 Con+ersion un$tions C#ass !
+e ta#le s+o?s t+e data tpe conversion functions of Class ". +ese functions must#e specified e$plicitl. +e result can also #e undefined if t+e destination tpe is notlarge enoug+.
Fou can c+ec for t+is situation ourself # including a limit c+ec or ou can +ave t+esstem mae t+e c+ec # selecting t+e >)E flag >option prior to compilation. 'nsituations ?+ere t+e result is undefined, t+e sstem sets t+e )E flag to %ALS(.
un$tion Na&e Con+ersion u#e :F
"))L[)['6 3)2J[)[' 6"))L[)[3)2J$// 6
"))L[)[J'6 J3)2J[)[J'6"))L[)[J3)2J$// 6
"F([)["))L Copies t+e least significant #it F
"F([)[C4A2 Copies t+e #it string 6
"F([)['6 3)2J[)['6"F([)[3)2J$// 6
"F([)[J'6 J3)2J[)[J'6"F([)[J3)2J$// 6
C4A2[)["F( Copies t+e #it string 6
C4A2[)['6 +e #it string in t+e input parameter is entered in t+e lo?er-order
#te of t+e function value. +e +ig+er-order #te is padded ?it+8eros.
6
JA([)[J'6 Copies t+e #it string 6
J'6[)[JA( Copies t+e #it string F
J'6[)[J3)2J Copies t+e #it string 6
J'6[)['6 Copies t+e #it for t+e sign. +e value in t+e input parameter isinterpreted in t+e data tpe '6. 'f t+e value is less t+an-
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
77/87
un$tion Na&e Con+ersion u#e :F
J'6[)[3)2J J3)2J[)[3)2JJ'6[)[J3)2J$//
J3)2J[)["))L Copies t+e least significant #it F
J3)2J[)["F( Copies t+e b least significant #its FJ3)2J[)[J'6 Copies t+e #it string 6
J3)2J[)[2(AL Copies t+e #it string 6
J3)2J[)[3)2J Copies t+e :I least significant #its F
J3)2J[)['6 J'6[)['6 J3)2J[) J'6$// F
'6[)[C4A2 Copies t+e #it string F
'6[)[3)2J Copies t+e #it string 6
'6[)["))L 3)2J[)["))L'6[)[3)2J$// F
'6[)["F( 3)2J[)["F('6[)[3)2J$// F
'6[)[J3)2J 3)2J[)[J3)2J'6[)[3)2J$// 6
'6[)[S2'6\ '[S26\$/ F2(AL[)[J'6 2ounds t+e '((( 2(AL value to J'6.
'f t+e value is less t+an -;[:D7[Db
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
78/87
un$tion Na&e Con+ersion u#e :F
"CJ[)[J'6$/J3)2J["CJ[)[J'6$/
+e e$pression $ is of t+e tpe J3)2J and is assumed to #e a"CJ-coded value #et?een -NNNNNNN and ONNNNNNN. After
conversion t+e result is availa#le as an integer #inar form/ of t+etpe J'6.'f an error occurs during conversion, t+e programma#le controllerc+anges to t+e S)5 state. +e cause of t+e stop can #e edited int+e )" :;:.
6
J'6[)["CJ$/J'6 [) ["CJ [J3)2J$/
+e e$pression $ is of t+e tpe J'6 and is assumed to #e aninteger ?it+ a value #et?een NNNNNNN and ONNNNNNN . Afterconversion t+e result is availa#le as a "CJ-coded num#er of t+etpe J3)2J.+e result is undefined outside t+e value range. 'f t+e >Set )E flag>option is activated, t+e )E flag +as t+e value WfalseW.
F
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
79/87
1/.1 .3. un$tions for oun%in' an% Trun$atin'
+e functions for rounding and truncating num#ers also #elong to t+e data tpeconversion functions. +e ta#le s+o?s t+e names, data tpes for t+e input
parameters and t+e function value/ and tass of t+ese functions!
un$tion Na&e ata Tpe Input,ara&eter
ata Tpe3un$tion Ka#ue
Tas;
2)B6J 2(AL J'6 2ounds forming a J'6 num#er/'n compliance ?it+ J'6 (6 I::
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
80/87
1/. Nu&eri$ Stan%ar% un$tions
1/..1 @enera# Arit>&eti$ Stan%ar% un$tions
+ese are t+e functions for calculating t+e a#solute value, t+e square or t+e squareroot of a value.
+e data tpe A6F[6B9 stands for '6, J'6 or 2(AL. 6ote t+at inputparameters of t+e '6 or J'6 tpe are converted internall to 2(AL varia#les ift+e function value is of t+e 2(AL tpe.
un$tion Na&e ata Tpe Input,ara&eter
ata Tpeun$tion Ka#ue
es$ription
A"S A6F[6B9 A6F[6B9 6um#er
S^2 A6F[6B9 2(AL Square
S^2 A6F[6B9 2(AL Square 2oot
1/.. Lo'arit>&i$ un$tions
+ese are functions for calculating an e$ponential value or a logarit+m of a value.
+e data tpe A6F[6B9 stands for '6, J'6 or 2(AL. 6ote t+at input
parameters of t+e tpe A6F[6B9 are converted internall into real varia#les.
un$tion Na&e ata Tpe Input,ara&eter
ata Tpeun$tion Ka#ue
es$ription
(]5 A6F[6B9 2(AL e to t+e po?er '6
(]5J A6F[6B9 2(AL :G to t+e po?er '6
L6 A6F[6B9 2(AL 6aturallogarit+m
L)\ A6F[6B9 2(AL Commonlogarit+m
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
81/87
1/..3 Tri'ono&etri$ un$tions
+e trigonometric functions represented in t+e ta#le calculate values of angles inradians.
+e data tpe A6F[6B9 stands for '6, J'6 or 2(AL. 6ote t+at input parametersof t+e tpe A6F[6B9 are converted internall into real varia#les.
un$tion Na&e ata T-pe Input ,ara&eter ata Tpe un$tionKa#ue
es$ription
AC)S A6F[6B9 2(AL Arc cosine
AS'6 A6F[6B9 2(AL Arc sine
AA6 A6F[6B9 2(AL Arc tangent
C)S A6F[6B9 2(AL Cosine
S'6 A6F[6B9 2(AL Sine
A6 A6F[6B9 2(AL angent
Note
Fou also +ave t+e option of using furt+er '(C functions for data tpe conversion. %orinformation a#out t+e functions, refer to t+e S(5 7 reference manual >Sstem andStandard %unctions for S7-.
1/../ (8a&p#es of Nu&eri$ Stan%ar% un$tions
Ca## (SULT
2(SBL !Q A"S -=/ K HH=
2(SBL !Q S^2 b:.G/K HHN
2(SBL !Q S^2 ;
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
82/87
1/./ un$tions for ,ro$essin' Strin's
1/./.1 un$tions for Strin' Manipu#at ion
L( N
+e L(6 function %C;:/ returns t+e current lengt+ of a string num#er of validc+aracters/. An empt string WW/ +as 8ero lengt+. +e function does not report errors.
E?a75le LEN
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
83/87
L(T or I@DT
+e L(% and 2'\4 functions %C;G and %C
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
84/87
INS(T
+e '6S(2 function %C:7/ inserts t+e c+aracter string at parameter '6; into t+estring at parameter '6: after t+e c+aracter identified # 5. 'f 5 equals 8ero, t+e second
string is inserted #efore t+e first string. 'f 5 is +ig+er t+an t+e current lengt+ of t+e firststring, t+e second string is appended to t+e first string. 'f 5 is negative, an empt stringis output. 3+en t+e resulting string is longer t+an t+e varia#le specified at t+e outputparameterK t+e resulting string is limited to t+e configured ma$imum lengt+.
E?a75le INSERT
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
85/87
(,LAC(
+e 2(5LAC( function %C
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
86/87
1/./.3 un$tions for Con+ertin' t>e ata or&at
INTOT:OSTIN@ an% STIN@OT:OINT+e functions '6[)[S2'6\ and S2'6\[)[' 6 convert a varia#le in t+e '6format into a c+aracter string or a string into an '6 varia#le. %unctions '[S26\ %C:I/and S26\[' %C'(C-functions>li#rar. +estring is represented ?it+ a leading sign. 'f t+e varia#le specified at t+e return parameteris too s+ort, no conversion is made.
,ara&eter e$#aration
%ata tpe Me&or Area es$ription
INTOT:OSTIN@
' '65B '6 ', , 9, J, L, const. 'nput value2eturn value S2'6\ J, L 2esulting string
STIN@OT:OINTS '65B S2'6\ J, L 'nput string
2eturn value '6 ', , 9, J, L 2esult
INTOT:OSTIN@ an% STI N@OT:OI NT
+e functions J'6[)[S2'6\ and S2'6\[)[J'6 convert a varia#le in t+e J'6format into a c+aracter string or a string into a J'6 varia#le. %unctions J'[S26\ %C=/and S26\[J' %C'(C-functions>li#rar. +estring is represented ?it+ a leading sign. 'f t+e varia#le specified at t+e return parameteris too s+ort, no conversion is made.
,ara&eter e$#aration
%atatpe
Me&or Area es$ription
INTOT:OSTIN@
' '65B J'6 ', ^, 9, J, L,const.
'nput value
2eturn value S2'6\ J, L 2esulting string
STIN@OT:OINTS '65B S2'6\ J, L 'nput string
2eturn value J'6 ', , 9, J, L 2esult
Stana r !unc t ions o f S7 -SCL
-
8/9/2019 Editando_s7-Scl for s7-300 and s7-400 - Manual
87/87
(ALOT:OSTIN@ an% STIN@OT:O(AL
+e functions 2(AL[)[S2'6\ and S2'6\[)[2(AL convert a varia#le in t+e2(AL format into a c+aracter string or a string into a 2(AL varia#le. %unctions
2[S26\ %C