> 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/digital-logic.md).

# Digital logic, outputs and jumps

BEGIN, END, TCE, TCD, NOT, EC, ECC, SETDIG, RESDIG, TOGGLE, OR, NOR, XOR and the jump instructions.

These instructions test digital points, combine the tests, write the outcome to outputs, and skip sections of the program.

## How the logic accumulator works

The "Logic Accumulator" is a system register that stores the true/false result of the previous logic operation. The state of the Logic Accumulator affects the behaviour of almost every command in the DLP.

* `BEGIN` sets it to true.
* Each test (`TCE`, `TCD`, or an analogue comparison) has an implicit logical AND built in: it tests whether the logic accumulator is true AND the digital register is on (or off, for `TCD`), and then loads the result into the logic accumulator.
* Output instructions act on it.

When two or more tests are strung together the logic would read something like "If Input #1 is true AND Input #2 is true AND Input #3 is greater than 50%, then...". The DLP executes sequentially from the top to the bottom, and the state the DLP has modified is only written at the end of the DLP execution. So, the LAST operation in the DLP that writes to a specific output will have the control over what that output does, even if several other parts of the DLP have also written to it previously.

## BEGIN

**Syntax:** `BEGIN`

Denotes the beginning of a logic rung - sets ACC True. The `BEGIN` instruction is used at the beginning of a logic rung or procedure. It initialises the DLP accumulators so that the following logic will operate correctly.

## END

**Syntax:** `END`

Denotes the end of a logic rung. The `END` instruction can be used at the end of a logic rung. This is not a compulsory instruction to end a logic rung, but helps with reading the code. When converting from Ladder to ASM each rung will automatically end with an `END`.

## TCE

**Syntax:** `TCE DigitalRegister`

Test if DigitalRegister is energised (set, true). This instruction tests a digital I/O. The accumulator will be set to true if the digital is true, on, energised or closed and the accumulator was previously true.

```
TCE  NDIN8    ; Is notional digital input 8 on?
```

## TCD

**Syntax:** `TCD DigitalRegister`

This instruction tests a digital I/O. The accumulator will be set to true if the digital is false, off, de-energised or open and the accumulator was previously true.

```
TCD  RDOUT12  ; Is real digital output 12 off?
```

{% hint style="warning" %}
Be careful when using `EC` (or `ECC`) in conjunction with `TCD` as the result may not be what you were expecting. If `TCD RDIN1` then `EC TDIN1` is used instead of `TCE`, the value that was written to the telemetry input would be the OPPOSITE of the real input, not the same!
{% endhint %}

## NOT

**Syntax:** `NOT`

Invert the state of the accumulator. The `NOT` instruction inverts the accumulator and is normally used to logically NAND digital I/O. Alternatively, `TCD` can be used for this operation instead.

```
TCE  RDIN1    ; Test digital input 1,
NOT           ; invert the result,
TCE  RDIN2    ; and AND it with digital input 2: (NOT RDIN1) AND RDIN2
```

## EC

**Syntax:** `EC DigitalRegister`

Copy ACC to the DigitalRegister and end the logical rung. `EC` stands for "Energise Contact". The register will be turned ON if the logic accumulator is TRUE, and will be turned off if the logic accumulator is FALSE. `EC` is designed to be used at the end of a logic rung, and will reset the logic accumulator to TRUE after it has executed.

```
TCD  RDIN34   ; If digital input 34 is off
TCE  RDIN24   ; and digital input 24 is on,
EC   RDOUT3   ; turn digital output 3 on; otherwise off
```

## ECC

**Syntax:** `ECC DigitalRegister`

Copy ACC to the DigitalRegister and leave the result in ACC. `ECC` ("Energise contact and then continue") copies the accumulator to a Digital I/O, leaving the result in the accumulator, so the rung may continue.

```
TCD  RDIN34
TCE  RDIN24
ECC  RDOUT3   ; Write the result to outputs 3,
ECC  RDOUT4   ; 4
EC   RDOUT5   ; and 5, then end the rung
```

## SETDIG

**Syntax:** `SETDIG DigitalRegister`

Set DigitalRegister if the accumulator is set. This instruction tests the accumulator. If the accumulator was True, the digital I/O is set to True. If the accumulator was False, there is no effect on the Digital I/O.

```
SETDIG  RDOUT12   ; If the accumulator is true, turn output 12 on
```

## RESDIG

**Syntax:** `RESDIG DigitalRegister`

Reset Digital Register if accumulator is set. This instruction tests the accumulator. If the accumulator was True, the digital I/O is set to False. If the accumulator was False, there is no effect on the digital I/O.

