LIS3DH Propeller C library  v1.0.0
Propeller C Library for the Parallax LIS3DH 3 Axis Accelerometer with ADC module
Functions
Functions

Functions

lis3dh_tlis3dh_init (int pinSCK, int pinSDI, int pinCS)
 Initialize the sensor with typical configuration for reading axis and adc. Uses 3-Wire SPI mode. More...
 
lis3dh_tlis3dh_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. More...
 
void lis3dh_shutdown (lis3dh_t *device)
 Shutdown the sensor, set configuration defaults with sensor disabled and clear buffers. More...
 
int lis3dh_getWhoAmI (lis3dh_t *device)
 Reads the sensor identification register (WhoAmI). More...
 
void lis3dh_setResolution (lis3dh_t *device, int bits)
 Sets the bit resolution for the sensor readings. 8-bit, 10-bit or 12-bit. More...
 
int lis3dh_getResolution (lis3dh_t *device)
 Reads the bit resolution setting from the sensor. 8-bit, 10-bit or 12-bit. More...
 
void lis3dh_setRange (lis3dh_t *device, int range)
 Sets the g measurement range to +/- 2, +/-4, +/- 8, +- 16 g. More...
 
int lis3dh_getRange (lis3dh_t *device)
 Reads the g measurement range; one of +/- 2, +/-4, +/- 8, +- 16 g. More...
 
void lis3dh_setSPImode (lis3dh_t *device, int mode)
 Sets the SPI mode to either 3-wire or 4-wire. More...
 
int lis3dh_getSPImode (lis3dh_t *device)
 Read the SPI mode currently configured in the LIS3DH module; either 3-wire or 4-wire. More...
 
void lis3dh_setBDUmode (lis3dh_t *device, int mode)
 Sets the sensor BDU (Block Data Update) mode. More...
 
void lis3dh_setADCmode (lis3dh_t *device, int adcEnable, int tempEnable)
 Sets ADC mode for the Parallax LIS3DH 3-Axis Accelerometer Module. More...
 
void lis3dh_adc (lis3dh_t *device, int *ad1, int *ad2, int *ad3)
 Gets raw adc measurements from all three ADC channels. More...
 
int lis3dh_getADC (lis3dh_t *device, int channel)
 Gets raw adc measurement from a single ADC channel. More...
 
void lis3dh_adc_mV (lis3dh_t *device, int *ad1, int *ad2, int *ad3)
 Gets mV adc measurements from all three ADC channels. More...
 
int lis3dh_getADC_mV (lis3dh_t *device, int channel)
 Gets mV adc measurement from a single ADC channel. More...
 
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. More...
 
void lis3dh_tempCal_C (lis3dh_t *device, int actualTempC)
 Sets the actual temperature to provide a reference for the relative temperature sensor. More...
 
void lis3dh_tempCal_F (lis3dh_t *device, int actualTempF)
 Sets the actual temperature to provide a reference for the relative temperature sensor. More...
 
int lis3dh_tempRaw (lis3dh_t *device)
 Reads the raw relative temperature value from the sensor. More...
 
int lis3dh_temp_C (lis3dh_t *device)
 Reads the calibrated (referenced) temperature value from the sensor. More...
 
int lis3dh_temp_F (lis3dh_t *device)
 Reads the calibrated (referenced) temperature value from the sensor. More...
 
int lis3dh_accel (lis3dh_t *device, int *x, int *y, int *z)
 Gets raw accelerometer measurements from all three axis (x, y, z). More...
 
int lis3dh_getAccel (lis3dh_t *device, int axis)
 Gets raw accelerometer measurements for a single axis (x, y, or z). More...
 
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). More...
 
int lis3dh_getAccel_mg (lis3dh_t *device, int axis)
 Gets mg (milli-g's) accelerometer measurements from a single axis (x, y, or z). More...
 
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. More...
 
int lis3dh_tiltConfig (lis3dh_t *device, int avg_factor)
 Optional configuration for tilt sensor. More...
 
void lis3dh_writeByte (lis3dh_t *device, unsigned char address, unsigned char value)
 write a byte value to an LIS3DH register More...
 
unsigned char lis3dh_readByte (lis3dh_t *device, unsigned char address)
 Read a value from an LIS3DH register. More...
 

Detailed Description

Function Documentation

◆ 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).

Resolution is user-configured in the sensor to 8, 10 or 12 bit. (Refer to setResolution and getResolution functions). Range is user-configured in the sensor as +/- 2, +/-4, +/- 8, +- 16 g. (Refer to setRange and getRange functions).

