Main Site Documentation

I2C return byte value 0 for all registry


#1

Hello comunity,
i´m trying to control CMOS camera via I2C.

Execute command returned 1, but value of read buffer is 0 always (for example I2C.Read(0x02) ). Same for write action, execute code is o.k., but read return 0 together.

This is datasheet from my camera: http://www.pvelectronic.eu/out/media/tcm8240md_e150405_rev13.pdf

This is code I2C:



    public class Camera
    {
        private static I2CDevice.Configuration I2CConfig;
        private static I2CDevice I2C;

        public Camera(byte address, int clockRateKHz)
        {
            I2CConfig = new I2CDevice.Configuration(address, clockRateKHz);
            I2C = new I2CDevice(I2CConfig);
        }

        public void Write(byte address, byte data)
        {
            byte[] buffer = new byte[2];
            buffer[0] = address;
            buffer[1] = data;

            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(buffer);

            if (I2C.Execute(xActions, 1000) == 0)
            {
                Debug.Print("Failed to perform I2C transaction");
            }
            else
            {
                Debug.Print("Success write");
            }
            
            Thread.Sleep(1000); // Mandatory after each Write transaction !!!
        }

        public byte Read(byte address)
        {
            byte[] RegisterValue = new byte[1];

            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { address });

            Thread.Sleep(1000); // Mandatory after each Write transaction !!!

            xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue);

            int result = 0;
            if ((result = I2C.Execute(xActions, 1000)) == 0)
            {
                Debug.Print("Failed to perform I2C transaction");
            }
            else
            {
                Debug.Print("Success read: " + RegisterValue[0].ToString() + " Result: " + result);
            }

            return RegisterValue[0];
        }

}

#2

Make sure you use the right address for I2C communication.


#3

@ Architect - Yes, if i used incorrect address, program has thrown exception

I use software I2C driver now - https://www.ghielectronics.com/community/codeshare/entry/236

But i dont receive second ACK (first after slave address is o.k), after subAddress, i dont know why…


#4

Have you tried to use hardware I2C first to make sure it works? I am not sure about that software I2C driver (new tried it myself).