6 Replies Latest reply on Sep 11, 2012 11:56 PM by gautam.das.g

    How to pointer to a different register I2C communication

    jerene.jacob

       Hi,

         

       

         

      I have a current sense INA219 which I am communicating through I2C. I am able to communicate with it but only the first register of INA219. INA219 has several registers which contain different values. I would like to access those registers. Could you tell me how I can read from those registers. For example the shunt voltage read by the INA219 is saved in 0x01 register of the INA219.

         

       

         

      Thanks,

         

      Jerry

        • 1. Re: How to pointer to a different register I2C communication
          user_14586677

          Basicaly you have an address to a register, a C pointer. Lets say you

             

          have -

             

           

             

          unsigned char regval = 0;

             

          unsigned char *xPtr = 0x01;

             

           

             

          Then to acess the next register

             

           

             

          regval = *(xPtr + 1);

             

           

             

          Regards, Dana.

          • 2. Re: How to pointer to a different register I2C communication
            gautam.das.g

            Hi jjthefreako,

               

             

               

            Since you are intending to read from the Shunt Voltage register, you'll first have to initialize the pointer to that location (0x01).

               

            The procedure would be as follows:

               

            1) Give a "Write" command with the appropriate slave address followed by the address of the register which you want to read from (0x01). Donot give a "Stop" command.

               

            2) Give a Repeat Start. Now the pointer is pointing to register 0x01.

               

            3) Now give a Read Command with the same Slave address. The two bytes hence read corresponds to the Shunt Voltage you are looking for.

            • 3. Re: How to pointer to a different register I2C communication
              jerene.jacob

              Hi,

                 

              First off thank you dan and dasg for ur replies.

                 

              I tried the following code and several variations of it but I am still not able to read. Could you please tell me where I am going wrong? 

                 

              I2C_UDB_M_MasterSendStart(EZI2C_SLAVE_ADDR_current1+0x01, I2C_UDB_M_WRITE_XFER_MODE);

                 

              I2C_UDB_M_MasterSendRestart(EZI2C_SLAVE_ADDR_current1+0x01, I2C_UDB_M_READ_XFER_MODE);

                 

               I2C_UDB_M_MasterReadBuf(EZI2C_SLAVE_ADDR_current1+0x01, (uint8 *) i2cBufferRead,2, I2C_UDB_M_MODE_COMPLETE_XFER);

                 

              while(0u == (I2C_UDB_M_MasterStatus() & I2C_UDB_M_MSTAT_RD_CMPLT))

                 

              { /* Wait read to be completed */

                 

              }

                 

              Thanks,

                 

              Jerene

              • 4. Re: How to pointer to a different register I2C communication
                gautam.das.g

                Hi Jerene,

                   

                 

                   

                A simple code for reading the location 0x01 would look something like this:

                   

                 

                   

                #include<device.h>

                   

                #define SLAVE_ADDRESS  0x00           /* Give the appropriate Slave Address */

                   

                 

                   

                uint8 shunt_volt[1] = {0x01};    /* The address of the Shunt Voltage register */

                   

                uint8 read_value[2];                  /* To store the read value */

                   

                 

                   

                void main()

                   

                {

                   

                     I2C_Start();

                   

                     CyGlobalIntEnable();

                   

                     I2C_MasterWriteBuf(SLAVE_ADDRESS, shunt_volt, 1, I2C_MODE_NO_STOP); /* This sets the pointer to Shunt Voltage register whose address is 0x01 */

                   

                     while(I2C_MasterStatus() == I2C_MSTAT_XFER_INP); /* Waits till the transfer is complete */

                   

                     I2C_MasterReadBuf(SLAVE_ADDRESS, read_value, 2, I2C_MODE_REPEAT_START ); /* read_value will hold the two bytes read. This begins with a repeat start */

                   

                     while(I2C_MasterStatus() == I2C_MSTAT_XFER_INP); /* Waits till the transfer is complete */

                   

                }

                • 5. Re: How to pointer to a different register I2C communication
                  gautam.das.g

                  Also not to forget, you have to use pull up resistors on the SCL and SDA lines.

                     

                  You can find a sample project in Creator by right clicking on the I2C Component and selecting the "Find Example Project.."

                     

                   

                     

                  For the code written in my previous post, I have assumed that I am using an I2C component with an instance name of I2C and is configured to operate in Master mode.

                  • 6. Re: How to pointer to a different register I2C communication
                    jerene.jacob

                     That worked great. Thank you dasg