Permalink
Browse files

Changed value to float

  • Loading branch information...
1 parent 025260c commit ccd396a8a361c2844d6160826df84cd585efde83 K.Townsend committed Jun 4, 2012
Showing with 59 additions and 35 deletions.
  1. +40 −18 Adafruit_INA219.cpp
  2. +6 −4 Adafruit_INA219.h
  3. +13 −13 examples/getcurrent/getcurrent.pde
View
@@ -38,7 +38,7 @@ void Adafruit_INA219::wireWriteRegister (uint8_t reg, uint16_t value)
Wire.beginTransmission(INA219_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg); // Register
- Wire.write(value >> 8); // Upper 8-bits
+ Wire.write((value >> 8) & 0xFF); // Upper 8-bits
Wire.write(value & 0xFF); // Lower 8-bits
#else
Wire.send(reg); // Register
@@ -55,6 +55,7 @@ void Adafruit_INA219::wireWriteRegister (uint8_t reg, uint16_t value)
/**************************************************************************/
void Adafruit_INA219::wireReadRegister(uint8_t reg, uint16_t *value)
{
+
Wire.beginTransmission(INA219_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg); // Register
@@ -66,10 +67,10 @@ void Adafruit_INA219::wireReadRegister(uint8_t reg, uint16_t *value)
Wire.requestFrom(INA219_ADDRESS, 2);
#if ARDUINO >= 100
// Shift values to create properly formed integer
- *value = ((Wire.read() << 8) | Wire.read());
+ *value = ((Wire.read() << 8) + Wire.read());
#else
// Shift values to create properly formed integer
- *value = ((Wire.receive() << 8) | Wire.receive());
+ *value = ((Wire.receive() << 8) + Wire.receive());
#endif
}
@@ -273,50 +274,71 @@ void Adafruit_INA219::begin() {
// Set chip to known config values to start
ina219SetCalibration_32V_2A();
}
-
+
/**************************************************************************/
/*!
- @brief Gets the shunt voltage (16-bit signed integer, so +-32767)
+ @brief Gets the raw bus voltage (16-bit signed integer, so +-32767)
*/
/**************************************************************************/
-int16_t Adafruit_INA219::getShuntVoltage() {
+uint16_t Adafruit_INA219::getBusVoltage_raw() {
uint16_t value;
- wireReadRegister(INA219_REG_SHUNTVOLTAGE, &value);
- return value;
+ wireReadRegister(INA219_REG_BUSVOLTAGE, &value);
+
+ // Shift to the right 3 to drop CNVR and OVF and multiply by LSB
+ return (value >> 3) * 4;
}
/**************************************************************************/
/*!
- @brief Gets the shunt voltage (16-bit signed integer, so +-32767)
+ @brief Gets the raw shunt voltage (16-bit signed integer, so +-32767)
*/
/**************************************************************************/
-int16_t Adafruit_INA219::getBusVoltage() {
+uint16_t Adafruit_INA219::getShuntVoltage_raw() {
uint16_t value;
- wireReadRegister(INA219_REG_BUSVOLTAGE, &value);
- // Shift to the right 3 to drop CNVR and OVF and then multiply by LSB
- return (value >> 3) * 4;
+ wireReadRegister(INA219_REG_SHUNTVOLTAGE, &value);
+ return value;
}
/**************************************************************************/
/*!
@brief Gets the raw current value (16-bit signed integer, so +-32767)
*/
/**************************************************************************/
-int16_t Adafruit_INA219::getCurrent() {
+uint16_t Adafruit_INA219::getCurrent_raw() {
uint16_t value;
wireReadRegister(INA219_REG_CURRENT, &value);
return value;
}
+
+/**************************************************************************/
+/*!
+ @brief Gets the shunt voltage in mV (so +-327mV)
+*/
+/**************************************************************************/
+float Adafruit_INA219::getShuntVoltage_mV() {
+ uint16_t value;
+ value = getShuntVoltage_raw();
+ return value * 0.01;
+}
+
+/**************************************************************************/
+/*!
+ @brief Gets the shunt voltage in volts
+*/
+/**************************************************************************/
+float Adafruit_INA219::getBusVoltage_V() {
+ uint16_t value = getBusVoltage_raw();
+ return value * 0.001;
+}
/**************************************************************************/
/*!
@brief Gets the current value in mA, taking into account the
config settings and current LSB
*/
/**************************************************************************/
-int16_t Adafruit_INA219::getCurrent_mA() {
- uint16_t value;
- wireReadRegister(INA219_REG_CURRENT, &value);
- return value / ina219_currentDivider_mA;
+float Adafruit_INA219::getCurrent_mA() {
+ float valueDec = getCurrent_raw() / ina219_currentDivider_mA;
+ return valueDec;
}
View
@@ -113,10 +113,9 @@ class Adafruit_INA219{
public:
Adafruit_INA219();
void begin(void);
- int16_t getShuntVoltage(void);
- int16_t getBusVoltage(void);
- int16_t getCurrent(void);
- int16_t getCurrent_mA(void);
+ float getBusVoltage_V(void);
+ float getShuntVoltage_mV(void);
+ float getCurrent_mA(void);
private:
// The following multipliers are used to convert raw current and power
@@ -128,4 +127,7 @@ class Adafruit_INA219{
void wireReadRegister(uint8_t reg, uint16_t *value);
void ina219SetCalibration_32V_2A(void);
void ina219SetCalibration_32V_1A(void);
+ uint16_t getBusVoltage_raw(void);
+ uint16_t getShuntVoltage_raw(void);
+ uint16_t getCurrent_raw(void);
};
@@ -10,26 +10,26 @@ void setup(void)
Serial.begin(115200);
Serial.println("Hello!");
- Serial.print("Measuring voltage and current with INA219 ...");
+ Serial.println("Measuring voltage and current with INA219 ...");
ina219.begin();
}
void loop(void)
{
- int16_t current_mA = 0;
- int16_t busvoltage = 0;
- int16_t shuntvoltage = 0;
- int16_t loadvoltage = 0;
-
- shuntvoltage = ina219.getShuntVoltage();
- busvoltage = ina219.getBusVoltage();
+ float shuntvoltage = 0;
+ float busvoltage = 0;
+ float current_mA = 0;
+ float loadvoltage = 0;
+
+ shuntvoltage = ina219.getShuntVoltage_mV();
+ busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
- loadvoltage = busvoltage + (shuntvoltage / 100);
+ loadvoltage = busvoltage + (shuntvoltage / 1000);
- Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage/100); Serial.print('.'); Serial.print(shuntvoltage%100); Serial.println("V");
- Serial.print("Bus Voltage: "); Serial.print(busvoltage/1000); Serial.print('.'); Serial.print(busvoltage%1000); Serial.println("V");
- Serial.print("Load Voltage: "); Serial.print(loadvoltage/1000); Serial.print('.'); Serial.print(loadvoltage%1000); Serial.println("V");
- Serial.print("Current: "); Serial.print(current_mA); Serial.println("mA");
+ Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V");
+ Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
+ Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V");
+ Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
Serial.println("");
delay(2000);

0 comments on commit ccd396a

Please sign in to comment.