Qmax / Mbed 2 deprecated LIS_Accelerometer_WIP

Dependencies:   mbed

main.cpp

Committer:
bala0x07
Date:
2017-08-24
Revision:
0:491bc4ee502e
Child:
1:ecd469d7323d
Child:
2:c4fb968de7d3

File content as of revision 0:491bc4ee502e:


/*
_____________________________________________________________________________________________________
_____________________________________________________________________________________________________

//                                  ACCELEROMETER : LIS2DE12                                       //
_____________________________________________________________________________________________________
_____________________________________________________________________________________________________


>>>>>>>>>>>>>>>>>>>>>>>>>>>>> THIS CODE IS SUBJECTED TO MODIFICATION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Updates :
--------
>>> Communication established with SA0 HIGH with internal pullup (Hence Slave Address : 0x32) // 
>>> I2C Communciation verified
>>> Interrupt trigger verified for INT1 (configurent for low event) but later trigger not working
>>> Interrupt source register was read successfully
>>> Interrupt source register reflects the immediate changes of the High and Low event of the axes
>>> IA bits goes high only when an interrupt is triggered
>>> IA bits is automatically cleared once the opposite event is observed (i.e) If IA bit goes high for a Low event, then it remains high until a High event is observed on these axis
>>> INTERRUPT SOURCE REGISTER 2 has a;so been checked and the corressponding event data has been reflected in that
>>> INTERRUPT ACTIVE nit is not getting set int the INTERRUPT SRC 2 REG, and have to be resolved
>>> INT2 pin toggling verified

Issues to be Resolved :
----------------------
>>> Acceleration data is not changing and always read as '0'
>>> If the INT_SRC register is not continuosly read, after the interrupt, reading INT_SRC inside the ISR returns 0x00 as value
>>> If the INT_SRC register is continuosly read inside a while loop, reading the INT_SRC inside the ISR returns correct value

To dos:
------
>>> ACTIVITY & INACTIVITY Interrupts to be configured on INT1 pin
>>> CLICK & DOUBLE CLICK Interrupt to be configured on INT2 pin

NOTE :
-----
Leave the SA0 pin as it is and fix the slave address 32h
__________29/7/17
now via interrupt we can read SRC

_____________________________________________________________________________________________________

LAST UPDATED : 15-JUN-2017 (WEDNESDAY)

____________________

Author : >> BALA <<
____________________
_____________________________________________________________________________________________________

*/

/*
*****************************************************************************************************
REFRENCES :
----------
>>> SLAVE ADDRESS : 1001 A2 A1 A0 R/W   : Page : 7 (Datasheet)
>>> TEMPERATURE OVERSHOOT SHUTDOWN REG CONFIGURATION : Page : 10 (Datasheet)

*****************************************************************************************************
*/


#include "mbed.h"
#include "main.h"


I2C i2c(PB_14, PB_13);
Serial pc(PC_0, NC,115200);
InterruptIn change(PC_6);  // INT1
InterruptIn jerk(PB_15);   //INT2
DigitalOut  ACC_LED(PC_10);

#define     acc_address             0x32       

int i=0,j=0,Steady_cnt=0;
float TIMING=0;
char src_data[1];
unsigned int int1_src_data,cnt=0;
char acc_reg[1] = {0x29};
char acc_data[6];
unsigned int x, y, z;
float X, Y, Z;
char int1_dur[2] = {0x33, 70};
char JERK_FLAG=0,MOVEMENT_FLAG=0,MOTION=0;
//char first_event = 1;

//____________________________________________________________________________________________________



//____________________________________________________________________________________________________

void movement_inertia()
{ 

    
//-------------------------------------------------------------------------------   
  //  pc.printf("\r\n\r\nINTERRUPT SOURCE REGISTER 1: ");
 //   print_data_bits(src_data[0]);

    MOVEMENT_FLAG=1;
    

}

//____________________________________________________________________________________________________

void sudden_jerk()
{
   JERK_FLAG=1;
}

//____________________________________________________________________________________________________


void print_data_bits(char data_fetched)
{
    unsigned int shifter;
    
        for(shifter = 0; shifter < 8; shifter++)
        {
            pc.printf("%d",((data_fetched&0x80)>>7));
            data_fetched = data_fetched << 1;
        }
        pc.printf("\r\n");       
}

