Docsity

Testo strutturato tutorial, Dispense di Sistemi elettronici automatici

Anteprima parziale del testo

Scarica Testo strutturato tutorial e più Dispense in PDF di Sistemi elettronici automatici solo su Docsity!

Structured Text (ST)

PLC Programming with IEC-61131-

Peter | www.plcacademy.com

Content of Structured Text Tutorial

● What is Structured Text Programming? ● The flow of Structured Text ● Syntax ○ Comments ● Statements ● Variables and Tags ○ Data Types ● Expressions ● Operators ○ Arithmetic Operators ○ Relational Operators ○ Logical Operators ○ Bitwise Operators ● Statements and Operators ● Conditional Statements ○ IF Statements ○ CASE Statements ● Repeating Loops ○ FOR Loops ○ WHILE Loops ○ REPEAT Loops ● Structured Text Programming Software ○ Beckhoff TwinCat 3 ○ Codesys ● Conclusion

The Flow of Structured Text

The first thing you should learn is the structure or the syntax of Structured Text. When you understand the structure, you will understand how the flow of your program works.

Starting with the example above, you can see that the whole program begins with PROGRAM and ends with END_PROGRAM. Everything in between is your PLC program. These two words are the delimiting keywords for program declarations. More on keywords later.

Don't be confused about the END_PROGRAM, because your program won't end completely here. When the PLC reaches the END_PROGRAM the PLC scan cycle will start over again, and your program will repeat itself.

Structured Text Program Flow.

This is just like ladder logic or any other PLC programming language - it will run over and over again. And if you are used to programming microcontrollers, the PROGRAM/END_PROGRAM will be similar to the infinite loop in C.

One thing to add here is that, when you are programming in Structured Text, you will often not use the PROGRAM/END_PROGRAM construct. It will already be done by the PLC programming software, and the code you have to write, is what you want inside that construct.

The flow control of PLC programs written in Structured Text is the same as in ladder logic: execute one line at a time.

Starting with the Syntax of Structured Text

The syntax of a programming language is the definition of how it is written. To be more precise, what symbols is used to give the language its form and meaning.

As you can see in the example, Structured Text is full of colons, semicolons and other symbols. All these symbols has a meaning and is used to represent something. Some of them are operators, some are functions, statements or variables.

All the details of the syntax will be explained as you move through this tutorial. But there are some general rules for the syntax of Structured Text you should know about. You don't have to memorize all the syntax rules for now, as you will when you get your hands into the programming:

● All statements are divided by semicolons. ● Structured Text consists of statements and semicolons to separate them. ● The language is not case-sensitive. ● Even though it is good practice to use upper- and lowercase for readability, it's not necessary. ● Spaces have no function. ● But they should be used for readability.

What's really important to understand here is that, when you write a PLC program in Structured Text, your computer will translate that to a language the PLC can understand.

When you upload the Structured Text PLC program to your PLC, the programming software you use will compile your program. This means that it will translate the code to a sort of machine code which can be executed by the PLC.

The compiler uses the syntax of the programming language to understand your program. For example: Each time the compiler sees a semicolon , it will know that the end of the current statement is reached. The compiler will read everything until it reaches a semicolon, and then execute that statement.

Comment Syntax

In textual programming languages you have the ability to write text that doesn't get executed. This feature is used to make comments in your code. Comments are good, and as a beginner you should always comment your code. It makes it easier to understand your code later.

In Structured Text you can make either one line comments or multiple line comments.

Single line comment:

Making Statements with Structured Text

So, Structured Text consists of statements. But what is statements? You probably know statements as something coming from humans. You can make a statement, a president or even a company can make a statement. And in PLC programming, statements are almost the same.

A statement is you telling the PLC what to do.

Let's take the first statement as an example:

X : BOOL;

The compiler will read this as one statement , because when it reaches the semicolon, it knows that this is the end of that statement.Remember, statements are separated by semicolons. That's the main syntax rule of this language.

