Skip to content
This repository was archived by the owner on Jun 22, 2018. It is now read-only.

Commit 07a0413

Browse files
SV-ZanshinSV-Zanshin
authored andcommitted
Fix for sporadic reading errors
Issue #14 had 2 users document sporadic measurement issues. The cause was missing calls to the memory structure which contains the pointer to the device's I2C address which had writes going to device 0 instead of the correct device.
1 parent bb4bffe commit 07a0413

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

Examples/DisplayReadings/DisplayReadings.ino

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
** **
3535
** Vers. Date Developer Comments **
3636
** ====== ========== ============================= ============================================================== **
37+
** 1.0.5 2018-06-08 https://github.com/SV-Zanshin removed unneeded prototype definitions **
3738
** 1.0.4 2018-06-01 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/11 Corrected loop **
3839
** 1.0.3 2017-09-18 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/6 Multiple INA226s **
3940
** 1.0.2 2017-08-09 https://github.com/SV-Zanshin Cosmetic changes **
@@ -52,11 +53,6 @@ const uint32_t SERIAL_SPEED = 115200; //
5253
INA226_Class INA226; // INA class instantiation //
5354
uint8_t devicesFound = 0; // Number of INA226s found //
5455
/*******************************************************************************************************************
55-
** Declare prototypes for all functions used **
56-
*******************************************************************************************************************/
57-
void setup(); // Called once on power-up/restart //
58-
void loop(); // Called repeatedly after setup() //
59-
/*******************************************************************************************************************
6056
** Method Setup(). This is an Arduino IDE method which is called first upon initial boot or restart. It is only **
6157
** called one time and all of the variables and other initialization calls are done here prior to entering the **
6258
** main loop for data measurement and storage. **
@@ -66,10 +62,12 @@ void setup() { //
6662
#ifdef __AVR_ATmega32U4__ // If we are a 32U4 processor, then //
6763
delay(2000); // wait 2 seconds for the serial //
6864
#endif // interface to initialize //
69-
Serial.print(F("\n\nDisplay INA226 Readings V1.0.3\n")); // Display program information //
65+
Serial.print(F("\n\nDisplay INA226 Readings V1.0.5\n")); // Display program information //
66+
Serial.print(F(" - Searching & Initializing INA226\n")); // Display program information //
7067
// The begin initializes the calibration for an expected ±1 Amps maximum current and for a 0.1Ohm resistor, and //
7168
// since no specific device is given as the 3rd parameter all devices are initially set to these values //
72-
devicesFound = INA226.begin(1,100000); // Set expected Amps and resistor //
69+
devicesFound = INA226.begin(1,1960000); // Set expected Amps and resistor //
70+
// devicesFound = INA226.begin(1,100000); // Set expected Amps and resistor //
7371
Serial.print(F("Detected ")); // //
7472
Serial.print(devicesFound); // //
7573
Serial.println(F(" INA226 devices on I2C bus")); // //
@@ -95,13 +93,13 @@ void loop() { //
9593
Serial.print(F("mV\nBus amperage ")); // //
9694
Serial.print(i+1); // //
9795
Serial.print(F(": ")); // //
98-
Serial.print((float)INA226.getBusMicroAmps(i)/1000.0,3); // Convert to milliamp //
96+
Serial.print((float)INA226.getBusMicroAmps(i)/1000.0,4); // Convert to milliamp //
9997
Serial.print(F("mA\nBus wattage ")); // //
10098
Serial.print(i+1); // //
10199
Serial.print(F(": ")); // //
102-
Serial.print((float)INA226.getBusMicroWatts(i)/1000.0,3); // Convert to milliwatts //
100+
Serial.print((float)INA226.getBusMicroWatts(i)/1000.0,4); // Convert to milliwatts //
103101
Serial.println(F("mW")); // //
104102
Serial.println(); // //
105103
} // of for-next each device loop // //
106-
delay(5000); // //
104+
delay(1000); // //
107105
} // of method loop //----------------------------------//

INA226.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ void INA226_Class::setAveraging(const uint16_t averages, //
213213
uint8_t averageIndex; // Store indexed value for register //
214214
int16_t configRegister; // Configuration register contents //
215215
inaDet ina; // Hold device details in structure //
216+
EEPROM.get((deviceNumber%_DeviceCount)*sizeof(ina),ina); // Read EEPROM values //
216217
for(uint8_t i=0;i<_DeviceCount;i++) { // Loop for each device found //
217218
if(deviceNumber==UINT8_MAX || deviceNumber%_DeviceCount==i ) { // If this device needs setting //
218219
configRegister = readWord(INA_CONFIGURATION_REGISTER,ina.address); // Get the current register //
@@ -236,6 +237,7 @@ void INA226_Class::setAveraging(const uint16_t averages, //
236237
void INA226_Class::setBusConversion(uint8_t convTime, // Set timing for Bus conversions //
237238
const uint8_t deviceNumber ) { // //
238239
inaDet ina; // Hold device details in structure //
240+
EEPROM.get((deviceNumber%_DeviceCount)*sizeof(ina),ina); // Read EEPROM values //
239241
int16_t configRegister; // Store configuration register //
240242
for(uint8_t i=0;i<_DeviceCount;i++) { // Loop for each device found //
241243
if(deviceNumber==UINT8_MAX || deviceNumber%_DeviceCount==i ) { // If this device needs setting //
@@ -253,6 +255,7 @@ void INA226_Class::setBusConversion(uint8_t convTime, //
253255
void INA226_Class::setShuntConversion(uint8_t convTime, // Set timing for Bus conversions //
254256
const uint8_t deviceNumber ) { // //
255257
inaDet ina; // Hold device details in structure //
258+
EEPROM.get((deviceNumber%_DeviceCount)*sizeof(ina),ina); // Read EEPROM values //
256259
int16_t configRegister; // Store configuration register //
257260
for(uint8_t i=0;i<_DeviceCount;i++) { // Loop for each device found //
258261
if(deviceNumber==UINT8_MAX || deviceNumber%_DeviceCount==i ) { // If this device needs setting //
@@ -271,6 +274,7 @@ void INA226_Class::setShuntConversion(uint8_t convTime, //
271274
void INA226_Class::waitForConversion(const uint8_t deviceNumber) { // Wait for current conversion //
272275
uint16_t conversionBits = 0; // //
273276
inaDet ina; // Hold device details in structure //
277+
EEPROM.get((deviceNumber%_DeviceCount)*sizeof(ina),ina); // Read EEPROM values //
274278
for(uint8_t i=0;i<_DeviceCount;i++) { // Loop for each device found //
275279
if(deviceNumber==UINT8_MAX || deviceNumber%_DeviceCount==i ) { // If this device needs setting //
276280
conversionBits = 0; // //
@@ -287,6 +291,7 @@ void INA226_Class::waitForConversion(const uint8_t deviceNumber) { //
287291
void INA226_Class::setAlertPinOnConversion(const bool alertState, // Enable pin change on conversion //
288292
const uint8_t deviceNumber ) { // //
289293
inaDet ina; // Hold device details in structure //
294+
EEPROM.get((deviceNumber%_DeviceCount)*sizeof(ina),ina); // Read EEPROM values //
290295
uint16_t alertRegister; // Hold the alert register //
291296
for(uint8_t i=0;i<_DeviceCount;i++) { // Loop for each device found //
292297
if(deviceNumber==UINT8_MAX || deviceNumber%_DeviceCount==i ) { // If this device needs setting //

INA226.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
** **
3434
** Vers. Date Developer Comments **
3535
** ====== ========== ============================= ============================================================== **
36+
** 1.0.7 2018-06-08 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/14. Missing calls **
37+
** EEPROM.Get() for device number caused errors sporadic errors **
3638
** 1.0.6 2018-06-01 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/12. Add getMode() **
3739
** 1.0.6 2018-05-29 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/10. Limit Scan addr**
3840
** 1.0.5 2017-09-24 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/6. Multiple INA226 **

0 commit comments

Comments
 (0)