Lab 2 for ECE 2036 (work in progress)

Dependencies:   mbed 4DGL-uLCD-SE SDFileSystem PinDetect

Embed: (wiki syntax)

« Back to documentation index

MMA8452 Class Reference

MMA8452 Class Reference

Wrapper for the MMA8452 I2C driven accelerometer. More...

#include <MMA8452.h>

Public Member Functions

 MMA8452 (PinName sda, PinName scl, int frequency)
 Create an accelerometer object connected to the specified I2C pins.
 ~MMA8452 ()
 Destructor.
int activate ()
 Puts the MMA8452 in active mode.
int standby ()
 Puts the MMA8452 in standby.
int getDeviceID (char *dst)
 Read the device ID from the accelerometer (should be 0x2a)
int getStatus (char *dst)
 Read the MMA8452 status register.
int readXYZRaw (char *dst)
 Read the raw x, y, an z registers of the MMA8452 in one operation.
int readXRaw (char *dst)
 Read the raw x register into the provided buffer.
int readYRaw (char *dst)
 Read the raw y register into the provided buffer.
int readZRaw (char *dst)
 Read the raw z register into the provided buffer.
int readXYZCounts (int *x, int *y, int *z)
 Read the x, y, and z signed counts of the MMA8452 axes.
int readXCount (int *x)
 Read the x axes signed count.
int readYCount (int *y)
 Read the y axes signed count.
int readZCount (int *z)
 Read the z axes signed count.
int readXYZGravity (double *x, double *y, double *z)
 Read the x, y, and z accelerations measured in G.
int readXGravity (double *x)
 Read the x gravity in G into the provided double pointer.
int readYGravity (double *y)
 Read the y gravity in G into the provided double pointer.
int readZGravity (double *z)
 Read the z gravity in G into the provided double pointer.
int isXYZReady ()
 Returns 1 if data has been internally sampled (is available) for all axes since last read, 0 otherwise.
int isXReady ()
 Returns 1 if data has been internally sampled (is available) for the x-axis since last read, 0 otherwise.
int isYReady ()
 Returns 1 if data has been internally sampled (is available) for the y-axis since last read, 0 otherwise.
int isZReady ()
 Returns 1 if data has been internally sampled (is available) for the z-axis since last read, 0 otherwise.
int readRegister (char addr, char *dst)
 Reads a single byte from the specified MMA8452 register.
int readRegister (char addr, char *dst, int nbytes)
 Reads n bytes from the specified MMA8452 register.
int writeRegister (char addr, char data)
 Write to the specified MMA8452 register.
int writeRegister (char addr, char *data, int nbytes)
 Write a data buffer to the specified MMA8452 register.

Detailed Description

Wrapper for the MMA8452 I2C driven accelerometer.

Definition at line 114 of file MMA8452.h.


Constructor & Destructor Documentation

MMA8452 ( PinName  sda,
PinName  scl,
int  frequency 
)

Create an accelerometer object connected to the specified I2C pins.

Parameters:
sdaI2C data port
sclI2C clock port
frequency
~MMA8452 (  )

Destructor.


Member Function Documentation

int activate (  )

Puts the MMA8452 in active mode.

Returns:
0 on success, 1 on failure.
int getDeviceID ( char *  dst )

Read the device ID from the accelerometer (should be 0x2a)

Parameters:
dstpointer to store the ID
Returns:
0 on success, 1 on failure.
int getStatus ( char *  dst )

Read the MMA8452 status register.

Parameters:
dstpointer to store the register value. @ return 0 on success, 1 on failure.
int isXReady (  )

Returns 1 if data has been internally sampled (is available) for the x-axis since last read, 0 otherwise.

int isXYZReady (  )

Returns 1 if data has been internally sampled (is available) for all axes since last read, 0 otherwise.

int isYReady (  )

Returns 1 if data has been internally sampled (is available) for the y-axis since last read, 0 otherwise.

int isZReady (  )

Returns 1 if data has been internally sampled (is available) for the z-axis since last read, 0 otherwise.

int readRegister ( char  addr,
char *  dst 
)

Reads a single byte from the specified MMA8452 register.

Parameters:
addrThe internal register address.
dstThe destination buffer address.
Returns:
1 on success, 0 on failure.
int readRegister ( char  addr,
char *  dst,
int  nbytes 
)

Reads n bytes from the specified MMA8452 register.

Parameters:
addrThe internal register address.
dstThe destination buffer address.
nbytesThe number of bytes to read.
Returns:
1 on success, 0 on failure.
int readXCount ( int *  x )

Read the x axes signed count.

See also:
readXYZCounts
int readXGravity ( double *  x )

Read the x gravity in G into the provided double pointer.

See also:
readXYZGravity
int readXRaw ( char *  dst )

Read the raw x register into the provided buffer.

See also:
readXYZRaw
int readXYZCounts ( int *  x,
int *  y,
int *  z 
)

Read the x, y, and z signed counts of the MMA8452 axes.

Count resolution is either 8 bits or 12 bits, and the range is either +-2G, +-4G, or +-8G depending on settings. The number of counts per G are 1024, 512, 256 for 2,4, and 8 G respectively at 12 bit resolution and 64, 32, 16 for 2, 4, and 8 G respectively at 8 bit resolution.

This function queries the MMA8452 and returns the signed counts for each axes.

Parameters:
xPointer to integer to store x count
yPointer to integer to store y count
zPointer to integer to store z count
Returns:
0 on success, 1 on failure.
int readXYZGravity ( double *  x,
double *  y,
double *  z 
)

Read the x, y, and z accelerations measured in G.

The measurement resolution is controlled via setBitDepth which can be 8 or 12, and by setDynamicRange, which can be +-2G, +-4G, or +-8G.

Parameters:
xA pointer to the double to store the x acceleration in.
yA pointer to the double to store the y acceleration in.
zA pointer to the double to store the z acceleration in.
Returns:
0 on success, 1 on failure.
int readXYZRaw ( char *  dst )

Read the raw x, y, an z registers of the MMA8452 in one operation.

All three registers are read sequentially and stored in the provided buffer. The stored values are signed 2's complement left-aligned 12 or 8 bit integers.

Parameters:
dstThe destination buffer. Note that this needs to be 3 bytes for BIT_DEPTH_8 and 6 bytes for BIT_DEPTH_12. It is upto the caller to ensure this.
Returns:
0 for success, and 1 for failure
See also:
setBitDepth
int readYCount ( int *  y )

Read the y axes signed count.

See also:
readXYZCounts
int readYGravity ( double *  y )

Read the y gravity in G into the provided double pointer.

See also:
readXYZGravity
int readYRaw ( char *  dst )

Read the raw y register into the provided buffer.

See also:
readXYZRaw
int readZCount ( int *  z )

Read the z axes signed count.

See also:
readXYZCounts
int readZGravity ( double *  z )

Read the z gravity in G into the provided double pointer.

See also:
readXYZGravity
int readZRaw ( char *  dst )

Read the raw z register into the provided buffer.

See also:
readXYZRaw
int standby (  )

Puts the MMA8452 in standby.

Returns:
0 on success, 1 on failure.
int writeRegister ( char  addr,
char  data 
)

Write to the specified MMA8452 register.

Parameters:
addrThe internal register address
dataData byte to write
int writeRegister ( char  addr,
char *  data,
int  nbytes 
)

Write a data buffer to the specified MMA8452 register.

Parameters:
addrThe internal register address
dataPointer to data buffer to write
nbytesThe length of the data buffer to write