In this statement you are telling the PLC to create a variable called X and that variable should be a BOOL type.

Using Variables in Structured Text

Before we dig deeper into the statement, let me get back to the keywords i mentioned before. As you can see, the variable X is defined in between two other keywords - VAR and END_VAR.

Both the PROGRAM/END_PROGRAM and VAR/END_VAR are constructs , meaning that they delimit a certain area in your program for something specific. The PROGRAM construct is where all your PLC program is, and the VAR construct is where you define variables.

All the four are called keywords, because they are reserved words. You can't use those words for anything else when you are programming in Structured Text. The name of your program cannot be PROGRAM or even program (STL is not case sensitive), because that word can only be used to make a construct to delimit your PLC program.

Back to variables ...

If you know other programming languages, chances are that you know about variables already. But if you don't, here's an introduction to variables you probably will like:

A variable is a place where you can store data.

Depending on what type of data you want to store, there are several data types available. The different kinds of data are called data types. For example, if you have a variable where you want to store either TRUE or FALSE , you can declare it as a BOOL type. The BOOL type is a boolean data type which means that it can contain a boolean value (TRUE or FALSE).

Now, that was two thing about variables. They have a certain data type, and they contain a value of that data type. But there's one more thing you can control in your variables. The name of the variable.

To make it easy for you to use your variables throughout your PLC program, they all have names. When you define a variable in the VAR construct, you start by giving the variable its name:

X : BOOL;

This statement will create a variable called X, with a BOOL data type.

Be aware, that when you are programming with some PLC software like Siemens STEP 7 or Rockwell you won't use the VAR/END_VAR til declare variables. Instead variables

Elementary data types ● Integers ● Floating points ● Time ● Strings ● Bit strings

Under each elementary data types there are several IEC data types available. These are the data types defined in IEC 61131-3 :

Integers:

IEC Data Type

Format Range

SINT Short Integer -128 ... 127

INT Integer - ... 32767

DINT Double Integer -2^31 ... 2^31-

LINT Long Integer -2^63 ... 2^63-

USINT Unsigned Short Integer

UINT Unsigned Integer 0 ... 2^16-

LDINT Long Double Integer

0 ... 2^32-

ULINT Unsigned Long Integer

0 ... 2^64-

Floating points:

IEC Data Type

Format Range

REAL Real Numbers ±10^±

LREAL Long Real Numbers

±10^±

Time:

IEC Data Type Format Use

TIME Duration of time after an event

T#10d4h38m57s12ms TIME#10d4h38m

DATE Calendar date D#1989-05- DATE#1989-05-

TIME_OF_DAY Time of day TOD#14:32: TIME_OF_DAY#14:32:07.

DATE_AND_TIME Date and time of day DT#1989-06-15-13:56:14. DATE_AND_TIME#1989-06-15-13:56: .

Strings:

IEC Data Type

Format Range

STRING Character String

'My string'

Bit strings:

IEC Data Type

Format Rang e

BOOL Boolean 1 bit

BYTE Byte 8 bits

WORD Word 16 bits

DWORD Double Word

bits

LWORD Long Word 64 bits

Derived data types ● Structured data types ● Enumerated data types ● Sub-ranges data types ● Array data types

The derived data types are your own custom data types. All the derived data types are built by making a construction of the keywords TYPE and END_TYPE. In between the keywords is the kind of derived data type you want to declare.

Operators and Expressions in STL

The next thing you should know about is operators. Operators are used to manipulate data and is a part of almost any programming language. This leads us to the second thing you should know about - expressions.

Just like operators, expressions are a crucial part of programming languages. An expression is a construct that, when evaluated, yields a value.

This means that when the compiler compiles an expression, it will evaluate the expression and replace the statement with the result.

Take this example with the two variables A and B. A contains the value 10 and B contains 8.

A+B

The result of this expression is 18. So instead of A+B, the compiler will put in the value

An expression are composed of operators and operands.

So what are operators and operands?

