Added code to manage Orientation, FreeFall and Motion Detection. Data is also available via IRQ.
Dependents: Test_FRDM_MMA8451Q AccelTest FRDM-KL46-Template KL25Z_Demo ... more
Fork of MMA8451Q by
Revision 9:2aa9b1668d14, committed 2013-05-28
- Comitter:
- clemente
- Date:
- Tue May 28 20:19:38 2013 +0000
- Parent:
- 8:7e6013f11b10
- Child:
- 10:fa532bf396fb
- Commit message:
- Data ready using IRQ. Tested: OK.
Changed in this revision
MMA8451Q.cpp | Show annotated file Show diff for this revision Revisions of this file |
MMA8451Q.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MMA8451Q.cpp Tue May 28 17:18:30 2013 +0000 +++ b/MMA8451Q.cpp Tue May 28 20:19:38 2013 +0000 @@ -55,6 +55,11 @@ * * Orientation Detect -- MMA8451Q_Int1.fall --- Orientation_IRQ --- user1_fptr * +* +* :: The data ready use the IRQ2 +* +* Data Ready -- MMA8451Q_Int2.fall --- DataReady_IRQ --- usr2_fptr +* */ void (*user2_fptr)(void); // Pointers to user function called after void (*user1_fptr)(void); // IRQ assertion. @@ -364,6 +369,52 @@ return 0; } +void MMA8451Q::DataReady( void(*fptr)(void), unsigned char ODR) +{ + // Soft Reset + Reset(); + + // Step 1: Put the device into Standby Mode: Register 0x2A CTRL_REG1 + // Set the device ODR value and Standby + unsigned char data[2] = {REG_CTRL_REG_1, ((ODR<<3) & 0xFE)}; + writeRegs(data, 2); + + // Step 2: Enable Data Ready Interrupt Function in the System (CTRL_REG4) + data[0] = REG_CTRL_REG_4; + data[1] = 0x01; + writeRegs(data, 2); + + // Step 6: Route the Data Ready Interrupt Function to INT2 hardware pin (CTRL_REG5) + data[0] = REG_CTRL_REG_5; + data[1] = 0x00; + writeRegs(data, 2); + + // Step 7: Put the device in Active Mode + data[0] = REG_CTRL_REG_1; + data[1] = ((ODR<<3) | 0x01); + writeRegs(data, 2); + + user2_fptr = fptr; + MMA8451Q_Int2.fall( this, &MMA8451Q::DataReady_IRQ); + +} + +void MMA8451Q::DataReady_IRQ( void) +{ + unsigned char t; + + // Determine source of the interrupt by first reading the system interrupt + readRegs( REG_INT_SRC, &t, 1); + // + if ( (t & 0x01) == 0x01) { + // Read the DataReady_IRQ Function to clear the interrupt + readRegs( REG_FF_MT_SRC, &t, 1); + // Run the user supplied function + user2_fptr(); + } +} + + void MMA8451Q::Active( void) { unsigned char t;
--- a/MMA8451Q.h Tue May 28 17:18:30 2013 +0000 +++ b/MMA8451Q.h Tue May 28 20:19:38 2013 +0000 @@ -86,6 +86,15 @@ #define cLAPO_PD 1 // Portrait Down: Equipment standing vertically in the inverted orientation #define cLAPO_LR 2 // Landscape Right: Equipment is in landscape mode to the right #define cLAPO_LL 3 // Landscape Left: Equipment is in landscape mode to the left. +// System Output Data Rate for acceleration samples +#define cODR_800HZ 0 // 1.25 ms +#define cODR_400HZ 1 // 2.5 ms +#define cODR_200HZ 2 // 5 ms +#define cODR_100HZ 3 // 10 ms +#define cODR_50HZ 4 // 20 ms +#define cODR_12_5HZ 5 // 80 ms +#define cODR_6_25HZ 6 // 160 ms +#define cODR_1_56HZ 7 // 640 ms class MMA8451Q { @@ -194,6 +203,14 @@ unsigned char GetOrientationState( void); /** + * Configure the Accelerometer to generate a IRQ when a new sample is available + * + * @param pointer to the user function to execute after IRQ assertion + * @return none + */ + void DataReady( void(*fptr)(void), unsigned char ODR); + + /** * Soft Reset * @param none * @return none @@ -211,6 +228,7 @@ void Fall_IRQ( void); void Motion_IRQ( void); void Orientation_IRQ( void); + void DataReady_IRQ( void); // unsigned char OrientationState; unsigned char OrientationStateUpdated;