- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
- Labels:
-
PSoC 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 */
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 */
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That worked great. Thank you dasg