Hello. I have been working with this INA219 chip for a while. I am able to read the output voltage and set the registers. But I am having troubles reading back the registers to check what I have written. I have been studying datasheet for a while and I still cant fully understand the process of reading the register. I am using HAL functions for i2c reads and writes. Link to datasheet: https://www.ti.com/lit/ds/symlink/i...984516&ref_url=https%3A%2F%2Feu.mouser.com%2F
write and read functions:
Lets say I have written 2 different registers and wish to read only one of them:
From what I understood from the datasheet, In order to read the register, the write command with 0 data has to be initiated to show what register you want to read. After that, A read command is initiated. A write and Read command difference is the first bit of the slave address ( either HIGH or LOW). Could someone give me any tips how do I properly initiate those two commands to read back a register.. I have tried to initiate just read command and It always returns a 0
write and read functions:
Code:
void wireWriteRegister (uint8_t reg, uint16_t value)
{
printf("register = %u, write value=%u \n",reg, value);
uint8_t i2c_temp[2];
i2c_temp[0] = value>>8;
i2c_temp[1] = value;
HAL_I2C_Mem_Write(&hi2c1, INA219_ADDRESS<<1, (uint16_t)reg, 1, i2c_temp, 2, 0xffffffff);
//HAL_I2C_Mem_Write_IT(&hi2c1, INA219_ADDRESS<<1, (uint16_t)reg, 1, i2c_temp, 2);
//HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_Delay(1);
}
/**************************************************************************/
/*!
@brief Reads a 16 bit values over I2C
*/
/**************************************************************************/
void wireReadRegister(uint8_t reg, uint16_t *value)
{
uint8_t i2c_temp[2];
HAL_I2C_Mem_Read(&hi2c1, INA219_ADDRESS<<1, (uint16_t)reg, 1,i2c_temp, 2, 0xffffffff);
//HAL_I2C_Mem_Read_IT(&hi2c1, INA219_ADDRESS<<1, (uint16_t)reg, 1, i2c_temp, 2);
HAL_Delay(1);
*value = ((uint16_t)i2c_temp[0]<<8 )|(uint16_t)i2c_temp[1];
printf("register = %u, read value=%i \n", reg, *value);
}
Lets say I have written 2 different registers and wish to read only one of them:
Code:
uint16_t read_val1=0;
uint16_t read_val2=0;
uint16_t read_val3=0;
// Set Calibration register to 'Cal' calculated above
wireWriteRegister(INA219_REG_CALIBRATION, ina219_calValue);
//wireReadRegister(INA219_REG_CALIBRATION,read_val1);
// Set Config register to take into account the settings above
uint16_t config = INA219_CONFIG_BVOLTAGERANGE_16V |
INA219_CONFIG_GAIN_1_40MV |
INA219_CONFIG_BADCRES_12BIT |
INA219_CONFIG_SADCRES_12BIT_1S_532US |
INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
wireWriteRegister(INA219_REG_CONFIG, config);
wireReadRegister(INA219_REG_CALIBRATION,read_val3);
From what I understood from the datasheet, In order to read the register, the write command with 0 data has to be initiated to show what register you want to read. After that, A read command is initiated. A write and Read command difference is the first bit of the slave address ( either HIGH or LOW). Could someone give me any tips how do I properly initiate those two commands to read back a register.. I have tried to initiate just read command and It always returns a 0