A test utility for mma8452

Dependencies:   MMA8452 mbed

main.cpp

Committer:
nherriot
Date:
2013-10-17
Revision:
7:2fa57cf76768
Parent:
6:94d7ca596f52

File content as of revision 7:2fa57cf76768:

#include "mbed.h"
//#include <bitset>
#include "MMA8452.h"
#include "helperFunctions.h"

// I2C i2c(p28,p27);
Accelerometer_MMA8452 accelerometer(p28, p27, 40000);
//Accelerometer_MMA8452 acclerometer(p28, p29);

Serial pc(USBTX, USBRX);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);



int main() {
    led1 = 0;
    led2 = 0;
    led3 = 0;

    char x_buffer[2];                                       // used to store a bit array from the x axis acclerometer 
    char y_buffer[2];                                       // this represents the raw data value from the acclerometer
    char z_buffer[2];                                       // which is converted from 12bit to 16bit value 2s compliment.

    x_buffer[0] = 0x00;
    x_buffer[1] = 0x00;
    y_buffer[0] = 0x00;
    y_buffer[1] = 0x00;
    z_buffer[0] = 0x00;
    z_buffer[0] = 0x00;

    int deviceSystemMode(0);
    
    // just playing with address values
    char mcu_address = 0x00;
    pc.printf("\nmcu_address is: 0x%x ", mcu_address);
    mcu_address = MMA8452_ADDRESS;
    pc.printf("\nmcu_address is: 0x%x ", mcu_address);
    mcu_address = (MMA8452_ADDRESS<< 1);           // shifting address by 1 bit as i2c is a 7 bit encoding, and this is 8bit encoded
    pc.printf("\nmcu_address is now : 0x%x ", mcu_address);
    
    wait(0.5);
    //init
    //set active mode
    pc.printf("\nWriting to master register\n"); 
        
    int deviceStatus = 0;
    if (accelerometer.get_Status(deviceStatus)==0)
    {
        pc.printf("\nGot the device Status - device status which is: 0x%x \n", deviceStatus);
    }
    else
    {
        pc.printf("\nFailed to get the device status. The id is set as: 0x%x ", deviceStatus);
    }
    
    
    int ControlRegister(0);
    if (accelerometer.get_CTRL_Reg1(ControlRegister)==0)
    {
        pc.printf("\nGot contents of Control Register1 which is: 0x%x \n", ControlRegister);
        
    
    }
    else
    {
        pc.printf("\nFailed to get the device status. The id is set as: 0x%x ", deviceStatus);
    }
    
      
    wait(5);
        
    for (int i=0;i<=50;i++)
        {
          
        if (accelerometer.activate()==0)
        {
            led1 = 1;
            led2 = 1;
            led3 = 1;
            pc.printf("\nActivated chip\n"); 
            wait(0.5);
            
            
            for(int i=0; i<10;i++)
            {
                
                /*
                if (accelerometer.read_x_raw(x_buffer)==0)
                {
                    //pc.printf("\nThe x-axis LSB is: 0x%x", x);
                    pc.printf("The value of my x_buffer[0] in digit form is: %d  in hex:  0x%x  \n",x_buffer[0],x_buffer[0]);
                    pc.printf("The value of my x_buffer[1] in digit form is: %d  in hex:  0x%x  \n",x_buffer[1],x_buffer[1]);
                    print2bytebinchar(x_buffer);
                    
                }
                else
                {
                    pc.printf("\nError - could not get xaxis LSB! ....\n");
                }
                
                */
                
                
                /*
                if (accelerometer.read_y_raw(y_buffer)==0)
                {
                    //pc.printf("\nThe x-axis LSB is: 0x%x", x);
                    pc.printf("The value of my y_buffer[0] in digit form is: %d  in hex:  0x%x  \n",y_buffer[0],y_buffer[0]);
                    pc.printf("The value of my y_buffer[1] in digit form is: %d  in hex:  0x%x  \n",y_buffer[1],y_buffer[1]);
                    print2bytebinchar(y_buffer);
                    
                }
                else
                {
                    pc.printf("\nError - could not get yaxis LSB! ....\n");
                }
                
                */
                

                
                if (accelerometer.read_z_raw(z_buffer)==0)
                {
                    //pc.printf("\nThe x-axis LSB is: 0x%x", x);
                    pc.printf("The value of my z_buffer[0] in digit form is: %d  in hex:  0x%x  \n",z_buffer[0],z_buffer[0]);
                    pc.printf("The value of my z_buffer[1] in digit form is: %d  in hex:  0x%x  \n",z_buffer[1],z_buffer[1]);
                    print2bytebinchar(z_buffer);
                    
                }
                else
                {
                    pc.printf("\nError - could not get zaxis LSB! ....\n");
                }
                

                
                wait(0.5);
            }      
        }
        else
        {
            pc.printf("Could not activate the chip\n");
        }
        
        
        int deviceID= 0;
        if (accelerometer.get_DeviceID(deviceID)==0)
        {
            pc.printf("\nGot the who am I - device id which is: 0x%x \n", deviceID);
        }
        else
        {
            pc.printf("\nFailed to get the device id. The id is set as: 0x%x ", deviceID);
        
        }
        
        if (accelerometer.get_Status(deviceStatus)==0)
        {
            pc.printf("\nGot the device Status - device system mode which is: 0x%x \n", deviceSystemMode);
        }
        else
        {
            pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode);
        
        }

        wait(2);
        
        pc.printf("In loop %d, of repeated initialisation.\n",i); 
        i++;
     }   
    
    //get analog data
    pc.printf("Set system into standby mode.........");
    
    if(accelerometer.standby()==0)
    {
        pc.printf("Device set into standby...\n");
    }
    else
    {
        pc.printf("Device failed to set into standby...\n");
    }

    pc.printf("\nGet system mode, it should now be standby! ...\n");
    if (accelerometer.get_SystemMode(deviceSystemMode)==0)
    {
        pc.printf("\nGot the device System Mode - device system mode which is: 0x%x \n", deviceSystemMode);
    }
    else
    {
        pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode);
        
    }
    
 }