//____________________________________________________________________________________________________

void read_acceleration()
{
    
    i2c.write(acc_address, acc_reg, 1);
    i2c.read(acc_address, acc_data, 6);
    
    x = (acc_data[0] << 6) | (acc_data[1]);
    y = (acc_data[2] << 6) | (acc_data[3]);
    z = (acc_data[4] << 6) | (acc_data[5]);
    
//    x = acc_data[2];
//    y = acc_data[4];  
//    z = acc_data[6];

    X = x;
    Y = y;
    Z = z;


//    X = x / 15974.4;
//    Y = y / 15974.4;
//    Z = z / 15974.4;
    

  
//-------------------------------------------------------------------------------

//     i2c.write(acc_address, int1_src, 1);
//     i2c.read(acc_address, src_data, 1);
//    
//     int1_src_data = (unsigned int)src_data[0];
    
//    pc.printf("\r\nINTERRUPT SOURCE 2 : ");
//    print_data_bits(src_data[0]);
    
        
//    if(!(int1_src_data & 0x40) && first_event)
//    {
//        first_event = 0;
//        pc.printf("\r\n\r\n>>> MOTION OBSERVED");
//    }
//-------------------------------------------------------------------------------
}


//____________________________________________________________________________________________________

void initialize_internal_temperature_sensor()
{
    char temperature_config_1[2] = {0x1F, 12};
    char temperature_config_2[2] = {0x23, 0x80};
    char temperature_reg[1] = {0x0C};
    char temp_value[2];
    float temperature;
    
    i2c.write(acc_address, temperature_config_1, 2);    // Internal Temperature Sensor is enabled
    i2c.write(acc_address, temperature_config_2, 2);    
    
    i2c.write(acc_address, temperature_reg, 1);
    i2c.read(acc_address, temp_value, 2);
    
    temperature = ((temp_value[1] << 8) | (temp_value[0]));
    
    pc.printf("\r\nTEMPERATURE : %d deg celsius", temperature);
} 

//____________________________________________________________________________________________________


