drivers/analog/hx711.c: fix tare calculation

Taring was done with user defined sign, which later resulted in tare
being "resigned" twice. Because of that taring was workign correctly
for one sign, and not the other.

Fix it to disable user sign when tarring, and tare always without
changing sign.

Signed-off-by: Michał Łyszczek <michal.lyszczek@bofc.pl>
This commit is contained in:
Michał Łyszczek
2024-03-02 17:27:37 +01:00
committed by Xiang Xiao
parent 459b4434b1
commit 768e533123

View File

@@ -137,6 +137,7 @@ static int hx711_tare(FAR struct hx711_dev_s *dev, float precision)
long tare;
int prec;
long taresave;
signed char signsave;
/* If value per unit is defined, we assume precision is specified
* in units, calculate raw value for precision
@@ -144,13 +145,17 @@ static int hx711_tare(FAR struct hx711_dev_s *dev, float precision)
prec = dev->val_per_unit > 0 ? precision * dev->val_per_unit : precision;
/* Save old tare value, which we will restore when we have an error */
/* Save old tare value and sign, which we will restore when we
* have an error
*/
taresave = dev->tare;
signsave = dev->sign;
/* Reset tare value during taring */
/* Reset tare value and sign during taring */
dev->tare = 0;
dev->sign = 1;
for (i = 0; i != HX711_TARE_NSAMPLES; i++)
{
@@ -158,6 +163,7 @@ static int hx711_tare(FAR struct hx711_dev_s *dev, float precision)
if (samples[i] == INT32_MIN)
{
dev->tare = taresave;
dev->sign = signsave;
return -EIO;
}
}
@@ -187,6 +193,7 @@ static int hx711_tare(FAR struct hx711_dev_s *dev, float precision)
tare /= HX711_TARE_NSAMPLES;
dev->tare = tare;
dev->sign = signsave;
return OK;
}
@@ -198,6 +205,7 @@ static int hx711_tare(FAR struct hx711_dev_s *dev, float precision)
if (samples[i % HX711_TARE_NSAMPLES] == INT32_MIN)
{
dev->tare = taresave;
dev->sign = signsave;
return -EIO;
}
}
@@ -207,6 +215,7 @@ static int hx711_tare(FAR struct hx711_dev_s *dev, float precision)
*/
dev->tare = taresave;
dev->sign = signsave;
return -ETIME;
}
@@ -618,7 +627,7 @@ static int32_t hx711_single_read(FAR struct hx711_dev_s *dev)
/* Apply tare value and sign at the end */
return dev->sign * (value + dev->tare);
return dev->sign * (value - dev->tare);
}
/****************************************************************************