Checking I2C code?

Hi, I was wondering if someone could possibly look over my I2C code here and check if I'm doing the write thing for this device:

http://ww1.microchip.com/downloads/en/DeviceDoc/22072b.pdf (Starts at about page 14)

And my code:

 void setup(){
  Serial.begin(115200);
  Wire.begin();
  Wire.beginTransmission(0x80);
  Wire.write(0); // Write config register to all 0's, incl RDY bit to store config data
  Wire.endTransmission();
}

void loop(){
  Serial.println(readADC(), HEX);
  delay(100);
}

byte readADC(){
  byte data;
  Wire.beginTransmission(ADCAddr);
  Wire.write(0x80); // write 10000000 to set RDY bit
  Wire.requestFrom(ADCAddr, 1);
  if (Wire.available()) {
    data = Wire.read();
  }
  Wire.endTransmission();
  return data;
}

I seem to be just getting either FF's or 0's back from it, but I bet I'm doing something really simple wrong :(

Dan

http://www.gammon.com.au/i2c

The order here is wrong:

  Wire.beginTransmission(ADCAddr);
  Wire.write(0x80); // write 10000000 to set RDY bit
  Wire.requestFrom(ADCAddr, 1);
  if (Wire.available()) {
    data = Wire.read();
  }
  Wire.endTransmission();

More correct would be:

  Wire.beginTransmission(ADCAddr);
  Wire.write(0x80); // write 10000000 to set RDY bit
  Wire.endTransmission();
  Wire.requestFrom(ADCAddr, 1);
  if (Wire.available()) {
    data = Wire.read();
  }

Thanks, although I'm still getting FF's back :(

I am wondering though, is the RDY bit actually in the same register as the config stuff? It just ignores the extra bits if RDY != 0?

I took some advice from that page you linked and put in an indicator on endTransmission, and it would appear that it is working, I just don't seem to be getting good data back?

Can you post your updated code please?

Also can you run the I2C scanner on the linked page? Getting 0xFF is typically what happens when the other end doesn't respond.

I2C scanner. Scanning ...
Found address: 104 (0x68)
Done.
Found 1 device(s).

and code (if there is things like variable defines etc missing, it's cos I've pulled this out of a bunch more code):

#define ADCAddr 0x68 // ADC address

void setup(){
  Wire.begin();
  Wire.beginTransmission(ADCAddr);
  Wire.write(0x00);
  Wire.endTransmission();
}

void loop(){
  Serial.println(readADC(), HEX);
  Serial.println(gotack);
  delay(100);
}

byte readADC(){
  byte data;
  Wire.beginTransmission(ADCAddr);
  Wire.write(0x80); // write 10000000 to set RDY bit
  gotack = Wire.endTransmission();
  Wire.requestFrom(ADCAddr, 1);
  if (Wire.available()) {
    data = Wire.read();
  }
  return data;
}

Thanks for the help :)

Looks OK, but I'm not sure why you are setting all the other bits to zero.

Well, from what I gathered on page 14, I send a 0 for RDY to put it into configuration mode, then I set 2 0's for the C bits, as they're not used anyway, then I want one-shot conversion mode, so 0 for that too, I want 12 bits so that's another 2 0's, and then I want the gain at 1, so another 2 0's. Am I reading that right?

What's with the R/W-0 and -1 above the registers mean?

That’s fine if that’s what you want. Maybe try not one-shot. Your code looks OK, so without measuring it with a logic analyzer, I can only suggest trying different configurations.

Your code as posted would return undefined data if the requestFrom didn’t work. Perhaps more explicitly test and report if the requestFrom does not return 1.

OK, it looks like it’s actually doing something now. The value is changing between about 0 and 5 when I apply voltage to the ADC’s input, so something is changing :smiley:

#include "Wire.h"
#define ADCAddr 0x68 // ADC address
int gotack = 1;

void setup(){
  Wire.begin();
  Wire.beginTransmission(ADCAddr);
  Wire.write(0x10);
  Wire.endTransmission();
}

void loop(){
  readADC();
  delay(100);
}

byte readADC(){
  byte data[3];
  int i = 0;
  Wire.beginTransmission(ADCAddr);
  Wire.write(0x80); // write 10000000 to set RDY bit
  gotack = Wire.endTransmission();
  Wire.requestFrom(ADCAddr, 2,true);
  while (Wire.available()) {
    data[i] = Wire.read();
    i++;
  }
  for (int w; w<sizeof(data); w++){
  Serial.print(data[w],BIN);
  }
  Serial.println(" ");
}
}

However, for some reason I seem to be getting 18 bits?? The values do change when I vary the input to the ADC though.

For a full scale + input voltage, I should be getting 0111111111111111, instead I’m getting 111111111111001111 ?

Oh? How are you getting 18 bits when you read two bytes? (16 bits)

Hmm, it looks like it was because I had the data array size set to 3 instead of 2, however now I’m only getting 15 bits?

byte readADC(){
  byte data[2];
  int i = 0;
  //Wire.beginTransmission(ADCAddr);
  //Wire.write(0x80); // write 10000000 to set RDY bit
  //gotack = Wire.endTransmission();
  Wire.requestFrom(ADCAddr, 2,true);
  while (Wire.available()) {
    data[i] = Wire.read();
    i++;
  }
  for (int w=0; w<sizeof(data); w++){
  Serial.print(data[w],BIN);
  }
  Serial.println(" ");
}

Nevermind, just looks like the serial monitor is truncating 0’s on me! Bit of a trap!

EDIT: All working now! Yay :smiley:

Dan