From 0449e086c628112c746e13fc8e12706552d9a968 Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Sat, 20 Feb 2021 18:58:11 +0100 Subject: [PATCH] samples: shields: lmp90100_evb: rtd: improve readability, fix off-by-one 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 --- samples/shields/lmp90100_evb/rtd/src/main.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/samples/shields/lmp90100_evb/rtd/src/main.c b/samples/shields/lmp90100_evb/rtd/src/main.c index bb4ea51029a..2aae4811e85 100644 --- a/samples/shields/lmp90100_evb/rtd/src/main.c +++ b/samples/shields/lmp90100_evb/rtd/src/main.c @@ -13,8 +13,18 @@ #include 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; @@ -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 }; @@ -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,