eBook: Automating Manufacturing Systems; with PLCs



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.

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. 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 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.

Figure 265 Variable Declaration Examples

Basic numbers are shown in Figure 266. 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.

Figure 267 Character String Data

Basic time and date values are described in Figure 268 and Figure 269. 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. 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. These will be used in expressions such as IF-THEN statements.

Figure 271 Comparisons

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

Figure 272 Boolean Functions

The precedence of operations are listed in Figure 273 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.

Figure 274 Flow Control Functions

Special instructions include those shown in Figure 275.

Figure 275 Special Instructions

19.2.2 Putting Things Together in a Program

Consider the program in Figure 276 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. 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 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 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 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. 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. 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

Search for More:

Custom Search