@@ -45,13 +45,12 @@ inaDet::inaDet(inaEEPROM &inaEE) {
45
45
break ;
46
46
47
47
case INA228:
48
- power_LSB = 0 ; // TODO
49
48
busVoltageRegister = INA228_BUS_VOLTAGE_REGISTER;
50
- busVoltage_LSB = INA228_BUS_VOLTAGE_LSB;
51
49
shuntVoltageRegister = INA228_SHUNT_VOLTAGE_REGISTER;
52
-
53
- currentRegister = INA226_CURRENT_REGISTER;
54
- shuntVoltage_LSB = INA226_SHUNT_VOLTAGE_LSB;
50
+ shuntVoltage_LSB = INA228_SHUNT_VOLTAGE_LSB;
51
+ currentRegister = INA228_CURRENT_REGISTER;
52
+ current_LSB = ((uint64_t )maxBusAmps * 1000000000ULL ) / 524288ULL ;
53
+ power_LSB = 34 * current_LSB;
55
54
break ;
56
55
57
56
case INA260:
@@ -366,6 +365,11 @@ void INA_Class::initDevice(const uint8_t deviceNumber) {
366
365
bitSet (tempRegister, INA219_BRNG_BIT); // set to 1 for 0-32 volts
367
366
writeWord (INA_CONFIGURATION_REGISTER, tempRegister, ina.address ); // Write to config register
368
367
break ;
368
+ case INA228:
369
+ // Compute calibration register
370
+ calibration = (131072ULL * (int64_t )ina.current_LSB * (int64_t )ina.microOhmR ) / 10000000000ULL ;
371
+ writeWord (INA228_CALIBRATION_REGISTER, calibration, ina.address ); // Write calibration
372
+ break ;
369
373
case INA226:
370
374
case INA230:
371
375
case INA231:
@@ -377,7 +381,8 @@ void INA_Class::initDevice(const uint8_t deviceNumber) {
377
381
case INA260:
378
382
case INA3221_0:
379
383
case INA3221_1:
380
- case INA3221_2: break ;
384
+ case INA3221_2:
385
+ break ;
381
386
} // of switch type
382
387
} // of method initDevice()
383
388
void INA_Class::setBusConversion (const uint32_t convTime, const uint8_t deviceNumber) {
@@ -580,7 +585,7 @@ uint16_t INA_Class::getBusMilliVolts(const uint8_t deviceNumber) {
580
585
uint32_t busVoltage = getBusRaw (deviceNumber); // Get raw voltage from device
581
586
if (ina.type == INA228) {
582
587
// The accuracy is 20bits and 195.3125uv is the LSB
583
- busVoltage = (uint64_t )busVoltage * 1953125 / 10000000 ; // conversion to get mV
588
+ busVoltage = (( uint64_t )busVoltage * ( uint64_t )INA228_BUS_VOLTAGE_LSB) / 10000000ULL ; // conversion to get mV
584
589
} else {
585
590
busVoltage = busVoltage * ina.busVoltage_LSB / 100 ; // conversion to get mV
586
591
} // if-then-else an INA228
@@ -626,7 +631,7 @@ int32_t INA_Class::getShuntMicroVolts(const uint8_t deviceNumber) {
626
631
shuntVoltage = busMicroAmps / 200 ; // 2mOhm resistor, convert with Ohm's law
627
632
} else {
628
633
if (ina.type == INA228) {
629
- shuntVoltage = shuntVoltage * ina.shuntVoltage_LSB / 10 ; // Convert to microvolts
634
+ shuntVoltage = (( uint32_t ) shuntVoltage * ( uint32_t ) ina.shuntVoltage_LSB ) / 10000UL ; // Convert to microvolts
630
635
} else {
631
636
shuntVoltage = shuntVoltage * ina.shuntVoltage_LSB / 10 ; // Convert to microvolts
632
637
} // if-then a INA228 with 20 bit accuracy
@@ -679,15 +684,31 @@ int32_t INA_Class::getBusMicroAmps(const uint8_t deviceNumber) {
679
684
@return int32_t signed integer for computed microamps on the bus */
680
685
readInafromEEPROM (deviceNumber); // Load EEPROM to ina structure
681
686
int32_t microAmps = 0 ;
682
- if (ina.type == INA3221_0 || ina.type == INA3221_1 ||
683
- ina.type == INA3221_2) // Doesn't compute Amps
684
- {
687
+ int32_t raw = 0 ;
688
+ switch (ina.type ) {
689
+ case INA3221_0: // Doesn't compute Amps
690
+ case INA3221_1:
691
+ case INA3221_2:
685
692
microAmps =
686
693
(int64_t )getShuntMicroVolts (deviceNumber) * ((int64_t )1000000 / (int64_t )ina.microOhmR );
687
- } else {
694
+ break ;
695
+ case INA228:
696
+ raw = read3Bytes (ina.currentRegister , ina.address );
697
+ // The number is two's complement, so if negative we need to pad when shifting //
698
+ if (raw & 0x800000 ) {
699
+ raw = (raw >> 4 ) | 0xFFF00000 ; // first 12 bits are "1"
700
+ } else {
701
+ raw = raw >> 4 ;
702
+ } // if-then negative
703
+
704
+ microAmps = ((int64_t )raw * (int64_t )ina.current_LSB ) / 1000ULL ;
705
+ break ;
706
+ default :
688
707
microAmps = (int64_t )readWord (ina.currentRegister , ina.address ) * (int64_t )ina.current_LSB /
689
708
(int64_t )1000 ;
690
- } // of if-then-else an INA3221
709
+ break ;
710
+ }
711
+
691
712
return (microAmps);
692
713
} // of method getBusMicroAmps()
693
714
int64_t INA_Class::getBusMicroWatts (const uint8_t deviceNumber) {
@@ -699,17 +720,27 @@ int64_t INA_Class::getBusMicroWatts(const uint8_t deviceNumber) {
699
720
@return int64_t signed integer for computed microwatts on the bus
700
721
*/
701
722
int64_t microWatts = 0 ;
723
+ int32_t raw = 0 ;
724
+
702
725
readInafromEEPROM (deviceNumber); // Load EEPROM to ina structure
703
- if (ina.type == INA3221_0 || ina.type == INA3221_1 ||
704
- ina.type == INA3221_2) // Doesn't compute Amps
705
- {
726
+ switch (ina.type ) {
727
+ case INA3221_0: // Doesn't compute Amps
728
+ case INA3221_1:
729
+ case INA3221_2:
706
730
microWatts =
707
731
((int64_t )getShuntMicroVolts (deviceNumber) * (int64_t )1000000 / (int64_t )ina.microOhmR ) *
708
732
(int64_t )getBusMilliVolts (deviceNumber) / (int64_t )1000 ;
709
- } else {
733
+ break ;
734
+ case INA228:
735
+ raw = read3Bytes (INA228_POWER_REGISTER, ina.address );
736
+
737
+ microWatts = ((int64_t )raw * (int64_t )ina.power_LSB ) / 10000ULL ;
738
+ break ;
739
+ default :
710
740
microWatts =
711
741
(int64_t )readWord (INA_POWER_REGISTER, ina.address ) * (int64_t )ina.power_LSB / (int64_t )1000 ;
712
742
if (getShuntRaw (deviceNumber) < 0 ) microWatts *= -1 ; // Invert if negative voltage
743
+ break ;
713
744
} // of if-then-else an INA3221
714
745
return (microWatts);
715
746
} // of method getBusMicroWatts()
@@ -1068,9 +1099,9 @@ void INA_Class::setAveraging(const uint16_t averages, const uint8_t deviceNumber
1068
1099
deviceNumber % _DeviceCount == i) // If this device needs setting
1069
1100
{
1070
1101
readInafromEEPROM (i); // Load EEPROM to struct
1071
- configRegister = readWord (INA_CONFIGURATION_REGISTER, ina.address ); // Get current register
1072
1102
switch (ina.type ) {
1073
1103
case INA219:
1104
+ configRegister = readWord (INA_CONFIGURATION_REGISTER, ina.address ); // Get current register
1074
1105
if (averages >= 128 )
1075
1106
averageIndex = 15 ;
1076
1107
else if (averages >= 64 )
@@ -1090,6 +1121,7 @@ void INA_Class::setAveraging(const uint16_t averages, const uint8_t deviceNumber
1090
1121
configRegister &= ~INA219_CONFIG_AVG_MASK; // zero out the averages part
1091
1122
configRegister |= averageIndex << 3 ; // shift in the SADC averages
1092
1123
configRegister |= averageIndex << 7 ; // shift in the BADC averages
1124
+ writeWord (INA_CONFIGURATION_REGISTER, configRegister, ina.address ); // Save new value
1093
1125
break ;
1094
1126
case INA226:
1095
1127
case INA230:
@@ -1098,6 +1130,7 @@ void INA_Class::setAveraging(const uint16_t averages, const uint8_t deviceNumber
1098
1130
case INA3221_1:
1099
1131
case INA3221_2:
1100
1132
case INA260:
1133
+ configRegister = readWord (INA_CONFIGURATION_REGISTER, ina.address ); // Get current register
1101
1134
if (averages >= 1024 )
1102
1135
averageIndex = 7 ;
1103
1136
else if (averages >= 512 )
@@ -1116,9 +1149,31 @@ void INA_Class::setAveraging(const uint16_t averages, const uint8_t deviceNumber
1116
1149
averageIndex = 0 ;
1117
1150
configRegister &= ~INA226_CONFIG_AVG_MASK; // zero out the averages part
1118
1151
configRegister |= averageIndex << 9 ; // shift in the averages to reg
1152
+ writeWord (INA_CONFIGURATION_REGISTER, configRegister, ina.address ); // Save new value
1153
+ break ;
1154
+ case INA228:
1155
+ configRegister = readWord (INA228_CONFIGURATION_REGISTER, ina.address ); // Get current register
1156
+ if (averages >= 1024 )
1157
+ averageIndex = 7 ;
1158
+ else if (averages >= 512 )
1159
+ averageIndex = 6 ;
1160
+ else if (averages >= 256 )
1161
+ averageIndex = 5 ;
1162
+ else if (averages >= 128 )
1163
+ averageIndex = 4 ;
1164
+ else if (averages >= 64 )
1165
+ averageIndex = 3 ;
1166
+ else if (averages >= 16 )
1167
+ averageIndex = 2 ;
1168
+ else if (averages >= 4 )
1169
+ averageIndex = 1 ;
1170
+ else
1171
+ averageIndex = 0 ;
1172
+ configRegister &= ~INA228_CONFIG_AVG_MASK; // zero out the averages part
1173
+ configRegister |= averageIndex; // shift in the averages to reg
1174
+ writeWord (INA228_CONFIGURATION_REGISTER, configRegister, ina.address ); // Save new value
1119
1175
break ;
1120
1176
} // of switch type
1121
- writeWord (INA_CONFIGURATION_REGISTER, configRegister, ina.address ); // Save new value
1122
1177
} // of if this device needs to be set
1123
1178
} // for-next each device loop
1124
1179
} // of method setAveraging()
0 commit comments