> 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/your-first-dlp.md).

# Write your first DLP

Write, build, load and test a short DLP that counts how many times an input turns on.

Make Real Digital Input 1 cause a counter to increment by one each time it is switched from OFF to ON. It takes you through every step: write, build, load and test.

{% hint style="info" %}
**Before you start:** you should have successfully installed the DLP IDE software. For first time users, it is important to check the Compiler Options. See [Install and license the DLP IDE](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/install.md). You will need a serial port on your computer and a RS232 serial cable. Plug one end of the RS232 serial cable to the serial port of your computer and plug the other end to the DIAG (diagnostic) port of the RTU.
{% endhint %}

## 1. Start a new source file

Any DLP is made up of two sections.

```
proginit
progstart
progend
```

* The first is the Initialization (or INIT) section, which is only ever executed once at start-up (Start-up can be when the power is turned on, when the reset button is pressed, or when a new DLP is loaded into memory). All of the initialization commands lie between the `PROGINIT` and `PROGSTART` statements.
* The second is the main body of the DLP that lies between the `PROGSTART` and `PROGEND` statements. This part of the DLP is executed sequentially the whole time the RTU is turned on.

## 2. Type the program

```
PROGINIT
        TELINP                       ; Disconnect inputs
        TELOUT                       ; Disconnect outputs
        EQU      RDIN1,INPUT         ; Rename digital input 1
        EQU      SPD1,OLDINPUT       ; A spare digital to remember the last state
        EQU      NAIN1,COUNTER       ; Rename notional analogue input 1
        INITANL  COUNTER,0           ; Set the counter to zero
PROGSTART
        BEGIN
        TCE      INPUT               ; IF the input is on
        TCD      OLDINPUT            ; AND the old input is off
        INCANL   COUNTER             ; THEN add 1 to the counter
        SETDIG   OLDINPUT            ; and set the old input on
        END
        BEGIN
        TCD      INPUT               ; IF the input turned off
        RESDIG   OLDINPUT            ; THEN set the old input off
        END
PROGEND
```

What each part does:

| Lines              | What they do                                                                                                                                                                                                                                                                                                                             |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `TELINP`, `TELOUT` | Disconnects Real Inputs from Telemetry Inputs. Disconnects Real Outputs from Telemetry Outputs.                                                                                                                                                                                                                                          |
| `EQU`              | Define a symbol representing an I/O point or constant. The programmer can name specific I/O instead of using the nominal I/O identification making programming easier. It is advisable not to use dashes/hyphens in the identifier (new name of the point), as this will cause errors in the compiler. Never include spaces in the name. |
| `INITANL`          | Initialise the AnalogRegister with the AnalogConstant. Use this instruction to initialise analogue registers to preset values.                                                                                                                                                                                                           |
| First rung         | IF Input ON AND Old Input OFF THEN increment Counter. Set Oldinput to ON.                                                                                                                                                                                                                                                                |
| Second rung        | IF Input turned OFF THEN Set OldInput to OFF.                                                                                                                                                                                                                                                                                            |

Each line of the program may be blank, have a ; (semicolon) to indicate the following text is documentation only or an instruction. These parameters may be separated by either a space, tab or comma.

{% hint style="warning" %}
TELINP and TELOUT are indiscriminate and will sever the link to all IO. There is no way of just specifying a few points, so we need a way of reconnecting the points we don't want to modify. That's where `CPDIG` (Copy Digitals) and `CPANL` (Copy Analogs) come in. See [Module 02: Set up your DLP](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-02-set-up-your-dlp.md).
{% endhint %}

## 3. Save and build

1. Save the file. Source files have the extension `.asm`.
2. Press the **F9** key to compile the ASM file.
3. The **Show build result message** option pops up a message when the build has completed successfully or failed. See [My DLP won't build](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/troubleshooting/wont-build.md).

## 4. Load it into the RTU

1. Plug one end of the RS232 serial cable to the serial port of your computer and plug the other end to the DIAG (diagnostic) port of the RTU.
2. In the right click popup menu of the **Module Connectivity** pane, select **Upload DLP** - Upload a compiled DLP to the module.
3. In the same menu, select **Enabled DLP** - Run the DLP in the module.

{% hint style="info" %}
Upload DLP - Upload a compiled DLP to the module. Download DLP - Download the DLP from the module.
{% endhint %}

## 5. Watch it run

1. Right click on the **Watch List** and select **Add Watch** - Add a new I/O to be included in the watch list.
2. Use the **\[...]** button to open the symbol list box and select `INPUT` and `COUNTER`. The ASM or LDR file must be compiled first (press F9 key) to get a full list of I/O.

For more on testing, see [Test with the Watch List](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/compile-and-load.md#test-with-the-watch-list).

## Next steps

* Learn the principles step by step: [DLP self-help course](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course.md)
* If any pulses are shorter than the DLP execution time, counting the edges will not be reliable: pulses could occur in the time between DLP cycles, and as a result would not be sampled. The most robust method for pulse accumulation is to access the RTU hardware pulse accumulators via the DLP, enabling reliable sampling of signals up to 40Hz: see [Module 05: Pulse counting](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-05-pulse-counting.md).
