Llosa de Viango Tollos – ADXL345.c



Getting started



  Tollos libraries

  Device libraries





  Sample application


ADXL345.c – accelerometer device I/O for Tollos

These functions provide basic IO for using the ADXL345 accelerometer device, attached via I2C.

The following connections are assumed:

  • SCL & SDA ... to I2C bus SCL & SDA, with appropriate pullups (channel is selectable)
  • SDO ... to GND (selects device alternate physical bus address)
  • CS ... to VCC (+3.3V) [enable I2C mode]
  • INT1 ... to a suitable input bit pin
  • VCC & GND ... to suitable power rails, with bypass capacitors

(INT2 is currently unused.)

This code assumes only one ADXL345 device will be used on a controller, so some state information can be held (the device channel and address). This simplifies the tollosAcc interface and allows initialization checking.
 accGetXYZIO   read latest x, y, and z accelerations
 accInitIO   configure and initialize accelerometer
 accRangeIO   change range of accelerometer
 accRateIO   change update rate of accelerometer
 accStopIO   stop and power-down accelerometer

accGetXYZIO – read latest x, y, and z accelerations

int accGetXYZIO(int *x, int *y, int *z);

x, y, z – receive accelerations [in milli-gs]

returns 0 if data read, −1 if the device is not initialized, or −2 if there was an error reading the data (or no data ready); 1 is returned if data overrun was raised – in this case data is read

Results are returned scaled to milli-gs (1000 = 1g), without any calibration adjustments.

This should normally be called only after a Data Ready interrupt; the interrupt is cleared by calling this.

accInitIO – configure and initialize accelerometer

int accInitIO(uint channel);

channel – I2C channel on which device is to be found

returns 0 if initialized OK, negative if error

The tollosAcc interface expects:

  • an update rate of at least 25 Hz (but need not be higher than that on initialization, to save power)
  • a range of at least 2g
  • an interrupt [from pin INT1] when Data Ready is raised.

The caller should set up the interrupt handler before calling this function, or clear Data Ready by calling accGetXYZIO.

accRangeIO – change range of accelerometer

int accRangeIO(uint range);

range – range requested in milli-g [1000 = 1 g]

returns the chosen range if changed OK, −1 if error (range too high)

The accelerometer will be set to a smallest available range that is >= the requested range, to maximize the precision of returned results.

accRateIO – change update rate of accelerometer

int accRateIO(uint rate);

rate – rate requested [Hz, 1..200]

returns the chosen rate (nearest Hz below) if changed OK, −1 if error (rate zero or too high)

The accelerometer will be set to a slowest available rate that is >= the requested rate, to minimize resource usage.

To ensure working with 100kHz I2C, the maximum rate is limited to 200Hz – but note that this may use significant processor bandwidth.

accStopIO – stop and power-down accelerometer

int accStopIO(void);

returns 0 if accelerometer stopped OK, negative if an error or it was not running.

Tollos and these web pages were written by Mike Cowlishaw; Please send me any corrections, suggestions, etc.
All content © Mike Cowlishaw, 2010–2012, except where marked otherwise. All rights reserved. The pages here are for non-commercial use only (see the separate licence for Tollos source code). Privacy policy: the Speleotrove website records no personal information and sets no ‘cookies’. However, statistics, etc. might be recorded by the web hosting service.

This page was last updated on 2012-01-08 by c2wiki.