> 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/timers-logging-macros.md).

# Timers, logging, COS and macros

TMR, TMV and the timer registers, SENDCOS, the logging instructions, and the macro instructions.

## Timers

The DLP has a stack of 128 timer registers available for the programmer to use for various timing functions throughout the DLP, catering for delays from 100 millisecond to 109 minutes. Each timer has three system variables associated with it, where *n* is the number of the counter register, so can be from 1 to 128:

| Register    | Type                | What it holds                                                         |
| ----------- | ------------------- | --------------------------------------------------------------------- |
| `TIMERS`*n* | Digital             | A Start flag which is True while the timer is running                 |
| `TIMERD`*n* | Digital             | A Delay flag which becomes True when the timer counter reaches zero   |
| `TIMERC`*n* | Analogue, read only | A counter which when set will decrement from the Set\_Value down to 0 |

### TMR

**Syntax:** `TMR TimerNumber, Duration`

`TMR` runs a timer using a constant as the timer duration. `Duration` is the delay time and requires 0 to 65536 \* 100mS.

Three things can happen when the `TMR` or `TMV` command is executed:

* If the timer is not already running, and the Logic Accumulator is TRUE, the timer will be started and the counter duration will be set. The Set\_Value will be loaded into its corresponding `TIMERC`, and `TIMERS` becomes true when the timer starts.
* If the timer has already started, and the Logic Accumulator is TRUE, the timer will be maintained and the timer counter will decrement accordingly. Once `TIMERC` equals zero, `TIMERD` will change from false to true, and it remains true until the timer is reset. The counter will not decrement below zero.
* If the Logic Accumulator is FALSE, the timer will be stopped and reset: the timer will cease operating and both `TIMERS` and `TIMERD` will be set to false.

```
TCE  RDIN1      ; If digital input 1 is on,
TMR  1,20       ; start timer 1 for 2 seconds.
TCE  TIMERD1    ; When the 2 seconds are up,
EC   RDOUT1     ; turn on digital output 1.

TCE  TIMERS1    ; While timer 1 is running,
EC   RDOUT2     ; digital output 2 is on.
```

### TMV

**Syntax:** `TMV TimerNumber, AnalogRegister`

The instruction `TMV` is very similar to the `TMR` instruction, used for creating a variable delay timing function. `TMV` runs a timer but allows the programmer to pass a variable to the timer function so the duration can be changed at runtime. `Reg_Value` is the delay time which reads from the analogue register. There are a total of 128 timers (`TMR` timers and `TMV` timers).

```
TCE  RDIN1        ; If digital input 1 is on,
TMV  1, NAOUT1    ; start timer 1, for the time in NAOUT1.
TCE  TIMERD1      ; When it's done,
EC   RDOUT1       ; turn on digital output 1.
```

{% hint style="warning" %}
Please remember that a timer cannot be used repeatedly in the same DLP.
{% endhint %}

For a worked example of using a timer to delay an alarm, see [Module 03: Digitals and DLP timers](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-03-digitals-and-timers.md).

## SENDCOS

**Syntax:** `SENDCOS`

When the accumulator is true and this instruction is executed, the RTU will do a COS when the DLP has finished. The result will be sent to the base.

{% hint style="warning" %}
If this instruction is used continuously then the RTU remote will continuously send COS to the base. Use it sparingly.
{% endhint %}

## Logging

These instructions control the RTU's data logging from the DLP. The points must already be set up for logging, for example with RTU data logging in DATRAN VI. See [Set up RTU data logging in DATRAN VI](https://help.qtech.co.nz/datran/datran-telemetry/datran-xl4-plus-rtu/data-logging/set-up-rtu-data-logging.md).

### LOGGINGOFF and LOGGINGON

**Syntax:** `LOGGINGOFF`, `LOGGINGON`

`LOGGINGOFF` suspends all logging on the RTU (event and interval logging), including base-driven events and intervals. `LOGGINGON` resumes it. These instructions are used to globally disable or enable any configured interval and event logging. The data logging configuration will not change; it will merely become inactive or active. By default, logging will be on unless the DLP specifies otherwise.

### LOGINTS and LOGINTR

**Syntax:** `LOGINTS Point, AnalogConstant` or `LOGINTR Point, AnalogRegister`

Cause the nominated point to begin logging at the interval specified in the analogue constant (`LOGINTS`) or the analogue register (`LOGINTR`). The second parameter is a 16-bit unsigned number in seconds, and must lie in the range 2 - 86400. A period of 0 will halt interval logging on this point.

* Only valid for Telemetry Digital Input (TDIN), Telemetry Analogue Input (TAIN), Notional Input (NDIN), Notional Analogue Input (NAIN) register types.
* Logging will be synchronized with the real time clock as the base-specified interval logs are. Since interval logging is tied to the absolute time, the timing of the next log entry is indeterminate.
* This instruction alters the logging rate without deleting the current logged data. This will not affect the Setup ID since the RTU simply stores what DATRAN has told it, and does not calculate it itself from the configuration. Thus DATRAN will not clear the data when it does a new request.

### LOGPOINT

**Syntax:** `LOGPOINT Point`

Cause the nominated point to be event logged at that instant in time. This instruction allows a DLP to trigger the logging of an analogue or digital event for any number of points.

* Only valid for Telemetry Digital Input (TDIN), Telemetry Analogue Input (TAIN), Notional Input (NDIN), Notional Analogue Input (NAIN) register types. It is only possible to log points that have already been configured for event logging using either Q90 Enhanced Data Logging (QEDL) or DATRAN VI Historical Data Logging (DHDL).
* The point names may be specified using either the reserved keyword names, such as `NDIN1`, `NAIN4` or names assigned using the `EQU` function.
* The instruction returns TRUE if the event is logged successfully, FALSE otherwise (e.g. if log is full).
* When retrieving data logged this way, it will be indistinguishable from an event log caused by one of the existing triggers.

## Macros

For how to use macros, see [Reuse code with macros](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/source-editor.md#reuse-code-with-macros).

| Instruction | Syntax                                         | What it does                                                                                                                                         |
| ----------- | ---------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| MacroName   | `MacroName Name`                               | This identifies a Macro, with replaceable parameters.                                                                                                |
| MacroDig    | `MacroDig Parameter [, Parameter]...`          | Specifies digital parameters to be passed to and from the Macro.                                                                                     |
| MacroAnl    | `MacroAnl Parameter [, Parameter]...`          | Specifies analogue parameters to be passed to and from the Macro.                                                                                    |
| MacroTmr    | `MacroTmr [TimerNumber, ]...`                  | Allocates timers. When timers are used in a macro, the compiler uses timer 128 first, then timer 127 and so on.                                      |
| MacroLab    | `MacroLab Label [, Label]...`                  | This informs the compiler that the macro will use one or more labels.                                                                                |
| MacroEnd    | `MacroEnd`                                     | This instruction tells the compiler that the macro has ended.                                                                                        |
| MacroDef    | `MacroDef Name [(Parameter [, Parameter]...)]` | This statement can be used to define an inline Macro. `MacroTmr` and/or `MacroLab` commands may be included in the macro code.                       |
| Macro       | `Macro Name [, Parameter]...`                  | This instruction tells the compiler to look for a macro with the name specified, and start inserting its code. `Name(Parameter, ...)` does the same. |
| MacroFile   | `MacroFile FileName`                           | Includes a `.LIB` file of macros. If a folder is not specified, the `*.LIB` files must be in the same folder as the `*.ASM` file.                    |

During compilation, a warning message will be generated if the macro uses a timer that has been used in the DLP code.
