3-Axis Accelerometer

Revision:
3:d8cf2591cab6
Parent:
2:292a5265228e
--- a/MC3635.h	Wed May 30 13:00:29 2018 +0000
+++ b/MC3635.h	Wed May 30 15:09:33 2018 +0000
@@ -21,7 +21,114 @@
     Example:
 
 @code
-[TODO]
+#include "mbed.h"
+#include "MC3635.h"
+
+MC3635 myMC3635   ( I2C_SDA, I2C_SCL, MC3635::MC3635_ADDRESS_LOW, 100000 );
+Serial pc         ( USBTX, USBRX );
+
+DigitalOut  myled       ( LED1 );
+Ticker      newReading;
+
+MC3635::MC3635_status_t  aux;
+MC3635::MC3635_data_t    myMC3635_data;
+uint32_t                 myState = 0;
+
+
+void changeDATA ( void )
+{
+    myState = 1;
+}
+
+
+int main()
+{
+    pc.baud ( 115200 );
+
+    myled   =   1;
+    wait(3);
+    myled   =   0;
+
+
+    // MC3635 CONFIGURATION
+    // MC3635 Software Reset    
+    aux  =   myMC3635.MC3635_SetSoftwareReset ();
+
+    //  MC3635 in Standby mode  
+    aux  =   myMC3635.MC3635_SetStandbyMode ();
+
+    //  MC3635 initialization sequence  
+    aux  =   myMC3635.MC3635_InitializationSequence ();
+
+    // MC3635 Get the offset for all the axis   
+    aux  =   myMC3635.MC3635_GetOffset ( MC3635::X_AXIS,  &myMC3635_data );
+    aux  =   myMC3635.MC3635_GetOffset ( MC3635::Y_AXIS,  &myMC3635_data );
+    aux  =   myMC3635.MC3635_GetOffset ( MC3635::Z_AXIS,  &myMC3635_data );
+    pc.printf( "XOFFSET: %d | YOFFSET: %d | ZOFFSET: %d\r\n", myMC3635_data.XOffset, myMC3635_data.YOffset, myMC3635_data.ZOffset );
+    
+    // MC3635 Get the gain for all the axis   
+    aux  =   myMC3635.MC3635_GetGain ( MC3635::X_AXIS,  &myMC3635_data );
+    aux  =   myMC3635.MC3635_GetGain ( MC3635::Y_AXIS,  &myMC3635_data );
+    aux  =   myMC3635.MC3635_GetGain ( MC3635::Z_AXIS,  &myMC3635_data );
+    pc.printf( "XGAIN: %d | YGAIN: %d | ZGAIN: %d\r\n", myMC3635_data.XGAIN, myMC3635_data.YGAIN, myMC3635_data.ZGAIN );
+    
+    // MC3635 Check Scratch register    
+    myMC3635_data.scratch    =   0x23;
+    aux  =   myMC3635.MC3635_WriteScratchpadRegister ( myMC3635_data );
+
+    myMC3635_data.scratch    =   0;
+    aux  =   myMC3635.MC3635_ReadScratchpadRegister ( &myMC3635_data );
+    pc.printf( "Scratchpad Register: %x ( 0x23 )\r\n", myMC3635_data.scratch );
+
+    // MC3635 FIFO disabled   
+    aux  =   myMC3635.MC3635_EnableFIFO ( MC3635::FIFO_C_FIFO_EN_DISABLED );
+
+    // MC3635 All interrupts DISABLED  
+    aux  =   myMC3635.MC3635_Set_INTN ( MC3635::INTR_C_INT_WAKE_DISABLED, MC3635::INTR_C_INT_ACQ_DISABLED, MC3635::INTR_C_INT_FIFO_EMPTY_DISABLED,
+                                        MC3635::INTR_C_INT_FIFO_FULL_DISABLED, MC3635::INTR_C_INT_FIFO_THRESH_DISABLED, MC3635::INTR_C_INT_SWAKE_DISABLED );
+
+    // MC3635 14-bits resolution ( FIFO not in use)   
+    aux  =   myMC3635.MC3635_SetResolution ( MC3635::RANGE_C_RES_14_BITS );
+
+    // MC3635 16g range    
+    aux  =   myMC3635.MC3635_SetRange ( MC3635::RANGE_C_RANGE_16G );
+
+    // MC3635 X/Y/Z axis enabled    
+    aux  =   myMC3635.MC3635_EnableAxis ( MC3635::MODE_C_X_AXIS_PD_ENABLED, MC3635::MODE_C_Y_AXIS_PD_ENABLED, MC3635::MODE_C_Z_AXIS_PD_ENABLED );
+
+    // MC3635 CWAKE mode in Low Power enabled and ODR is 54Hz    
+    aux  =   myMC3635.MC3635_SetMode ( MC3635::MODE_C_MCTRL_CWAKE, MC3635::LOW_POWER_MODE, MC3635::ODR_7 );
+    // END MC3635 CONFIGURATION
+
+
+    newReading.attach( &changeDATA, 1 );                                        // the address of the function to be attached ( changeDATA ) and the interval ( 1s )
+
+    // Let the callbacks take care of everything
+    while(1) {
+        sleep();
+
+        myled = 1;
+
+        if ( myState == 1 ) {
+            //  Wait until a new data is available 
+            do{
+                // MC3635 Read register Status1    
+                aux  =   myMC3635.MC3635_ReadStatusRegister1 ( &myMC3635_data );
+            }while( ( myMC3635_data.status_1 & MC3635::STATUS_1_NEW_DATA_MASK ) == MC3635::STATUS_1_NEW_DATA_FALSE );   // [TODO] Dangerous!!! The uC may get stuck here if something goes wrong!
+                                                                                                                        // [WORKAROUND] Insert a counter.
+
+            // MC3635 Read the data    
+            aux  =   myMC3635.MC3635_ReadRawData ( &myMC3635_data );
+            
+
+            // Send data through the UART   
+            pc.printf( "X: %d | Y: %d | Z: %d\r\n", myMC3635_data.XAxis_mg, myMC3635_data.YAxis_mg, myMC3635_data.ZAxis_mg );
+            myState  =   0;                                                                                             // Reset the variable
+        }
+
+        myled = 0;
+    }
+}
 @endcode
 
 */