How do I read data via i2c from a MAX11613 chip using C++ on a RPI 3B+

  adc, c++, i2c, raspberry-pi3

I’m trying to write a driver for a MAX11613 ADC chip (MAX11613 Datasheet) in c++. I think I’ve got the write code correct for the setup and config, but I’m having some trouble with the read code. I am setting the chip up to read using the internal clock in Unipolar mode and the internal voltage reference, then writing the config to scan using AIN0 as the + signal and AIN1 as the – signal channels and report the differential reading. It does seem to read data, though the data appears to be very erratic and not what is anticipated based on oscilloscope results.

Setup bits: //1111 0000=0xF0 SEL2=1, SEL1=1, SEL0=1, INTERNAL CLOCK, UNIPOLAR

Config bits: //0110 0000=0x60 SCAN1=1, SCAN0=1, AIN0-AIN1, DIFFERENTIAL

Here’s my read code, which may be part of the problem:

static uint16_t readMAXRegister(uint8_t i2cAddress, uint8_t reg) {
  unsigned char buff[16];
  i2c_smbus_read_i2c_block_data(i2cMAXHandle, reg, 16, buff);
  uint16_t res = (buff[1] << 8) + buff[0];
  return res;
int16_t MAX11613::readMAXADC_Differential_0_1() {
  // Write config register to the ADC
  writeMAXRegister(m_i2cAddress, MAX_WRITE, MAX_CONFIG);
  // Wait for the conversion to complete
  // Read the conversion results
  uint16_t res = readMAXRegister(m_i2cAddress, 1) >> m_bitShift;
  // Shift 12-bit results right 4 bits
  res = (res >> 11) == 0 ? res : -1 ^ 0xFFF | res;
  std::bitset<12> y(res);
  std::cout << "READ RESULT " << y << " " << res << std::endl;
  return (int16_t)res;

Source: Windows Questions C++