write
Write data to I2C device
Description
write(
writes data to an I2C device.myi2cdevice
,data
,dataPrecision
)
The write
method stops when it finishes writing the specified
data to the device, or when the timeout period elapses.
Examples
Connect and Exchange Data with I2C Devices
You can connect and exchange data with a pair of I2C devices.
Create a connection from the MATLAB® software to the Raspberry Pi® board.
mypi = raspi
mypi = Raspi with Properties: DeviceAddress: 'raspberrypi-hysdu8X38o' Port: 18725 BoardName: 'Raspberry Pi Model B Rev 2' AvailableLEDs: {'led0'} AvailableDigitalPins: [4 7 8 9 10 11 14 15 17 18 22 23 24 25 27 30 31] AvailableSPIChannels: {} AvailableI2CBuses: {'i2c-0' 'i2c-1'} I2CBusSpeed: 100000 Supported peripherals
Redisplay AvailableI2CBuses
and I2CBusSpeed
.
mypi.AvailableI2CBuses mypi.I2CBusSpeed
ans = 1×2 cell array {'i2c-0'} {'i2c-1'} ans = 100000
Show the location of the I2C pins.
showPins(mypi)
The pin map shows that, for this model and revision of the board,
the i2c-1
bus is available on the GPIO header pins I2C1_SDA
(GPIO 2)
and I2C1_SCL (GPIO 3)
.
After physically connecting your I2C device to the I2C
pins, get the addresses of I2C devices attached to the I2C bus, 'i2c-1'
.
scanI2CBus(mypi,'i2c-1')
ans = 1×2 cell array {'0x55'} {'0x20'}
Create a connection, i2csensor
,
from the MATLAB software to the I2C sensor at '0x20'
.
i2csensor = i2cdev(mypi,'i2c-1','0x20')
i2csensor = I2C with Properties: Bus: i2c-1 I2CAddress: 0x20
Read two uint8 numbers from the sensor.
output1 = read(i2csensor,2)
Read the value of register 14 from the sensor.
output2 = readRegister(i2csensor,14)
Create a connection, i2cdisplay
,
from the MATLAB software to the I2C LED display at '0x55'
.
i2cdisplay = i2cdev(mypi,'i2c-1','0x55')
i2cdisplay = I2C with Properties: Bus: i2c-1 I2CAddress: 0x55
Write characters to the display.
write(i2cdisplay,[hex2dec('20') hex2dec('51')])
Write a scalar hexadecimal value, hex2dec('08')
,
to register 3 on an I2C device.
writeRegister(i2cdisplay,3,hex2dec('08'),'uint8')
If you are not using I2C, disable I2C to free additional GPIO pins.
disableI2C(mypi)
Before using I2C again, enable I2C.
enableI2C(mypi)
When you enable I2C, you can change the mypi.I2CBusSpeed
property.
disableI2C(mypi) enableI2C(mypi,400000) mypi.I2CBusSpeed
ans = 400000
Input Arguments
myi2cdevice
— Connection to I2C device
connection
Connection to I2C device, specified as a i2cdev
object.
Example: myi2cdevice
data
— Data to write to I2C device
data
Data to write to I2C device, specified as vector of hexadecimal values.
Example: [hex2dec('20') hex2dec('51')]
Data Types: int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Note
If the data written is bigger than uint8
, the data is in
little-endian format.
dataPrecision
— Data precision
'uint8'
(default) | 'int8'
| 'int16'
| 'uint16'
| 'int32'
| 'uint32'
| 'int64'
| 'uint64'
Data precision, specified as a string. Match the data precision to the size of the register on the device. Optional.
Example: 'int16'
Data Types: char
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
You can also deploy
write
on the Raspberry Pi hardware in MATLAB Online™.
See Also
raspi
| i2cdev
| scanI2CBus
| read
| readRegister
| writeRegister
| enableI2C
| disableI2C
| showPins
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
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)