```
RESDIG  NDOUT12   ; If the accumulator is true, turn notional output 12 off
```

## TOGGLE

**Syntax:** `TOGGLE DigitalRegister`

Toggle the DigitalRegister if the accumulator is set. This instruction toggles the digital I/O if the accumulator is on.

```
TCD     RDIN34    ; If digital input 34 is off,
TOGGLE  RDOUT24   ; change the state of digital output 24
```

{% hint style="warning" %}
If the input remains true for each cycle through the DLP, an output can toggle on/off at the rate of the DLP execution rate. See the change-detection example in [Module 07](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-07-logic-and-jumps.md).
{% endhint %}

## OR and OREND

**Syntax:** `OR` … `OREND`

Logically OR digital registers inside an OR rung. Use this instruction if digital I/O are to be logical OR'ed together. `OREND` must be placed at the end of an OR rung. It tells the program to finish the OR(s). The result is stored in the accumulator.

```
TCE  RDIN1
OR
TCE  RDIN2
OR
TCE  RDIN3
OREND         ; RDIN1 OR RDIN2 OR RDIN3
```

It is important to use the correct order that statements appear in such a program, as the order can change the result dramatically. `TCE A`, `TCE B`, `OR`, `TCE C`, `OREND` would actually give a result of (A and B) or C, as the DLP executes in a sequential manner and the and function has a higher level of precedence than the OR function. To achieve A and (B or C), it is necessary to rearrange it so that the OR part of it comes first. The structure then becomes (B or C) and A. See [Module 07](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-07-logic-and-jumps.md).

## NOR and NOREND

**Syntax:** `NOR` … `NOREND`

Logically NOR digital registers inside a NOR rung. The NOR and NOREND commands work in the same manner as the OR and OREND commands. Use this instruction if the digital I/O are to be logically not OR'ed together. `NOREND` must be placed at the end of a NOR rung, telling the program to finish the NOR(s) and put the result into the accumulator.

## XOR and XOREND

**Syntax:** `XOR` … `XOREND`

Logically XOR digital registers inside an XOR rung. The `XOR` instruction follows the logic table given below. The result of an XOR is stored in the accumulator. `XOREND` must be placed at the end of an XOR rung, telling the program to finish the XORs.

| Input 1 | Input 2 | Result |
| ------- | ------- | ------ |
| Off     | Off     | Off    |
| Off     | On      | On     |
| On      | Off     | On     |
| On      | On      | Off    |

```
TCE  RDIN1
XOR
TCE  RDIN2
XOR
TCE  RDIN3
XOREND        ; RDIN1 XOR (RDIN2 XOR RDIN3)
```

`XOR` is widely used in code that is used to detect the change of state of a digital value. See [Module 07](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-07-logic-and-jumps.md).

## Jumps and labels

A jump statement allows the programmer to skip certain sections of code in the DLP if specific criteria are not met. The program steps which are jumped will not be executed by the RTU. There are two reasons why we would want to do this: the section of code contains commands that will always execute such as `INITANL` or `INITDIG`, and improving performance by jumping around sections of code that are not necessary.

| Instruction             | Jumps                            |
| ----------------------- | -------------------------------- |
| `JUMPA LabelIdentifier` | Always, whatever the accumulator |
| `JUMPT LabelIdentifier` | If the accumulator is true       |
| `JUMPF LabelIdentifier` | If the accumulator is false      |

A label is a user-defined piece of text that shows the compiler where the destination of a jump command is. You can use `Label1` to `Label255`, or your own name. This way the compiler can calculate the number of bytes to jump for you.

```
    TCE   DIN1      ; If digital input 1 is false,
    JUMPF Label2    ; jump to Label2.
    ECC   DOUT1     ; Otherwise, write the result to
    ECC   DOUT34    ; outputs 1 and 34.
Label2
    TCE   DIN2      ; This line always runs.
```

In the above example Digital Outputs 1 and 34 will always be set to True.

Things to know:

* **EQULAB:** With older versions of the DLP compiler, it was necessary to define a label in the `PROGINIT` section of the DLP using a `EQULAB` command. This is no longer necessary as the compiler is now smart enough to identify these labels without predefining them. `EQULAB` is still supported for backwards compatibility. In the ladder editor, a label needs to be declared before a jump statement, and with **Inhibit unequated label identifiers** checked, named labels have to be equated before they can be used in the `PROGSTART` section.
* **Jump forwards.** Jump statements to a label must be placed before the label statement. If the label is placed before the jump statement, compiling the ladder will cause a compiler error to occur.
* **Labels, not line counts.** In older compilers (before version 2) it was possible to jump a number of lines, now it is only possible to jump to specific labels.
