STMicro L3GD20
The L3GD20 is a low-power three-axis angular rate sensor. It includes a sensing element and an IC interface capable of
providing the measured angular rate to the external world through a digital interface (I2C/SPI).
This driver is implemented in source files l3gd20.c/.h and provides communications support with the device over an I²C bus.
Device Information
Basic Usage
This driver reads the device's control registers upon being opened and keeps them cached for subsequent calls. Use the
functions provided by the driver to modify the device's settings or for complete control, use the control registers directly
and use the L3GD20_SubmitChanges() function to push changes to the physical device.
The code snippet below shows how to create and initialize an instance of the driver for the L3GD20 sensor.
#include "Drivers/ST/l3gd20.h"
#include <assert.h>
static SPI spi; /* Allocate an SPI port for communicating with the device */
static L3GD20 driver; /* Allocate an instance of the driver */
void APP_Example(void)
{
STATUS status;
L3GD20_SAMPLE sample;
status = SPI_Open(&spi, 1, 4000000, OPT_NONE); /* Open a SPI port (port=1, clk=4Mhz) */
assert(status == SUCCESS);
status = L3GD20_Open(&gyro, &spi, PE_03); /* Open the gyro driver (chip-select=PE_03) */
assert(status == SUCCESS);
status = L3GD20_SetEnabled(&gyro, TRUE); /* Turn on the gyro */
assert(status == SUCCESS);
status = L3GD20_SubmitChanges(&gyro); /* Push all changes to the physical gyro device */
assert(status == SUCCESS);
status = L3GD20_ReadSample(&gyro, &sample); /* Read the latest gyro sample */
if (status == SUCCESS) {
sample.x; /* The x-axis DPS value */
sample.y; /* The y-axis DPS value */
sample.z; /* The z-axis DPS value */
}
}
Advanced Usage
The code snippet below shows how to directly modify the device's control registers.
void APP_Example(void)
{
STATUS status;
L3GD20_CTRL* ctrl;
status = L3GD20_Open(&gyro, &spi, PE_03); /* Open the gyro driver (chip-select=PE_03) */
assert(status == SUCCESS);
ctrl = L3GD20_Ctrl(&gyro); /* Get a pointer to the cached control register values */
ctrl->CTRL5.HPEN = 1; /* Enable the high-pass filter */
status = L3GD20_SubmitChanges(&gyro); /* Send the changes to the device */
assert(status == SUCCESS);
}