Logix 5000 Controllers Structured Text - Rockwell Automation

Transcription

Logix 5000 ControllersStructured Text1756 ControlLogix , 1756 GuardLogix , 1769CompactLogix , 1769 Compact GuardLogix , 1789SoftLogix , 5069 CompactLogix , 5069 CompactGuardLogix , Studio 5000 Logix Emulate Rockwell Automation Publication 1756-PM007J-EN-P - March 2022Supersedes Publication 1756-PM007I-EN-P - September 2020Programming ManualOriginal Instructions

Logix 5000 Controllers Structured TextImportant User InformationRead this document and the documents listed in the additional resources section about installation, configuration, andoperation of this equipment before you install, configure, operate, or maintain this product. Users are required to familiarizethemselves with installation and wiring instructions in addition to requirements of all applicable codes, laws, and standards.Activities including installation, adjustments, putting into service, use, assembly, disassembly, and maintenance are required tobe carried out by suitably trained personnel in accordance with applicable code of practice.If this equipment is used in a manner not specified by the manufacturer, the protection provided by the equipment may beimpaired.In no event will Rockwell Automation, Inc. be responsible or liable for indirect or consequential damages resulting from the useor application of this equipment.The examples and diagrams in this manual are included solely for illustrative purposes. Because of the many variables andrequirements associated with any particular installation, Rockwell Automation, Inc. cannot assume responsibility or liability foractual use based on the examples and diagrams.No patent liability is assumed by Rockwell Automation, Inc. with respect to use of information, circuits, equipment, or softwaredescribed in this manual.Reproduction of the contents of this manual, in whole or in part, without written permission of Rockwell Automation, Inc., isprohibited.Throughout this manual, when necessary, we use notes to make you aware of safety considerations.WARNING: Identifies information about practices or circumstances that can cause an explosion in a hazardous environment, which may lead topersonal injury or death, property damage, or economic loss.ATTENTION: Identifies information about practices or circumstances that can lead to personal injury or death, property damage, or economic loss.Attentions help you identify a hazard, avoid a hazard, and recognize the consequence.IMPORTANT Identifies information that is critical for successful application and understanding of the product.Labels may also be on or inside the equipment to provide specific precautions.SHOCK HAZARD: Labels may be on or inside the equipment, for example, a drive or motor, to alert people that dangerous voltage may be present.BURN HAZARD: Labels may be on or inside the equipment, for example, a drive or motor, to alert people that surfaces may reach dangeroustemperatures.ARC FLASH HAZARD: Labels may be on or inside the equipment, for example, a motor control center, to alert people to potential Arc Flash. Arc Flash willcause severe injury or death. Wear proper Personal Protective Equipment (PPE). Follow ALL Regulatory requirements for safe work practices and forPersonal Protective Equipment (PPE).Rockwell Automation recognizes that some of the terms that are currently used in our industry and in this publication are not in alignment with the movementtoward inclusive language in technology. We are proactively collaborating with industry peers to find alternatives to such terms and making changes to our productsand content. Please excuse the use of such terms in our content while we implement these changes.2Rockwell Automation Publication 1756-PM007J-EN-P - March 2022

Summary of changesThis manual includes new and updated information. Use these referencetables to locate changed information.Grammatical and editorial style changes are not included in this summary.Global changesThis table identifies changes that apply to all information about a subject inthe manual and the reason for the change. For example, the addition of newsupported hardware, a software design change, or additional referencematerial would result in changes to all of the topics that deal with that subject.ChangeTopicNew Studio 5000 Logix Designer application brandingStudio 5000 environment on page 7New or enhanced featuresNone in this release.Rockwell Automation Publication 1756-PM007J-EN-P - March 20223

Table of ContentsSummary of changesPrefaceStudio 5000 environment . 7Additional resources . 7Legal Notices . 8Chapter 1Program Structured TextStructured Text Syntax. 11Structured Text Components: Assignments. 12Specify a non-retentive assignment . 13Assign an ASCII character to a string data member . 14Character string literals. 14Structured Text Components: Expressions . 15Use arithmetic operators and functions . 16Use relational operators . 18Use logical operators . 19Use bitwise operators . 20Determine the order of execution . 20Structured Text Components: Instructions. 21Structured Text Components: Constructs . 22IF THEN .23CASE OF. 26FOR DO . 28WHILE DO . 30REPEAT UNTIL .32Structured Text Components: Comments . 35IndexRockwell Automation Publication 1756-PM007J-EN-P - March 20225

