LIS3DH Propeller C library  v1.0.0
Propeller C Library for the Parallax LIS3DH 3 Axis Accelerometer with ADC module
lis3dh.h
Go to the documentation of this file.
1 
15 #ifndef LIS3DH_H
16 #define LIS3DH_H
17 
18 #if defined(__cplusplus)
19 extern "C" {
20 #endif
21 
22 
23 
24 #include "simpletools.h"
25 
26 
35 #define LIS3DH_STATUS_REG_AUX 0x07 // r 07 000 0111 Output
36 #define LIS3DH_OUT_ADC1_L 0x08 // r 08 000 1000 Output
37 #define LIS3DH_OUT_ADC1_H 0x09 // r 09 000 1001 Output
38 #define LIS3DH_OUT_ADC2_L 0x0A // r 0A 000 1010 Output
39 #define LIS3DH_OUT_ADC2_H 0x0B // r 0B 000 1011 Output
40 #define LIS3DH_OUT_ADC3_L 0x0C // r 0C 000 1100 Output
41 #define LIS3DH_OUT_ADC3_H 0x0D // r 0D 000 1101 Output
42 #define LIS3DH_WHO_AM_I 0x0F // r 0F 000 1111 00110011 Device identification register
43 #define LIS3DH_WHO_AM_I_REPLY 0x33 // Expected reply to WHO_AM_I
44 #define LIS3DH_CTRL_REG0 0x1E // rw 1E 001 1110 00010000
45 #define LIS3DH_TEMP_CFG_REG 0x1F // rw 1F 001 1111 00000000
46 #define LIS3DH_CTRL_REG1 0x20 // rw 20 010 0000 00000111
47 #define LIS3DH_CTRL_REG2 0x21 // rw 21 010 0001 00000000
48 #define LIS3DH_CTRL_REG3 0x22 // rw 22 010 0010 00000000
49 #define LIS3DH_CTRL_REG4 0x23 // rw 23 010 0011 00000000
50 #define LIS3DH_CTRL_REG5 0x24 // rw 24 010 0100 00000000
51 #define LIS3DH_CTRL_REG6 0x25 // rw 25 010 0101 00000000
52 #define LIS3DH_REFERENCE 0x26 // rw 26 010 0110 00000000
53 #define LIS3DH_STATUS_REG 0x27 // r 27 010 0111 Output
54 #define LIS3DH_OUT_X_L 0x28 // r 28 010 1000 Output
55 #define LIS3DH_OUT_X_H 0x29 // r 29 010 1001 Output
56 #define LIS3DH_OUT_Y_L 0x2A // r 2A 010 1010 Output
57 #define LIS3DH_OUT_Y_H 0x2B // r 2B 010 1011 Output
58 #define LIS3DH_OUT_Z_L 0x2C // r 2C 010 1100 Output
59 #define LIS3DH_OUT_Z_H 0x2D // r 2D 010 1101 Output
60 #define LIS3DH_FIFO_CTRL_REG 0x2E // rw 2E 010 1110 00000000
61 #define LIS3DH_FIFO_SRC_REG 0x2F // r 2F 010 1111 Output
62 #define LIS3DH_INT1_CFG 0x30 // rw 30 011 0000 00000000
63 #define LIS3DH_INT1_SRC 0x31 // r 31 011 0001 Output
64 #define LIS3DH_INT1_THS 0x32 // rw 32 011 0010 00000000
65 #define LIS3DH_INT1_DURATION 0x33 // rw 33 011 0011 00000000
66 #define LIS3DH_INT2_CFG 0x34 // rw 34 011 0100 00000000
67 #define LIS3DH_INT2_SRC 0x35 // r 35 011 0101 Output
68 #define LIS3DH_INT2_THS 0x36 // rw 36 011 0110 00000000
69 #define LIS3DH_INT2_DURATION 0x37 // rw 37 011 0111 00000000
70 #define LIS3DH_CLICK_CFG 0x38 // rw 38 011 1000 00000000
71 #define LIS3DH_CLICK_SRC 0x39 // r 39 011 1001 Output
72 #define LIS3DH_CLICK_THS 0x3A // rw 3A 011 1010 00000000
73 #define LIS3DH_TIME_LIMIT 0x3B // rw 3B 011 1011 00000000
74 #define LIS3DH_TIME_LATENCY 0x3C // rw 3C 011 1100 00000000
75 #define LIS3DH_TIME_WINDOW 0x3D // rw 3D 011 1101 00000000
76 #define LIS3DH_ACT_THS 0x3E // rw 3E 011 1110 00000000
77 #define LIS3DH_ACT_DUR 0x3F // rw 3F 011 1111 00000000
78 
89 #define LIS3DH_MASK_READSINGLE 0x80 // 0b10000000
90 #define LIS3DH_MASK_READREPEAT 0xC0 // 0b11000000
91 #define LIS3DH_MASK_BDUMODE 0x80 // 0b10000000
92 #define LIS3DH_MASK_SPIMODE 0x01 // 0b00000001
93 #define LIS3DH_RANGE_16 0x03 // 0b00000011
94 #define LIS3DH_RANGE_8 0x02 // 0b00000010
95 #define LIS3DH_RANGE_4 0x01 // 0b00000001
96 #define LIS3DH_RANGE_2 0x00 // 0b00000000
97 #define LIS3DH_AXIS_X 0b01 // Axis X mask
98 #define LIS3DH_AXIS_Y 0b10 // Axis Y mask
99 #define LIS3DH_AXIS_Z 0b11 // Axis Z mask
100 #define LIS3DH_ADC_1 0b01 // ADC 1 mask
101 #define LIS3DH_ADC_2 0b10 // ADC 2 mask
102 #define LIS3DH_ADC_3 0b11 // ADC 3 mask
103 #define LIS3DH_SPIMODE_3 0b01 // SPI Mode 3 mask
104 #define LIS3DH_SPIMODE_4 0b00 // SPI Mode 4 mask
105 #define LIS3DH_BDUMODE_CONTINUOUS 0b01 // BDU continuous mode mask
106 #define LIS3DH_BDUMODE_CONSISTENT 0b00 // BDU consistent mode mask
107 
122 typedef struct lis3dh_st
123 {
124  volatile int tempcalC; // for degrees Celcius
125 
126  volatile int adccal_m1000; // for mV ADC calibration
127  volatile int adccal_b1000; // for mV ADC calibration
128 
129  volatile int tiltavgX;
130  volatile int tiltavgY;
131  volatile int tiltavgZ;
132  volatile int tiltavg_factor;
133 
134  int sdi_pin;
135  int sdo_pin;
136  int sck_pin;
137  int cs_pin;
138 
139 } lis3dh_t;
140 
141 
142 // Simpler type name for use with SimpleIDE
143 typedef lis3dh_t lis3dh;
144 
145 
146 
172 lis3dh_t *lis3dh_init(int pinSCK, int pinSDI, int pinCS);
173 
174 
175 
189 lis3dh_t *lis3dh_init4wire(int pinSCK, int pinSDI, int pinSDO, int pinCS);
190 
191 
192 
198 void lis3dh_shutdown(lis3dh_t *device);
199 
200 
201 
210 int lis3dh_getWhoAmI(lis3dh_t *device);
211 
212 
213 
224 void lis3dh_setResolution(lis3dh_t *device, int bits);
225 
226 
227 
236 int lis3dh_getResolution(lis3dh_t *device);
237 
238 
239 
250 void lis3dh_setRange(lis3dh_t *device, int range);
251 
252 
253 
262 int lis3dh_getRange(lis3dh_t *device);
263 
264 
265 
274 void lis3dh_setSPImode(lis3dh_t *device, int mode);
275 
276 
277 
286 int lis3dh_getSPImode(lis3dh_t *device);
287 
288 
289 
301 void lis3dh_setBDUmode(lis3dh_t *device, int mode);
302 
303 
304 
316 void lis3dh_setADCmode(lis3dh_t *device, int adcEnable, int tempEnable);
317 
318 
319 
336 void lis3dh_adc(lis3dh_t *device, int *ad1, int *ad2, int *ad3);
337 
338 
339 
354 int lis3dh_getADC(lis3dh_t *device, int channel);
355 
356 
357 
374 void lis3dh_adc_mV(lis3dh_t *device, int *ad1, int *ad2, int *ad3);
375 
376 
377 
392 int lis3dh_getADC_mV(lis3dh_t *device, int channel);
393 
394 
395 
409 void lis3dh_adcCal_mV(lis3dh_t *device, int mV_L, int mV_H, int value_L, int value_H);
410 
411 
422 void lis3dh_tempCal_C(lis3dh_t *device, int actualTempC);
423 
424 
425 
438 void lis3dh_tempCal_F(lis3dh_t *device, int actualTempF);
439 
440 
441 
455 int lis3dh_tempRaw(lis3dh_t *device);
456 
457 
458 
472 int lis3dh_temp_C(lis3dh_t *device);
473 
474 
475 
489 int lis3dh_temp_F(lis3dh_t *device);
490 
491 
492 
493 
511 int lis3dh_accel(lis3dh_t *device, int *x, int *y, int *z);
512 
513 
527 int lis3dh_getAccel(lis3dh_t *device, int axis);
528 
529 
530 
548 int lis3dh_accel_mg(lis3dh_t *device, int *xmg, int *ymg, int *zmg);
549 
550 
551 
565 int lis3dh_getAccel_mg(lis3dh_t *device, int axis);
566 
567 
568 
594 int lis3dh_tilt(lis3dh_t *device, int *ax, int *ay, int *az, int *motion);
595 
596 
612 int lis3dh_tiltConfig(lis3dh_t *device, int avg_factor);
613 
614 
615 
625 void lis3dh_writeByte(lis3dh_t *device, unsigned char address, unsigned char value);
626 
627 
637 unsigned char lis3dh_readByte(lis3dh_t *device, unsigned char address);
638 
642 #if defined(__cplusplus)
643 }
644 #endif
645 /* __cplusplus */
646 #endif
647 /* LIS3DH_H */
648 
649 
650 
651 
lis3dh_tempCal_C
void lis3dh_tempCal_C(lis3dh_t *device, int actualTempC)
Sets the actual temperature to provide a reference for the relative temperature sensor.
Definition: temp.c:66
lis3dh_init
lis3dh_t * lis3dh_init(int pinSCK, int pinSDI, int pinCS)
Initialize the sensor with typical configuration for reading axis and adc. Uses 3-Wire SPI mode.
Definition: init.c:21
lis3dh_shutdown
void lis3dh_shutdown(lis3dh_t *device)
Shutdown the sensor, set configuration defaults with sensor disabled and clear buffers.
Definition: shutdown.c:21
lis3dh_tiltConfig
int lis3dh_tiltConfig(lis3dh_t *device, int avg_factor)
Optional configuration for tilt sensor.
Definition: tilt.c:106
lis3dh_getRange
int lis3dh_getRange(lis3dh_t *device)
Reads the g measurement range; one of +/- 2, +/-4, +/- 8, +- 16 g.
Definition: getRange.c:21
lis3dh_setRange
void lis3dh_setRange(lis3dh_t *device, int range)
Sets the g measurement range to +/- 2, +/-4, +/- 8, +- 16 g.
Definition: setRange.c:21
lis3dh_writeByte
void lis3dh_writeByte(lis3dh_t *device, unsigned char address, unsigned char value)
write a byte value to an LIS3DH register
Definition: writeByte.c:21
lis3dh_getWhoAmI
int lis3dh_getWhoAmI(lis3dh_t *device)
Reads the sensor identification register (WhoAmI).
Definition: getWhoAmI.c:21
lis3dh_adcCal_mV
void lis3dh_adcCal_mV(lis3dh_t *device, int mV_L, int mV_H, int value_L, int value_H)
Sets the adc calibration values for adc mV reading on AD1.
Definition: adc_mV.c:97
lis3dh_setSPImode
void lis3dh_setSPImode(lis3dh_t *device, int mode)
Sets the SPI mode to either 3-wire or 4-wire.
Definition: setSPImode.c:21
lis3dh_t
struct lis3dh_st lis3dh_t
lis3dh_setADCmode
void lis3dh_setADCmode(lis3dh_t *device, int adcEnable, int tempEnable)
Sets ADC mode for the Parallax LIS3DH 3-Axis Accelerometer Module.
Definition: setADCmode.c:21
lis3dh_temp_F
int lis3dh_temp_F(lis3dh_t *device)
Reads the calibrated (referenced) temperature value from the sensor.
Definition: temp.c:57
lis3dh_getAccel
int lis3dh_getAccel(lis3dh_t *device, int axis)
Gets raw accelerometer measurements for a single axis (x, y, or z).
Definition: accel.c:62
lis3dh_init4wire
lis3dh_t * lis3dh_init4wire(int pinSCK, int pinSDI, int pinSDO, int pinCS)
Initialize the sensor with typical configuration for reading axis and adc. Uses 4-Wire SPI mode.
Definition: init.c:29
lis3dh_adc_mV
void lis3dh_adc_mV(lis3dh_t *device, int *ad1, int *ad2, int *ad3)
Gets mV adc measurements from all three ADC channels.
Definition: adc_mV.c:35
lis3dh_setBDUmode
void lis3dh_setBDUmode(lis3dh_t *device, int mode)
Sets the sensor BDU (Block Data Update) mode.
Definition: setBDUmode.c:21
lis3dh_getADC_mV
int lis3dh_getADC_mV(lis3dh_t *device, int channel)
Gets mV adc measurement from a single ADC channel.
Definition: adc_mV.c:78
lis3dh_tempRaw
int lis3dh_tempRaw(lis3dh_t *device)
Reads the raw relative temperature value from the sensor.
Definition: temp.c:23
lis3dh_getAccel_mg
int lis3dh_getAccel_mg(lis3dh_t *device, int axis)
Gets mg (milli-g's) accelerometer measurements from a single axis (x, y, or z).
Definition: accel_mg.c:64
lis3dh_tilt
int lis3dh_tilt(lis3dh_t *device, int *ax, int *ay, int *az, int *motion)
Gets tilt angle measurements for each axis, and magnitude of motion.
Definition: tilt.c:51
lis3dh_setResolution
void lis3dh_setResolution(lis3dh_t *device, int bits)
Sets the bit resolution for the sensor readings. 8-bit, 10-bit or 12-bit.
Definition: setResolution.c:21
lis3dh_accel_mg
int lis3dh_accel_mg(lis3dh_t *device, int *xmg, int *ymg, int *zmg)
Gets mg (milli-g's) accelerometer measurements from all three axis (x, y, z).
Definition: accel_mg.c:33
lis3dh_adc
void lis3dh_adc(lis3dh_t *device, int *ad1, int *ad2, int *ad3)
Gets raw adc measurements from all three ADC channels.
Definition: adc.c:21
lis3dh_getADC
int lis3dh_getADC(lis3dh_t *device, int channel)
Gets raw adc measurement from a single ADC channel.
Definition: adc.c:48
lis3dh_tempCal_F
void lis3dh_tempCal_F(lis3dh_t *device, int actualTempF)
Sets the actual temperature to provide a reference for the relative temperature sensor.
Definition: temp.c:76
lis3dh_readByte
unsigned char lis3dh_readByte(lis3dh_t *device, unsigned char address)
Read a value from an LIS3DH register.
Definition: readByte.c:21
lis3dh_getResolution
int lis3dh_getResolution(lis3dh_t *device)
Reads the bit resolution setting from the sensor. 8-bit, 10-bit or 12-bit.
Definition: getResolution.c:21
lis3dh_accel
int lis3dh_accel(lis3dh_t *device, int *x, int *y, int *z)
Gets raw accelerometer measurements from all three axis (x, y, z).
Definition: accel.c:25
lis3dh_getSPImode
int lis3dh_getSPImode(lis3dh_t *device)
Read the SPI mode currently configured in the LIS3DH module; either 3-wire or 4-wire.
Definition: getSPImode.c:21
lis3dh_st
Definition: lis3dh.h:122
lis3dh_temp_C
int lis3dh_temp_C(lis3dh_t *device)
Reads the calibrated (referenced) temperature value from the sensor.
Definition: temp.c:48