Concatenate Wire.read()

Hi…

I am trying to read 24bits from my ADC but the ADC only returns 8bits and print them onto a new line. It is important that I read all 24 bits in 1 single output so I can code the decoding of the ADC…Anyone knows how can this be done?

Thanks in Advance! :slight_smile:

#include <SPI.h>
#include <Wire.h>

#define ADCAddr 0x14         //Address of I2C

uint32_t data;
int dataBits;
int dataBits2;
uint8_t adcChannel[8] = { 0xB0, 0xB8, 0xB1, 0xB9, 
                          0xB2, 0xBA, 0xB3, 0xBB };

void setup() {
  Wire.begin();
  
  Wire.beginTransmission(ADCAddr);
  Wire.write(0);
  Wire.write(adcChannel[0]);
  Wire.endTransmission();

  Serial.begin(9600);
  Serial.println("Welcome to BMS");
} 


void loop() { 
  for (int x = 0; x < 8; x++) {
    delay(170);
    
    Serial.print(adcChannel[x], HEX);
    Serial.println(":");

    Wire.beginTransmission(ADCAddr);
    Wire.write(adcChannel[x]);
    Wire.endTransmission();

    delay(170);

    Wire.requestFrom(ADCAddr, 3);

    while(Wire.available()) {
      data = Wire.read();
      Serial.println(data, BIN);
    }
    Serial.println();
  } 
  // wait a second
  delay(1000);
}

Which ADC part? Why the big delay between reads?

The part will likely be perfectly happy to send out 3 bytes at once if you give it a command that supports that.

CrossRoads:
Which ADC part? Why the big delay between reads?

The part will likely be perfectly happy to send out 3 bytes at once if you give it a command that supports that.

Hi Crossroads

I am using LTC2497.

send out 3 bytes at once if you give it a command that supports that.

I tried using the following,

if(Wire.available()<4) {
      data = Wire.read()<<16 | Wire.read()<<8 | Wire.read(); 
      Serial.println(data, BIN);
    }

The problem is that whenever i do a 16bit shift, it returns 8 bits again

If your device supports “autoincrement” when reading 8 bit registers, this is one way people go about reading a 24 bit number from the I2C bus:

  Wire.beginTransmission(device_addr);
  Wire.write(reg_addr);  //send starting register address
  Wire.endTransmission(false); //restart for read
  Wire.requestFrom(device_addr, 3, true); //get three bytes and combine
  long data = ((long) Wire.read() << 16) | (Wire.read()<<8) | Wire.read();

The above assumes the high byte comes first. Make sure you know the correct order of low, medium and high byte values.

jremington:
If your device supports “autoincrement” when reading 8 bit registers, this is one way people go about reading a 24 bit number from the I2C bus:

  Wire.beginTransmission(device_addr);

Wire.write(reg_addr);  //send starting register address
  Wire.endTransmission(false); //restart for read
  Wire.requestFrom(device_addr, 3, true); //get three bytes and combine
  long data = ((long) Wire.read() << 16) | (Wire.read()<<8) | Wire.read();



The above assumes the high byte comes first. **Make sure** you know the correct order of low, medium and high byte values.

Hi jremington

I got it to work it was the “(long)” that was missing that caused it not to capture all the reading.

Thanks alot!