PrefaceThis manual shows how to program Logix 5000 controllers with structuredtext programming language.This manual is one of a set of related manuals that show common proceduresfor programming and operating Logix 5000 controllers.For a complete list of common procedures manuals, refer to the Logix 5000 Controllers Common Procedures Programming Manual, publication1756-PM001.The term Logix 5000 controller refers to any controller based on the Logix5000 operating system.Studio 5000 environmentThe Studio 5000 Automation Engineering & Design Environment combinesengineering and design elements into a common environment. The firstelement is the Studio 5000 Logix Designer application. The Logix Designerapplication is the rebranding of RSLogix 5000 software and will continue tobe the product to program Logix 5000 controllers for discrete, process,batch, motion, safety, and drive-based solutions.The Studio 5000 environment is the foundation for the future ofRockwell Automation engineering design tools and capabilities. The Studio5000 environment is the one place for design engineers to develop allelements of their control system.Additional resourcesThese documents contain additional information concerning relatedRockwell Automation products.ResourceDescriptionLogix 5000 Controllers Program ParametersProgramming Manual publication 1756-PM021Describes how to use program parameters whenprogramming Logix 5000 controllers.Logix 5000 Controllers General InstructionsReference Manual, publication 1756-RM003Describes the available instructions for a Logix5000 controller.Rockwell Automation Publication 1756-PM007J-EN-P - March 20227

PrefaceResourceDescriptionLogix 5000 Controllers Process and DrivesInstructions Reference Manual, publication1756-RM006Describes how to program a Logix 5000 controllerfor process or drives applications.Logix 5000 Controllers Motion Instruction SetReference Manual, publication MOTION-RM002Describes how to program a Logix 5000 controllerfor motion applications.Product Certifications website,http://ab.rockwellautomation.comProvides declarations of conformity, certificates, andother certification details.You can view or download publications athttp://www.rockwellautomation.com/literature. To order paper copies oftechnical documentation, contact your local Rockwell Automation distributoror sales representative.Legal NoticesRockwell Automation publishes legal notices, such as privacy policies, licenseagreements, trademark disclosures, and other terms and conditions on theLegal Notices page of the Rockwell Automation website.End User License Agreement (EULA)You can view the Rockwell Automation End User License Agreement (EULA)by opening the license.rtf file located in your product's install folder on yourhard drive.The default location of this file is:C:\Program Files (x86)\Common Files\Rockwell\license.rtf.Open Source Software LicensesThe software included in this product contains copyrighted software that islicensed under one or more open source licenses.You can view a full list of all open source software used in this product andtheir corresponding licenses by opening the index.html file located yourproduct's OPENSOURCE folder on your hard drive.The default location of this file is:C:\Program Files (x86)\Common Files\Rockwell\Help\ productname \Release Notes\OPENSOURCE\index.htmYou may obtain Corresponding Source code for open source packagesincluded in this product from their respective project web site(s).Alternatively, you may obtain complete Corresponding Source code bycontacting Rockwell Automation via the Contact form on the RockwellAutomation website:8Rockwell Automation Publication 1756-PM007J-EN-P - March 2022

out-us/contact/contact.page.Please include "Open Source" as part of the request text.Rockwell Automation recognizes that some of the terms that are currentlyused in our industry and in this publication are not in alignment with themovement toward inclusive language in technology. We are proactivelycollaborating with industry peers to find alternatives to such terms andmaking changes to our products and content. Please excuse the use of suchterms in our content while we implement these changes.Rockwell Automation Publication 1756-PM007J-EN-P - March 20229

