Skip to content

Commit

Permalink
Optimize getCurrent, getShuntVoltage data
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyu-Zhao committed Aug 5, 2023
1 parent 8e60aae commit 2bff7a1
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/INA3221.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ int32_t INA3221::getShuntVoltage(ina3221_ch_t channel) {

_read(reg, &val_raw);

// 1 LSB = 5uV
res = (int16_t)val_raw * 5;
// 1 LSB = 40uV
res = (int32_t)(val_raw >> 3) * 40;

return res;
}
Expand Down Expand Up @@ -474,7 +474,7 @@ float INA3221::getCurrent(ina3221_ch_t channel) {
float current_A = 0;

shunt_uV = getShuntVoltage(channel);
current_A = shunt_uV / (int32_t)_shuntRes[channel] / 1000.0;
current_A = shunt_uV / 1000.0 / (int32_t)_shuntRes[channel];
return current_A;
}

Expand Down

1 comment on commit 2bff7a1

@pogo555
Copy link

@pogo555 pogo555 commented on 2bff7a1 Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi.
I'm trying to understand this code. The function getCurrent() returns a float. So why is the _shuntRes[] value cast to an integer for the division? If I use a non-integer value shunt resistor, say 6.67, then this function will return an incorrect value for the current.

Please sign in to comment.