adc and temp sense example
This commit is contained in:
parent
50a491a1be
commit
804c415a13
@ -18,3 +18,18 @@ Set up single-click detection
|
|||||||
### double-click.c
|
### double-click.c
|
||||||
|
|
||||||
Set up double-click detection
|
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
|
58
example/adc.c
Normal file
58
example/adc.c
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#define _GNU_SOURCE
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <math.h>
|
||||||
|
#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;
|
||||||
|
}
|
65
example/temp.c
Normal file
65
example/temp.c
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#define _GNU_SOURCE
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <math.h>
|
||||||
|
#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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user