Back to tutorials
Intermediate

PLC Programming Computational Mathematical Instructions – CPT | Compute

By
Vladimir Romanov
Table of Contents

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:

  1. A CompactLogix 1769-:24ER-QB1B Allen Bradley PLC is used to control a process.
  2. 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.
  3. The result of the computation is set to be stored in the “Dest” register which is specified to be System_DINT[5].
  4. 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.
  5. In Rung 1, a CPT instruction is used to compute a mathematical instruction utilizing System_REAL[0] and System_REAL[1] registers.
  6. The result of the computation is set to be stored in the “Dest” register which is specified to be System_REAL[2].
  7. 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.
  8. 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].
  9. The result of the computation is set to be stored in the “Dest” register which is specified to be System_DINT[9].
  10. 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:

CPTInstructionRSLogixStudio5000

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

Video Tutorial

Back to tutorials

Related Tutorials

Intermediate

MicroLogix 1100 RSLogix 500 External IO PLC Programming Tutorial

The MicroLogix 1100 PLC is still heavily used in the industry and is one of the most recommended PLCs for learning PLC programming. The MicroLogix 1100 PLC is compact, offers a variety of I/O configurations and is inexpensive relative to other Rockwell options. Furthermore, the MicroLogix 1100 PLC is programmable using the free version of RSLogix 500 that can be downloaded from the official Rockwell website.

By
Vladimir Romanov
Beginner

PLC Simulator - PLC Programming & Testing within a Simulated Environment

A PLC Simulator is an excellent avenue to practice PLC ladder logic programming. Although the software environment is not the perfect substitute for hands on experience on an actual PLC, the simulation delivers a very accurate representation of ladder logic execution.

By
Vladimir Romanov
Intermediate

HMI Software - Different Tools, Applications and Solutions to Meet Your Needs

HMI Software is commonly referred to as the development application that will be used to create the runtime application for an HMI terminal. It’s important to understand this distinction as we will be going over the HMI development environments used by PLC and HMI programmers to create the screens rather than the end-user applications you will see at the plant level.Numerous HMI programming software solutions are found on the market. Although they aim to achieve the same result, different packages have a distinct advantage over others in terms of price, reliability, ease-of-use and technical capability. It is therefore possible to find the right solution that will fit your specific application if you spend the time to research all options.

By
Vladimir Romanov