###### Structured Text

# Structured Text PLC Programming | Mathematical Operators Analog Scaling Inputs

## Introduction

In structured text, the user has access to numerous mathematical functions. However, there are **six operators that have been implemented directly into the programming language**. They are the addition, subtraction, multiplication, division, exponent and modulo operators. They all behave similarly to operators in other languages such as C, C++, Java and Python.

The six operators play an important role in structured text programming in numerous applications. They're used for **scaling analog signals, computing ratios, updating counters, converting between different constants and much more**. In this tutorial, we will go over the operators mentioned above and create a few basic examples which would utilize them in a real PLC program. Lastly, we will go over the importance of parenthesis when it comes to the order of operation. It's not something that you'd normally worry about in ladder logic and is therefore an important point to understand when you're migrating to structured text.

## Structured Text Addition and Subtraction PLC Instructions

We've covered the ADD and SUB Instructions in ladder logic in great detail. There are no equivalent instructions of functions when it comes to structured text. Instead, the user must utilize the plus (+) operator to compute and addition and the minus (-) operator to compute a subtraction. These operators, just like in ladder logic, are extremely important. They need to be used through an assignment instruction which we saw in our first tutorial on structured text. Here are several examples of an addition and subtraction in structured text:

LocDINT[0] := LocDINT[1] + 5;

LocDINT[2] := LocDINT[3] - 4;

LocREAL[4] := LocDINT[0] + LocDINT[2] - 5.25;

Notice that the addition and subtraction operators can be used on integers, double integers and real data types. Just as we had seen in ladder logic, storing a value that isn't compatible with the data type which we've defined as the end-point will truncate the data. In other words, subtracting 6.78 from an integer will round down the final value.

## Structured Text Multiplication and Division Instructions

The MUL and DIV instructions are also inexistent in structured text. Instead, the **multiplication (*)** and **division (/)** operators must be used in computations. Just as we saw above, here are a few examples as to how they are utilized in structured text:

LocDINT[4] := LocDINT[0] * 3;

LocDINT[5] := 20 / LocDINT[2];

You may notice that in the second line, the user is risking to **run into a "division by 0"**. However, we've mentioned in a previous tutorial that this problem is ignored in RSLogix and Studio 5000 based PLCs. Instead of faulting out the controller, the resulting division is the original value of the numerator. In other words, the expression on the second line will evaluate to "20" while "LocDINT[2]" is equal to 0.

## Structured Text Exponent and Modulo Instructions

The exponent and Modulo instructions aren't seen as often as the instructions above. However, they have been given their own operators and are thus important to remember when you're utilizing structured text. The **exponent operator (**)** will take the value on the left and raise it to the exponent specified on the right of the operator. Similarly the **modulo operator (MOD)** will take the value on the left, divide it by the value on the right and store the remainder in the specified register. Here are a few examples of these instructions in structured text:

LocDINT[6] := LocDINT[0] ** 3;

LocDINT[7] := LocDINT[6] MOD 4;

## Structured Text Examples of Computations

As mentioned in the introduction, it's important to see a few practical examples of where the instructions above can apply and be chained for efficiency. You may achieve the same results as we've discussed and will see through the CMP instruction in ladder logic. However, it's generally advised against using that instruction due to increased complexity and lack of visual aid it provides. In structured text, we don't have an option.

### Conversion from Fahrenheit to Celsius

The simplest example and the most obvious for all would be a conversion of temperature values from Fahrenheit to Celsius. This conversion is quite common in systems that are deployed in multiple regions and is easy to understand regardless of where you're located. Here's a **basic implementation of this function in structured text**:

TempC := (TempF - 32) * 5/9;

Notice that this is the first time that we see parenthesis used in our mathematical equations. Although many understand the order of mathematical operations, it's important to notice that the following implementations would yield different results: (TempF - 32) * 5/9 =/= TempF - 32 * 5/9. In the first case, the subtraction is executed first. In the second case, the multiplication takes precedence. Try it yourself to notice the difference.

### Analog Input Scaling

We've explored analog input scaling through function blocks in a previous tutorial. However, the same methodology can be applied to structured text. As discussed in the FBD tutorial, the scaling instruction is just a mathematical formula that's applied to the values specified by the user and the input register in order to compute the scaled output. For a simple input, the same one as we saw in that tutorial, we can easily compute that we just need to multiple the input by 3 and divide by 160 based on our scaling parameters. Therefore, **the same instruction can be implemented as follows in structured text**:

AScaled := AnalogInput[0] / 160 * 3;

The AScaled value would be computed to the exact same value as the FBD instruction we implemented.

Note that for complex conversions, it's still possible to create a mathematical equivalent. However, this is done for you in FBD by the instruction. In case of structured text, you'd have to find the mathematical equation yourself. We can get into it if there's enough interest from our readers.

## Conclusion

Structured text provides a number of mathematical operators for the users. Six of them, **addition, subtraction, multiplication, division, exponent and modulo** have a dedicated handle of implementation. They can be used on integers, double integers and real values in order to compute appropriate outcomes and store the resulting value into the specified register.

The fault handling procedures of PLCs are different from other languages. For example, a division by zero won't throw an error.

The mathematical functions are seen in many applications in the real world. They can be used to compute variables, scale analog values, convert between two systems and so on.

Lastly, remember that the order of execution is important.