void configure_accelerometer()
{
/*   
    char ctrl_reg_0[2]      = {0x1E, 0x10};             // SA0 Internal pullup Enabled
    char ctrl_reg_1[2]      = {0x20, 0x2F};             // ODR -> 10Hz, X,Y,Z axis Enabled
    char ctrl_reg_2[2]      = {0x21, 0x8E};             // High pass filter enabled for CLICK function, cutoff : 0.2Hz, High pass filter AOI function is routed to INT2 pin
    char ctrl_reg_3[2]      = {0x22, 0x40};             // (Default value of this register is 0x00, so eliminate this statement later)...Disable CLICK, IA, ZYXDA, WTM, OVERRUN interrupts on INT1 pin..... 
    char ctrl_reg_4[2]      = {0x23, 0x80};             // BDU enabled, 2g full scale selection, Self test diabled
    char ctrl_reg_5[2]      = {0x24, 0x40};             // Reboot memory content disabled, FIFO enabled, Interrupt request not latched on INT1 & INT2, 4D detection disbaled
    char ctrl_reg_6[2]      = {0x25, 0xA0};             // CLICK Interrupt on INT2 pin, INT1 function on INT2 pin disabled, INT2 function on INt2 pin enabled, BOOT on INT2 pi disabled, Activity on INT2 pin disabled, INT1 & INT2 pin polarity is set to active HIGH
    char fifo_ctrl_reg[2]   = {0x2E, 0x80};             // STREAM Mode selected, Trigger event allows triggerring signal on INT1, WATER MARK LEVEL set to 0 (default value)                  
    char int1_cfg[2]        = {0x30, 0x1F};             // OR combination of interrupts, Interrupts enabled for all X/Y/Z high and low except Z-high event
    char int1_ths[2]        = {0x32, 7};                // For 2g Full scale, 1LSB = 16mg, Aim : Threshold : 100mg, 100/16 = 6.25, rounded to '7'
    char int1_dur[2]        = {0x33, (50 & 127)};       // Configured for 5sec : Duration time = N / ODR ; N = time * ODR = 5 * 10 = '50'   (Value should not exceed 127)        
    
    i2c.write(acc_address, ctrl_reg_0, 2);
    i2c.write(acc_address, ctrl_reg_1, 2);
    i2c.write(acc_address, ctrl_reg_2, 2);
    i2c.write(acc_address, ctrl_reg_3, 2);
    i2c.write(acc_address, ctrl_reg_4, 2);
    i2c.write(acc_address, ctrl_reg_5, 2);
    i2c.write(acc_address, ctrl_reg_6, 2);
    i2c.write(acc_address, fifo_ctrl_reg, 2);
    i2c.write(acc_address, int1_cfg, 2);
    i2c.write(acc_address, int1_ths, 2);
    i2c.write(acc_address, int1_dur, 2);  
    
    char ver_data[1];
    i2c.write(acc_address, ctrl_reg_1, 1);
    i2c.read(acc_address, ver_data, 1);
    pc.printf("\r\n\r\n Verification data : ");
    print_data_bits(ver_data[0]);
*/
    ///*
    char ctrl_reg_0[2]      = {0x1E, 0x10};         //added additionally this line
    char ctrl_reg_1[2]      = {0x20, 0x27};         // Modified point (worked) changed from 0x27
    char ctrl_reg_2[2]      = {0x21, 0x03};         // Modified to Auto Reset on Interrupt event CHANGE FROM 0X01
    char ctrl_reg_3[2]      = {0x22, 0x40};         // INT1 on INT1,also attach CLK_INT //#C0
    char ctrl_reg_4[2]      = {0x23, 0x80};         // Modified changed from 0x08 now block update
    char ctrl_reg_5[2]      = {0x24, 0x00};         // Modified to FIFO Enabled changed from 00
    char ctrl_reg_6[2]      = {0x25, 0x28};         // IA2 Interrupt on INT2 pin & activity also on INT2 //activityy int also enabled
       //fifo bypass mode
       /////////char fifo_ctrl_reg[2]   = {0x2E, 40};      
    
    char int1_cfg[2]        = {0x30, 0x43};         // 6D direction detection enabled ,XH & XL enabled
    char int1_ths[2]        = {0x32, 0x01};         //INT1 threshold value  
    char int1_dur[2]        = {0x33, 0x00};
    
    char int2_cfg[2]        = {0x34, 0x3F};         //INT2 is Enabled when XHIE
    char int2_ths[2]        = {0x36, 0x02};         //changed from A8 to 28
    char int2_dur[2]        = {0x37, 0x01};
    
    char act_ths[2]         = {0x3E, 00};
    char act_dur[2]         = {0x3F, 00};
    
    //32 33 30
    //36 37 34
    i2c.write(acc_address, ctrl_reg_0, 2);
    i2c.write(acc_address, ctrl_reg_1, 2);
    i2c.write(acc_address, ctrl_reg_2, 2);
    i2c.write(acc_address, ctrl_reg_3, 2);
    i2c.write(acc_address, ctrl_reg_4, 2);
    i2c.write(acc_address, ctrl_reg_5, 2);
    i2c.write(acc_address, ctrl_reg_6, 2);
 
    i2c.write(acc_address, int1_cfg, 2);   
    i2c.write(acc_address, int1_ths, 2);
    i2c.write(acc_address, int1_dur, 2);

    
    i2c.write(acc_address, int2_ths, 2);
    i2c.write(acc_address, int2_dur, 2);
    i2c.write(acc_address, int2_cfg, 2);

    i2c.write(acc_address, act_ths, 2);
    i2c.write(acc_address, act_dur, 2);
    i2c.write(acc_address, int1_dur, 2);
                    //////i2c.write(acc_address, fifo_ctrl_reg, 2); 
    //*/
}


//____________________________________________________________________________________________________

