###### PLC Instructions

# PLC Programming Computational Mathematical Instructions – CPT | Compute

## Introduction

The CPT, also known as the compute, is an instruction which has ** extensive capabilities when it comes to mathematical and logic functions** in ladder logic. This instruction allows the user to input a complex string of instructions composed of computational, operational and comparison functions and computes the result which is stored in the specified register. The instruction will work with operands used in the based instructions as well as many others.

The CPT instruction has **many advantages as well as drawbacks**. Based on personal experience, you should only use this instruction if absolutely necessary and the same could not be achieved with several basic instructions. In other words, you can compute an ADD instruction through the CPT, but you shouldn’t.

The drawbacks come from the fact that it’s not easy to see what’s going on with the logic within this particular instruction. Multiple operands interact with each other which leads to a confusing execution and inability to troubleshoot or tweak when needed.

## Example & Usage of CPT

**Here’s a real-world scenario of a CPT instruction**:

- A CompactLogix 1769-:24ER-QB1B Allen Bradley PLC is used to control a process.
- In Rung 0, a CPT instruction is used to compute a mathematical instruction utilizing System_DINT[0], System_DINT[1], System_DINT[2], System_DINT[3] and System_DINT[4] registers.
- The result of the computation is set to be stored in the “Dest” register which is specified to be System_DINT[5].
- Since the
**mathematical equation is defined as System_DINT[0] + System_DINT[1] – System_DINT[2] * System_DINT[3] / System_DINT[4]**and the registers are set to 12, 32, 54, 4 and 31 respectively, the result evaluates to 38. - In Rung 1, a CPT instruction is used to compute a mathematical instruction utilizing System_REAL[0] and System_REAL[1] registers.
- The result of the computation is set to be stored in the “Dest” register which is specified to be System_REAL[2].
- Since the
**mathematical equation is defined as COS(System_REAL[0] )* SIN(System_REAL[1])**and the registers are set to 23 and 42 respectively, the result evaluates to 0.48835304. - In Rung 2 a CPT instruction is used to compute an average of three registers: System_DINT[6], System_DINT[1] and System_DINT[2].
- The result of the computation is set to be stored in the “Dest” register which is specified to be System_DINT[9].
- Since the
**mathematical equation is defined as (System_DINT[6] + System_DINT[1] + System_DINT[2]) / 3**and the registers are set to 100, 50 and 200 respectively, the result evaluates to 116.

**Programming example in RSLogix 5000**:

**Outcome**:

The CPT instruction is a powerful mathematical tool which can be used for complex computations, instructional combinations & for special functions such as the ones found in trigonometry. This instruction allows one to quickly combine a set of mathematical instruction through a traditional formula input which we’re all used to as opposed to ADD Instructions, MUL Instructions, etc.

It may not be obvious from the example above, but a clear drawback of the CPT instruction is the fact that ** you can’t easily troubleshoot it**. The function does not break down the different operands and requires one to go into controller scoped tags in order to see what the values are set to.

The general rule of thumb I’d recommend when using this instruction is that you should **only use it if you can’t accomplish the same operation through 3 basic instructions**. In other words, if you can use ADD -> MUL -> SUB, use them instead of the CPT.

## Data Types Allowed for CPT

The CPT can be used to compare two values of identical types or mismatching types such as float and integer. You may also specify a boolean register which is frequently used for logical evaluations.

**Integer**– You may specify each operand to be of “Integer” type.**Float**– You may specify each operand to be of “Float” type.**Boolean**– You may specify each operand to be of “Boolean” type