AC Drive China Forum
Forum » PLC - Programmable Logic Controller » PLC Mathematical Functions
Topics: PLC Mathematical Functions on PLC - Programmable Logic Controller
#1
Start by
Brian
09-21-2012 09:07 AM

PLC Mathematical Functions

The basic mathematical functions performed by most PLC's are: add, subtract, multiply and divide.These types of instructions are used by PLC's to gather data from various memory locations, to compare data, and to scale values.

Data comparison is an extremely useful programming tool that allows outputs to be energized depending on wether data in one memory location is greater than, less than, or equal to data stored in another memory location. Scaling is also an important application of math function because it allows numbers which are very small or large to be elarged or reduced by a fixed constant.

The standard format for math functions in many PLC's is is that the desired math operation is performed on a memory location, and not directly on a number. In other words, if you wish to add two numbers, these numbers must be entered into memory locations, and the contents of the addresses will be added together.
09-22-2012 09:08 AM
Top #2
Joke
09-22-2012 09:08 AM
Huh, well here Brian, you touch on a subject that is quite important. But I’m not talking about maths functions – maths functions are the very things that all computers, including PLCs do most naturally and most easily. What? PLCs were designed for Boolean logic? Well that’s true, but processors were originally designed to do maths functions, and all the other clever things they appear to do are really about doing maths functions. It was never particularly challenging to make PLCs do maths, even floating point maths. (Even PLCs sometimes had maths co-processors.)

The much more important point that you touch on is this business of using memory addresses or what are correctly termed literals. Some people call them constants, but in computer programming, constants are just memory locations that you have told the compiler are constants, so that any subsequent attempts to assign a value to them after initialisation will flag an error. If, instead of typing in a memory location that you have named and to which you have assigned a value, you just type in the value directly, that is a literal. The processor handles it quite differently from the named memory locations we call variables or constants. When you use a literal, there is no attempt to access memory. The value is just pushed directly onto the stack and then popped off it when the desired operation is performed. For this reason some people believe it is more efficient and the better way to do it. But it isn’t. It is lazy, sloppy programming. If you are converting from Bar to PSI, for example, you could just use the literal 14.504, and it would work. But it is much better to declare a constant called something like BAR2PSI, assign it the value 14.504 and then whenever you do a conversion, multiply the bar value by BAR2PSI to find the PSI value. Some say that this technique is better because if you have several references to the conversion value, and then for some reason you have to change it – say you require kilopascals instead of PSI – then instead of having to type in 100 wherever you previously had 14.504, you can just type it in once where the constant is assigned. Clearly, in this case it might be good to change the name of the constant as well, but nevertheless, it is much less work. This is important, but it isn’t the main reason for not using literals. It is entirely about readability of the program. Whether or not a program is well written is not simply about whether or not it works. Much less is it about the apparent genius of the person who wrote it. A well written program is one that any programmer worth his salt can quickly and easily look through and understand what it does and how. It is much easier to understand the function of a constant called Bar2PSI than it is to understand why a value is being multiplied by 14.504.
Reply to Thread