Skip to content

Commit

Permalink
samples: shields: lmp90100_evb: rtd: improve readability, fix off-by-one
Browse files Browse the repository at this point in the history
Improve the readability of the LMP90100-EVB RTD sample by adding
comments and further definitions instead of magic values within the
code.

This furthermore fixes an off-by-one error for the maximum ADC value
used in the formula for calculating the resistance of the RTD (8388607
vs. 8388608).

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
  • Loading branch information
henrikbrixandersen authored and carlescufi committed Mar 15, 2021
1 parent bbdeee6 commit 0449e08
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions samples/shields/lmp90100_evb/rtd/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@
#include <logging/log.h>
LOG_MODULE_REGISTER(main);

/* Nominal RTD (PT100) resistance in ohms */
#define RTD_NOMINAL_RESISTANCE 100

/* ADC resolution in bits */
#define ADC_RESOLUTION 24U

/* ADC maximum value (taking sign bit into consideration) */
#define ADC_MAX BIT_MASK(ADC_RESOLUTION - 1)

/* Bottom resistor value in ohms */
#define BOTTOM_RESISTANCE 2000

static double sqrt(double value)
{
double sqrt = value / 3;
Expand Down Expand Up @@ -65,7 +75,7 @@ void main(void)
.channels = BIT(0),
.buffer = &buffer,
.buffer_size = sizeof(buffer),
.resolution = 24,
.resolution = ADC_RESOLUTION,
.oversampling = 0,
.calibrate = 0
};
Expand All @@ -87,7 +97,7 @@ void main(void)
if (err) {
LOG_ERR("failed to read ADC (err %d)", err);
} else {
resistance = (buffer / 8388608.0) * 2000;
resistance = (buffer / (double)ADC_MAX) * BOTTOM_RESISTANCE;
printf("R: %.02f ohm\n", resistance);
printf("T: %.02f degC\n",
rtd_temperature(RTD_NOMINAL_RESISTANCE,
Expand Down

0 comments on commit 0449e08

Please sign in to comment.