Parameters
devicePointer to the sensor device structure
*xVariable to store x axis reading into, scaled by resolution and range.
*yVariable to store x axis reading into, scaled by resolution and range.
*zVariable to store x axis reading into, scaled by resolution and range.
Returns
1 if new data is available, 0 if no new data is available.

◆ 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).

Resolution is user-configured in the sensor to 8, 10 or 12 bit. (Refer to setResolution and getResolution functions). Range is user-configured in the sensor as +/- 2, +/-4, +/- 8, +- 16 g. (Refer to setRange and getRange functions).

Parameters
devicePointer to the sensor device structure
*xmgVariable to store x axis reading into, scaled by resolution and range, converted to milli-g's.
*ymgVariable to store x axis reading into, scaled by resolution and range, converted to milli-g's.
*zmgVariable to store x axis reading into, scaled by resolution and range, converted to milli-g's.
Returns
1 if new data is available, 0 if no new data is available.

◆ lis3dh_adc()

void lis3dh_adc ( lis3dh_t device,
int *  ad1,
int *  ad2,
int *  ad3 
)

Gets raw adc measurements from all three ADC channels.

ADC resolution is user-configured in the sensor to 8 or 10 bit. (Refer to setResolution and getResolution functions).

Note
Channel 1 range is 0-7800mV (+- 200mV). Channel 2 and 3 ranges are 900-1700mV (+- 100mV).
Parameters
devicePointer to the sensor device structure
*ad1Variable to store ADC channel 1 reading into, scaled to ADC resolution.
*ad2Variable to store ADC channel 2 reading into, scaled to ADC resolution.
*ad3Variable to store ADC channel 3 reading into, scaled to ADC resolution.

◆ 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.

ADC resolution is user-configured in the sensor to 8 or 10 bit. (Refer to setResolution and getResolution functions).

Note
Channel 1 range is 0-7800mV (+- 200mV). Channel 2 and 3 ranges are 900-1700mV (+- 100mV).
Parameters
devicePointer to the sensor device structure
*ad1Variable to store ADC channel 1 reading into, scaled to ADC resolution.
*ad2Variable to store ADC channel 2 reading into, scaled to ADC resolution.
*ad3Variable to store ADC channel 3 reading into, scaled to ADC resolution.

◆ 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.

The internal adc has an initial tolerance of +-400mV, and may require calibration before use.

Parameters
devicePointer to the sensor device structure
mV_LValue for ADC mV calibration - voltage at which lower calibration reading taken, in millivolts
mV_HValue for ADC mV calibration - voltage at which upper calibration reading taken, in millivolts
value_LValue for ADC mV calibration - value measured at lower calibration voltage, in millivolts
value_HValue for ADC mV calibration - value measured at upper calibration voltage, in millivolts

◆ lis3dh_getAccel()

int lis3dh_getAccel ( lis3dh_t device,
int  axis 
)

Gets raw accelerometer measurements for a single axis (x, y, or z).

Resolution is user-configured in the sensor to 8, 10 or 12 bit. (Refer to setResolution and getResolution functions). Range is user-configured in the sensor as +/- 2, +/-4, +/- 8, +- 16 g. (Refer to setRange and getRange functions).

Parameters
devicePointer to the sensor device structure
axisValue of the axis to read from. Must be 1, 2 or 3 (representing x, y or z).
Returns
1 if new data is available, 0 if no new data is available, or invalid axis requested.

◆ 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).

Resolution is user-configured in the sensor to 8, 10 or 12 bit. (Refer to setResolution and getResolution functions). Range is user-configured in the sensor as +/- 2, +/-4, +/- 8, +- 16 g. (Refer to setRange and getRange functions).

Parameters
devicePointer to the sensor device structure
axisValue of the axis to read from. Must be 1, 2 or 3 (representing x, y or z).
Returns
1 if new data is available, 0 if no new data is available, or invalid axis requested.

◆ lis3dh_getADC()

int lis3dh_getADC ( lis3dh_t device,
int  channel 
)

Gets raw adc measurement from a single ADC channel.

ADC resolution is user-configured in the sensor to 8 or 10 bit. (Refer to setResolution and getResolution functions).

Note
Channel 1 range is 0-7800mV (+- 200mV). Channel 2 and 3 ranges are 900-1700mV (+- 100mV).
Parameters
devicePointer to the sensor device structure
channelValue of the ADC channel to read from. Must be 1, 2 or 3.
Returns
raw ADC reading from specified channel, scaled to ADC resolution.

