writeRead
Write and read data from SPI device
Add-On Required: This feature requires the MATLAB Support Package for Arduino Hardware add-on.
Description
Examples
Write and Read Data from a SPI Device on an Arduino Board
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
Specify Precision to Write and Read Data from a SPI Device on an Arduino Board
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
dev
— SPI device connection
device object
SPI device connection, specified as a device
object.
dataIn
— Data to write to SPI device
scalar | vector
Data to write, specified as a scalar, vector, hexadecimal, or binary. The range of the values
in the array is based on the precision
.
precision
— Data precision
'uint8'
(default) | 'uint16'
| 'uint32'
| 'uint64'
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
out
— Data read or returned from SPI device
scalar | vector
Data read or returned from the SPI device, returned as a scalar
or vector
.
More About
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 theprecision
. ThedataIn
values will be saturated when it goes beyond the range of the specifiedprecision
.
Version History
Introduced in R2014b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)