char initialize_accelerometer()
{
    char dev_id_address[2] = {0x0F};
    char dev_id[1];

    //SA0 = 0;            // I2C LSB Address
    
    i2c.write(acc_address, dev_id_address, 1);
    wait(0.1);
    i2c.read(acc_address, dev_id, 1);
    wait(2);
    
    pc.printf("\r\n\r\nDEVICE ID : ");
    print_data_bits(dev_id[0]);
    
    if(dev_id[0] == 0x33)//00110011(who am i)
    {
        pc.printf("\r\nAccelerometer Initialized Successfully");
        return 1;
    }
    else
    {
        pc.printf("\r\nAccelerometer Initialization Failed..... Can't communicate with the device");
        return 0; 
    }      
}

//____________________________________________________________________________________________________
Timer t;
int main()
{
    char retry_attempt = 0;
   
    change.rise(&movement_inertia);
    jerk.rise(&sudden_jerk);
    
    i2c.frequency(100000);
    //SA0 = 0;
    // i2c.write(acc_address, int1_dur, 2);  
    pc.printf("\r\n\r\n\r\n>>> ACCELEROMETER UNDER TEST <<<");
    
    retry:
    if(initialize_accelerometer())
    {
        configure_accelerometer();
        
        while(1)
        {
            //read_acceleration();
            //pc.printf("\r\n\r\nX : %d", x);
            //pc.printf("\r\nY : %d", y);
            // pc.printf("\r\nZ : %d", z);
            //movement_inertia();
            //sudden_jerk();

//           pc.printf("\n(%d)",cnt);
//           cnt++;
//           if(cnt>100)
//           cnt=0;

          
            //t.stop();
            TIMING=t.read();
            pc.printf("The time taken was %f seconds\n", t.read());
            if(TIMING>3)
                {
                    pc.printf("MOOOVINGGGG....");
                    MOTION=1;
                    //TIMING=0;
                    //t.stop();
                    /// t.reset();
                    ACC_LED=1;
                    Steady_cnt=0;
                }
            else
                {
                    if(Steady_cnt>3)
                        {
                            ACC_LED=0;
                            Steady_cnt=0;
                            pc.printf("REST....");
                            MOTION=0;
                        }
                }
            wait(1);
//                                                    if(JERK_FLAG)
//                                                    {
//                                                        JERK_FLAG=0;
//                                                        i2c.write(acc_address, int2_src, 1);
//                                                        i2c.read(acc_address, src_data, 1);
//                                                        pc.printf("\rSRC2 : ");
//                                                        print_data_bits(src_data[0]);
//                                                        src_data[0]=src_data[0]&0x42;
//                                                        // if( src_data[0]==0x42)
//                                                        {
//                                                             pc.printf(" **********SUDDEN JERK********** ");
//                                                        }
//                                        
//                                                    }
            
            if(MOVEMENT_FLAG)
            {
                Steady_cnt++;
                MOVEMENT_FLAG=0;
                t.reset();
                t.start();
                
                
                     ///////////////////////////////////////////////////
//                                                            i2c.write(acc_address, int1_src, 1);//acc_address=0x32 ..INITIALISE COMMN
//                                                            i2c.read(acc_address, src_data, 1);//READING FROM INT1 STATUS REG
//                                                            pc.printf("\rSRC1 : ");
//                                                            print_data_bits(src_data[0]);
//                                                            int1_src_data = (unsigned int)src_data[0];//READ DATA STORED
//                                                            if(i>100)
//                                                            i=0;
//                                                            if(j>100)
//                                                            j=0;
//                                                            if(int1_src_data & 0x42)                          //changed from (0x2A)
//                                                            {
//                                                                 i++;
//                                                                 pc.printf("                 %d=X_INCREMENT",i);
//                                                                 //char int1_dur[2] = {0x33, 50};
//                                                                 //i2c.write(acc_address, int1_dur, 2);    
//                                                            }
//                                                        
//                                                    
//                                                           else if(int1_src_data & 0x41)
//                                                            {
//                                                                j++;
//                                                                pc.printf("   %d=X_decrement",j);
//                                                              //  char int1_dur[2] = {0x33, 3};
//                                                                //i2c.write(acc_address, int1_dur, 2);  
//                                                            }

            }
       }
   }
    
    else
    {
        if(retry_attempt < 100)
        {
            retry_attempt++;
            goto retry;
        }
    }
    

    return 0;
}
 
//____________________________________________________________________________________________________