I2C Read, Write, WriteRead, having trouble understanding

Morning folks, got some basic understanding questions about the I2C methods.
I am new to I2C in general, but thought I understood it. For a device I am using (ADS122C04) this is the process I am trying to do:
//Initialization starts with a reset
//Then set the MUX
//Then set the voltage reference
//Start/Sync begin data conversions
//Read data
//convert data to usable information

To initialize I use a device.Write(new byte[] { 0x06 }); everything goes ok so far (I think, no exceptions anyways). Next, to set the multiplexer up, which reg address is 0x00, I need to set to 0x40, and I want to read an address to confirm the changes took place so I send device.WriteRead({ 0x00, 0x40 }, readBuffer).
This doesn’t work, my real question is how do you write to a specific address a specific value, seems any time I use more than one byte in the write buffer it sends an exception. Also, I get that the readbuffer is where it then stores the read data, but how to set which reg to read?

my code so far:

static void Main()
{
//START/SYNC Start or restart conversions 0000 100X = 0x08
//RESET Reset the device 0000 011X = 0x06
//POWERDOWN Enter power-down mode 0000 001X = 0x02
//RDATA Read data by command 0001 XXXX = 0x10
//RREG Read register at address 0010 RRXX = 0x20, 0x24, 0x28, 0x2C
//WREG Write register at address 0100 RRXX = 0x40, 0x44, 0x48, 0x4C
byte start = 0x08, reset = 0x06, powerdown = 0x02, rData = 0x10;
byte rRegA = 0x20, rRegB = 0x24, rRegC = 0x28, rRegD = 0x2C;
byte wRegA = 0x40, wRegB = 0x44, wRegC = 0x48, wRegD = 0x4C;

        Thread.Sleep(10);//delay to allow power up cycle to settle.

        byte _420mA_Sensor = 0x4C;//Slave Address
        uint _Bus_Speed = 400_000;//

        byte[] readBuffer = new byte[3];

        var settings = new I2cConnectionSettings(_420mA_Sensor, _Bus_Speed);
        var controller = I2cController.FromName(SC20100.I2cBus.I2c2);
        var device = controller.GetDevice(settings);

        try
        {
            //Initialization starts with a reset
            device.Write(new byte[] { reset });
            Thread.Sleep(10);

            //Then set the MUX
            device.Write(new byte[] { 0x00 });
            device.WriteRead(new byte[] { wRegA }, readBuffer);

            //Then set the voltage reference
            //device.Write(new byte[] { 0x01, 0x04 });

            //Start/Sync begin data conversions

            //Read data

            //convert data to usable information


            foreach (byte a in readBuffer)
            {
                Debug.WriteLine(a.ToString());
            }
        }
        catch(Exception exe)
        {
            Debug.WriteLine(exe.Message);
        }
    }

Write is simply
Start -> write -> stop

Write Read is simply
Start -> Write -> restart -> Read -> Stop

The second one is what you need to read a register usually. The first wire is the register number followed by read of the register value.

That part I get, maybe its something more basic I am getting messed up on. I need to write 0x40 to reg 0x00, but Write(new byte[] {0x00, 0x40}) throws an exception, I am more confused on how to tell it where and what to write in one line. My brain is faintly remembering I have to combine the bytes and send as one, is that right?

I should also mention I am band new to .netmf/tinyclr, im used to programming micro’s in C, and even that was 4 years ago.

You have the wrong address or you forgot the pull up resistors.

You are right, I had the wrong address, i had to combine the write address and target reg to get the proper address. Got it working now, thank you.

1 Like

@bradChrabaszcz
I have been doing some I2C development recently as well. I can’t tell you valuable my logic analyzer is. It is a huge time saver. I have a Saleae Logic 8 ($399) and is more than adequate for what I am doing. Seeing what is actually “on the wire” is an invaluable time-saver. There are other brands less expensive but make sure they have good software. Saleae has protocol analyzers that help spot the errors with I2c, SPI, UART, etc.

Yes, that is next on my list of bench top tools to get, that and a proper desktop multimeter instead of my handheld.

1 Like

Here you go. Desktop DMM + Bluetooth Speaker + Alarm Clock! Talk about feature creep :crazy_face:

https://www.amazon.com/BSIDE-Display-Digital-Multimeter-Wireless/dp/B0825MX58J/ref=psdc_14244471_t3_B06XX6F7DC