Automated Manufacturing Systems

st



19. STRUCTURED TEXT PROGRAMMING

 

19.1 INTRODUCTION

If you know how to program in any high level language, such as Basic or C, you will be comfortable with Structured Text (ST) programming. ST programming is part of the IEC 61131 standard. An example program is shown in Figure 261 A Structured Text Example Program. The program is called main and is defined between the statements PROGRAM and END_PROGRAM. Every program begins with statements the define the variables. In this case the variable i is defined to be an integer. The program follows the variable declarations. This program counts from 0 to 10 with a loop. When the example program starts the value of integer memory i will be set to zero. The REPEAT and END_REPEAT statements define the loop. The UNTIL statement defines when the loop must end. A line is present to increment the value of i for each loop.

 

Figure 261 A Structured Text Example Program

One important difference between ST and traditional programming languages is the nature of program flow control. A ST program will be run from beginning to end many times each second. A traditional program should not reach the end until it is completely finished. In the previous example the loop could lead to a program that (with some modification) might go into an infinite loop. If this were to happen during a control application the controller would stop responding, the process might become dangerous, and the controller watchdog timer would force a fault.

ST has been designed to work with the other PLC programming languages. For example, a ladder logic program can call a structured text subroutine.

19.2 THE LANGUAGE

The language is composed of written statements separated by semicolons. The statements use predefined statements and program subroutines to change variables. The variables can be explicitly defined values, internally stored variables, or inputs and outputs. Spaces can be used to separate statements and variables, although they are not often necessary. Structured text is not case sensitive, but it can be useful to make variables lower case, and make statements upper case. Indenting and comments should also be used to increase readability and documents the program. Consider the example shown in Figure 262 A Syntax and Structured Programming Example.

 

Figure 262 A Syntax and Structured Programming Example

19.2.1 Elements of the Language

ST programs allow named variables to be defined. This is similar to the use of symbols when programming in ladder logic. When selecting variable names they must begin with a letter, but after that they can include combinations of letters, numbers, and some symbols such as '_'. Variable names are not case sensitive and can include any combination of upper and lower case letters. Variable names must also be the same as other key words in the system as shown in Figure 263 Acceptable Variable Names. In addition, these variable must not have the same name as predefined functions, or user defined functions.

 

Figure 263 Acceptable Variable Names

When defining variables one of the declarations in Figure 264 Variable Declarations can be used. These define the scope of the variables. The VAR_INPUT, VAR_OUTPUT and VAR_IN_OUT declarations are used for variables that are passed as arguments to the program or function. The RETAIN declaration is used to retain a variable value, even when the PLC power has been cycled. This is similar to a latch application. As mentioned before these are not used when writing Allen Bradley programs, but they are used when defining tags to be used by the structured programs.

 

Figure 264 Variable Declarations

Examples of variable declarations are given in Figure 265 Variable Declaration Examples.

 

Figure 265 Variable Declaration Examples

