Measuring current with KL25Z board and INA219 using I2C

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Measuring current with KL25Z board and INA219 using I2C

916 Views
huy_dang
Contributor III

Hi, I'm new to micro-controller and have been struggling with this application for the past couple days. The title should suggest it, I'm trying to measure the current using the INA219 from Adafruit with KL25z board. I followed one of the example already on mcuxpresso using polling interrupt transfer.

My code is not working, but when I measured the sda signal on an oscilloscope, I see that the sda signal coming from the master is correct, just that the slave (INA219) is not sending back an ACK. 

I also have pull up resistors on both sda and scl line. 

Here's my code 

#include <string.h>
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_i2c.h"

#include "pin_mux.h"

#define EXAMPLE_I2C_MASTER_BASEADDR I2C0
#define I2C_MASTER_CLK_SRC I2C0_CLK_SRC
#define I2C_MASTER_CLK_FREQ CLOCK_GetFreq(I2C0_CLK_SRC)
#define I2C_MASTER_SLAVE_ADDR_7BIT 0x00U
#define I2C_BAUDRATE 100000U
#define I2C_DATA_LENGTH 2U
uint8_t g_master_buff[I2C_DATA_LENGTH];

i2c_master_handle_t g_m_handle;
status_t status;
int main(void)
{

i2c_master_config_t masterConfig;
uint32_t sourceClock;
i2c_master_transfer_t masterXfer;

BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();

PRINTF("\r\nI2C example -- MasterPolling_SlaveInterrupt\r\n");
I2C_MasterGetDefaultConfig(&masterConfig);
masterConfig.baudRate_Bps = I2C_BAUDRATE;

sourceClock = I2C_MASTER_CLK_FREQ;

I2C_MasterInit(EXAMPLE_I2C_MASTER_BASEADDR, &masterConfig, sourceClock);

memset(&g_m_handle, 0, sizeof(g_m_handle));
memset(&masterXfer, 0, sizeof(masterXfer));

for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++)
{
g_master_buff[i] = 0;
}
PRINTF("\r\n0x%2x\r\n", I2C_MASTER_SLAVE_ADDR_7BIT);
masterXfer.slaveAddress = I2C_MASTER_SLAVE_ADDR_7BIT;
masterXfer.direction = kI2C_Read;
masterXfer.subaddress = 0x00U;
masterXfer.subaddressSize = 1;
masterXfer.data = g_master_buff;
masterXfer.dataSize = I2C_DATA_LENGTH;
masterXfer.flags = kI2C_TransferDefaultFlag;


while(1) {

status = I2C_MasterTransferBlocking(EXAMPLE_I2C_MASTER_BASEADDR, &masterXfer);
if (status == kStatus_Success)
{
PRINTF("success\r\n");
}
if (status & kI2C_ReceiveNakFlag)
{
PRINTF("NAK\r\n");
}
PRINTF("Master received data :");
for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++)
{
if (i % 8 == 0)
{
PRINTF("\r\n");
}
PRINTF("0x%2x ", g_master_buff[i]);
}
PRINTF("\r\n\r\n");
}
}

Please help!

Tags (3)
2 Replies

835 Views
Sebastian_Del_Rio
NXP Employee
NXP Employee

Hi Huy, I hope you're doing well!

 

It seems like the KL25Z is working as intended, and the problem could be more closely related to the I2C frame you're sending to your sensor. I would recommend you check the datasheet for the INA219 so that you can confirm that the correct address and frame is being sent and formatted correctly for your sensor.

 

You could also try contacting your sensor's manufacturer for support.

 

If you need any more information regarding the KL25Z, please let me know.

 

Best regards,

Sebastian

0 Kudos
Reply

835 Views
huy_dang
Contributor III

Hi Sebastian,

Thank you for the reply. I found out in the end that the board wasn't working and I got the code to work with another I2C device (should have checked this before testing the code and waste a day on this).

Thank you again for the help.