Chapter 1Program Structured TextStructured Text SyntaxStructured text is a textual programming language that uses statements todefine what to execute. Structured text is not case sensitive. Use tabs and carriage returns (separate lines) to make your structuredtext easier to read. They have no effect on the execution of thestructured text.Structured text is not case sensitive. Structured text can contain e an assignment statement to assign values to tags. The : operator is theassignment operator.Terminate the assignment with a semi colon ‘;.’tag : expression;ExpressionAn expression is part of a complete assignment or construct statement. Anexpression evaluates to a number (numerical expression), a String (stringexpression), or to a true or false state (BOOL expression)Tag ExpressionA named area of the memory where data is stored (BOOL, SINT, INT, DINT, REAL,String).value1ImmediateExpressionA constant value4OperatorsExpressionA symbol or mnemonic that specifies an operation within an expression.FunctionExpressionWhen executed, a function yields one value. Use parentheses to contain theoperand of a function.Even though their syntax is similar, functions differ from instructions in thatfunctions can be used only in expressions. Instructions cannot be used inexpressions.tag1 tag2tag1 value1function(tag1)InstructionAn instruction is a standalone statement.An instruction uses parentheses to contain its operands.Depending on the instruction, there can be zero, one, or multiple operands.When executed, an instruction yields one or more values that are part of a datastructure. Terminate the instruction with a semi colon(;).Even though their syntax is similar, instructions differ from functions in thatinstructions cannot be used in expressions. Functions can be used only inexpressions.A conditional statement used to trigger structured text code (that is, otherstatements). Terminate the construct with a semi colon (;).ConstructRockwell Automation Publication 1756-PM007J-EN-P - March (operand1, operand2,operand3);IF.THEN CASE FOR.DO WHILE.DOREPEAT.UNTILEXIT11

Chapter 1Program Structured TextTermDefinitionExamplesCommentText that explains or clarifies what a section of structured text does.Use comments to make it easier to interpret the structured text.Comments do not affect the execution of the structured text.Comments can appear anywhere in structured text.//comment(*start of comment . . . end of comment*)/*start of comment . . . end of comment*/See alsoStructured Text Components: Assignments on page 12Structured Text Components: Expressions on page 15Structured Text Components: Instructions on page 21Structured Text Components: Constructs on page 22Structured Text Components: Comments on page 35Structured TextComponents: AssignmentsUse an assignment to change the value stored within a tag. An assignmenthas this syntax:tag : expression;where:ComponentDescriptionTagRepresents the tag that is getting the new value; the tag must be a BOOL, SINT, INT, DINT,STRING, or REAL.Tip: The STRING tag is applicable to CompactLogix 5380, CompactLogix 5480,ControlLogix 5580, Compact GuardLogix 5380, and GuardLogix 5580 controllers only.: Is the assignment symbolExpressionRepresents the new value to assign to the tag;If tag is this data typeUse this type of pactLogix 5380,CompactLogix 5480, ControlLogix 5580, Compact GuardLogix 5380,and GuardLogix 5580 controllersonly).String type, including string tag and string literal(CompactLogix 5380, CompactLogix 5480,ControlLogix 5580, Compact GuardLogix 5380,and GuardLogix 5580 controllers only).Ends the assignmentThe tag retains the assigned value until another assignment changes thevalue.The expression can be simple, such as an immediate value or another tagname, or the expression can be complex and include several operators andfunctions, or both. Refer to Expressions for more information.12Rockwell Automation Publication 1756-PM007J-EN-P - March 2022

Chapter 1Program Structured TextTip: I/O module data updates asynchronously to the execution of logic. If you reference an inputmultiple times in your logic, the input could change state between separate references. If you needthe input to have the same state for each reference, buffer the input value and reference that buffertag. For more information, see Logix 5000 Controllers Common Procedures, publication 1756-PM001.You can also use Input and Output program parameters which automatically buffer the data duringthe Logix Designer application execution. See Logix 5000 Controllers Program ParametersProgramming Manual, publication 1756-PM021.See alsoAssign an ASCII character to a string data member on page 14Specify a non-retentive assignment on page 13Structured Text Components: Expressions on page 15Character string literals on page 14Specify a non-retentiveassignmentThe non-retentive assignment is different from the regular assignmentdescribed above in that the tag in a non-retentive assignment is reset to zeroeach time the controller: Enters the Run mode Leaves the step of an SFC if you configure the SFC for Automatic reset.This applies only if you embed the assignment in the action of the stepor use the action to call a structured text routine by using a JSRinstruction.A non-retentive assignment has this syntax:tag [: ] expression ;where:ComponentDescriptiontagRepresents the tag that is getting the new value; the tag must be a BOOL, SINT,INT, DINT, STRING, or REAL.Tip: The STRING tag is applicable to CompactLogix 5380, CompactLogix 5480, ControlLogix 5580, Compact GuardLogix 5380, and GuardLogix 5580controllers only.[: ]Is the non-retentive assignment symbol.expressionRepresents the new value to assign to the tag.If tag is this data typeUse this type of expressionBOOLBOOLSINTNumericINTDINTREALRockwell Automation Publication 1756-PM007J-EN-P - March 202213

