LSM303DLHC 3D accelerometer/magnetometer

Device driver for the LSM303DLHC 3D accelerometer/magnetometer.

This driver provides [S]ensor [A]ctuator [U]ber [L]ayer capabilities.

LSM303DLHC_ACC_DEFAULT_ADDRESS
1
(0x19)
LSM303DLHC_MAG_DEFAULT_ADDRESS
1
(0x1e)
enum lsm303dlhc_acc_sample_rate_t
LSM303DLHC_ACC_SAMPLE_RATE_1HZ = 0x10
1Hz sample rate
LSM303DLHC_ACC_SAMPLE_RATE_10HZ = 0x20
10Hz sample rate
LSM303DLHC_ACC_SAMPLE_RATE_25HZ = 0x30
25Hz sample rate
LSM303DLHC_ACC_SAMPLE_RATE_50HZ = 0x40
50Hz sample rate
LSM303DLHC_ACC_SAMPLE_RATE_100HZ = 0x50
100Hz sample rate
LSM303DLHC_ACC_SAMPLE_RATE_200HZ = 0x60
200Hz sample rate
LSM303DLHC_ACC_SAMPLE_RATE_400HZ = 0x70
400Hz sample rate
LSM303DLHC_ACC_SAMPLE_RATE_1620HZ = 0x80
1620Hz sample rate
LSM303DLHC_ACC_SAMPLE_RATE_N1344HZ_L5376HZ = 0x90
1344Hz normal mode, 5376Hz low power mode
enum lsm303dlhc_acc_scale_t
LSM303DLHC_ACC_SCALE_2G = 0x00
+- 2g range
LSM303DLHC_ACC_SCALE_4G = 0x10
+- 4g range
LSM303DLHC_ACC_SCALE_8G = 0x20
+- 8g range
LSM303DLHC_ACC_SCALE_16G = 0x30
+-16g range
enum lsm303dlhc_mag_sample_rate_t
LSM303DLHC_MAG_SAMPLE_RATE_0_75HZ = 0x00
0.75Hz sample rate
LSM303DLHC_MAG_SAMPLE_RATE_1_5HZ = 0x04
1.5 Hz sample rate
LSM303DLHC_MAG_SAMPLE_RATE_3HZ = 0x08
3 Hz sample rate
LSM303DLHC_MAG_SAMPLE_RATE_7_5HZ = 0x0c
7.5 Hz sample rate
LSM303DLHC_MAG_SAMPLE_RATE_15HZ = 0x10
15 Hz sample rate
LSM303DLHC_MAG_SAMPLE_RATE_30HZ = 0x14
30 Hz sample rate
LSM303DLHC_MAG_SAMPLE_RATE_75HZ = 0x18
75 Hz sample rate
LSM303DLHC_MAG_SAMPLE_RATE_220HZ = 0x1c
220 Hz sample rate
enum lsm303dlhc_mag_gain_t
LSM303DLHC_MAG_GAIN_1100_980_GAUSS = 0x20
1100Gauss XYZ 980Gauss Z
LSM303DLHC_MAG_GAIN_855_760_GAUSS = 0x40
855Gauss XYZ 760Gauss Z
LSM303DLHC_MAG_GAIN_670_600_GAUSS = 0x60
670Gauss XYZ 600Gauss Z
LSM303DLHC_MAG_GAIN_450_400_GAUSS = 0x80
450Gauss XYZ 400Gauss Z
LSM303DLHC_MAG_GAIN_400_355_GAUSS = 0xa0
400Gauss XYZ 355Gauss Z
LSM303DLHC_MAG_GAIN_330_295_GAUSS = 0xc0
330Gauss XYZ 295Gauss Z
LSM303DLHC_MAG_GAIN_230_205_GAUSS = 0xe0
230Gauss XYZ 205Gauss Z
int lsm303dlhc_init(lsm303dlhc_t * dev, const lsm303dlhc_params_t * params)

Initialize a new LSM303DLHC device.

Parameters

dev:device descriptor of an LSM303DLHC device
params:initialization parameters

Return values

  • 0 on success
  • -1 on error
int lsm303dlhc_read_acc(const lsm303dlhc_t * dev, lsm303dlhc_3d_data_t * data)

Read a accelerometer value from the sensor.

This function provides raw acceleration data. To get the corresponding values in g please refer to the following table: measurement range | factor —————–+—— +- 2g | 1*10^-3 +- 4g | 2*10^-3 +- 8g | 4*10^-3 +-16g | 8*10^-3

Parameters

dev:device descriptor of an LSM303DLHC device
data:the measured accelerometer data

Return values

  • 0 on success
  • -1 on error
int lsm303dlhc_read_mag(const lsm303dlhc_t * dev, lsm303dlhc_3d_data_t * data)

Read a magnetometer value from the sensor.

This function returns raw magnetic data. To get the corresponding values in gauss please refer to the following table: measurement range | factor —————–+—— +- 1.3 gauss | 1/1100 +- 1.9 gauss | 1/855 +- 2.5 gauss | 1/670 +- 4.0 gauss | 1/450 +- 4.7 gauss | 1/400 +- 5.6 gauss | 1/330 +- 8.1 gauss | 1/230

Parameters

dev:device descriptor of an LSM303DLHC device
data:the measured magnetometer data

Return values

  • 0 on success
  • -1 on error
int lsm303dlhc_read_temp(const lsm303dlhc_t * dev, int16_t * value)

Read a temperature value from the sensor.

Parameters

dev:device descriptor of an LSM303DLHC device
value:the measured temperature

Return values

  • 0 on success
  • -1 on error
int lsm303dlhc_enable(const lsm303dlhc_t * dev)

Enable the given sensor.

Parameters

dev:device descriptor of an LSM303DLHC device

Return values

  • 0 on success
  • -1 on error
int lsm303dlhc_disable(const lsm303dlhc_t * dev)

Disable the given sensor.

Parameters

dev:device descriptor of an LSM303DLHC device

Return values

  • 0 on success
  • -1 on error
struct lsm303dlhc_3d_data_t

3d data container

int16_t x_axis

holds the x axis value.

WOW!

int16_t y_axis

holds the y axis value.

Not kidding.

int16_t z_axis

surprise, holds the z axis value

struct lsm303dlhc_params_t

Data structure holding all the information needed for initialization.

i2c.h::i2c_t i2c

I2C bus used.

uint8_t acc_addr

accelerometer I2C address

gpio.h::gpio_t acc_pin

accelerometer EXTI pin

lsm303dlhc.h::lsm303dlhc_acc_sample_rate_t acc_rate

accelerometer sample rate

lsm303dlhc.h::lsm303dlhc_acc_scale_t acc_scale

accelerometer scale factor

uint8_t mag_addr

magnetometer I2C address

gpio.h::gpio_t mag_pin

magnetometer EXTI pin

lsm303dlhc.h::lsm303dlhc_mag_sample_rate_t mag_rate

magnetometer sample rate

lsm303dlhc.h::lsm303dlhc_mag_gain_t mag_gain

magnetometer gain

struct lsm303dlhc_t

Device descriptor for LSM303DLHC sensors.

lsm303dlhc_params_t params

device initialization parameters