Data Acquisition Toolbox | ![]() ![]() |
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 line.
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',1);
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
![]() | Reading Digital Values | Generating Timer Events | ![]() |