This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Write and read data from SPI device


data = writeRead(spidevObj,dataIn)
data = writeRead(spidevObj,dataIn,precision)



data = writeRead(spidevObj,dataIn) returns the data, written to the SPI device specified as a device object.

data = writeRead(spidevObj,dataIn,precision) returns the data, written to the SPI device, based on the data precision.


collapse all

Create object for an SPI device that is already connected to SPI bus.

microbitObj = microbit('COM3');
spidev1 = device(microbitObj, 'SPIChipSelectPin', P16);
spidev1 =  
  device with properties: 

      SPIChipSelectPin: "P16" 
                SCLPin: "P13" 
               MISOPin: "P14" 
               MOSIPin: "P15" 

Use the SPI device object to write data to the SPI device.

writeCmd = bin2dec('0000 0010');
address = [255 0];
data = [250 155];
dataIn = [writeCmd address data];
out = writeRead(spidev1,dataIn)
out = 
			0 0 0 0 0

Read data from the SPI device.

readCmd = bin2dec('0000 0011');
dataIn = [readCmd address zeros(1,2)];
out = writeRead(spidev1,dataIn)
out = 
			0 0 0 250 155 

Input Arguments

collapse all

Device connection to a SPI device, specified as a device object, connected to the SPI bus on the BBC micro:bit board. The SPI device object is created using the device function.

Data to read and write to the SPI device, specified as a scalar or vector. The range of the values in the array is based on the precision.

Data Types: double

Precision of data to read and write to the SPI device.

See Also

Introduced in R2017b