◆ lis3dh_getADC_mV()

int lis3dh_getADC_mV ( lis3dh_t device,
int  channel 
)

Gets mV adc measurement from a single ADC channel.

ADC resolution is user-configured in the sensor to 8 or 10 bit. (Refer to setResolution and getResolution functions).

Note
Channel 1 range is 0-7800mV (+- 200mV). Channel 2 and 3 ranges are 900-1700mV (+- 100mV).
Parameters
devicePointer to the sensor device structure
channelValue of the ADC channel to read from. Must be 1, 2 or 3.
Returns
mV ADC reading from specified channel, scaled to ADC resolution.

◆ lis3dh_getRange()

int lis3dh_getRange ( lis3dh_t device)

Reads the g measurement range; one of +/- 2, +/-4, +/- 8, +- 16 g.

Parameters
devicePointer to the sensor device structure
Returns
Value of the GForce sensitivy range. 2, 4, 8 or 16

◆ lis3dh_getResolution()

int lis3dh_getResolution ( lis3dh_t device)

Reads the bit resolution setting from the sensor. 8-bit, 10-bit or 12-bit.

Parameters
devicePointer to the sensor device structure
Returns
Value representing the sensor resolution in bits (8, 10 or 12) or 0 for invalid configuration.

◆ lis3dh_getSPImode()

int lis3dh_getSPImode ( lis3dh_t device)

Read the SPI mode currently configured in the LIS3DH module; either 3-wire or 4-wire.

Parameters
devicePointer to the sensor device structure
Returns
Value representing the active SPI mode: 3 or 4

◆ lis3dh_getWhoAmI()

int lis3dh_getWhoAmI ( lis3dh_t device)

Reads the sensor identification register (WhoAmI).

Parameters
devicePointer to the sensor device structure
Returns
Value must be 0x33 (51) to indicate valid LIS3DH sensor.

◆ 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.

Parameters
pinSCKI/O pin connected to the SCK pin.
pinSDII/O pin connected to the SDI pin.
pinCSI/O pin connected to the /CS pin.
Returns
A Pointer to the sensor device structure, or NULL on failure

◆ 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.

Parameters
pinSCKI/O pin connected to the SCK pin.
pinSDII/O pin connected to the SDI pin.
pinSDOI/O pin connected to the SDO pin.
pinCSI/O pin connected to the /CS pin.
Returns
A Pointer to the sensor device structure, or NULL on failure

◆ lis3dh_readByte()

unsigned char lis3dh_readByte ( lis3dh_t device,
unsigned char  address 
)

Read a value from an LIS3DH register.

Parameters
devicePointer to the sensor device structure
addressLIS3DH register address.
Returns
Value stored by the LIS3DH register.

◆ lis3dh_setADCmode()

void lis3dh_setADCmode ( lis3dh_t device,
int  adcEnable,
int  tempEnable 
)

Sets ADC mode for the Parallax LIS3DH 3-Axis Accelerometer Module.

Parameters
devicePointer to the sensor device structure
adcEnable1 = ADC enabled, 0 = ADC disabled
tempEnableADC channel 3 (AD3) source control. 1 = Internal temperature sensor, 0 = Input on AD3 pin
Note
To read from the internal temperature sensor, both adcEnable and tempEnable must be set to 1.

◆ lis3dh_setBDUmode()

void lis3dh_setBDUmode ( lis3dh_t device,
int  mode 
)

Sets the sensor BDU (Block Data Update) mode.

Parameters
devicePointer to the sensor device structure
modeexpects a constant which represents the BDU mode (LIS3DH_BDUMODE_COMPLETE or LIS3DH_BDUMODE_CONTINUOUS)

LIS3DH_BDUMODE_CONTINUOUS means data is ready to be read as each register is updated LIS3DH_BDUMODE_CONSISTENT means data is only ready to be read when both LSB and MSB registers are updated

◆ lis3dh_setRange()

void lis3dh_setRange ( lis3dh_t device,
int  range 
)

Sets the g measurement range to +/- 2, +/-4, +/- 8, +- 16 g.

This function makes the sensor apply scalars to the accelerometer readings. Higher range setting reduces sensitivy.

Parameters
devicePointer to the sensor device structure
rangeValue representing the GForce range (sensitivity). Must be 2, 4, 8 or 16

