spidev

Connection to SPI device on Arduino hardware

Description

A spidev object represents a connection to the SPI device on Arduino® hardware. Attach an SPI device to the appropriate pins on the Arduino hardware. Create an spidev object using the spidev function. You can read from and write to your SPI device in MATLAB® using writeRead.

Note

The spidev object will not be supported in future releases of MATLAB Support Package for Arduino Hardware. Use device instead.

Creation

Description

example

dev = spidev(a,cspin) creates a connection to the SPI device on the specified Arduino board and chip select pin.

example

dev = spidev(a,cspin,Name,Value) adds additional options specified by one or more Name,Value pair arguments.

Input Arguments

expand all

Arduino hardware connection created using arduino, specified as an object.

Example: dev = spidev(a,'D10') creates a connection to SPI device on the specified arduino object a.

Chip select pin number used to communicate with the SPI device, specified as a character vector of the form 'Ax' or 'Dx'. Here, x is the pin number, D is digital and A is analog. This is the pin on your Arduino hardware that is connected to the chip select line on the SPI device.

Example: dev = spidev(a,'D10') creates a connection to the SPI device specified by the chip select pin 'D10'.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: dev = spidev(a,'D4','mode',3,'bitorder','lsbfirst');

SPI communication mode, specified as a number between 0 and 3. Specify the clock polarity and phase by setting the mode.

SPI ModeClock PolarityClock Phase
000
101
210
311

Example: dev = spidev(a,'D4','mode',3) creates a connection to the SPI device and sets the clock polarity to 1 and clock phase to 1.

SPI communication bit order, specified as one of the following character vectors:

  • 'msbfirst', where the most significant bit is in the first column

  • 'lsbfirst', where the least significant bit is in the first column

Example: dev = spidev(a,'D4','bitorder','lsbfirst') creates a connection to the SPI device and sets the least significant bit in the first column.

The maximum speed of SPI communication in Hertz, specified as a positive integer.

Example: dev = spidev(a,'D4','bitrate',2000000) creates a connection to the SPI device and sets the maximum bit rate to 2MHz.

Properties

expand all

This property is read-only.

Arduino pin connected to chip select line on SPI device, specified as a character vector of the form 'Ax' or 'Dx'. Here, x is the pin number, D is digital and A is analog.

Example:

>> dev.ChipSelectPin

ans =

    'D10'

Data Types: char

This property is read-only.

Arduino device pins connected to SPI device, specified as a cell array of character vectors.

Example:

>> dev.Pins

ans =

  1×4 cell array

    {'D11'}    {'D12'}    {'D13'}    {'D10'}

Data Types: cell

This property is read-only.

SPI communication mode, specified as a number between 0 and 3. Specify the clock polarity and phase by setting the mode.

SPI ModeClock PolarityClock Phase
000
101
210
311

Example:

>> dev.Mode

ans =

     0

Data Types: double

SPI communication bit order, specified as msbfirst or lsbfirst. If it is msbfirst, the most significant bit is in the first column of the writeRead output. If it is lsbfirst, the least significant bit is in the first column.

Example:

>> dev.BitOrder

ans =

    'msbfirst'

Data Types: char

Maximum speed of SPI communication in Hertz, specified as a positive integer.

Example:

>> dev.BitRate

ans =

     4000000

Data Types: double

Object Functions

Use an object function to read from and write to your SPI device.

writeReadWrite and read data from SPI device

Examples

collapse all

Create an Arduino connection, and connect it to an SPI device.

a = arduino('COM4','Uno','Libraries','SPI');
dev = spidev(a,'D10')
dev = 
  spidev with properties:

    ChipSelectPin: D10
             Pins: D11(MOSI), D12(MISO), D13(SCK), D10(SS)
             Mode: 0               (0, 1, 2 or 3)
         BitOrder: msbfirst        ('msbfirst' or 'lsbfirst')
          BitRate: 4000000         (Hz)

Set the communication mode to 3, bit order to lsbfirst, and bit rate to 20000000Hz.

a = arduino('COM4','Uno','Libraries','SPI');
dev = spidev(a,'D10','Mode',3,'BitOrder','lsbfirst','Bitrate',20000000)
dev = 
  spidev with properties:

    ChipSelectPin: D10
             Pins: D11(MOSI), D12(MISO), D13(SCK), D10(SS)
             Mode: 3               (0, 1, 2 or 3)
         BitOrder: lsbfirst        ('msbfirst' or 'lsbfirst')
          BitRate: 20000000        (Hz)

Change the rate of SPI communication to 10000000Hz.

dev.BitRate = 10000000
dev = 
  spidev with properties:

    ChipSelectPin: D10
             Pins: D11(MOSI), D12(MISO), D13(SCK), D10(SS)
             Mode: 3               (0, 1, 2 or 3)
         BitOrder: lsbfirst        ('msbfirst' or 'lsbfirst')
          BitRate: 10000000        (Hz)

Introduced in R2014b