Transmitting Data over the SPI Interface
The typical workflow for transmitting data over the SPI interface involves adaptor
discovery, connection, communication, and cleanup. Discovery can be done only at the
adaptor level. You must have a Total Phase Aardvark adaptor or an NI-845x adaptor board
installed to use the spi
interface.
Transmit Data Over SPI Using Aardvark
This example shows how to communicate with an EEPROM chip on a circuit board, with a board index of 0 and using port 0.
Ensure that the Aardvark adaptor is installed so that you can use the
spi
interface.Look at the adaptor properties.
Make sure that you have the Aardvark software driver installed and that the
aardvark.dll
is on your MATLAB® path. For details, see SPI Interface Usage Requirements and Guidelines.Create the SPI object called
S
, using these properties:% Vendor = aardvark % BoardIndex = 0 % Port = 0 S = spi('aardvark', 0, 0);
You must provide these three parameters to create the object.
Look at the object properties.
When you create the
spi
object, default communication settings are used, as shown here. To change any of these settings, see Using Properties on the SPI Object for more information and a list of the properties.Connect to the chip.
connect(S);
Read and write to the chip.
% Create a variable containing the data to write dataToWrite = [3 0 0 0]; % Write the binary data to the chip write(S, dataToWrite); % Create a variable that contains the number of values to read numData = 5; % Read the binary data from the chip data = read(S, numData);
Disconnect the SPI device and clean up by clearing the object.
disconnect(S); clear('S');
Transmit Data Over SPI Using NI-845x
This example shows how to communicate with an EEPROM chip on a circuit board, with a board index of 0 and using port 0.
Ensure that the NI-845x adaptor is installed so that you can use the
spi
interface.Look at the NI-845x adaptor properties.
Make sure that you have the NI-845x software driver installed. For details, see SPI Interface Usage Requirements and Guidelines.
Create the SPI object called
s2
, using these properties:% Vendor = ni845x % BoardIndex = 0 % Port = 0 s2 = spi('ni845x', 0, 0);
You must provide these three parameters to create the object.
Look at the object properties.
When you create the
spi
object, default communication settings are used, as shown here. To change any of these settings, see Using Properties on the SPI Object for more information and a list of the properties.Connect to the chip.
connect(s2);
Read and write to the chip.
% Create a variable containing the data to write dataToWrite = [3 0 0 0]; % Write the binary data to the chip write(s2, dataToWrite); % Create a variable that contains the number of values to read numData = 5; % Read the binary data from the chip data = read(s2, numData); ans = 0 0 0 0 0
Disconnect the SPI device and clean up by clearing the object.
disconnect(s2); clear('s2');
SPI Functions
You can use these functions with the spi
object.
Note
SPI is a full duplex communication protocol, and data must be written in order to
read data. You can use the read
function to write dummy data to
the device. The write
function flushes the data returned by the
device. The writeAndRead
function does the read and write
together.
Function | Purpose |
---|---|
instrhwinfo | Check that the Aardvark and/or NI-845x adaptor is installed.
Look at the adaptor properties.
|
spiinfo | Returns information about devices and displays the information on a per vendor basis.
|
connect | Connect the SPI object to the device. Use this syntax:
|
read | Synchronously read binary data from the device. To read data,
first create a variable, such as
Or you can use this syntax:
|
write | Synchronously write binary data to the device. To write data,
first create a variable, such as
|
writeAndRead | Synchronously do a simultaneous read and write of binary data
with the device. In this case, the function synchronously writes the
data specified by the variable
|
disconnect | Disconnect SPI object from the device. Use this syntax:
|
Note
To get a list of options you can use on a function, press the Tab key after entering a function on the MATLAB command line. The list expands, and you can scroll to choose a property or value. For information about using this advanced tab completion feature, see Using Tab Completion for Functions.