> For the complete documentation index, see [llms.txt](https://help.qtech.co.nz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/instruction-reference/analogue.md).

# Analogue comparison and arithmetic

Compare analogue values with setpoints or other registers, and add, subtract, multiply, divide, increase and decrease them.

Analogue registers hold values between 0 and 65535. The comparison instructions set the logic accumulator to true, otherwise they set it to false. The RTU maintains two analogue system registers called the Analogue Accumulators, `ANLACC1` and `ANLACC2`, which store the results of arithmetic functions.

Each instruction comes in two forms. The **R** form works on two registers. The **S** form works on a register and a constant (setpoint). The setpoint value must be in the decimal range 0 to 65535. The programmer must select the appropriate command as they are not interchangeable.

## Compare analogue values

Each comparison has an implicit logical AND built in.

| Register form | Setpoint form  | True when                     |
| ------------- | -------------- | ----------------------------- |
| `EQR A1, A2`  | `EQS A, value` | equal                         |
| `NER A1, A2`  | `NES A, value` | not equal                     |
| `GTR A1, A2`  | `GTS A, value` | the first is greater          |
| `GER A1, A2`  | `GES A, value` | the first is greater or equal |
| `LTR A1, A2`  | `LTS A, value` | the first is less             |
| `LER A1, A2`  | `LES A, value` | the first is less or equal    |

```
GES  RAIN1, 1234   ; True if analogue input 1 is 1234 or more
GTR  RAIN1, RAIN2  ; True if analogue input 1 is greater than analogue input 2
```

{% hint style="warning" %}
`EQR`, `EQS`, `NER` and `NES` should not be used when comparing real analogue inputs as analogue values tend to change rapidly. Be aware that real analogue inputs change continuously, therefore comparing them against set values without using hysteresis can result in erratic behaviour of any controlled outputs and of the digital accumulator. See [Module 04](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-04-analogue-setpoints.md).
{% endhint %}

## The analogue accumulators

| Register  | Holds                                                                                                                                                             |
| --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ANLACC1` | Stores the primary result of arithmetic functions                                                                                                                 |
| `ANLACC2` | Stores (when appropriate) things like a remainder or an overflow: the overflow (addition), the most significant word (multiplication) or the remainder (division) |

Use `CPANL` to copy the result from `ANLACC1`:

```
CPANL  ANLACC1, NAIN1, 1   ; Copy the result to NAIN1
CPANL  ANLACC1, NAIN1, 2   ; Copy ANLACC1 to NAIN1 and ANLACC2 to NAIN2
```

## ADDR and ADDS: add

**Syntax:** `ADDR AnalogRegister1, AnalogRegister2` or `ADDS AnalogRegister, AnalogConstant`

The result of the addition is placed into the system analogue `ANLACC1`. If the result is greater than 65535 then `ANLACC1` wraps around to zero and counts up, and 1 is placed in `ANLACC2`.

| First number | Second number | Actual result | ANLACC1 | ANLACC2 |
| ------------ | ------------- | ------------- | ------- | ------- |
| 50,000       | 10,000        | 60,000        | 60,000  | 0       |
| 60,000       | 5,535         | 65,535        | 65,535  | 0       |
| 60,000       | 5,536         | 65,536        | 0       | 1       |
| 60,000       | 10,000        | 70,000        | 4,464   | 1       |

```
INITANL  SPA1, 45
INITANL  SPA2, 34
ADDR     SPA1, SPA2        ; ANLACC1 = 79
CPANL    ANLACC1, NAIN1, 1
```

## SUBR and SUBS: subtract

**Syntax:** `SUBR AnalogRegister1, AnalogRegister2` or `SUBS AnalogRegister, AnalogConstant`

This instruction subtracts an analogue from another analogue (the second value is subtracted from the first), placing the result in the system analogue, `ANLACC1`. `SUBS` subtracts a set-point from an analogue. If the result is less than zero then zero is placed in `ANLACC1`.

| First number | Second number | Actual result | ANLACC1 |
| ------------ | ------------- | ------------- | ------- |
| 50,000       | 10,000        | 40,000        | 40,000  |
| 100          | 100           | 0             | 0       |
| 100          | 200           | −100          | 0       |

```
SUBR  NAIN1, NAIN2   ; ANLACC1 = NAIN1 − NAIN2
SUBS  NAIN1, 1234    ; ANLACC1 = NAIN1 − 1234
```

## MULR and MULS: multiply

**Syntax:** `MULR AnalogRegister1, AnalogRegister2` or `MULS AnalogRegister, AnalogConstant`

This multiplies two analogues (`MULS` multiplies an analogue with a constant). The result is placed into `ANLACC1` and `ANLACC2`. If the result is greater than 65535, then the result is calculated as a 32-bit unsigned number where `ANLACC2` is the most significant word. In other words the result can be found by (`ANLACC2 *65536 + ANLACC1`).

| First number | Second number | Actual result | ANLACC1 | ANLACC2 |
| ------------ | ------------- | ------------- | ------- | ------- |
| 30,000       | 2             | 60,000        | 60,000  | 0       |
| 60,000       | 2             | 120,000       | 54,464  | 1       |
| 60,000       | 20            | 1,200,000     | 20,352  | 18      |

```
INITANL  SPA1, 15
MULS     SPA1, 17          ; 15 × 17 = 255
CPANL    ANLACC1, NAIN1, 2 ; NAIN1 = 255, NAIN2 = 0
```

## DIVR and DIVS: divide

**Syntax:** `DIVR AnalogRegister1, AnalogRegister2` or `DIVS AnalogRegister, AnalogConstant`

Divides the first value by the second. The result quotient is placed in `ANLACC1` and the remainder in `ANLACC2`. If the divisor is zero, then a zero will be placed in `ANLACC1` and `ANLACC2`.

| First number | Second number | Actual result | ANLACC1 | ANLACC2 |
| ------------ | ------------- | ------------- | ------- | ------- |
| 10           | 2             | 5             | 5       | 0       |
| 15           | 2             | 7.5           | 7       | 1       |
| 33           | 7             | 4.7142        | 4       | 5       |

```
INITANL  SPA1, 145
DIVS     SPA1, 17          ; 145 / 17 = 8 remainder 9
CPANL    ANLACC1, NAIN1, 2 ; NAIN1 = 8, NAIN2 = 9
```

## INCANL and DECANL: add or take away 1

**Syntax:** `INCANL AnalogRegister` or `DECANL AnalogRegister`

`INCANL` increments an analogue by 1 and `DECANL` decrements it by 1. You can use them to increment or decrement a nominated analogue register and not have to worry about copying analog accumulators.

* If a register is already at 65535 and the DLP tries to increment it using `INCANL`, the value will stay at 65535.
* If the register is at 0 and the DLP tries to decrement it using `DECANL`, the value will remain at 0.

```
INCANL  RAOUT1   ; Add 1 to analogue output 1
```

## Show a 32-bit result in DATRAN VI

To be able to use the total value of `ANLACC1` and `ANLACC2` in DATRAN VI, the following needs to be done:

1. In the DLP, place the results into Notional Analogue 1 and Notional Analogue 2, for example `CPANL ANLACC1, NAIN1, 2`. `ANLACC2` holds the highest word, and `ANLACC1` holds the remainder.
2. Create a node for the ANLACC Result (i.e ANLACC\_Result), and add a `_Formula_` node.
3. Enter the following formula: `[..\ANLACC1] + (([..\ANLACC2] == 0) ? 0 : (([..\ANLACC2]*65535)+1))`

DATRAN VI does not display a negative value. If due to subtraction the result is less than 0, `ANLACC1` will display 0 value.