◆ 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.

Values returned when reading the accelerometer axis or adc will be sampled according to the resolution number of bits.

Parameters
devicePointer to the sensor device structure
bitsValue to set the sensor resolution. Must be 8, 10 or 12.

◆ lis3dh_setSPImode()

void lis3dh_setSPImode ( lis3dh_t device,
int  mode 
)

Sets the SPI mode to either 3-wire or 4-wire.

Parameters
devicePointer to the sensor device structure
modeValue representing SPI wire mode. Must be 3 or 4.

◆ lis3dh_shutdown()

void lis3dh_shutdown ( lis3dh_t device)

Shutdown the sensor, set configuration defaults with sensor disabled and clear buffers.

Parameters
devicePointer to the sensor device structure

◆ lis3dh_temp_C()

int lis3dh_temp_C ( lis3dh_t device)

Reads the calibrated (referenced) temperature value from the sensor.

The internal temperature sensor is a relative mode sensor, and requires calibration before use. In 8-bit mode, the raw value will change 1 digit / degree Celcius change.

Note
The sensor operates in Celcius internally. Fahrenheit values are converted automatically, and may lose accuracy.
Parameters
devicePointer to the sensor device structure
Returns
Value of the temperature, in degrees Celcius.

◆ lis3dh_temp_F()

int lis3dh_temp_F ( lis3dh_t device)

Reads the calibrated (referenced) temperature value from the sensor.

The internal temperature sensor is a relative mode sensor, and requires calibration before use. In 8-bit mode, the raw value will change 1 digit / degree Celcius change.

Note
The sensor operates in Celcius internally. Fahrenheit values are converted automatically, and may lose accuracy.
Parameters
devicePointer to the sensor device structure
Returns
Value of the temperature, in degrees Fahrenheit.

◆ 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.

The internal temperature sensor is a relative mode sensor, and requires calibration before use.

Parameters
devicePointer to the sensor device structure
actualTempCValue of the temperature in degrees Celcius, where the sensor is being used.

◆ 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.

The internal temperature sensor is a relative mode sensor, and requires calibration before use.

Note
The sensor operates in Celcius internally. Fahrenheit values are converted automatically, and may lose accuracy compared with Celcius calibration.
Parameters
devicePointer to the sensor device structure
actualTempFValue of the temperature in degrees Fahrenheit, where the sensor is being used.

◆ lis3dh_tempRaw()

int lis3dh_tempRaw ( lis3dh_t device)

Reads the raw relative temperature value from the sensor.

The internal temperature sensor is a relative mode sensor, and requires calibration before use. In 8-bit mode, the raw value will change 1 digit / degree Celcius change.

Note
The sensor operates in Celcius internally. Fahrenheit values are converted automatically, and may lose accuracy.
Parameters
devicePointer to the sensor device structure
Returns
Value of the temperature change.

◆ 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.

Angle to each axis in degrees, X relative to ground Y relative to ground Z relative to gravity. Motion is the sum of g-force on all axis relative to gravity at ground level (1G). 0 is motionless, larger positve or negative values represent more intense motion. The motion value could be used on it's own for projects requiring vibration sensing.

Parameters
devicePointer to the sensor device structure
*axVariable to store angle of x axis relative to ground.
*ayVariable to store angle of y axis relative to ground.
*azVariable to store angle of z axis relative to gravity.
*motionVariable to store positive or negative value representing intensity of motion, with 0 being motionless at ground level.
Returns
1 if new data is available, 0 if no new data is available, or invalid request.

◆ lis3dh_tiltConfig()

int lis3dh_tiltConfig ( lis3dh_t device,
int  avg_factor 
)

Optional configuration for tilt sensor.

Values stored in RAM. Reasonable defaults are set by the library.

Parameters
devicePointer to the sensor device structure
avg_factorPercentage of new data added to tilt readings low pass filter (moving average). Valid values are 0 to 100. (Setting 0 or 100 will disable the filter. For example, if avg_factor is 75, moving average calculation will include 75% new data and 25% existing data. Default value = 100 (moving average low pass filter disabled).
Returns
1 if operation successful, 0 if operation failed (invalid values).

◆ lis3dh_writeByte()

void lis3dh_writeByte ( lis3dh_t device,
unsigned char  address,
unsigned char  value 
)

write a byte value to an LIS3DH register

Parameters
devicePointer to the sensor device structure
addressLIS3DH register address.
valueConfiguration value for the register.