Generate Timer Events


The fact that analog input and analog output objects make use of data stored in the engine and clocked I/O leads to the concept of a "running" device object and the generation of events.

However, because Data Acquisition Toolbox™ software does not support buffered digital I/O (DIO) operations, DIO objects do not store data in the engine. Additionally, reading and writing line values are not clocked at a specific rate in the way that data is sampled by an analog input or analog output subsystem. Instead, values are either written directly to digital lines with putvalue, or read directly from digital lines with getvalue.

Therefore, the concept of a running DIO object does not make sense in the same way that it does for analog I/O. However, you can "run" a DIO object to perform one task: generate timer events. You can use timer events to update and display the state of the DIO object. Refer to the diopanel example.

Timer Events

The only event supported by DIO objects is a timer event. Timer events occur after a specified period of time has passed. Properties associated with generating timer events are given below.

Table 10-4. Digital I/O Timer Event Properties

Property Name



Indicate if the device object is running.


Specify the callback function to execute whenever a predefined period of time passes.


Specify the period of time between timer events.

A timer event is generated whenever the time specified by TimerPeriod passes. This event executes the callback function specified for TimerFcn. Time is measured relative to when the device object starts running (Running is On). Starting a DIO object is discussed in the next section.

Some timer events might not be processed if your system is significantly slowed or if the TimerPeriod value is too small. For example, a common application for timer events is to display data. However, because displaying data can be a CPU-intensive task, some of these events might be dropped. For digital I/O objects, timer events are typically used to display the state of the object.

To see how to construct a callback function, refer to Create and Execute Callback Functions or the example below.

Start and Stop a Digital I/O Object

You use the start function to start a DIO object. For example, to start the digital I/O object dio:


After start is issued, the Running property is automatically set to On, and timer events can be generated. If you attempt to start a digital I/O object that does not contain any lines or that is already running, an error is returned.

A digital I/O object will stop executing under these conditions:

  • The stop function is issued.

  • An error occurred in the system.

When the device object stops, Running is automatically set to Off.

Generate Timer Events

This example illustrates how to generate timer events for a DIO object. The callback function daqcallback displays the event type and device object name. Note that you must issue a stop command to stop the execution of the object.

You can run this example by typing daqdoc7_2 at the MATLAB Command Window.

  1. Create a device object — Create the digital I/O object dio for a National Instruments® board. The installed adaptors and hardware IDs are found with daqhwinfo.

    dio = digitalio('nidaq','Dev1');
  2. Add lines — Add eight input lines from port 0 (line-configurable).

  3. Configure property values — Configure the timer event to call daqcallback every five seconds.

    dio.TimerFcn = @daqcallback
    dio.TimerPeriod = 5.0

    Start the digital I/O object. You must issue a stop command when you no longer want to generate timer events.


    The pause command ensures that two timer events are generated when you run daqdoc7_2 from the command line.

  4. Clean up — When you no longer need dio, you should remove it from memory and from the MATLAB workspace.

    clear dio
Was this topic helpful?