This example shows how to read and write digital signals using a digital I/O object by adding lines to the digital I/O object, and writing and reading data from the configured lines.
See also DIGITALIO, ADDLINE, DAQHELP, PROPINFO.
Note: This can only be run using the 32-bit version of MATLAB® and Data Acquisition Toolbox™. To learn more about using data acquisition devices on other platforms, see this example.
First, find any running data acquisition objects and stop them. This stops all running data acquisition objects from interfering with this example. This code is usually not necessary outside this example unless there are multiple data acquisition objects running.
if (~isempty(daqfind)) stop(daqfind) end
In this example, digital input/output interactions are explored. This example uses National Instruments® hardware.
To begin, create a digital I/O object associated with National Instruments hardware. Four output lines are added to the digital I/O object from port 0. This configuration allows you to write values to these lines.
dio = digitalio('nidaq', 'Dev1'); addline(dio, 0:3, 0, 'Out')
Index: LineName: HwLine: Port: Direction: 1 '' 0 0 'Out' 2 '' 1 0 'Out' 3 '' 2 0 'Out' 4 '' 3 0 'Out'
You can write values to the digital I/O lines as either a decimal value or as a binvec value. A binvec value is a binary vector, which is written with the least significant bit as the leftmost vector element and the most significant bit as the rightmost vector element. For example, the decimal value 23 is written in binvec notation as
[1 1 1 0 1]
The binvec value is converted to a decimal value as follows,
1*2^0 + 1*2^1 + 1*2^2 + 0*2^3 + 1*2^4
The toolbox provides you with two convenient functions for converting between decimal values and binvec values.
You can convert a decimal value to a binvec value with the DEC2BINVEC function.
binvec = dec2binvec(196)
binvec = 0 0 1 0 0 0 1 1
You can convert a binvec value to a decimal value with the BINVEC2DEC function.
decimal = binvec2dec([1 0 1 0 1 1])
decimal = 53
You can use the PUTVALUE function to write values to the digital I/O object's lines. You can write to a line configured with a direction of 'Out'. An error will occur if you write to a line configured with a direction of 'In'.
For example, to write the binvec value [ 0 1 0 0 ] to the digital I/O object, dio
putvalue(dio, [0 1 0 0]);
Similarly, you can write to a subset of lines as follows.
putvalue(dio.Line([ 1 3 ]), [ 1 1 ]);
The lines not written to remain at their current values (in this example, lines with index 2 and index 4 remain at [1 0], respectively).
It is important to note that if a decimal value is written to a digital I/O object, and the value is too large to be represented by the object, an error is returned. Otherwise, the word is padded with zeros.
In this example, the digital I/O object contains four lines. Therefore, the largest decimal value allowed is 15 or
(1*2^0 + 1*2^1 + 1*2^2 + 1*2^3).
If the decimal value 5 is written to the digital I/O object, only a 3-element binvec vector is needed to represent the decimal value. The remaining line will have a value of 0 written to it. Therefore, the following two commands are equivalent.
putvalue(dio, 5); putvalue(dio, [1 0 1 0]);
You can read the values of the digital I/O lines with the GETVALUE function. GETVALUE always returns a binvec value. You can read from a line configured with a direction of either 'In' or 'Out'.
value = getvalue(dio)
value = 1 0 1 0
You can convert the binvec value to a decimal value with the BINVEC2DEC function.
value = binvec2dec(getvalue(dio))
value = 5
Similarly, you can read a subset of lines as follows.
getvalue(dio.Line([ 1 3 ]))
ans = 1 1
When you are done, you should clean up any unneeded data acquisition objects. Delete the digital I/O object and clear it from the workspace.
delete (dio); clear dio;