Go Down

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

#### robtillaart

#15
##### Mar 27, 2011, 09:34 pm
why 0x28 ?
Rob Tillaart

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

#### wayneft

#16
##### Mar 27, 2011, 09:46 pm
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.

#### brainbooster

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

#### wayneft

#18
##### Mar 28, 2011, 06:51 pm
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.

#### wayneft

#19
##### Mar 28, 2011, 06:54 pm
Quote
tested with 0x28 and it always yelds:
end:0
req:2
16

This means it's working!

#### robtillaart

#20
##### Mar 28, 2011, 06:55 pmLast 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

#21
##### Mar 28, 2011, 07:12 pm
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

#22
##### Mar 28, 2011, 07:53 pm
You need to divide that number by 16.  It's a 12 bit A/D and the LSB is trailed by 4 zeroes.

#### wayneft

#23
##### Mar 28, 2011, 08:10 pm
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.

#### brainbooster

#24
##### Mar 28, 2011, 08:52 pm
let me understand the procedure...
how did you get to the final result in volts?

#### wayneft

#25
##### Mar 28, 2011, 09:06 pm
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?

#### robtillaart

#26
##### Mar 28, 2011, 09:08 pm
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

#27
##### Mar 28, 2011, 09:32 pm
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

#28
##### Mar 28, 2011, 11:40 pm
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

#29
##### May 07, 2011, 03:34 am
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