The
PLC
Guy

  Controlling Your World One Contact At A Time  


AB to AD - Numeric Types

The AD PLC system makes use of various numeric types. One has been discussed which is used for addressing. OCTAL or base 8 which uses the digits 0 through 7 only. The system provides an instruction for converting an OCTAL reference into the form suitable for use as a memory reference or 'pointer' in other ladder instructions. This is the LDA instruction. An OCTAL number with the letter 'O' prefixed is converted. The values which are used in 'pointer' type references after conversion are of the 'binary' type. Any adjustment to a memory location used as a 'pointer' type must use 'binary' math (see below).

In deference to the world of base 10 numbers and their representation, the AD CPUs utilize BCD or Binary Coded Decimal. A 16 bit register is divided into four 4-bit sections. Each of these sections is allowed to hold the numbers 0 through 9 only. So the 16 bit register can only hold the numbers 0 through 9999. This was a very appropriate feature when input and output devices routinely needed this type structure to process human usable information. Unfortunately the use of BCD numbers is deeply established in the instruction set. The accumulators of timers and counters are expressed in BCD. Their preset values must also have this representation. All the standard mathematical (ADD, SUB, MUL DIV etc.) instructions expect BCD operands. They will give unexpected results if the operands are not originally written as BCD and actually fail if any digits of the operands are not 0-9.

The 'binary' type (as AD refers to it) is the normal number which uses a word fully. 16 bits can hold the 'binary' numbers 0 through 65535. Instructions which expect that their operands will be of the 'binary' type have the letter 'B' appended to the standard mathematical instructions. If a 'binary' type number is to be entered as a constant operand (K) for an instruction it must be entered in its hexadecimal representation. (Fire up the Windows 'calculator' in its 'scientific' mode') As mentioned above use the 'binary' form of mathematical instructions when working with 'pointer' values.

Both the BCD and the 'binary' representations have their double word (32 bit) formats also (0 – 99,999,999 for BCD and 0 - 4,294,967,295 for binary). Most all of the instructions have a double word representation and have the letter 'D' as an added part of the instruction mnemonic.

The 'Real' data type (equivalent to the 'float' data type in AB) is also available with its own set of instructions.

Numeric Type Storage Considerations

The standard BCD and binary data type take up 1 word of 'V' memory. Their doubled versions as well as the real data type take up 2 'V' memory locations. The storage is 'little-endian'. This means that the least significant word occurs in the lower V-memory location and the most siginificant word occurs in the following location. There is no mechanism in the CPU to distinguish which memory type the programmer has chosen to store in a given V memory location. It is up to the programmer to ensure that the types of numbers are commented as a clue to prevent this misuse. I make it a habit to comment the second word of the 2 word data types to flag against assignment for some other use.

The standard timers and counters have 1 word presets (remember they are BCD) as well as accumulators. But some timers (TMRA) and counters (UDC) have double word presets and accumulated values. When these last types are used they also occupy the next Timer or Counter assignment. Thus if T6 is assigned as a TMRA type then T7's space is being used also. Be sure to mark this in comments. Use of that second space for another timer has led to many hard-to-detect problems.

Numeric Type Instruction Considerations

There are separate mathematical instructions depending on the expected type of the operands. Thus there are BCD, doubled BCD, binary, doubled binary and real forms of instructions. There is no automatic conversion of data types when performing mathematical instructions as in Allen Bradley. You, as the programmer have to understand the data type you are manipulating and use the correct instruction for that type. The mathematical instructions generally cannot perform with operands of different types.

There are instructions provided for conversion between the different types.

If there are execution speed considerations you should realize that operations on BCD numbers can take twice the amount of time as the same instruction on binary types. The instruction times are listed in Appendix 'C' of the DL06 manual. In interrupt routines I attempt to limit any necessary math to the 'binary' type.

Comparison instructions (similar to the EQU, NEQ, GRT etc instructions in AB) are performed on single word values inline. These are very intuitive. Just make sure that the types (BCD or binary) are the same to prevent confusion. Comparison on the 2 word data types take place in output boxes. These comparisons affect 'Less Than', 'Equal To' and 'Greater Than' system flags which must be tested and/or utilized before another instruction is performed which affects these flags.

Accumulator Stack based mathematics

In the Allen Bradley PLCs most math instructions (we'll use and ADD for example) contain within a single box the loading of the first operand, performing the mathematical operation using a second operand then sending the result to a destination. ( ADD N7:0 N7:1 N7:2 ).

In the AD world this is broken up into three steps. The loading of the first operand (LD V2000), the mathematical operation (ADD V2001) and sending to a final destination (OUT V2002). The LD command brings a number into the accumulator (the 'math register' in AB terms). The mathematical operation leaves its result in the accumulator. The OUT sends a copy of the accumulator's contents to the destination.

It is not necessary to keep these three steps contiguous. I have written code in which various first operands are loaded, various mathematical operations are performed based on conditions and the result is sent to various destinations, again based on logic. The breaking up of the process makes this easy as long as you stay aware of the contents of the accumulator.

Another treatment of number storage and usage.

Back To Introduction

Please email comments to me at the address below.





Copyright © 2005, 2006, 2007 Bernard Carlton
berniec@theplcguy.com