From 804c415a133db91ef215d546a434a46ea8768a96 Mon Sep 17 00:00:00 2001 From: William Clark Date: Mon, 1 Jan 2024 13:27:55 +0000 Subject: [PATCH] adc and temp sense example --- example/README.md | 17 ++++++++++++- example/adc.c | 58 ++++++++++++++++++++++++++++++++++++++++++ example/temp.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 example/adc.c create mode 100644 example/temp.c diff --git a/example/README.md b/example/README.md index 09625a6..328244c 100644 --- a/example/README.md +++ b/example/README.md @@ -17,4 +17,19 @@ Set up single-click detection ### double-click.c -Set up double-click detection \ No newline at end of file +Set up double-click detection + +### adc.c + +Enable and read built-in ADCs. + +> - Input range: 800 mV to 1600 mV +> - Resolution: 8-bit in LP mode, 10-bit in normal and in HR mode. +> - Sampling frequency: same as ODR + +### temp.c + +Enable and read built-in temperature sensor + +> - Operating range: -40 to 85°C +> - Step size: ±1°C \ No newline at end of file diff --git a/example/adc.c b/example/adc.c new file mode 100644 index 0000000..44f92d9 --- /dev/null +++ b/example/adc.c @@ -0,0 +1,58 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include "lis3dh.h" +#include "i2c.h" + +int main() { + + lis3dh_t lis; + + /* set fn ptrs to rw on bus (i2c or SPI) */ + lis.dev.init = i2c_init; + lis.dev.read = i2c_read; + lis.dev.write = i2c_write; + lis.dev.sleep = usleep; + lis.dev.deinit = i2c_deinit; + + /* initialise LIS3DH struct */ + if (lis3dh_init(&lis)) { + /* error handling */ + } + + /* reset device because it sometimes corrupts itself */ + if (lis3dh_reset(&lis)) { + /* error handling */ + } + + /* set up config */ + lis.cfg.mode = LIS3DH_MODE_HR; + lis.cfg.range = LIS3DH_FS_2G; + lis.cfg.rate = LIS3DH_ODR_400_HZ; + lis.cfg.en_adc = 1; /* enable ADC */ + + + /* write device config */ + if (lis3dh_configure(&lis)) { + /* error handling */ + } + + /* Read all 3 ADCs */ + if (lis3dh_read_adc(&lis)) { + /* error handling */ + } + + /* print measured mV */ + printf("ADC1: %04.04f mV\n", lis.adc.adc1); + printf("ADC2: %04.04f mV\n", lis.adc.adc2); + printf("ADC3: %04.04f mV\n", lis.adc.adc3); + + /* deinitalise struct */ + if (lis3dh_deinit(&lis)) { + /* error handling */ + } + + return 0; +} \ No newline at end of file diff --git a/example/temp.c b/example/temp.c new file mode 100644 index 0000000..bfd7559 --- /dev/null +++ b/example/temp.c @@ -0,0 +1,65 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include "lis3dh.h" +#include "i2c.h" + +int main() { + + lis3dh_t lis; + + /* set fn ptrs to rw on bus (i2c or SPI) */ + lis.dev.init = i2c_init; + lis.dev.read = i2c_read; + lis.dev.write = i2c_write; + lis.dev.sleep = usleep; + lis.dev.deinit = i2c_deinit; + + /* initialise LIS3DH struct */ + if (lis3dh_init(&lis)) { + /* error handling */ + } + + /* reset device because it sometimes corrupts itself */ + if (lis3dh_reset(&lis)) { + /* error handling */ + } + + /* set up config */ + lis.cfg.mode = LIS3DH_MODE_HR; + lis.cfg.range = LIS3DH_FS_2G; + lis.cfg.rate = LIS3DH_ODR_400_HZ; + lis.cfg.en_adc = 1; /* enable ADC */ + lis.cfg.en_temp = 1; /* enable temp sensing */ + + + /* write device config */ + if (lis3dh_configure(&lis)) { + /* error handling */ + } + + if (lis3dh_read_adc(&lis)) { + /* error handling */ + } + + /* ADC3 now reports the temperature instead of reading from its input */ + /* the temperature data is overwritten to lis.adc.adc3 */ + if (lis3dh_read_temp(&lis)) { + /* error handling */ + } + + printf("ADC1: %04.04f mV\n", lis.adc.adc1); + printf("ADC2: %04.04f mV\n", lis.adc.adc2); + + /* no decimals, step size is 1 celsius */ + printf("ADC3: %0.f oC\n", lis.adc.adc3); + + /* deinitalise struct */ + if (lis3dh_deinit(&lis)) { + /* error handling */ + } + + return 0; +} \ No newline at end of file