So, about 5 year ago I wrote some code that read multiple sensors (INA219 chips) data via I2C bus.
Now I am using same sensors and tried to copy/paste to my new project some of the code I wrote back in 2010. I think i was using version 0017 back then.
So, there is a subroutine that is called frequently to read sensors. It is given a pointer in memory where to start write data from that sensor. And after subroutine is completed, data should be available in a memory part which starts at given pointer. Instead of real data there is just some garbage, while sensor is read correctly.
Code was copied from final working version that was coded on 0017 version.
subroutine itself is following:
void readINA219(byte address, byte PGA, int* datapointer){
//shift right for correct voltage and multiply with 4 as every bit = 4mV
//Bus voltage written to given datapointer address
int busvoltage = 4*(I2Cread(address, 2) >> 3);
*datapointer = busvoltage;
//increase datapointer address or array pointer address
datapointer = datapointer++;
//shunt voltage
int shuntvoltage = I2Cread(address, 1);
switch (PGA) {
case 1:
shuntvoltage = bitClear(shuntvoltage,14);
shuntvoltage = bitClear(shuntvoltage,13);
shuntvoltage = bitClear(shuntvoltage,12);
break;
case 2:
shuntvoltage = bitClear(shuntvoltage,14);
shuntvoltage = bitClear(shuntvoltage,13);
break;
case 4:
shuntvoltage = bitClear(shuntvoltage,14);
break;
case 8:
break;
}
*datapointer = shuntvoltage;
datapointer = datapointer++;
//power
*datapointer = (I2Cread(address, 3))/5000;
datapointer = datapointer++;
//current
int current = I2Cread(address, 4);
*datapointer = current;
datapointer = datapointer++;
//calibration
*datapointer = I2Cread(address, 5);
datapointer = datapointer++;
//configuration
*datapointer = I2Cread(address, 0);
}
And part of code that calls subroutine is following:
//create array for storing INA219 values
int INA219data[6];
//create pointer to value array first element for function
int* INA219datapointer = &(INA219data[0]);
// read INA219 values. chip Address + shunt voltage setting
//Critical current sensor
readINA219(76,4,INA219datapointer);
AnalogSensorint[0] = INA219data[0];
Serial.print(F("Bus voltage: "));
Serial.println(INA219data[0]);
AnalogSensorint[2] = INA219data[3];
Serial.print(F("Consumed critical current: "));
Serial.println(INA219data[3]);
I wonder what goes wrong in new version?