Skip to content

Commit

Permalink
arduino 1.0 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
DeCristofaro John committed Jun 6, 2012
1 parent 39535cc commit 68bcd20
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
*
* Known bugs:
* * may return unreliable values if not connected to a bus or at
* bus currents below 1uA.
* bus currents below 10uA.
*
* Arduino 1.0 compatible as of 6/6/2012
*
* Dependencies:
* * Arduino Wire library
Expand Down
56 changes: 46 additions & 10 deletions ina219.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@
* 6 May 2012 by John De Cristofaro
*
*
* Only tested at standard i2c 100kbps signaling rate
* Tested at standard i2c 100kbps signaling rate.
*
* This library does not handle triggered conversion modes. It uses the INA219
* in continuous conversion mode. All reads are from continous conversions.
*
* A note about the gain (PGA) setting:
* The gain of the ADC pre-amplifier is programmable in the INA219, and can
* be set between 1/8x (default) and unity. This allows a shunt voltage
* range of +/-320mV to +/-40mV respectively. Something to keep in mind,
* however, is that this change in gain DOES NOT affect the resolution
* of the ADC, which is fixed at 1uV. What it does do is increase noise
* immunity by exploiting the integrative nature of the delta-sigma ADC.
* For the best possible reading, you should set the gain to the range
* of voltages that you expect to see in your particular circuit. See
* page 15 in the datasheet for more info about the PGA.
*
* Known bugs:
* * may return unreliable values if not connected to a bus or at
* bus currents below 10uA.
*
* Arduino 1.0 compatible as of 6/6/2012
*
* Dependencies:
* * Arduino Wire library
*
* MIT license
******************************************************************************/

Expand Down Expand Up @@ -71,10 +91,10 @@ void INA219::calibrate(float shunt_val, float v_shunt_max, float v_bus_max, floa
}


// config values (gain, bus adc, shunt adc, mode) can be derived from pp26-27 in the datasheet
// config values (range, gain, bus adc, shunt adc, mode) can be derived from pp26-27 in the datasheet
// defaults are:
// range = 1 (0-32V bus voltage range)
// gain = 3 (unity gain - 320mV range)
// gain = 3 (1/8 gain - 320mV range)
// bus adc = 3 (12-bit, single sample, 532uS conversion time)
// shunt adc = 3 (12-bit, single sample, 532uS conversion time)
// mode = 7 (continuous conversion)
Expand Down Expand Up @@ -146,10 +166,18 @@ void INA219::write16(uint8_t a, uint16_t d) {
uint8_t temp;
temp = (uint8_t)d;
d >>= 8;
Wire.beginTransmission(i2c_address); // start transmission to device
Wire.send(a); // sends register address to read from
Wire.send((uint8_t)d); // write data hibyte
Wire.send(temp); // write data lobyte;
Wire.beginTransmission(i2c_address); // start transmission to device

#if ARDUINO >= 100
Wire.write(a); // sends register address to read from
Wire.write((uint8_t)d); // write data hibyte
Wire.write(temp); // write data lobyte;
#else
Wire.send(a); // sends register address to read from
Wire.send((uint8_t)d); // write data hibyte
Wire.send(temp); // write data lobyte;
#endif

Wire.endTransmission(); // end transmission
delay(1);
}
Expand All @@ -162,9 +190,17 @@ int16_t INA219::read16(uint8_t a) {
write16(a, 0);

Wire.requestFrom((int)i2c_address, 2); // request 2 data bytes
ret = Wire.receive(); // rx hi byte
ret <<= 8;
ret |= Wire.receive(); // rx lo byte

#if ARDUINO >= 100
ret = Wire.read(); // rx hi byte
ret <<= 8;
ret |= Wire.read(); // rx lo byte
#else
ret = Wire.receive(); // rx hi byte
ret <<= 8;
ret |= Wire.receive(); // rx lo byte
#endif

Wire.endTransmission(); // end transmission

return ret;
Expand Down
30 changes: 28 additions & 2 deletions ina219.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,44 @@
* 6 May 2012 by John De Cristofaro
*
*
* Only tested at standard i2c 100kbps signaling rate
* Tested at standard i2c 100kbps signaling rate.
*
* This library does not handle triggered conversion modes. It uses the INA219
* in continuous conversion mode. All reads are from continous conversions.
*
* A note about the gain (PGA) setting:
* The gain of the ADC pre-amplifier is programmable in the INA219, and can
* be set between 1/8x (default) and unity. This allows a shunt voltage
* range of +/-320mV to +/-40mV respectively. Something to keep in mind,
* however, is that this change in gain DOES NOT affect the resolution
* of the ADC, which is fixed at 1uV. What it does do is increase noise
* immunity by exploiting the integrative nature of the delta-sigma ADC.
* For the best possible reading, you should set the gain to the range
* of voltages that you expect to see in your particular circuit. See
* page 15 in the datasheet for more info about the PGA.
*
* Known bugs:
* * may return unreliable values if not connected to a bus or at
* bus currents below 10uA.
*
* Arduino 1.0 compatible as of 6/6/2012
*
* Dependencies:
* * Arduino Wire library
*
* MIT license
******************************************************************************/

#ifndef ina219_h
#define ina219_h

#include <WProgram.h>

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#include <Wire.h>

#define INA219_DEBUG 0
Expand Down

0 comments on commit 68bcd20

Please sign in to comment.