23. Structured Text Programming

• This is a procedural programming language that resembles BASIC, and will be comfortable for those that have learned other programming languages such as C, Fortran, Pascal, etc.

• The language was developed to meet the IEC 1131-3 standard.

• A simple program to count from 1 to 10 is shown below,


23.1 The Language

• These programs can also coexist with ladder logic programs. This gives the ability to get solutions that are the best of both worlds.

• Some general notes about the language are,

The language can use normal I/O or memory values as well as named variables.

Separate operators with spaces; when in doubt use spaces. Indents will also make the program more readable.

Comments will be on lines that start with ‘%’.

Semicolons are used at the end of instructions: this does not mean every line.

the instructions can be upper or lower case.

instructions can be nested.

• Remember: unlike normal programming languages these programs must run and then stop each ladder logic scan.

• Variable names can be anything but those listed below, and the other instruction names.


• Variables are defined with the following keywords.


• Examples of variable declarations are given below,


• Basic numbers are shown below. Note the underline ‘_’ can be ignored, it can be used to break up long numbers, ie. 10_000 = 10000.


• Character strings are shown below.


• Basic time duration values are described below.


• Date values are given below. These are meant to be used to compare to system time and date clocks.


• Basic math functions include,


• Functions for logical comparison include,


• Functions for Boolean algebra and logic include,


• The precedence of operations are listed below 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.)


• Language structures include those below,


• Special instructions include those shown below.


• Consider the program below to find the average of five values in floating point memory.


• Consider the program below to find the average of five values in floating point memory.


• The example below will set different outputs depending upon the stat of an input.


• The example below will set output bits 00-03 depending upon the value of the integer in N7:0, if the value is not between 0 and 3 the outputs will all go off.


• The example below accepts a BCD input from (I:000) and will use it to change the delay time for an on delay timer that will examine input I:002/00 drive output O:001/00.


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

• Normal ladder logic output functions can be used except for those listed below.


• The list below gives a most of the IEC1131-3 defined functions with arguments. 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 line preset values for timers. When arguments are left out they default to values, typically 0.



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

• We can define functions that return single values,


Problem 23.3 Write a structured text program control a set of traffic lights.

23.2 Problems

Problem 23.4 Write logic for a traffic light controller using structured text.


[an error occurred while processing this directive]