Main Content

writeRead

R2026b

Write and read data from SPI device

Description

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

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

Alternative Functionality

App

Use Arduino Explorer to configure the SPI device and then read and write data to SPI devices connected to Arduino or any other supported hardware. You can use the app in the installed version of MATLAB and MATLAB Online™. You can also use the Plot pane of the app to interactively view data read from the Arduino board.

Version History

Introduced in R2014b