writeRead

Write and read data from SPI device

Description

example

out = writeRead(dev,dataIn) writes the data to the SPI device and returns the data from the SPI device as result of the write operation.

example

out = writeRead(dev,dataIn,precision) also specifies the precision.

Examples

collapse all

Create a connection to a SPI device on an Arduino® board.

a = arduino('COM9','Uno','Libraries','SPI');
dev = device(a,'SPIChipSelectPin','D8');
writeEnable = bin2dec('0000 0110');
writeRead(dev,writeEnable);

Write data to the SPI EEPROM.

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

     0     0     0     0     0

Read data from the SPI EEPROM

readCmd = bin2dec('0000 0011');
dataIn = [readCmd address zeros(1,2)];
out = writeRead(dev,dataIn)
out = 1×5

     0     0     0   255   255

Create a connection to a SPI device on an Arduino® board.

a = arduino('COM9','Uno','Libraries','SPI');
dev = device(a,'SPIChipSelectPin','D8');

Enable write on EEPROM

writeEnable = 6;
writeRead(dev,writeEnable);

Write data to the SPI EEPROM with the precision of uint16.

writeCmd = 512;
address = [0 0];
data = [51200 51201];
dataIn = [writeCmd address data];
out = writeRead(dev,dataIn,'uint16')
out = 1×5 uint16 row vector

   0   0   0   0   0

Read data from the SPI EEPROM with the precision of uint16.

readCmd = 768
readCmd = 768
dataIn = [readCmd address zeros(1,2)];
out = writeRead(dev,dataIn,'uint16')
out = 1×5 uint16 row vector

      0      1    515   1029      0

Input Arguments

collapse all

SPI device connection, specified as a device object.

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

Data precision, specified as one of the following character vectors:

  • 'int8'

  • 'int16'

  • 'int32'

  • 'int64'

  • 'uint8'

  • 'uint16'

  • 'uint32'

  • 'uint64'

The precision must match the size of the SPI device register.

Output Arguments

collapse all

Data read or returned from the SPI device, returned as a scalar or vector.

Introduced in R2014b