mirror of
https://github.com/esphome/esphome.git
synced 2025-04-16 07:40:29 +01:00
fix negative anwser interpretation
This commit is contained in:
parent
d25d0a559a
commit
7eac405ed8
@ -102,6 +102,12 @@ float MCP3428Component::request_measurement(MCP3428Multiplexer multiplexer, MCP3
|
|||||||
}
|
}
|
||||||
|
|
||||||
// got valid measurement, clean up unused bits from the anwser code and prepare tick size
|
// got valid measurement, clean up unused bits from the anwser code and prepare tick size
|
||||||
|
bool negative = false;
|
||||||
|
if (anwser[0] & 0b10000000 != 0) {
|
||||||
|
// negative number, clean up bit and mark as negative
|
||||||
|
negative = true;
|
||||||
|
anwser[0] &= 0b01111111;
|
||||||
|
}
|
||||||
float tick_voltage = 2.048f / 32768; // ref voltage 2.048V/non-sign bits, default 15 bits
|
float tick_voltage = 2.048f / 32768; // ref voltage 2.048V/non-sign bits, default 15 bits
|
||||||
switch (resolution) {
|
switch (resolution) {
|
||||||
case MCP3428Resolution::MCP3428_12_BITS:
|
case MCP3428Resolution::MCP3428_12_BITS:
|
||||||
@ -132,6 +138,9 @@ float MCP3428Component::request_measurement(MCP3428Multiplexer multiplexer, MCP3
|
|||||||
}
|
}
|
||||||
// convert code (first 2 bytes of cleaned up anwser) into voltage ticks
|
// convert code (first 2 bytes of cleaned up anwser) into voltage ticks
|
||||||
int16_t ticks = anwser[0] << 8 | anwser[1];
|
int16_t ticks = anwser[0] << 8 | anwser[1];
|
||||||
|
if (negative) {
|
||||||
|
ticks *= (-1);
|
||||||
|
}
|
||||||
|
|
||||||
this->status_clear_warning();
|
this->status_clear_warning();
|
||||||
return tick_voltage * ticks;
|
return tick_voltage * ticks;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user