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 18.1 A Syntax and Structured Programming Example.
Figure 18.1 A Syntax and Structured Programming Example
18.1.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 18.1 Acceptable Variable Names. In addition, these variable must not have the same name as predefined functions, or user defined functions.
Figure 18.1 Acceptable Variable Names
When defining variables one of the declarations in Figure 18.1 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 18.1 Variable Declarations
Examples of variable declarations are given in Figure 18.1 Variable Declaration Examples.
Figure 18.1 Variable Declaration Examples
Basic numbers are shown in Figure 18.1 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 18.1 Literal Number Examples
Character strings defined as shown in Figure 18.1 Character String Data.
Figure 18.1 Character String Data
Basic time and date values are described in Figure 18.1 Time Duration Examples and Figure 18.1 Time and Date Examples. Although it should be noted that for ControlLogix the GSV function is used to get the values.
Figure 18.1 Time Duration Examples
Figure 18.1 Time and Date Examples
The math functions available for structured text programs are listed in Figure 18.1 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.
Functions for logical comparison are given in Figure 18.1 Comparisons. These will be used in expressions such as IF-THEN statements.
Boolean algebra functions are available, as shown in Figure 18.1 Boolean Functions. The can be applied to bits or integers.
The precedence of operations are listed in Figure 18.1 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 18.1 Operator Precedence
Common language structures include those listed in Figure 18.1 Flow Control Functions.
Figure 18.1 Flow Control Functions
Special instructions include those shown in Figure 18.1 Special Instructions.
Figure 18.1 Special Instructions
18.1.2 Putting Things Together in a Program
Consider the program in Figure 18.1 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 18.1 A Program To Average Five Values In Memory With A For-Loop
The previous example is implemented with a WHILE loop in Figure 18.1 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 18.1 A Program To Average Five Values In Memory With A While-Loop
The example in Figure 18.1 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 18.1 Example With An If Statement
Figure 18.1 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 18.1 Use of a Case Statement
The example in Figure 18.1 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 18.1 Function Data Conversions
Most of the IEC61131-3 defined functions with arguments are given in Figure 18.1 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 18.1 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 18.1 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.