简体   繁体   中英

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

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];
  beginMAXTransmission(i2cAddress);
  i2c_smbus_read_i2c_block_data(i2cMAXHandle, reg, 16, buff);
  endMAXTransmission();
  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
  usleep(m_conversionDelay);
  // 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;
}

It appears that I am now able to read data from the device after some more review. Here's the final code that works, for anyone else that might be interested.

static void writeMAXRegister(uint8_t i2cAddress, uint8_t reg, uint8_t value) {
    beginMAXTransmission(i2cAddress);
    i2c_smbus_write_word_data(i2cMAXHandle, reg, payload);
    endMAXTransmission();
    uint8_t payload = value;
}
static uint16_t readMAXRegister(uint8_t i2cAddress, uint8_t reg) {
    const uint8_t datalength = 2;
    unsigned char data[datalength];
    beginMAXTransmission(i2cAddress);
    i2c_smbus_read_i2c_block_data(i2cMAXHandle, reg, datalength, data);
    endMAXTransmission();
    uint16_t res =((data[0]&0xF)<<8)+data[1];//<---THIS READS 16 BITS AND REMOVES FIRST 4 '1111' BITS OF DATA
    return res;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM