Uncategorized

I2C write two bytes

Asked 
Active today
Viewed 15 times
1

I am trying to write 16 bit data to i2c device . As there are no registers specified to write to the slave device, I am not able to use the built-in wiringPiI2CWriteReg16 Api.

Can some one guide me how to write 16 bit data to device address. I tried using wiringPiI2CWrite() API consecutively for 2 times to write 16 bit data but no luck!

 New contributor

0

Answer

(1) I never use Wiring Pi. So I don’t understand your WiringPi problem.

(2) I only know how to use smBus to communicate with I2C devices, with or without registers.

(3) Referring the Appendix A below, I usually

(a) First import smbus,

(b) Define one function to write one byte to the I2C device,

(c) Define another function to write two bytes to the I2C device.

(4) I very seldom use the first function – writing only one byte to the I2C device.

(5) I heavily use the second function – writing two bytes to the I2C device.

(6) I use the i2cBus.write_byte method to write the two bytes.

(7) i2cBus.write does not care what the two write bytes are. It just writes out blindly.

(8) But if the first byte is the device’s register address, say config register at address 0x00, and second byte is a data byte 0x55, then 0x55 would be written into the config register.

(9) Now coming back to your problem. What you want is just writing two bytes out, say first byte is 0x77, second byte is 0x88, then i2cBus just again do its job blindly, sending out 0x77, 0x88, I guess this is what you want. Let me know otherwise,

(10) I have a scope to display waveforms, I usually repeated indefinitely write two bytes, and pause 10mS every writing two bytes. I am happy to display the waveforms fro you.

Appendices

Appendix A – Import and definitions to write one byte and two bytes to I2C device

import smbus
i2cBus1 = smbus.SMBus(1) 

def quickWriteDevOneByte(i2cBus, devAddr, writeByte):
    i2cBus.write_byte(devAddr, writeByte)
    return 

def writeDevTwoBytes(i2cBus, devAddr, writeByte1, writeByte2):
    i2cBus.write_byte_data(devAddr, writeByte1, writeByte2)
    return

Appendix B – I2C Sending two bytes

What is the max i2c speed of the raspberry pi 4?

Listing of program to write two bytes to device (actually at the same time read the ADXL345’s ID register)


write two bytes


References

(1) System Management Bus – Wikipedia

(2) SMBus Quick Start Guide, App Note AN4471 – NXP 2010

(3) SMBus Protocol Summary – Linux Kernel documentation v5.4.0

(4) What is the max i2c speed of the raspberry pi 4?


Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: