Writing and Reading Digital I/O Line Values

Writing Digital Values

You write values to digital lines with the putvalue function. putvalue requires the DIO object and the values to be written as input arguments. You can specify the values to be written as a decimal value or as a binary vector (binvec). A binary vector is a logical array that is constructed with the least significant bit (LSB) in the first column and the most significant bit (MSB) in the last column. For example, the decimal value 23 is written in binvec notation as [1 1 1 0 1] = 20 + 21 + 22 + 24. You might find that binvecs are easier to work with than decimal values because there is a clear association between a given line and the value (1 or 0) that is written to it. You can convert decimal values to binvec values with the dec2binvec function.

For example, suppose you create the digital I/O object dio and add eight output lines to it from port 0.

dio = digitalio('nidaq','Dev1');
addline(dio,0:7,'out');

To write a value of 23 to the eight lines contained by dio, you can write to the device object.

data = 23;
putvalue(dio,data)

Alternatively, you can write to individual lines through the Line property.

putvalue(dio.Line(1:8),data)

To write a binary vector of values using the device object and the Line property:

bvdata = dec2binvec(data,8);
putvalue(dio,bvdata)
putvalue(dio.Line(1:8),bvdata)

The second input argument supplied to dec2binvec specifies the number of bits used to represent the decimal value. Because the preceding commands write to all eight lines contained by dio, an eight element binary vector is required. If you do not specify the number of bits, then the minimum number of bits needed to represent the decimal value is used.

Alternatively, you can create the binary vector without using dec2binvec.

bvdata = logical([1 1 1 0 1 0 0 0]);
putvalue(dio,bvdata)

Rules for Writing Digital Values

Writing values to digital I/O lines follows these rules:

Reading Digital Values

You can read values from one or more lines with the getvalue function. getvalue requires the DIO object as an input argument. You can optionally specify an output argument, which represents the returned values as a binary vector. Binary vectors are described in Writing Digital Values.

For example, suppose you create the digital I/O object dio and add eight input lines to it from port 0.

dio = digitalio('nidaq','Dev1');
addline(dio,0:7,'in');

To read the current value of all the lines contained by dio:

portval = getvalue(dio)
portval =
     1     1     1     0     1     0     0     0

To read the current values of the first five lines contained by dio:

lineval = getvalue(dio.Line(1:5))
lineval =
     1     1     1     0     1

You can convert a binvec to a decimal value with the binvec2dec function. For example, to convert the binary vector lineval to a decimal value:

out = binvec2dec(lineval)
out =
    23

Rules for Reading Digital Values

Reading values from digital I/O lines follows these rules:

Example: Writing and Reading Digital Values

This example illustrates how to read and write digital values using a line-configurable subsystem. With line-configurable subsystems, you can transfer values on a line-by-line basis.

You can run this example by typing daqdoc7_1 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 output lines from port 0 (line-configurable).

    addline(dio,0:7,'out');
  3. Read and write values — Write a value of 13 to the first four lines as a decimal number and as a binary vector, and read back the values.

    data = 13;
    putvalue(dio.Line(1:4),data)
    val1 = getvalue(dio);
    bvdata = dec2binvec(data);
    putvalue(dio.Line(1:4),bvdata)
    val2 = getvalue(dio);

    Write a value of 3 to the last four lines as a decimal number and as a binary vector, and read back the values.

    data = 3;
    putvalue(dio.Line(5:8),data)
    val3 = getvalue(dio.Line(5:8));
    bvdata = dec2binvec(data,4);
    putvalue(dio.Line(5:8),bvdata)
    val4 = getvalue(dio.Line(5:8));

    Read values from the last four lines but switch the most significant bit (MSB) and the least significant bit (LSB).

    val5 = getvalue(dio.Line(8:-1:5));
  4. Clean up — When you no longer need dio, you should remove it from memory and from the MATLAB workspace.

    delete(dio)
    clear dio

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS