Qmax / Mbed 2 deprecated LIS_Accelerometer_WIP

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
subinmbed
Date:
Sat Sep 02 11:09:35 2017 +0000
Parent:
0:491bc4ee502e
Commit message:
Accelerometer commented

Changed in this revision

ACCELEROMETER.cpp Show annotated file Show diff for this revision Revisions of this file
ACCELEROMETER.h Show annotated file Show diff for this revision Revisions of this file
OBD.cpp Show annotated file Show diff for this revision Revisions of this file
OBD.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ACCELEROMETER.cpp	Sat Sep 02 11:09:35 2017 +0000
@@ -0,0 +1,276 @@
+
+#include "mbed.h"
+#include "ACCELEROMETER.h"
+#include "OBD.h"
+#include "main.h"
+
+
+#define     acc_address             0x32      
+int i=0,j=0,Steady_cnt=0;
+char src_data[1],FIRST=1,SECOND,MOVING_FLAG=0,TIMER_RUNNING;
+unsigned int int1_src_data,cnt=0;
+char acc_reg[1] = {0x29};
+char acc_data[6];
+//char int1_dur[2] = {0x33, 0x70};
+long speed,FIRST_SPEED=0,CURRENT_SPEED=0;
+Timer t;
+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, 0x21};         //[ODR3 ODR2 ODR1 ODR0 nop Zen Yen Xen]////data rate=10hz(T=100MS)and x enabled
+    char ctrl_reg_2[2]      = {0x21, 0x03};         //HIGH PASS FILTER  Autoreset on interrupt event BUT NOT ENABLED FOR ANY FUNCTION
+    char ctrl_reg_3[2]      = {0x22, 0x40};         //INT1 on INT1, CLK_INT DISABLED 
+    char ctrl_reg_4[2]      = {0x23, 0x80};         //block update ENABLED #NOT NEED FOR OUR APPLICATION
+    char ctrl_reg_5[2]      = {0x24, 0x04};         //4D ENABLED AND IT ACTS WITH 6D ENABLING IN OTHER REGISTER 
+    char ctrl_reg_6[2]      = {0x25, 0x20};         // INT2 Interrupt on INT2 pin /clk INT2 & activity INT DISABLED..FOR CLICK=0XA0 
+        
+    char int1_cfg[2]        = {0x30, 0x43};         // 6D direction motion detection enabled ,XH & XL enabled
+    char int1_ths[2]        = {0x32, 0x01};         //INT1 threshold value  
+    char int1_dur[2]        = {0x33, 0x03};         //INT1 DURATION(GIVEN VALUE *100MS) value 
+    
+    char int2_cfg[2]        = {0x34, 0xFF};         //6D direction position detection enabled for XYZ high OR low
+    char int2_ths[2]        = {0x36, 0x01};         //INT2 threshold value 
+    char int2_dur[2]        = {0x37, 0x02};         //INT2 DURATION(GIVEN VALUE *100MS) value 
+    
+    char act_ths[2]         = {0x3E, 0x00};         //#not used .sleep_to_wake up threshold
+    char act_dur[2]         = {0x3F, 0x00};         //#not used.sleep_to_wake up duration
+    
+//  char clk_cfg[2]         = {0x38, 0x15};          //enable XYZ single clicks
+//  char clk_src[2]         = {0x39, 0x5F};          //Enable single click
+//  char clk_ths[2]         = {0x3A, 0x01};          //click threshold
+//  char clk_Tlimit[2]      = {0x3B, 0x03};          //max time for a tap to abv ths
+//  char clk_Tlatncy[2]     = {0x3C, 0x0};           //time delay to start timr for waiting  scnd  tap
+//  char clk_Twindow[2]     = {0x3D, 0x0};           //max time for timer to wait for scnd
+    
+    //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, clk_cfg, 2);
+//   i2c.write(acc_address, clk_src, 2);
+//   i2c.write(acc_address, clk_ths, 2);
+//   i2c.write(acc_address, clk_Tlimit, 2);
+//   i2c.write(acc_address, clk_Tlatncy, 2);
+//   i2c.write(acc_address, clk_Twindow, 2);
+}
+//////////////////////////////////////////////////////////////////
+char initialize_accelerometer()
+{   
+    i2c.frequency(100000);
+    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; 
+    }      
+}
+////////////////////////////////////////////////////////////////////////////////
+void initialise_configure_accelerometer()//CALLED BY PARENT FUNCTION
+{
+    char retry_attempt = 0;
+    retry:  
+    if(initialize_accelerometer())
+    {
+        configure_accelerometer();
+    }
+    else
+    {
+        if(retry_attempt < 100)          //it will try 100 times to initialise accelerometer in case prev attempts are failed
+        {
+            retry_attempt++;
+            goto retry;
+        }
+    }
+    
+}
+////////////////////////////////////////////////////////////////////
+void print_data_bits(char data_fetched) //# now not using
+{
+    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 process_accelerometer()
+{
+//  if(MOVEMENT_FLAG)
+//    {   
+//        MOVEMENT_FLAG=0;
+//        TIMING=t.read();
+//    }
+    
+// if(MOTION)
+    {
+
+///////////////////////////////////////////////////////////if in motion no interrupt comes
+//       if(FIRST)
+//       {         
+//           t.reset();
+//           t.start();
+//           FIRST=0;
+//           FIRST_TIME=t.read();
+//           FIRST_SPEED=fetch_vehicle_speed();
+//       }
+/////////////////////////////////////////////////////////
+        {
+            CURRENT_TIME=t.read();
+            
+            CURRENT_SPEED=fetch_vehicle_speed();
+            pc.printf("\nspeed=%ld",CURRENT_SPEED);
+            if((CURRENT_SPEED)>1)
+                {
+                    pc.printf("START_TO_MOVING....");
+                    ACC_LED=1;
+                    MOVING_FLAG=1;
+                    START_TO_MOVING=1;
+                    MOVING_TO_STOP=0;
+                    PRE_MOVING=1;
+                    TIMER_RUNNING=1;               //# not needed incase of if in motion no interrupt comes,it will enable reading current time in main
+                    jerk_disable();                // jerk is disabled by disabling stm32 interrupt
+                }
+              if((CURRENT_SPEED<2)&&(MOVING_FLAG==1))//when speed reach from higher value to 1/0 #can edit according to real speed behavior
+                {
+                    pc.printf("MOVING_TO_STOP....");
+                    MOVING_FLAG=0;
+                    MOVING_TO_STOP=1;
+                    START_TO_MOVING=0;
+                    ACC_LED=0;
+                    jerk_enable();                 // jerk is enabled by enabling stm32 interrupt again
+                    TIMER_RUNNING=0;
+                    t.stop();                      //# not needed incase of if in motion no interrupt comes, it will stop timer when stop detected during time interval
+                } 
+               else                    //speed is one at first itself it will not do anythimg consider as fault jerk,this we can eliminate if we set moving speed>1 and stopping at speed=0
+                {   MOVING_TO_STOP=0;
+                    START_TO_MOVING=0;
+                    ACC_LED=0;
+                    TIMER_RUNNING=0;
+                    t.stop();
+                }
+        }
+    }
+}
+/////////////////////////////////////////////////////////////////
+void stopped()
+{
+    START_TO_MOVING=0;
+    MOVING_TO_STOP=1;
+    ACC_LED=0;
+    PRE_MOVING=0;
+    pc.printf("STOPPED");
+    jerk_enable();
+    TIMER_RUNNING=0;
+}
+////////////////////////////////////////////////////////////////
+void jerk_threshold(char jt)   //#editing via this function is not working,inorder to edit threshold we have to change in configuration function
+{
+
+    char int2_cfg[2]        = {0x34, 0xFF};         //INT2 is disabled for XYZ HIE/LIE
+    char int2_ths[2]        = {0x36, 0x09};         //changed from 2 more threshold
+    char int2_dur[2]        = {0x37, 0x02};
+    i2c.write(acc_address, int2_ths, 2);
+    i2c.write(acc_address, int2_dur, 2);
+    i2c.write(acc_address, int2_cfg, 2);
+   
+}
+void jerk_duration(char jd)//#editing via this function is not working,inorder to edit threshold we have to change in configuration function
+{
+   char int2_dur[1] = {jd};
+    i2c.write(acc_address, int2_dur, 2);
+}
+void movement_threshold(char mt)//#editing via this function is not working,inorder to edit threshold we have to change in configuration function
+{
+     char int1_ths[1] =  {mt}; 
+    i2c.write(acc_address, int1_ths, 2); 
+}
+void movement_duration(char md)//#editing via this function is not working,inorder to edit threshold we have to change in configuration function
+{
+     char int1_dur[1] = {md}; 
+    i2c.write(acc_address, int1_dur,2); 
+}
+void jerk_enable()           //enable stm32 interrupt
+{
+    jerk.rise(&sudden_jerk); 
+     
+}
+void jerk_disable()          //enable stm32 interrupt
+{
+     jerk.rise(NULL);
+     JERK_FLAG=0;   
+}
+void movement_enable()       //enable stm32 interrupt
+{
+    change.rise(&movement_inertia);
+}
+void movement_disable()       //enable stm32 interrupt
+{
+    change.rise(NULL);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ACCELEROMETER.h	Sat Sep 02 11:09:35 2017 +0000
@@ -0,0 +1,23 @@
+//char int1_src[1] = {0x31};
+//char int2_src[1] = {0x35};
+extern char MOVING_FLAG;
+extern  char int2_ths[],int1_ths[],int1_dur[],int2_dur[];
+
+void movement_inertia();
+void sudden_jerk();
+void sbn();//not used
+void sbn1();//not used
+void initialise_configure_accelerometer();
+void configure_accelerometer();
+char initialize_accelerometer();//call from parent function
+void print_data_bits(char);
+void process_accelerometer();
+void jerk_threshold(char);
+void jerk_duration(char);
+void stopped();
+void movement_duration(char);
+void movement_threshold(char);
+void jerk_enable();
+void jerk_disable();
+void movement_enable();
+void movement_disable();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OBD.cpp	Sat Sep 02 11:09:35 2017 +0000
@@ -0,0 +1,749 @@
+
+
+
+/*
+______________________________________________________________________________________________________________________
+
+//                                  <<<<<<<<< OBD LIBRARIES >>>>>>>>>
+
+CHIP : STN1110 / ELM327
+-----
+
+NOTE :
+-----
+THESE CODE BLOCKS ARE TESTED IN REAL TIME BY COMMUNICATING DIRECTLY WITH CAR OBD PORT
+THERE ARE SOME UNTESTED PORTIONS IN THE CAR
+>>> VIN NUMBER
+>>> DTC
+VIN NUMBER PID NOT SUPPORTED IN THE CAR TESTED
+THE CAR DOESN'T HAVE ANY DTC AND THIS IS ALSO LEFT UNTESTED
+
+>>> THIS PARTICULAR SOURCE CODE IS COMPILED AND FOUND WITH ZERO ERRORS 
+>>> DATE : 29-MAR-2017
+>>> TIME : 1.00 PM
+
+>>> THIS PARTICULAR SOURCE CODE HAS BEEN TESTED SUCCESSFULLY
+>>> DATE : 10-APR-2017
+>>> TIME : 7:00 PM
+
+___________________
+
+Author : >> BALA <<
+___________________
+
+______________________________________________________________________________________________________________________
+
+*/
+
+#include "mbed.h"
+#include "OBD.h"
+#include "ACCELEROMETER.h"
+//#include "main.h"
+//#include "Common_Defs.h"
+//#include "Lora.h"
+
+
+RawSerial OBD_UART(PA_0, PA_1);
+Serial DEBUG_UART(USBTX, USBRX);
+
+char pass                       =   0;
+char reception_complete         =   0;
+AnalogIn   ain(PA_7);
+
+/*
+__________________________________________________________________________________________________
+
+     THE FOLLWOING ARE THE OBD COMMAND SET FOR THE STN1110 / ELM327 OBD - UART INTERPRETER
+__________________________________________________________________________________________________
+
+*/
+
+
+char obd_reset_cmd[]            =   {"ATZ\r"};
+char battery_voltage_cmd[]      =   "ATRV\r";
+char protocol_auto_detect_cmd[] =   "ATSP0\r";
+char read_CAN_protocol_cmd[]    =   "ATDPN\r";
+char allow_long_cmd[]           =   "ATAL\r";
+char engine_rpm_cmd[]           =   "010C\r";
+char vehicle_speed_cmd[]        =   "010D\r";
+char vin_number_cmd[]           =   "0902 5\r";
+char check_dtc_cmd[]            =   "03\r";
+char check_mil_cmd[]            =   "0101\r";
+
+
+/*
+__________________________________________________________________________________________________
+
+     THE FOLLOWING ARE GLOBAL VARIABLES WHICH CAN BE ACCESSED FOR FURTHER DATA PROCESSING
+__________________________________________________________________________________________________
+
+*/
+
+float   car_battery_voltage;
+long    rpm;
+float   speed1,speed2;
+long    vehicle_speed;
+char    vin_number[17];
+char    mil;
+char    no_of_stored_dtc;
+
+//_________________________________________________________________________________________________
+
+// THE FOLLWOING VARIABLES ARE GLOBAL BUT ARE USED ONLY FOR IN-LIBRARY PROCESSING
+
+char OBD_UART_RX_Buffer[100];
+char OBD_RxBuffer_End_Pos;
+char OBD_UART_RX_Size = 50;
+
+//_________________________________________________________________________________________________
+
+
+
+//*********************************************************************************************************************************************************************************
+
+// FUNCTION CALLED BY PARENT FUNCTION
+// THE FOLLOWING CODE BLOCK IS USED TO VERIFY THE RECEIVED DATA
+
+void received_data_verification(char *rcv_data_pointer, char *ref_data_pointer, char num)
+{
+    char dummy_data[num], count;
+    for(count = 0; count < num; count++)
+    {
+        //OBD_UART.putc(*rcv_data_pointer);
+        if(*rcv_data_pointer++ == *ref_data_pointer++)
+            pass = 1;
+        else   
+        {
+            pass = 0;
+            return;
+        }
+    } 
+}
+
+//*********************************************************************************************************************************************************************************
+
+void rpm_padding_for_comm_test()
+{
+
+    char count;
+    char virtual_engine_rpm[] = "010C\r41 0C 0E A2 \r\r>";
+    char engine_rpm[4];     // Vehicle speed data is returned by a 4 byte value
+    char *rpm_data_pointer;
+    char internal_count = 0;
+    char padded_rpm_17_bytes[17];
+    OBD_UART.printf("RPM Data\r\n");
+    
+    for(count = 0; count < strlen(engine_rpm_cmd); count ++)
+        OBD_UART.putc(engine_rpm_cmd[count]);
+    wait(1);
+    while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+    
+    received_data_verification(OBD_UART_RX_Buffer, engine_rpm_cmd, (strlen(engine_rpm_cmd)-1));
+    for(count = 0; count < 5; count++)
+        OBD_UART.printf("0x%2x ",OBD_UART_RX_Buffer[count]);
+        process_engine_rpm(OBD_UART_RX_Buffer);
+    DEBUG_UART.printf("RPM Received\r\n");
+    rpm_data_pointer = virtual_engine_rpm;
+    rpm_data_pointer += 11;
+    
+    for(count = 0; count < 5; count++)
+    {
+        if(*rpm_data_pointer == ' ') {        // Negate the spaces added in between
+            rpm_data_pointer++;
+            continue; }
+        else {
+            engine_rpm[internal_count] = *rpm_data_pointer++;
+            internal_count++; }
+    }
+    
+    //OBD_UART.printf("\r\n%s", engine_rpm);
+    //OBD_UART.printf("\r\nRPMMMMMM = ");
+    for(count = 0; count < strlen(engine_rpm); count++)
+        OBD_UART.putc(engine_rpm[count]);
+    
+    for(count = 0; count < 4; count++){
+        DEBUG_UART.printf("\r\nPadded = ");
+        for(internal_count = 0; internal_count < strlen(padded_rpm_17_bytes); internal_count++)
+            OBD_UART.putc(padded_rpm_17_bytes[internal_count]);
+        //OBD_UART.printf("\r\nPadded RPM = %s", padded_rpm_17_bytes);
+        strcat(padded_rpm_17_bytes, engine_rpm); 
+    }
+        
+    strcat(padded_rpm_17_bytes, "0");
+    DEBUG_UART.printf("\r\n\r\n PADDED RPM = %s",padded_rpm_17_bytes);
+    DEBUG_UART.putc('\r');
+    for(count = 0; count < strlen(padded_rpm_17_bytes); count++){
+        DEBUG_UART.putc(padded_rpm_17_bytes[count]);
+        //Misc_Packet_Data.VIN[count] = padded_rpm_17_bytes[count];
+    }
+}
+//*********************************************************************************************************************************************************************************
+// FUNCTION CALLED FROM PARENT FUNCTION
+// THE FOLLOWING CODE BLOCK IS USED TO PROCESS THE DTC DATA FETCHED FROM THE OBD PORT OF THE CAR
+
+   
+/*
+|______________________________________________________________________________________________|
+
+FORMULA:
+-------
+t = total bytes of returned data
+n = no of dtc
+
+t = 2n + (n -1) , where (n-1) indicates the added spaces between two bytes
+t = 3n -1
+
+Therefore, n = (t + 1) / 3
+
+|______________________________________________________________________________________________|
+
+*/ 
+
+void process_dtc_data(char *dtc_data_pointer)   // Incomplete Code block
+{     
+    // char virtual_dtc_buffer[] = "03\r43 00 85\r\r>";
+    char count;
+    char internal_count;
+    char dtc_data[(4*(no_of_stored_dtc))];
+    char dtc_codes[(6*(no_of_stored_dtc))];            // Refer below 
+    
+    /*
+    _____________________________________________
+    
+    NOTE :
+    -----
+    DTC CODE ARRAY SPACE ALLOCATION
+    
+    ONE DTC CONTAINS 5 CHARACTERS
+    '\r' IS USED AS SEPARATOR (END BYTE)
+    TOTALLY TO STORE ONE DTC 6 BYTES ARE USED
+    _____________________________________________
+    
+    */
+    
+    dtc_data_pointer += 6;
+
+    // THE FOLLOWING LINES OF CODE COPIES THE DTC DATA IN A VARIABLE AND ELIMINATES THE SPACE CHARACTER
+    
+    for(count = 0; count < ((5*no_of_stored_dtc) + (no_of_stored_dtc - 1)); count++)
+    { 
+        if((*dtc_data_pointer) == '\r')
+            break;
+            
+        if((*dtc_data_pointer) == ' ')
+        {
+            dtc_data_pointer++;
+            continue;
+        }
+        
+        else
+        {
+            if((*dtc_data_pointer - 0x30) <= 9)
+                dtc_data[internal_count] = (*dtc_data_pointer) - 0x30;
+            else
+            {
+                dtc_data[internal_count] = (*dtc_data_pointer) - 0x37;
+            }
+            internal_count++;              
+            dtc_data_pointer++;
+        }
+    }   
+       
+    internal_count = 0;
+
+    char dtc_data_merged[((4*(no_of_stored_dtc)) / 2)];
+      
+    for(count = 0; count < ((4*no_of_stored_dtc)); count += 2)
+    {
+        dtc_data_merged[internal_count] = ((dtc_data[count] << 4) | dtc_data[count + 1]);
+        internal_count++;
+    }
+    
+    internal_count = 0;
+        
+    for(count = 0; count < (2*(no_of_stored_dtc)); count += 2)
+    {
+        switch(dtc_data_merged[count] & 0b11000000)
+        {
+            case 0:
+                dtc_codes[internal_count] = 'P';
+                break;
+            case 1:
+                dtc_codes[internal_count] = 'C';
+                break;
+            case 2:
+                dtc_codes[internal_count] = 'B';
+                break;
+            case 3:
+                dtc_codes[internal_count] = 'U';
+                break;
+        }
+        
+        internal_count++;
+            
+        dtc_codes[internal_count] = ((dtc_data_merged[count] & 0b00110000) + 0x30); 
+        internal_count++;
+        dtc_codes[internal_count] = ((dtc_data_merged[count] & 0b00001111) + 0x30);
+        internal_count++;
+        dtc_codes[internal_count] = (((dtc_data_merged[count + 1] & 0b11110000) >> 4) + 0x30);
+        internal_count++;
+        dtc_codes[internal_count] = ((dtc_data_merged[count + 1] & 0b00001111) + 0x30);
+        internal_count++;
+        dtc_codes[internal_count] = '\r';
+            
+        DEBUG_UART.printf("\r\nDTC CODES :");
+        DEBUG_UART.printf("\r\n%s", dtc_codes);
+    }
+}
+
+//*********************************************************************************************************************************************************************************
+
+// FUNCTION CALLED FROM PARENT FUNCTION
+// THE FOLLOWING CODE BLOCK IS USED TO PROCESS MIL DATA FETCHED FROM THE OBD PORT OF THE CAR
+
+void process_mil_data(char *mil_data_pointer)
+{
+    DEBUG_UART.printf("\r\n\r\nENTERED PROCESS MIL DATA FUNCTION");
+    char mil_data[2], mil_data_converted;
+    char count;
+    
+    // char virtual_mil_buffer = "0101\r41 01 00 04 60 00 \r\r";
+    mil_data_pointer += 11;
+    
+    for(count = 0; count < 2; count++)
+    {
+        mil_data[count] = *mil_data_pointer++;
+    }
+    
+    // THE FOLLOWING LINES OF CODE CONVERTS THE ASCII DATA TO THE HEX DATA  
+    
+    if((((mil_data[0] - 0x30) & 0x7F) <= 9) && (((mil_data[1] - 0x30) & 0x7F) <= 9))
+        mil_data_converted = (((mil_data[0] - 0x30)<<4) + (mil_data[1] - 0x30)); // Converting the ASCII data to the Hex data
+    
+    else if((((mil_data[0] - 0x30) & 0x7F) <= 9))
+        mil_data_converted = (((mil_data[0] - 0x30)<<4) + (mil_data[1] - 0x37)); // Converting the ASCII data to the Hex data
+    
+    else if((((mil_data[1] - 0x30) & 0x7F) <= 9))
+        mil_data_converted = (((mil_data[0] - 0x37)<<4) + (mil_data[1] - 0x30)); // Converting the ASCII data to the Hex data
+        
+    else
+            mil_data_converted = (((mil_data[0] - 0x37)<<4) + (mil_data[1] - 0x37)); // Converting the ASCII data to the Hex data
+    
+    // The following code lines checks for the number of Mal function indicator lamp enabled by tghe CAN ECU
+    
+    if(mil_data_converted & 0x80) // Checks for the MSB bit enabled or not which inc=dicates that the MIL is On or NOT
+    {
+        mil = 1;                // Setting the MIL flag
+        DEBUG_UART.printf("\r\nMIL (MALFUNCTION INDICATOR LAMP) IS ON");
+        no_of_stored_dtc = (mil_data_converted & 0b01111111);
+        DEBUG_UART.printf("\r\n NO OF STORED DYNAMIC TROUBLE CODE = %d", no_of_stored_dtc);
+    }
+    else {
+        mil = 0;                // Clearing the MIL flag
+        DEBUG_UART.printf("\r\nMIL (MALFUNCTION INDICATOR LAMP) IS OFF"); }
+}
+
+//*********************************************************************************************************************************************************************************
+
+// FUNCTION CALLED FROM PARENT FUNCTION
+// THE FOLLWOING CODE BLOCK IS USED TO PROCESS THE VEHICLE SPEED DATA FETCHED FROM THE OBD PORT OF THE CAR
+
+float process_vehicle_speed(char *vehicle_speed_pointer)
+{
+    char vehicle_speed_data[2];     // Vehicle speed data is returned by a 2 byte value
+    char *strtol_pointer;
+    char count;
+    //"010D\r41 0D 4F\r\r>"
+    vehicle_speed_pointer += 11;
+    //for(count = 0; count < 2; count++)
+    {
+        //vehicle_speed_data[count] = *vehicle_speed_pointer++;
+       // vehicle_speed_data[count] = 5;
+    }
+    
+    vehicle_speed =  strtol(vehicle_speed_data, &strtol_pointer, 16);
+    //speed1=0;
+    speed1=ain.read()*100.0f;///*************************adc value for tesing need to load actual speed********************************************
+    
+    DEBUG_UART.printf("\r\nVEHICLE SPEED = %ld", vehicle_speed);
+    return(speed1);
+}
+
+//*********************************************************************************************************************************************************************************
+
+// FUNCTION CALLED BY PARENT FUCTION
+// THE FOLLWOING CODE BLOCK IS USED TO PROCESS THE ENGINE RPM FETCHED FROM THE OBD PORT OF THE CAR
+
+unsigned int process_engine_rpm(char *rpm_data_pointer)
+{
+    //char virtual_engine_rpm[] = "010C\r41 0C 00 00 \r\r>";
+    char engine_rpm[4];     // Vehicle speed data is returned by a 4 byte value
+    char *strtol_pointer;
+    char count;
+    char internal_count = 0;
+    
+    rpm_data_pointer += 11;
+    
+    for(count = 0; count < 5; count++)
+    {
+        if(*rpm_data_pointer == ' ') {        // Negate the spaces added in between
+            rpm_data_pointer++;
+            continue; }
+        else {
+            engine_rpm[internal_count] = *rpm_data_pointer++;
+            internal_count++; }
+    }
+    
+    DEBUG_UART.printf("\r\n%s", engine_rpm);
+    
+    rpm =  ((strtol(engine_rpm, &strtol_pointer, 16)) / 4);
+    
+    DEBUG_UART.printf("\r\nENGINE RPM = %ld", rpm);
+    return(rpm);
+}
+
+//*********************************************************************************************************************************************************************************
+
+// FUNCTION CALLED BY PARENT FUNCTION
+// THE FOLLOWING CODE BLOCK IS USED TO PROCESS THE BATTERY VOLTAGE FETCHED FROM THE OBD PORT OF THE CAR
+
+void process_battery_voltage(char *battery_voltage_pointer)
+{
+    char battery_voltage_data[4];   // One decimal point precision ( For ex : 12.5 )
+    char count;
+    battery_voltage_pointer += 5;   // ATRV<CR> counts to 5
+    for(count = 0; count < 4; count++)
+    {
+        battery_voltage_data[count] = *battery_voltage_pointer++;
+    }
+    car_battery_voltage = atof(battery_voltage_data);   // Converts the Battery Volatge from String to Float data type
+    //Misc_Packet_Data.Car_Battery_Voltage = (car_battery_voltage * 100);
+    DEBUG_UART.printf("\r\nCAR BATTERY VOLTAGE = %f",car_battery_voltage);
+}
+
+ 
+//*********************************************************************************************************************************************************************************
+
+/*
+_______________________________________________________________________________________________________________________
+
+NOTE :
+-----
+THE FOLLOWING CODE O DETERMINE THE DIAGNOSTIC TROUBLE CODE (DTC) IS BASED ON THE INTERPRETATION GIVEN IN WIKIPEDIA
+FOR THE  ISO 15765-2 PROTOCOL
+DIFFERENT INTERPRETATION METHODOLOGY IS GIVEN IN THE ELM327 DATASHEET FOR SAE PROTOCOL
+SO THESE THINGS ARE SUBJECTED TO MODIFICATION AND HAVE NOT BEEN CONFIRMED WITH THE REAL TIME DATA 
+_______________________________________________________________________________________________________________________
+
+*/
+
+// FUNCTION WILL BE CALLED ON REQUEST
+// THE FOLLOWING CODE BLOCK WILL FETCH THE DTC DATA FROM THE OBD PORT OF THE CAR AND SENDS THE SAME FOR PROCESSING
+
+void check_for_dtc()
+{
+    char virtual_dtc_buffer[] = "03\r43 00 85\r\r>";
+    
+    OBD_UART.printf(check_dtc_cmd);
+    wait(1);
+    while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+    DEBUG_UART.printf("Reception Complete\r\n");
+    received_data_verification(OBD_UART_RX_Buffer, check_dtc_cmd, (strlen(check_dtc_cmd)-1));
+    
+    //process_dtc_data(virtual_dtc_buffer);
+    
+    
+    if(pass == 1) {
+        process_dtc_data(OBD_UART_RX_Buffer);
+        DEBUG_UART.printf("\r\VEHICLE DIAGNOSTIC TROUBLE CODE RECEIVED SUCCESSFULLY \r\n\r\n"); }
+    else
+        DEBUG_UART.printf("\r\nVEHICLE DIAGNOSTIC TROUBLE CODE DATA RECEPTION FAILED\r\n\r\n");
+    reception_complete = 0;     // Disabling the reception complete flag
+    OBD_RxBuffer_End_Pos = 0;   // Rx Buffer will be overwritten in the next data reception
+
+}
+
+//*********************************************************************************************************************************************************************************
+
+// FUNCTION WILL BE CALLED ON REQUEST
+// THE FOLLOWING CODE BLOCK WILL FETCH THE MIL DATA FROM THE OBD PORT OF THE CAR
+
+void check_for_MIL()
+{
+    
+    DEBUG_UART.printf("\r\n\r\nENTERED CHECK FOR MIL FUNCTION");
+    char count;
+    char virtual_mil_buffer[] = "0101\r41 01 82 04 60 00 \r\r";
+    
+    /*
+    OBD_UART.printf(check_mil_cmd);
+    
+    for(count = 0; count < strlen(check_mil_cmd); count++)
+        OBD_UART.putc(check_mil_cmd[count]);
+    wait(1);
+    while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+    OBD_UART.printf("Reception Complete\r\n");
+    received_data_verification(OBD_UART_RX_Buffer, check_mil_cmd, (strlen(check_mil_cmd)-1));
+    */
+    
+    process_mil_data(virtual_mil_buffer);
+    
+    
+    if(pass == 1) {
+        process_mil_data(OBD_UART_RX_Buffer);
+        DEBUG_UART.printf("\r\nVEHICLE MIL DATA RECEIVED SUCCESSFULLY \r\n\r\n");}
+    else
+        DEBUG_UART.printf("\r\nVEHICLE MIL DATA RECEPTION FAILED\r\n\r\n");
+    reception_complete = 0;     // Disabling the reception complete flag
+    OBD_RxBuffer_End_Pos = 0;   // Rx Buffer will be overwritten in the next data reception
+    
+}
+
+
+//*********************************************************************************************************************************************************************************
+
+char fetch_vin_number()
+{
+    char count;
+    //char virtual_rx_vin_buffer[] = "3147 43\r314b \r58: 43 38 01 54 4D 42 \r1: 46 4B 4A 35 4A 32 43 \r2: 47 30 31 34 37 33 33 \r\r>"; 
+    char virtual_rx_vin_buffer[] = "0902 5\r014 \r0: 49 02 01 54 4D 42 \r1: 40 41 42 35 4A 32 43 \r2: 47 30 31 34 37 33 33 \r\r>"; 
+    //char virtual_rx_vin_buffer[] = "0902 5\r014 \r0: 49 02 01 54 4D 42 \r1: 46 4B 4A 35 4A 32 43 \r2: 47 30 31 34 37 33 33 \r\r>"; 
+    //char virtual_rx_vin_buffer[] = "0902 5\r014 \r0: 49 02 01 54 4D 42 \r1: 46 4B 4A 35 4A 32 43 \r2: 47 30 31 34 37 33 33 \r\r>"; 
+//    1GC1KXC82BF134775->31 47 43 31 4b 58 43 38 32 42 46 31 33 34 37 37 35
+//    1G1PC5SB0E7341780->31 47 31 50 43 35 53 42 30 45 37 33 34 31 37 38 30
+//    1G1RD6S56GU125328->31 47 31 52 44 36 53 35 36 47 55 31 32 35 33 32 38
+    char vin_number_cmd1[]           =   "0902 5\r";
+    OBD_UART.printf("VIN Reception Started24432\r\n");
+    
+    
+    char vin_buffer[100];
+    char *vin_data_pointer;
+    char internal_counter = 0;
+    char small_buffer[2];
+    long ascii_converted_data;
+    char *vin_conv_pointer;
+    char vin_number[17];
+    wait(1);
+    OBD_RxBuffer_End_Pos = 0;
+    //for(count = 0; count < strlen(vin_number_cmd1); count++)
+      //  OBD_UART.putc(vin_number_cmd[count]);
+    OBD_UART.putc(0x30);
+    OBD_UART.putc(0x39);
+    OBD_UART.putc(0x30);
+    OBD_UART.putc(0x32);
+    OBD_UART.putc(0x20);
+    OBD_UART.putc(0x35);
+    OBD_UART.putc(0x0D);
+    
+    OBD_UART.printf("VIN Reception Started   %d\r\n",strlen(virtual_rx_vin_buffer));
+    
+    vin_data_pointer = virtual_rx_vin_buffer;
+    vin_data_pointer += 23;
+    while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'))        // Waits here until the reception complete flag has been enabled
+    {
+        if(OBD_RxBuffer_End_Pos >=6)
+            break;
+    }
+    for(count = 0; count < 86; count++)
+       OBD_UART.putc(OBD_UART_RX_Buffer[count]);
+    //OBD_UART.printf("VIN Reception Complete\r\n");
+    //return(1);
+    vin_data_pointer = OBD_UART_RX_Buffer;
+    vin_data_pointer += 23;
+    
+    int length;
+    for(count = 0; count < (strlen(virtual_rx_vin_buffer) - 1); count++)
+    {
+        if((*vin_data_pointer == '\r') | (*vin_data_pointer == ' ') | (*vin_data_pointer == '>'))
+        {
+            vin_data_pointer++;
+            continue;
+        }
+        else if(*(vin_data_pointer + 1) == ':')
+        {
+            vin_data_pointer += 2;
+            continue;
+        }
+        else
+        {
+            //printf("\r\ncount2 = %d", count); 
+            vin_buffer[internal_counter] = *vin_data_pointer;
+            internal_counter++;
+        }
+        vin_data_pointer++;
+    }
+    
+    length = strlen(vin_buffer);
+    DEBUG_UART.printf("\r\n VIN BUFFER LENGTH = %d", length);
+    
+    DEBUG_UART.printf("\r\n VIN NUMBER : %s", vin_buffer);
+    
+    internal_counter = 0;
+    
+    for(count = 0; count < strlen(vin_buffer); count+=2)
+    {
+        small_buffer[0] = vin_buffer[count];
+        small_buffer[1] = vin_buffer[count+1];
+        ascii_converted_data =  strtol(small_buffer, &vin_conv_pointer, 16);
+        vin_number[internal_counter] = ascii_converted_data;
+        //Misc_Packet_Data.VIN[internal_counter] = ascii_converted_data;
+        internal_counter++;
+    }
+    DEBUG_UART.printf("\r\n\r\nVEHICLE CHASSIS NUMBER : %s", vin_number);
+
+}
+
+//*********************************************************************************************************************************************************************************
+
+
+// FUNCTION WILL BE CALLED ON REQUEST
+// THE FOLLOWING CODE BLOCK FETCHES THE VEHICLE SPEED DATA FROM THE OBD PORT OF THE CAR AND SENDS THE SAME FOR PROCESSING
+
+float fetch_vehicle_speed()
+{
+    char virtual_rx_speed_buffer[] = "010D\r41 0D 4F \r\r>";     
+    char count;
+    //OBD_UART.printf(vehicle_speed_cmd);
+    
+    //for(count = 0; count < strlen(vehicle_speed_cmd); count++)
+       // OBD_UART.putc(vehicle_speed_cmd[count]);
+    //wait(1);
+   // while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+   // DEBUG_UART.printf("Reception Complete\r\n");
+    //received_data_verification(OBD_UART_RX_Buffer, vehicle_speed_cmd, (strlen(vehicle_speed_cmd)-1));
+    
+    //
+    speed2=process_vehicle_speed(virtual_rx_speed_buffer);
+    
+    if(pass == 1) {
+       speed2= process_vehicle_speed(OBD_UART_RX_Buffer);
+        DEBUG_UART.printf("\r\nVEHICLE SPEED DATA RECEIVED SUCCESSFULLY \r\n\r\n"); }
+    else
+        DEBUG_UART.printf("\r\nVEHICLE SPEED DATA RECEPTION FAILED\r\n\r\n");
+    reception_complete = 0;     // Disabling the reception complete flag
+    OBD_RxBuffer_End_Pos = 0;   // Rx Buffer will be overwritten in the next data reception
+    return(speed2);
+    
+}  
+
+//*********************************************************************************************************************************************************************************
+
+// FUNCITON WILL BE CALLED  ON REQUEST
+// THE FOLLOWING CODE BLOCK FETCHES THE RPM DATA FROM THE OBD PORT OF THE CAR AND WILL SEND THE SAME FOR PROCESSING
+
+unsigned int fetch_engine_rpm()
+{
+    char count;
+    unsigned int RPM;
+    OBD_RxBuffer_End_Pos = 0;
+   
+    char virtual_engine_rpm[] = "010C\r41 0C 0E A2 \r\r>";
+    DEBUG_UART.printf("rpm Reception Start\r\n");
+//    for(count = 0; count < strlen(engine_rpm_cmd); count ++)
+//        OBD_UART.putc(engine_rpm_cmd[count]);
+//    wait(1);
+//    while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+//    OBD_UART.printf("Reception Complete\r\n");
+//    received_data_verification(OBD_UART_RX_Buffer, engine_rpm_cmd, (strlen(engine_rpm_cmd)-1));
+//    
+    
+    //process_engine_rpm(virtual_engine_rpm);
+    if(pass == 1) {
+        RPM = process_engine_rpm(virtual_engine_rpm);
+        DEBUG_UART.printf("\r\nVEHICLE SPEED DATA RECEIVED SUCCESSFULLY \r\n\r\n"); }
+    else
+        DEBUG_UART.printf("\r\nVEHICLE SPEED DATA RECEPTION FAILED\r\n\r\n");
+    reception_complete = 0;     // Disabling the reception complete flag
+    OBD_RxBuffer_End_Pos = 0;   // Rx Buffer will be overwritten in the next data reception
+    return(RPM);
+}
+ 
+//*********************************************************************************************************************************************************************************
+
+// FUNCTION WILL BE CALLED ON REQUEST
+// THE FOLLOWING CODE BLOCK FETCHED THE BATTERY VOLTAGE DATA FROM THE OBD PORT AND PASSES THE SAME DATA FOR PROCESSING
+
+void fetch_battery_voltage()
+{
+    char count;
+    //OBD_UART.printf(battery_voltage_cmd);
+    OBD_UART.printf("Battery Voltage\r\n");
+    for(count = 0; count < strlen(battery_voltage_cmd); count++){
+        OBD_UART.putc(battery_voltage_cmd[count]);
+        OBD_UART.printf("0x%2x ",battery_voltage_cmd[count]);
+    }
+    wait(1);
+    
+    while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+    DEBUG_UART.printf("Reception Complete\r\n");
+    received_data_verification(OBD_UART_RX_Buffer, battery_voltage_cmd, (strlen(battery_voltage_cmd)-1));
+    
+
+    if(pass == 1) {
+        DEBUG_UART.printf("\r\nOBD READ BATTERY VOLTAGE SUCCESSFUL");
+        process_battery_voltage(OBD_UART_RX_Buffer); }
+    else
+        DEBUG_UART.printf("\r\nOBD READ BATTERY VOLTAGE FAILED");
+    reception_complete = 0;     // Disabling the reception complete flag
+    OBD_RxBuffer_End_Pos = 0;   // Rx Buffer will be overwritten in the next data reception
+}
+  
+    
+//*********************************************************************************************************************************************************************************
+
+// FUNCTION WILL BE CALLED ON REQUEST
+// TYHE FOLLOWING CODE BLOXK INITIALIZES THE OBD INTERFACE
+// IT IS MANDATORY TO CALL THIS FUNCTION BEFORE USING ANY OF THE OTHER FUNCTIONS INCLUDED IN THIS PARTICULAR LIBRARY
+
+void initialize_obd()
+{
+    char count;
+    char data[3];
+    char *data_pointer;  
+
+    data_pointer = data;
+    
+    //OBD_UART.baud(9600);
+//    OBD_UART.attach(&OBD_onDataRx);
+    
+    DEBUG_UART.printf("Reception Started\r\n");
+    
+ //--------------------------------------------------------------------------------------------------------------------------     
+    //for(Misc_Count=0; Misc_Count < 5; Misc_Count++) {
+//        OBD_UART.putc(battery_voltage_cmd[Misc_Count]);
+//    }
+    OBD_UART.printf(obd_reset_cmd);
+
+    wait(1);
+    
+  //~sbn~   while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+    DEBUG_UART.printf("Reception Complete\r\n");
+    received_data_verification(OBD_UART_RX_Buffer, obd_reset_cmd, (strlen(obd_reset_cmd)-1));
+
+    if(pass == 1)
+        DEBUG_UART.printf("\r\nOBD RESET SUCCESSFUL \r\n\r\n");
+    else
+        DEBUG_UART.printf("\r\nOBD RESET FAILED \r\n\r\n");
+   // fetch_battery_voltage();        
+    reception_complete = 0;     // Disabling the reception complete flag
+    OBD_RxBuffer_End_Pos = 0;   // Rx Buffer will be overwritten in the next data reception
+    
+//--------------------------------------------------------------------------------------------------------------------------  
+
+    //OBD_UART.printf(allow_long_cmd);
+    for(count = 0; count < strlen(allow_long_cmd); count++)
+        OBD_UART.putc(allow_long_cmd[count]);
+    wait(1);
+//~sbn~  while(!(OBD_UART_RX_Buffer[OBD_RxBuffer_End_Pos-1] == '>'));        // Waits here until the reception complete flag has been enabled
+    DEBUG_UART.printf("Reception Complete\r\n");
+    received_data_verification(OBD_UART_RX_Buffer, allow_long_cmd, (strlen(allow_long_cmd)-1));
+
+    if(pass == 1)
+        DEBUG_UART.printf("\r\nLONG DATA RECEPTION ENABLED SUCCESSFULLY \r\n\r\n");
+    else
+        DEBUG_UART.printf("\r\nLONG DATA RECEPTION ENABLING FAILED\r\n\r\n");
+    reception_complete = 0;     // Disabling the reception complete flag
+    OBD_RxBuffer_End_Pos = 0;   // Rx Buffer will be overwritten in the next data reception
+    
+}
+ /*********************************************************************************************************************************************************************************/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OBD.h	Sat Sep 02 11:09:35 2017 +0000
@@ -0,0 +1,29 @@
+#ifndef __OBD_H__
+#define __OBD_H__
+
+//#include "Common_Defs.h"
+
+//extern RawSerial OBD_UART;
+
+extern char OBD_UART_RX_Buffer[100];
+extern char OBD_RxBuffer_End_Pos;
+
+void received_data_verification(char*, char*, char);
+//char* copy_characters(char *copy_char_pointer, char start_position, char end_position);
+void process_battery_voltage(char *battery_voltage_pointer);
+float process_vehicle_speed(char *vehicle_speed_pointer); //*********change it as returnable function
+void process_dtc_data(char *dtc_data_pointer);
+void process_mil_data(char *mil_data_pointer);
+void process_dtc_data(char *dtc_data_pointer);
+void fetch_battery_voltage(void);
+float fetch_vehicle_speed(void); //*********change it as returnable function
+char fetch_vin_number(void);
+void check_for_dtc(void);
+void check_for_MIL(void);
+void check_for_dtc(void);
+unsigned int fetch_engine_rpm(void);
+unsigned int process_engine_rpm(char*);
+void rpm_padding_for_comm_test();
+void initialize_obd(void);
+
+#endif
\ No newline at end of file
--- a/main.cpp	Thu Aug 24 08:54:43 2017 +0000
+++ b/main.cpp	Sat Sep 02 11:09:35 2017 +0000
@@ -1,36 +1,7 @@
 
 /*
 _____________________________________________________________________________________________________
-_____________________________________________________________________________________________________
 
-//                                  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
 
@@ -65,374 +36,92 @@
 
 #include "mbed.h"
 #include "main.h"
+#include "OBD.h"
+#include "ACCELEROMETER.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);
+I2C i2c(PB_14, PB_13); 
 
 #define     acc_address             0x32       
+int TIMING_M;
+float speed_m=1;
+Serial pc(PC_0, NC,115200);
+char JERK_FLAG=0,MOVEMENT_FLAG=0,MOTION=0,START_TO_MOVING=0,MOVING_TO_STOP=0,PRE_MOVING=0,MOTION_FIRST_STEP=0;
+float TIMING=0,FIRST_TIME=0,CURRENT_TIME=0;
 
-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]);
-
+pc.printf("\nmvmnt");
     MOVEMENT_FLAG=1;
-    
-
+   if(fetch_vehicle_speed()>speed_m) //after interrupt comes and speed is there 
+    {
+        MOTION=1;
+        MOTION_FIRST_STEP=1;         //local flag for main
+       
+    }
+//    else if(MOVING_FLAG)
+//    {
+//         pc.printf("MOVING_TO_STOP");
+//         ACC_LED=0;
+//         MOVING_FLAG=0;
+//    }
+    else
+    {
+        MOVING_TO_STOP=0;
+        START_TO_MOVING=0;        
+        ACC_LED=0;
+    }
 }
 
 //____________________________________________________________________________________________________
 
 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");       
+   pc.printf("\njerk");
+   JERK_FLAG=1;             //now nowere it is clearing once it is set
 }
-
-//____________________________________________________________________________________________________
-
-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);  
+    initialize_obd();
+    initialise_configure_accelerometer();  
    
-    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(MOTION_FIRST_STEP) //if movement interrupt comes 
+            {
+                MOTION_FIRST_STEP=0;
+                process_accelerometer();
+            }
+            if((PRE_MOVING)&&(fetch_vehicle_speed()<1))//for ensuring stop if there is no movement interrupt during sudden stop
+            {   
+                stopped();
+            }
+//////////////////////////////////////////////////////////
+//if in motion no interrupt comes it will ckeck after 2s// 
+//            if(TIMER_RUNNING)
+//            {
+//                CURRENT_TIME=t.read();
+//                pc.printf("\nTIMER_RUNNING");
+//                if((CURRENT_TIME-FIRST_TIME)>2)
+//                {
+//                    process_accelerometer();
+//                    TIMER_RUNNING=0;
+//                    FIRST=1;
+//                }
+//            }
+////////////////////////////////////////////////////////
             
-            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;
+  return 0;
 }
  
 //____________________________________________________________________________________________________
\ No newline at end of file
--- a/main.h	Thu Aug 24 08:54:43 2017 +0000
+++ b/main.h	Sat Sep 02 11:09:35 2017 +0000
@@ -1,9 +1,18 @@
 
-char int1_src[1] = {0x31};
-char int2_src[1] = {0x35};
+//extern char int1_src[1] = {0x31};
+//extern char int2_src[1] = {0x35};
+//char clk_src[1] = {0x39};
+
+extern char JERK_FLAG,MOVEMENT_FLAG,MOTION,FIRST,SECOND,NEXT_TIME,START_TO_MOVING,MOVING_TO_STOP,PRE_MOVING,TIMER_RUNNING;
+extern Serial pc;
+extern float TIMING,FIRST_TIME,CURRENT_TIME;
 
-char initialize_accelerometer(void);
-void configure_accelerometer(void);
-void initialize_internal_temperature_sensor(void);
-void read_acceleration(void);
-void print_data_bits(char);
\ No newline at end of file
+extern long speed,FIRST_SPEED,CURRENT_SPEED;
+
+extern DigitalOut  ACC_LED;
+extern I2C i2c; 
+extern Timer t;
+
+
+extern InterruptIn change;  // INT1
+extern InterruptIn jerk;   //INT2