Basic numbers are shown in Figure 266 Literal Number Examples. Note the underline `_' can be ignored, it can be used to break up long numbers, ie. 10_000 = 10000. These are the literal values discussed for Ladder Logic.

 

Figure 266 Literal Number Examples

Character strings defined as shown in Figure 267 Character String Data.

 

Figure 267 Character String Data

Basic time and date values are described in Figure 268 Time Duration Examples and Figure 269 Time and Date Examples. Although it should be noted that for ControlLogix the GSV function is used to get the values.

 

Figure 268 Time Duration Examples

 

Figure 269 Time and Date Examples

The math functions available for structured text programs are listed in Figure 270 Math Functions. It is worth noting that these functions match the structure of those available for ladder logic. Other, more advanced, functions are also available - a general rule of thumb is if a function is available in one language, it is often available for others.

 

Figure 270 Math Functions

Functions for logical comparison are given in Figure 271 Comparisons. These will be used in expressions such as IF-THEN statements.

 

Figure 271 Comparisons

Boolean algebra functions are available, as shown in Figure 272 Boolean Functions. The can be applied to bits or integers.

 

Figure 272 Boolean Functions

The precedence of operations are listed in Figure 273 Operator Precedence from highest to lowest. As normal expressions that are the most deeply nested between brackets will be solved first. (Note: when in doubt use brackets to ensure you get the sequence you expect.)

 

Figure 273 Operator Precedence

Common language structures include those listed in Figure 274 Flow Control Functions.

 

Figure 274 Flow Control Functions

Special instructions include those shown in Figure 275 Special Instructions.

 

Figure 275 Special Instructions

19.2.2 Putting Things Together in a Program

Consider the program in Figure 276 A Program To Average Five Values In Memory With A For-Loop to find the average of five values in a real array 'f[]'. The FOR loop in the example will loop five times adding the array values. After that the sum is divided to get the average.

 

Figure 276 A Program To Average Five Values In Memory With A For-Loop

The previous example is implemented with a WHILE loop in Figure 277 A Program To Average Five Values In Memory With A While-Loop. The main differences is that the initial value and update for 'i' must be done manually.

 

Figure 277 A Program To Average Five Values In Memory With A While-Loop

The example in Figure 278 Example With An If Statement shows the use of an IF statement. The example begins with a timer. These are handled slightly differently in ST programs. In this case if 'b' is true the timer will be active, if it is false the timer will reset. The second instruction calls 'TONR' to update the timer. (Note: ST programs use the FBD_TIMER type, instead of the TIMER type.) The IF statement works as normal, only one of the three cases will occur with the ELSE defining the default if the other two fail.

 

Figure 278 Example With An If Statement

Figure 279 Use of a Case Statement shows the use of a CASE statement to set bits 0 to 3 of 'a' based upon the value of 'test'. In the event none of the values are matched, 'a' will be set to zero, turning off all bits.

 

Figure 279 Use of a Case Statement

The example in Figure 280 Function Data Conversions accepts a BCD input from 'bcd_input' and uses it to change the delay time for TON delay time. When the input 'test_input' is true the time will count. When the timer is done 'set' will become true.

 

Figure 280 Function Data Conversions

Most of the IEC61131-3 defined functions with arguments are given in Figure 281 Structured Text Functions. Some of the functions can be overloaded, for example ADD could have more than two values to add, and others have optional arguments. In most cases the optional arguments are things like preset values for timers. When arguments are left out they default to values, typically 0. ControlLogix uses many of the standard function names and arguments but does not support the overloading part of the standard.

 

 

Figure 281 Structured Text Functions

Control programs can become very large. When written in a single program these become confusing, and hard to write/debug. The best way to avoid the endless main program is to use subroutines to divide the main program. The IEC61131 standard allows the definition of subroutines/functions as shown in Figure 282 Declaration of a Function. The function will accept up to three inputs and perform a simple calculation. It then returns one value. As mentioned before ControlLogix does not support overloading, so the function would not be able to have a variable size argument list.

 

Figure 282 Declaration of a Function

19.3 AN EXAMPLE

The example beginning in Figure 284 Traffic Light Subroutine shows a subroutine implementing traffic lights in ST for the ControlLogix processor. The variable 'state' is used to keep track of the current state of the lights. Timer enable bits are used to determine which transition should be checked. Finally the value of 'state' is used to set the outputs. (Note: this is possible because '=' and ':=' are not the same.) This subroutine would be stored under a name such as 'TrafficLights'. It would then be called from the main program as shown in Figure 283 The Main Traffic Light Program.

 

Figure 283 The Main Traffic Light Program

 

Figure 284 Traffic Light Subroutine

19.4 SUMMARY

· Structured text programming variables, functions, syntax were discussed.

· The differences between the standard and the Allen Bradley implementation were indicated as appropriate.

· A traffic light example was used to illustrate a ControlLogix application

19.5 PRACTICE PROBLEMS

(Note: Problem solutions are available at http://sites.google.com/site/automatedmanufacturingsystems/)

1. Write a structured text program that will replace the following ladder logic.

2. Implement the following Boolean equations in a Structured Text program. If the program was for a state machine what changes would be required to make it work?

3. Convert the following state diagram to a Structured Text program.

4. A temperature value is stored in F8:0. When it rises above 40 the following sequence should occur once. Write a ladder logic program that implement this function with a Structured Text program.

5. Write a structured text program that will replace the following ladder logic.

19.6 ASSIGNMENT PROBLEMS

1. Write logic for a traffic light controller using structured text.

2. Write a structured text program to control a press that has an advance and retract with limit switches. The press is started and stopped with start and stop buttons.

3. Write a structured text program to sort a set of ten integer numbers and then find the median value.

4. Write a ST program to accept an input string `teststring' with the sample format of `XWT55.36KG'. The program should extract the 5 digit number in the middle of the string starting at the 4th position. If the number is less than or equal to 20, or greater than 60, the output Y should be set.

5. Write a ST program to convert two string `AA' and `BB' to real numbers, add them, and convert the result back to a string `CC'.

 

Site Links


Engineer On A Disk Main Page

- PLC Book Book Page