Since, you just saw an example of an expression, you just saw both an operator and two operands. A and B are both operands and the + is an operator.

Programming language expressions with operands and operators.

Remember that operators are used to manipulate data. That is exactly what the + is doing. It is taking the value of the variable A and adding it to the value in B. The + is also called the addition operator because the operation is addition.

Operators

There are several operators available in Structured Text. Again, IEC 61131-3 describes all the standard operators in the Structured Text language:

Operation Symbol Precedenc e

Parenthesizat ion

(expressio n)

Highest

Function Evaluation

MAX(A,B)

Negation Complement

NOT

Exponentiatio n

Multiply Divide Modulo

MOD

Add Subtract

Comparison <, >, <=, >=

Equality Inequality

Boolean AND Boolean AND

AND

Boolean Exclusive OR

XOR

Boolean OR OR Lowest

All the operators in the table above are sorted after precedence. This is also called order of operations, and you may know about if from mathematics.

● - (subtract/negate) ● ***** (multiply) ● ****** (exponent) ● / (divide) ● MOD (modulo divide)

Example: 15 MOD 4 Result: 3

Relational Operators

To compare or find a relation between two values you can use one of the relational operators. They are used for comparison and the result will be a boolean value (BOOL type), either TRUE or FALSE. ● = (equal) ● < (less than) ● <= (less than or equal) ● > (greater than) ● >= (greater than or equal) ● <> (not equal)

Example: TEMPERATURE := 93.9; TEMPERATURE >= 100.

Result: FALSE

Logical Operators

If you want to compare boolean values (BOOL) and make some logic out of it, you have to use logical operators. These operators also yields a boolean value of TRUE or FALSE as a result of the expression. ● & or AND ● OR ● XOR ● NOT

Example: LIMIT_SWITCH1 := TRUE; LIMIT_SWITCH2 := FALSE; LIMIT_SWITCH1 OR LIMIT_SWITCH

Result: TRUE

Bitwise Operators

The last group of operators are called bitwise operators because the operations are performed bitwise. It simply means that a logic operation is performed for each bit of two numbers. The result is a new number - the total result of the bitwise operations. ● & or AND ● OR ● XOR ● NOT

Example: 15 AND 8

Result: 15

Since this operation is bitwise the calculation will be per bit. So to understand what's going on here, you have to convert the numbers to binary values:

15 = 1111 8 = 1000

Now each bit in the number 1111 (15) can be used in a logical operation with the other number 1000 (8):

1111 AND 1000

Bit number

1000 (8) Resul t

0 1 1 1

1 1 0 0

2 1 0 0

3 1 0 0

When this line of code is compiled, the expression A + 2 will be evaluated to 12. The compiler will replace the expression with the result 12. The statement will now look like this to the compiler:

B := 12;

What will happen now, is that the compiler will assign the value 12 to the variable B.

How an assignment statement with an expression will be evaluated by the compiler.

The last thing is that the := symbol is called the assignment operator. Yes, it is an operator just like the operators used in expressions. Often those two types of operators are mistaken for each other and used wrong.

A common mistake is to use the equality operator (=) instead of the assignment operator (:=). But even though they look like each other there's a huge difference.

Take these two examples:

A = B A := B;

The first line is an expression. Since this is an expression, the operator will be used to evaluate the line. The equality operator evaluates in the following way: If the right side and the left side is equal it evaluates to TRUE or 1. If not, it will evaluate to FALSE or 0.

With some other operators, the equality operator is a relational operator. All the relational operators will evaluate to either TRUE or FALSE.

On the second line you'll see a statement. This time the operator will be used for an action instead of an evaluation. Assignment is the action, and here the value of A will be given the value of B.

At last, you can always identify a statement by the semi colon. Once again, the semicolon is how the compiler knows when the end of a statement is reached. You can have all sorts of expressions in your assignment statements, from simple values like numbers to variables and functions. Because all expressions will be evaluated first, and then, the result of that evaluation will be used in the assignment statement.