Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

spi

Create SPI object

Syntax

  • S = spi(Vendor,BoardIndex,Port);
    example

Description

example

S = spi(Vendor,BoardIndex,Port); constructs an spi object associated with Vendor, BoardIndex, and Port. Vendor must be set to either 'aardvark', for use with a Total Phase Aardvark adaptor, or to 'ni845x', for use with the NI-845x adaptor board, to use this interface. BoardIndex specifies the board index of the hardware and is usually 0. Port specifies the port number within the device and must be set to 0.

SPI, or Serial Peripheral Interface, is a synchronous serial data link standard that operates in full duplex mode. Instrument Control Toolbox™ SPI support lets you open connections with individual chips and to read and write over the connections to individual chips using an Aardvark host adaptor.

The primary uses for the spi interface involve the write, read, and writeAndRead functions for synchronously reading and writing binary data. To identify SPI devices in the Instrument Control Toolbox, use the instrhwinfo function on the SPI interface, called spi.

Once the SPI object is created, there are properties that can be used to change communication settings. These includes properties for clock speed, clock phase, and clock polarity. For a list of all the properties and information about setting them, see the link for "Using Properties on the SPI Object" at the end of the Examples section.

    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.

Examples

collapse all

This example shows how to create a SPI object and communicate with a SPI device, using an Aardvark adaptor board.

Ensure that the Aardvark adaptor is installed so that you can use the spi interface, and then look at the adaptor properties.

instrhwinfo('spi')
instrhwinfo('spi', 'aardvark') 
ans = 

           VendorName: 'aardvark'
    VendorDescription: 'Total Phase I2C/SPI Driver'
    VendorLibraryName: 'aardvark.dll'
    InstalledBoardIds:  {[0]}
   BoardSerialNumbers:  {'2237722838'}
   ObjectConstructors:  {'spi('aardvark', 0, 0)'}

Construct a spi object called S using Vendor 'aardvark', with BoardIndex of 0, and Port of 0.

S = spi('aardvark', 0, 0); 

You can optionally change property settings such as BitRate, ClockPhase, or ClockPolarity. For example, set the ClockPhase from the default of FirstEdge.

S.ClockPhase = 'SecondEdge' 

For a list of all the properties and information about setting them, see the link for "Using Properties on the SPI Object" at the end of the Examples section.

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 to contain 5 bytes of returned data
numData = 5 

% Read the binary data from the chip
read(S, numData)

Disconnect the SPI device and clean up by clearing the object.

disconnect(S); 
clear('S');

Related Examples

Input Arguments

collapse all

Adaptor board vendor, specified as the character vector 'aardvark' or 'ni845x'. You need to use a Total Phase Aardvark adaptor or an NI-845x adaptor board to use the SPI interface. You must specify this as the first argument when you create the spi object.

Example: S = spi('aardvark', 0, 0);

Data Types: char

Board index of your hardware, specified as a numeric value. This is usually 0. You must specify this as the second argument when you create the spi object.

Example: S = spi('aardvark', 0, 0);

Data Types: double

Port number of your hardware, specified as the number 0. The Aardvark adaptor uses 0 as the port number. You must specify this as the third argument when you create the spi object.

Example: S = spi('aardvark', 0, 0);

Data Types: double

Introduced in R2013b

Was this topic helpful?