Pages: 1 [2]   Go Down
Author Topic: interfacing to max127 and i2c  (Read 3269 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

why 0x28 ?
Logged

Rob Tillaart

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

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

Deep south of Italy
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2955
The quieter you become, the more you can hear
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

tested with 0x28 and it always yelds:
end:0
req:2
16
« Last Edit: March 28, 2011, 11:47:45 am by BrainBooster » Logged

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
tested with 0x28 and it always yelds:
end:0
req:2
16
This means it's working!
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley


« Last Edit: March 28, 2011, 11:57:41 am by robtillaart » Logged

Rob Tillaart

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

Deep south of Italy
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2955
The quieter you become, the more you can hear
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}

Logged

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

Deep south of Italy
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2955
The quieter you become, the more you can hear
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..


Logged

Rob Tillaart

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

Deep south of Italy
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2955
The quieter you become, the more you can hear
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
}
Logged

Rob Tillaart

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

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: 1 [2]   Go Up
Jump to: