Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 3 > How to pointer to a different register I2C communication

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



How to pointer to a different register I2C communication
Moderator:
RKRM

Post Reply
Follow this topic



How to pointer to a different register I2C communication

jjthefreako posted on 10 Sep 2012 4:38 PM PST
Member
5 Forum Posts

 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




Re: How to pointer to a different register I2C communication

danaaknight posted on 10 Sep 2012 04:47 PM PST
Top Contributor
1773 Forum Posts

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.



Re: How to pointer to a different register I2C communication

Gautam Das posted on 11 Sep 2012 10:31 PM PST
Cypress Employee
742 Forum Posts

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.



Re: How to pointer to a different register I2C communication

jjthefreako posted on 11 Sep 2012 01:49 PM PST
Member
5 Forum Posts

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



Re: How to pointer to a different register I2C communication

Gautam Das posted on 11 Sep 2012 09:48 PM PST
Cypress Employee
742 Forum Posts

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 */

}



Re: How to pointer to a different register I2C communication

Gautam Das posted on 11 Sep 2012 09:56 PM PST
Cypress Employee
742 Forum Posts

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.



Re: How to pointer to a different register I2C communication

jjthefreako posted on 12 Sep 2012 02:35 PM PST
Member
5 Forum Posts

 That worked great. Thank you dasg






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20