Documentation Center

  • Trial Software
  • Product Updates

spidev

Create connection to SPI device

Syntax

  • myspidevice = spidev(mypi,channel)
  • myspidevice = spidev(mypi,channel,modespeed)

Description

myspidevice = spidev(mypi,channel) connects to an SPI device and returns a handle, myspidevice. You can exchange data with the SPI device by using myspidevice with the writeRead method. You can use myspidevice to read properties of the SPI device.

myspidevice = spidev(mypi,channel,modespeed) overrides the default SPI mode, 0, that many SPI devices use.

Examples

expand all

Connect and Exchange Data with SPI Devices

You can connect and exchange data with an SPI device.

Create a connection to the Raspberry Pi™ board.

mypi = raspi
mypi = 

  Raspi with Properties:

           DeviceAddress: 'raspberrypi-computername'
                    Port: 18725
               BoardName: 'Raspberry Pi Model B Rev 2'
           AvailableLEDs: {'led0'}
    AvailableDigitalPins: [4 7 8 9 10 11 14 15 17 18 22 23 24 25 27 30 31]
    AvailableSPIChannels: {}
      AvailableI2CBuses: {'i2c-0'  'i2c-1'}
             I2CBusSpeed: 100000

  Supported peripherals

By default, SPI is disabled and AvailableSPIChannels does not show any channels.

Enable SPI and get the channels.

enableSPI(mypi);
mypi.AvailableSPIChannels
ans = 

    'CE0'    'CE1'

Show the location of the SPI pins, GPIO 10 (SPI0_MOSI), GPIO 9 (SPI0_MISO), and GPIO 11 (SPI0_SCLK), on the GPIO header.

showPins(mypi)

After physically connecting your SPI device to the three SPI pins, connect to the SPI device.

myspidevice = spidev(mypi,'CE1',0)
myspidevice = 

Spidev with Properties:
           Channel: 'CE1'
              Mode: 0 
       BitsPerWord: 8
             Speed: 500000

Write data to, and read data from, the SPI device.

out = writeRead(myspidevice,[hex2dec('08') hex2dec('D4')])
out = 
         7 211

If you are not using SPI, disable SPI to make additional GPIO pins available.

disableSPI(mypi)

Input Arguments

expand all

mypi — Connection to specific boardhandle

Connection to a specific board, specified as a handle.

Use raspi to create this connection.

Example: mypi

channel — SPI channel'CE0' | 'CE1'

SPI channel, specified as a string.

Example: 'CE1'

Data Types: char

mode — SPI mode0 (default) | 1 | 2 | 3

SPI mode, specified as a scalar. This value determines the clock polarity (CPOL) and phase (CPHA) values.

ModeCPOLCPHA
000
101
210
311

The most common mode is 0.

Example: 0

Data Types: double

speed — SPI speed500000 | 1000000 | 2000000 | 4000000 | 8000000 | 16000000 | 32000000

SPI speed, specified as a scalar.

Example: 500000

Data Types: char

Output Arguments

expand all

myspidevice — Connection to SPI devicehandle

Connection to an SPI device, specified as a handle.

You can use myspidevice to exchange data with SPI devices using the writeRead method.

You can use myspidevice to read the following properties for the SPI connection.

Property NameData TypeDimensionUnits
Channelchar[1,N] 
Modedouble[1,1] 
BitsPerWorddouble[1,1]Bits
Speeddouble[1,1]Clock cycles

See Also

| | | |

Was this topic helpful?