Go Down

Topic: interfacing to max127 and i2c (Read 3842 times) previous topic - next topic

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

wayneft

The datasheet says the 4 MSB are 0101 followed by 3 address select bits and a final R/W bit.  Assuming the 3 address bits are set to 0 and the R/W is 0 yields an 8 bit address of 0x50. That should yield a 7 bit address of 0x28.
I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

BrainBooster

#17
Mar 28, 2011, 06:45 pm Last Edit: Mar 28, 2011, 06:47 pm by BrainBooster Reason: 1
tested with 0x28 and it always yelds:
end:0
req:2
16

wayneft

I just went back and looked at the posts because I was going to recommend Todbots I2C buss scanner, but it looks like you already ran that.  Just so you know, it returned back a value of 40 as the address.  The address of 40 is decimal 40, which converts to 0x28 hex so I would continue to use 0x28 as the address.
I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

wayneft

Quote
tested with 0x28 and it always yelds:
end:0
req:2
16

This means it's working!
I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

robtillaart

#20
Mar 28, 2011, 06:55 pm Last Edit: Mar 28, 2011, 06:57 pm by robtillaart Reason: 1
Quote
tested with 0x28 and it always yelds:
end:0
req:2

end: 0 ==> succes so the number 0x28 is OK

req: 2  lets look in the library

Code: [Select]
int8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
{
 // clamp to buffer length
 if(quantity > BUFFER_LENGTH){
   quantity = BUFFER_LENGTH;
 }
 // perform blocking read into buffer
 uint8_t read = twi_readFrom(address, rxBuffer, quantity);
 // set rx buffer iterator vars
 rxBufferIndex = 0;
 rxBufferLength = read;

 return read;
}

So this indicate you have read 2 bytes from the device, just as requested

Does the value 16 make sense?

Now make a nice library out of it :)


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

BrainBooster

Great! it works!
now i can read a value on channel 0, even if it is strangely high...
with the Vin pin  connected to channel 0 i read 29744, but what that number mean? where it comes from?
i'm using the following code:
Code: [Select]

int deviceAddress = 0x28;

#include <Wire.h>

void setup()
{
  Wire.begin();       
  Serial.begin(9600); 
}

void loop()
{


Wire.beginTransmission(deviceAddress);
Wire.send(0x88);
Wire.endTransmission();


Wire.requestFrom(deviceAddress, 2);
int result = 0;
for (int c = 0; c < 2; c++ )
if (Wire.available()) result = result * 256 + Wire.receive();
delay(500);
Serial.println(result,DEC);
}


wayneft

You need to divide that number by 16.  It's a 12 bit A/D and the LSB is trailed by 4 zeroes.
I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

wayneft

Quote
with the Vin pin  connected to channel 0 i read 29744, but what that number mean? where it comes from?

Doing the math I translate that to about 4.53 volts. 
I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

BrainBooster

let me understand the procedure...
how did you get to the final result in volts?

wayneft

Without seeing how you have the IC wired I made some assumptions and figured you were using the standard internal reference.  Taking the number you get from the A/D, 29744, and dividing that by 16 yields 1859.  Based off the control code you are sending 0x88, you have the A/D converter set to a range of 0 - 10 volts.  Each count should correspond to 2.44 mV ( 10volts/4096 counts).  2.44mV * 1859 counts = 4.53 volts.  Again this all assumes you are using a 10 volt reference.  What is the actual reference you are using and what is Vin measuring?
I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

robtillaart

The math

float Vref = 10.0;                               // see datasheet !! depends on control byte
float volts = Vref * value /16 /4096;   // 12 bits = max 4096
or shorter
float volts = Vref * value / 65536;     // 16 * 4096= 65536

10.0 * 29744 / 16 / 4096 = 4.53..


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

BrainBooster

i would like to use the 0 5v range and the schematic i'm using is the one you can see on the max127 datasheet (so, yes i'm using internal reference).
...so ( if got the point) to have a 0 5v range i should send 0x80 and then get the replay , divide it for 16 and multiplay for  0.001?
is it correct?

robtillaart

Quote
i would like to use the 0 5v range and the schematic i'm using is the one you can see on the max127 datasheet (so, yes i'm using internal reference).
...so ( if got the point) to have a 0 5v range i should send 0x80 and then get the replay , divide it for 16 and multiplay for  0.001?
is it correct?

Look at Table 1 the control byte format and Table 3 the Range and polarity.

You want 0..5 Volt => RNG = 0 , BIP = 0  (assume rest equal,  Channel 0) => control byte becomes 1000 0000 ==> 0x80   (right)

code: stays same except for the Vref.

float Vref = 5.0;                             
float volts = Vref * value / 65536;

Note you should make the variable result unsigned as it will become larger ...

Code: [Select]

int deviceAddress = 0x28;

#include <Wire.h>

void setup()
{
  Wire.begin();       
  Serial.begin(9600); 
}

void loop()
{
  // REQUEST SAMPLE
  Wire.beginTransmission(deviceAddress);
  Wire.send(0x80);   
  Wire.endTransmission();
  Wire.requestFrom(deviceAddress, 2);
  unsigned int result = 0;
  for (int c = 0; c < 2; c++ )
    if (Wire.available()) result = result * 256 + Wire.receive();

  // DISPLAY RAW RESULT
  Serial.println(result,DEC);

  // CONVERT TO VOLTS
  float Vref = 5.0;
  float voltage = Vref * result / 65535;
  Serial.print("voltage: ");
  Serial.println(voltage, 2);

  // ...
  delay(1000);
}


now to rewrite to something like

unsigned int max127(int deviceAddress, int Channel, ...)
{
  ...
  return result;
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

WanaGo

Hello

How did you get on with this, did you manage to read the correct result in etc?

I am about to start using one of these chips in the next few days.

Cheers
James

Go Up