From 432bf4d421739c6481dbfa07140621c8e7c5605f Mon Sep 17 00:00:00 2001 From: William Clark Date: Sun, 31 Dec 2023 15:41:24 +0000 Subject: [PATCH] temperature --- lis3dh.c | 18 +++++++++++++++++- lis3dh.h | 1 + main.c | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lis3dh.c b/lis3dh.c index 94c2fb6..cc9d54e 100644 --- a/lis3dh.c +++ b/lis3dh.c @@ -1,5 +1,6 @@ #include #include +#include #include "lis3dh.h" #include "registers.h" @@ -411,4 +412,19 @@ int lis3dh_read_adc(lis3dh_t *lis3dh) { lis3dh->adc.adc3 = 1200.0 + (400.0 * (float)(((int16_t)(data[5] | (data[4] << 8))) >> shift) / divisor); return err; -} \ No newline at end of file +} + +/* the temperature sensor only reports the difference between its current temp, + and the factory calibrated temperature, 25 celsius. + in increments of plus or negative 1 unit celsius. + the reported temperature is stored inplace of adc3 + temp sensing is always in 8-bit mode + operating range: -40 to 85 celsius */ +int lis3dh_read_temp(lis3dh_t *lis3dh) { + uint8_t data[2]; + int err = 0; + + err |= lis3dh->dev.read(REG_OUT_ADC3_L | 0x80, data, 2); + lis3dh->adc.adc3 = (float)((int16_t)(data[1] | (data[0] << 8)) >> 8) + 25.0; + return err; +} diff --git a/lis3dh.h b/lis3dh.h index e0a1b17..47e453e 100644 --- a/lis3dh.h +++ b/lis3dh.h @@ -225,5 +225,6 @@ int lis3dh_clear_int2(lis3dh_t *lis3dh); int lis3dh_reference(lis3dh_t *lis3dh); int lis3dh_reset(lis3dh_t *lis3dh); int lis3dh_read_adc(lis3dh_t *lis3dh); +int lis3dh_read_temp(lis3dh_t *lis3dh); #endif \ No newline at end of file diff --git a/main.c b/main.c index e297b7a..a2a6219 100644 --- a/main.c +++ b/main.c @@ -62,6 +62,7 @@ int main() { lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8; lis.cfg.en_adc = 1; + lis.cfg.en_temp = 1; /* write device config */ if (lis3dh_configure(&lis)) { @@ -89,6 +90,11 @@ int main() { quit("read_adc()", &lis); } + /* read temp from ADC3 and overwrite local ADC reading for ADC3 */ + if (lis3dh_read_temp(&lis)) { + quit("read_temp()", &lis); + } + for(k=0; k