Chapter 1Program Structured TextComponentDescriptionSTRING(CompactLogix 5380,CompactLogix 5480, ControlLogix 5580, Compact GuardLogix 5380,and GuardLogix 5580 controllersonly).String type, including string tag andstring literalCompactLogix 5380, CompactLogix 5480, ControlLogix 5580, CompactGuardLogix 5380, and GuardLogix 5580 controllers( only)See alsoAssign an ASCII character to a string data member on page 14Structured Text Components: Assignments on page 12Assign an ASCII characterto a string data memberAssign an ASCII character to a string data memberUse the assignment operator to assign an ASCII character to an element ofthe DATA member of a string tag. To assign a character, specify the value ofthe character or specify the tag name, DATA member, and element of thecharacter. For example:This is OKThis is not OKstring1.DATA[0] : 65;string1.DATA[0] : A;string1.DATA[0]: string2.DATA[0];string1 : string2;Tip: This assigns all content of string2 to string1instead of just one character.To add or insert a string of characters to a string tag, use either of these ASCIIstring instructions:ToUse this instructionAdd characters to the end of a stringCONCATInsert characters into a stringINSERTSee alsoStructured Text Components: Expressions on page 15Character string literals on page 14Character string literals14Character string literals include single byte or double byte encodedcharacters. A single-byte string literal is a sequence of zero or more charactersthat are prefixed and terminated by the single quote character ('). In singlebyte character strings, the three-character combination of the dollar sign ( )followed by two hexadecimal digits is interpreted as the hexadecimalrepresentation of the eight-bit character code as shown in the following table.Rockwell Automation Publication 1756-PM007J-EN-P - March 2022

Chapter 1Program Structured TextTip: Character string literals are only applicable to the CompactLogix 5380, CompactLogix 5480,ControlLogix 5580, Compact GuardLogix 5380, and GuardLogix 5580 controllers.Studio 5000 only supports single byte characters.Character string literalsNo.DescriptionExample1aEmpty string (length zero)''1bString of length one or character CHAR containing a single character'A'1cString of length one or character CHAR containing the “space”character''1dString of length one or character CHAR containing the “single quote”character' ''1eString of length one or character CHAR containing the “double quote”character'"'1f1gSupport of two character combinationsSupport of a character representation with ‘ ’ and two hexadecimalcharacters' R L'' 0A'Two-character combinations in character stringsNo.DescriptionExample1Dollar sign 2Single quote '3Line feed L or I4Newline N or n5Form feed (page) P or p6Carriage return R or r7Tabulator T or tTip: The newline character provides an implementation-independent means of defining the end of aline of data for both physical and file I/O; for printing, the effect is that of ending a line of data andresuming printing at the beginning of the next line.The ' combination is only valid inside single quoted string literals.See alsoStructured Text Components: Assignments on page 12Structured TextComponents: ExpressionsAn expression is a tag name, equation, or comparison. To write an expression,use any of the following: Tag name that stores the value (variable) Number that you enter directly into the expression (immediate value) String literal that you enter directly into the expression(CompactLogix 5380, CompactLogix 5480, ControlLogix 5580,Compact GuardLogix 5380, and GuardLogix 5580 controllers only) Functions, such as: ABS, TRUNC Operators, such as: , -, , , And, OrFollow these guidelines for writing expressions:Rockwell Automation Publication 1756-PM007J-EN-P - March 202215

Chapter 1Program Structured Text Use any combination of upper-case and lower-case letter. For example,these variations of "AND" are acceptable: AND, And, and. For more complex requirements, use parentheses to group expressionswithin expressions. This makes the whole expression easier to read,and ensures that the expression executes in the desired sequence.Use these expressions for structured text:BOOL expression: An expression that produces the BOOL value of 1 (true) or 0(false). A bool expression uses bool tags, relational operators, and logicaloperators to compare values or check if conditions are true or false. Forexample, tag1 65. A simple bool expression can be a single BOOL tag. Typically, use bool expressions to condition the execution of otherlogic.Numeric expression: An expression that calculates an integer orfloating-point value. A numeric expression uses arithmetic operators, arithmetic functions,and bitwise operators. For example, tag1 5. Nest a numeric expression within a BOOL expression. For example,(tag1 5) 65.String expression: An expression that represents a string A simple expression can be a string literal or a string tagUse this table to select the operators for expressions.IfUseCalculating an arithmetic valueArithmetic operators and functionsComparing two values or stringsVerifying if conditions are true or falseRelational operatorsLogical operatorsComparing the bits within valuesBitwise operatorsSee alsoUse arithmetic operators and functions on page 16Use relational operators on page 17Use logical operators on page 19Use bitwise operators on page 20Use arithmetic operatorsand functions16Combine multiple operators and functions in arithmetic expressions.Operators calculate new values.ToUse this operatorOptimal data typeAdd DINT, REALRockwell Automation Publication 1756-PM007J-EN-P - March 2022

Chapter 1Program Structured TextToUse this operatorOptimal data typeSubtract/negate-DINT, REALMultiply*DINT, REALExponent (x to the power of y)**DINT, REALDivide/DINT, REALModulo-divideMODDINT, REALFunctions perform math operations. Specify a constant, a non-Boolean tag, oran expression for the function.ForUse this functionOptimal data typeAbsolute valueABS (numeric expression)DINT, REALArc cosineACOS (numeric expression)REALArc sineASIN (numeric expression)REALArc tangentATAN (numeric expression)REALCosineCOS (numeric expression)REALRadians to degreesDEG (numeric expression)DINT, REALNatural logLog base 10LN (numeric expression)LOG (numeric expression)REALREALDegrees to radiansRAD (numeric expression)DINT, REALSineSIN (numeric expression)REALSquare rootSQRT (numeric expression)DINT, REALTangentTAN (numeric expression)REALTruncateTRUNC (numeric expression)DINT, REALThe table provides examples for using arithmetic operators and functions.Use this formatExampleFor this situationWritevalue1 operator value2If gain 4 and gain 4 adj are DINT tags and your specification says:‘Add 15 to gain 4 and store the result in gain 4 adj’"gain 4 adj : gain 4 15;operator value1If alarm and high alarm are DINT tags and your specification says:‘Negate high alarm and store the result in alarm.’alarm: -high alarm;function(numeric expression)If overtravel and overtravel POS are DINT tags and yourspecification says: ‘Calculate the absolute value of overtravel andstore the result in overtravel POS.’overtravel POS : ABS(overtravel);value1 operator (function((value2 value3)/2)If adjustment and position are DINT tags and sensor1 and sensor2are REAL tags and your specification says: ‘Find the absolute valueof the average of sensor1 and sensor2, add the adjustment, andstore the result in position.’position : adjustment ABS((sensor1 sensor2)/2);See alsoStructured Text Components: Expressions on page 15Rockwell Automation Publication 1756-PM007J-EN-P - March 202217

Chapter 1Program Structured TextUse relational operatorsRelational operators compare two values or strings to provide a true or falseresult. The result of a relational operation is a BOOL value.If the comparison isThe result isTrue1False0Use these relational operators.For this comparisonUse this operatorOptimal data typeEqual DINT, REAL, String typeLess than DINT, REAL, String typeLess than or equal DINT, REAL, String typeGreater than DINT, REAL, String typeGreater than or equalNot equal DINT, REAL, String typeDINT, REAL, String typeThe table provides examples of using relational operatorsUse this formatExampleFor this situationIf temp is a DINT tag and your specification says: ‘Iftemp is less than 100 then ’value1 operator value2WriteIF temp 100 THEN.stringtag1 operator stringtag2If bar code and dest are string tags and yourspecification says: ‘If bar code equals dest then ’IF bar code dest THEN.stringtag1 operator 'character stringliteral'If bar code is a string tag and your specification says:‘If bar code equals ’Test PASSED’ then.’IF bar code ’Test PASSED’ THEN.char1 operator char2To enter an ASCII character directly intothe expression, enter the decimal valueof the character.If bar code is a string tag and your specification says:‘If bar code.DATA[0] equals ’A’ then ’IF bar code.DATA[0] 65 THEN.bool tag : bool expressionsIf count and length are DINT tags, done is a BOOL tag,and your specification says: ‘If count is greater than orequal to length, you are done counting.’Done : (count length);How strings are evaluatedThe hexadecimal values of the ASCII characters determine if one string is lessthan or greater than another string.18Rockwell Automation Publication 1756-PM007J-EN-P - March 2022

Chapter 1Program Structured Text When the two strings are sorted as in a telephone directory, the orderof the strings determines which one is greater. Strings are equal if their characters match. Characters are case sensitive. Upper case "A" ( 41) is not equal to lowercase "a" ( 61).See alsoStructured Text Components: Expressions on page 15Use logical operatorsUse logical operators to verify if multiple conditions are true or false. Theresult of a logical operation is a BOOL value.If the comparison isThe result istrue1false0Use these logical operators.For this comparisonUse this operatorOptimal data typelogical AND&, ANDBOOLlogical ORORBOOLlogical exclusive ORXORBOOLlogical complementNOTBOOLThe table provides examples of using logical operators.Use this formatExampleFor this situationUseBOOLtagIf photoeye is a BOOL tag and your specification says: "If photoeye 1 ison then."IF photoeye THEN.NOT BOOLtagIf photoeye is a BOOL tag and your specification says: "If photoeye isoff then."IF NOT photoeye THEN.expression1 & expression2If photoeye is a BOOL tag, temp is a DINT tag, and your specificationsays: "If photoeye is on and temp is less than 100 then."IF photoeye & (temp 100) THEN.expression1 OR expression2If photoeye is a BOOL tag, temp is a DINT tag, and your specificationsays: "If photoeye is on or temp is less than 100 then.".IF photoeye OR (temp 100)THEN.Rockwell Automation Publication 1756-PM007J-EN-P - March 202219

Chapter 1Program Structured Textexpression1 XOR expression2If photoeye1 and photoeye2 are BOOL tags and your specification says: IF photoeye1 XOR photoeye2"If:THEN.photoeye1 is on while photoeye2 is off orphotoeye1 is off while photoeye2 is onthen."BOOLtag : expression1 &expression2If photoeye1 and photoeye2 are BOOL tags, open is a BOOL tag, and your open : photoeye1 & photoeye2;specification says: "If photoeye1 and photoeye2 are both on, set open totrue"See alsoStructured Text Components: Expressions on page 15Use bitwise operatorsBitwise operators manipulate the bits within a value based on two values.The following provides an overview of the bitwise operators.ForUse this operatorOptimal data typebitwise AND&, ANDDINTbitwise ORORDINTbitwise exclusive ORXORDINTbitwise complementNOTDINTThis is an example.Use this formatExampleFor this situationvalue1 operator value2If input1, input2, and result1 are DINT tags and your specification says: result1 : input1 AND input2;"Calculate the bitwise result of input1 and input2. Store the result inresult1."UseSee alsoStructured Text Components: Expressions on page 15Determine the orderof execution20The operations written into an expression perform in a prescribed order. Operations of equal order perform from left to right. If an expression contains multiple operators or functions, group theconditions in parenthesis "( )". This ensures the correct order ofexecution, and makes it easier to read the expression.OrderOperation1()2function (.)3**4- (negate)5NOT67*,/,MOD ,- (subtract)8 , , , 9 , Rockwell Automation Publication 1756-PM007J-EN-P - March 2022

Chapter 1OrderOperation10&,AND11XOR12ORProgram Structured TextSee alsoStructured Text Components: Expressions on page 15Structured TextComponents: InstructionsStructured text statements can also be instructions. A structured textinstruction executes each time it is scanned. A structured text instructionwithin a construct executes every time the conditions of the construct aretrue. If the conditions of the construct are false, the statements within theconstruct are not scanned. T

Rockwell Automation Publication 1756 -PM007J-EN-P - March 2022 Supersedes Publication 1756 -PM007I-EN-P - September 2020 Programming Manual Original Instructions Logix 5000 Controllers