Main Content

writeRead

Write and read data from SPI device

Add-On Required: This feature requires the MATLAB Support Package for Arduino Hardware add-on.

Description

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.

example

Examples

collapse all

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

a = arduino('COM6','Mega2560','Libraries','SPI');
Updating server code on board Mega2560 (COM6). This may take a few minutes.
dev = device(a,'SPIChipSelectPin','D46');
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   250   155

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

a = arduino('COM6','Mega2560','Libraries','SPI');
dev = device(a,'SPIChipSelectPin','D46');

Enable write on EEPROM

writeEnable = 6;
writeRead(dev,writeEnable);

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

writeCmd = 2;
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 = 3
readCmd = 3
dataIn = [readCmd address zeros(1,2)];
out = writeRead(dev,dataIn,'uint16')
out = 1×5 uint16 row vector

       0       0       0   51200   51201

Input Arguments

collapse all

SPI device connection, specified as a device object.

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

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

  • 'uint8'

  • 'uint16'

  • 'uint32'

  • 'uint64'

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

Data can be also be specified in the following formats:

  • hexadecimal

  • binary

  • char

  • string

Output Arguments

collapse all

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

More About

collapse all

Code Generation Using MATLAB Function Block

  • Use writeRead in a MATLAB® Function block with the Simulink® Support Package for Arduino® Hardware to generate code that can be deployed on Arduino Hardware.

  • Values of dataIn is not validated against the precision. The dataIn values will be saturated when it goes beyond the range of the specified precision.

Version History

Introduced in R2014b