OBD Source Code -Section 1- Without Car / Section 2 - With car
Dependencies: mbed
accelerometer.cpp@2:49712259aa71, 2017-04-03 (annotated)
- Committer:
- bala0x07
- Date:
- Mon Apr 03 06:02:26 2017 +0000
- Revision:
- 2:49712259aa71
- Parent:
- 0:e36d80703ed0
This particular source code has two independent sections, comment any one of the following before proceeding
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bala0x07 | 0:e36d80703ed0 | 1 | |
bala0x07 | 0:e36d80703ed0 | 2 | #include "mbed.h" |
bala0x07 | 0:e36d80703ed0 | 3 | #include "obd_libraries.h" |
bala0x07 | 0:e36d80703ed0 | 4 | #include "accelerometer.h" |
bala0x07 | 0:e36d80703ed0 | 5 | #include "common_definitions.h" |
bala0x07 | 0:e36d80703ed0 | 6 | |
bala0x07 | 0:e36d80703ed0 | 7 | #define IDLE 0 |
bala0x07 | 0:e36d80703ed0 | 8 | #define ACTIVE 1 |
bala0x07 | 0:e36d80703ed0 | 9 | |
bala0x07 | 0:e36d80703ed0 | 10 | I2C i2c(PB_9, PB_8); |
bala0x07 | 0:e36d80703ed0 | 11 | Serial pc(USBTX, USBRX); |
bala0x07 | 0:e36d80703ed0 | 12 | |
bala0x07 | 0:e36d80703ed0 | 13 | InterruptIn double_tap(PC_1); // Pin assignment may vary |
bala0x07 | 0:e36d80703ed0 | 14 | InterruptIn inactivity(PC_0); // Pin assignment may vary |
bala0x07 | 0:e36d80703ed0 | 15 | |
bala0x07 | 0:e36d80703ed0 | 16 | DigitalOut led(LED1); |
bala0x07 | 0:e36d80703ed0 | 17 | |
bala0x07 | 0:e36d80703ed0 | 18 | // INTERFACING ADXL345 ACCELEROMETER USING I2C |
bala0x07 | 0:e36d80703ed0 | 19 | |
bala0x07 | 0:e36d80703ed0 | 20 | /* |
bala0x07 | 0:e36d80703ed0 | 21 | |
bala0x07 | 0:e36d80703ed0 | 22 | NOTE : |
bala0x07 | 0:e36d80703ed0 | 23 | Due to communication speed limitations, the maximum output |
bala0x07 | 0:e36d80703ed0 | 24 | data rate when using 400 kHz I2C is 800 Hz and scales linearly with |
bala0x07 | 0:e36d80703ed0 | 25 | a change in the I2C communication speed |
bala0x07 | 0:e36d80703ed0 | 26 | |
bala0x07 | 0:e36d80703ed0 | 27 | */ |
bala0x07 | 0:e36d80703ed0 | 28 | |
bala0x07 | 0:e36d80703ed0 | 29 | //InterruptIn activity(PB_0); // Button B1 (Blue) |
bala0x07 | 0:e36d80703ed0 | 30 | |
bala0x07 | 0:e36d80703ed0 | 31 | |
bala0x07 | 0:e36d80703ed0 | 32 | |
bala0x07 | 0:e36d80703ed0 | 33 | const int slave_address_acc = 0xA6; |
bala0x07 | 0:e36d80703ed0 | 34 | char axis_data[6] = {0,0,0,0,0,0}; |
bala0x07 | 0:e36d80703ed0 | 35 | int16_t x_axis, y_axis, z_axis; |
bala0x07 | 0:e36d80703ed0 | 36 | char interrupt_source[2]; |
bala0x07 | 0:e36d80703ed0 | 37 | char axis_data_start_address[2]; |
bala0x07 | 0:e36d80703ed0 | 38 | char intr_source_address[2] = {0x30, 0}; |
bala0x07 | 0:e36d80703ed0 | 39 | char all_interrupt_clear_command[2] = {0x2E, 0x00}; |
bala0x07 | 0:e36d80703ed0 | 40 | char all_interrupt_enable_command[2] = {0x2E, 0x18}; |
bala0x07 | 0:e36d80703ed0 | 41 | char activity_interrupt_disable_command[2] = {0x2E, 0x28}; |
bala0x07 | 0:e36d80703ed0 | 42 | char inactivity_interrupt_disable_command[2] = {0x2E, 0x30}; |
bala0x07 | 0:e36d80703ed0 | 43 | char accelerometer_status_registered = 0; |
bala0x07 | 0:e36d80703ed0 | 44 | unsigned int interrupt_source_duplicate; |
bala0x07 | 0:e36d80703ed0 | 45 | |
bala0x07 | 0:e36d80703ed0 | 46 | char previous_state = 0; |
bala0x07 | 0:e36d80703ed0 | 47 | char current_state = 0; |
bala0x07 | 0:e36d80703ed0 | 48 | |
bala0x07 | 0:e36d80703ed0 | 49 | extern long vehicle_speed; |
bala0x07 | 0:e36d80703ed0 | 50 | char current_speed, previous_speed; |
bala0x07 | 0:e36d80703ed0 | 51 | char speed_threshold = 10; |
bala0x07 | 0:e36d80703ed0 | 52 | |
bala0x07 | 0:e36d80703ed0 | 53 | //---------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 54 | |
bala0x07 | 0:e36d80703ed0 | 55 | void char_to_int(char data_fetched) |
bala0x07 | 0:e36d80703ed0 | 56 | { |
bala0x07 | 0:e36d80703ed0 | 57 | unsigned int shifter; |
bala0x07 | 0:e36d80703ed0 | 58 | |
bala0x07 | 0:e36d80703ed0 | 59 | interrupt_source_duplicate = 0x00; |
bala0x07 | 0:e36d80703ed0 | 60 | |
bala0x07 | 0:e36d80703ed0 | 61 | for(shifter = 0; shifter < 8; shifter++) |
bala0x07 | 0:e36d80703ed0 | 62 | { |
bala0x07 | 0:e36d80703ed0 | 63 | interrupt_source_duplicate |= (((data_fetched >> shifter) & 0x01) << shifter); // Converts char data into unsigned int |
bala0x07 | 0:e36d80703ed0 | 64 | } |
bala0x07 | 0:e36d80703ed0 | 65 | } |
bala0x07 | 0:e36d80703ed0 | 66 | |
bala0x07 | 0:e36d80703ed0 | 67 | //---------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 68 | |
bala0x07 | 0:e36d80703ed0 | 69 | void print_data_bits(char data_fetched) |
bala0x07 | 0:e36d80703ed0 | 70 | { |
bala0x07 | 0:e36d80703ed0 | 71 | unsigned int shifter; |
bala0x07 | 0:e36d80703ed0 | 72 | |
bala0x07 | 0:e36d80703ed0 | 73 | for(shifter = 0; shifter < 8; shifter++) |
bala0x07 | 0:e36d80703ed0 | 74 | { |
bala0x07 | 0:e36d80703ed0 | 75 | pc.printf("%d",((data_fetched&0x80)>>7)); |
bala0x07 | 0:e36d80703ed0 | 76 | data_fetched = data_fetched << 1; |
bala0x07 | 0:e36d80703ed0 | 77 | } |
bala0x07 | 0:e36d80703ed0 | 78 | pc.printf("\r\n\r\n"); |
bala0x07 | 0:e36d80703ed0 | 79 | } |
bala0x07 | 0:e36d80703ed0 | 80 | |
bala0x07 | 0:e36d80703ed0 | 81 | //---------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 82 | |
bala0x07 | 0:e36d80703ed0 | 83 | /*---------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 84 | |
bala0x07 | 0:e36d80703ed0 | 85 | AS FAR NOW, ONE INTERRUPT PIN (INT1) OF ACCELEROMETER IS NOT WORKING |
bala0x07 | 0:e36d80703ed0 | 86 | USE THIS TO CUSTOMIZE ACCELEROMETER WHEN THAT PIN IS WORKING |
bala0x07 | 0:e36d80703ed0 | 87 | |
bala0x07 | 0:e36d80703ed0 | 88 | void use_me_later() { |
bala0x07 | 0:e36d80703ed0 | 89 | char count; |
bala0x07 | 0:e36d80703ed0 | 90 | for(count = 0; count < 10; count++) |
bala0x07 | 0:e36d80703ed0 | 91 | { |
bala0x07 | 0:e36d80703ed0 | 92 | led = !led; |
bala0x07 | 0:e36d80703ed0 | 93 | wait(0.10); |
bala0x07 | 0:e36d80703ed0 | 94 | } |
bala0x07 | 0:e36d80703ed0 | 95 | } |
bala0x07 | 0:e36d80703ed0 | 96 | ----------------------------------------------------------------------------------------------------------*/ |
bala0x07 | 0:e36d80703ed0 | 97 | |
bala0x07 | 0:e36d80703ed0 | 98 | void interrupt_sudden_jerk() |
bala0x07 | 0:e36d80703ed0 | 99 | { |
bala0x07 | 0:e36d80703ed0 | 100 | char count; |
bala0x07 | 0:e36d80703ed0 | 101 | |
bala0x07 | 0:e36d80703ed0 | 102 | i2c.write(slave_address_acc, all_interrupt_clear_command, 2); |
bala0x07 | 0:e36d80703ed0 | 103 | pc.printf("~~~ ENTERED SUDDEN JERK CONDITION ~~~\r\n\r\n"); |
bala0x07 | 0:e36d80703ed0 | 104 | |
bala0x07 | 0:e36d80703ed0 | 105 | for(count = 0; count < 2; count++) |
bala0x07 | 0:e36d80703ed0 | 106 | { |
bala0x07 | 0:e36d80703ed0 | 107 | led = 1; |
bala0x07 | 0:e36d80703ed0 | 108 | wait(2); |
bala0x07 | 0:e36d80703ed0 | 109 | led = 0; |
bala0x07 | 0:e36d80703ed0 | 110 | wait(1); |
bala0x07 | 0:e36d80703ed0 | 111 | } |
bala0x07 | 0:e36d80703ed0 | 112 | |
bala0x07 | 0:e36d80703ed0 | 113 | i2c.write(slave_address_acc, all_interrupt_enable_command, 2); |
bala0x07 | 0:e36d80703ed0 | 114 | |
bala0x07 | 0:e36d80703ed0 | 115 | } |
bala0x07 | 0:e36d80703ed0 | 116 | |
bala0x07 | 0:e36d80703ed0 | 117 | //********************************************************************************************************* |
bala0x07 | 0:e36d80703ed0 | 118 | |
bala0x07 | 0:e36d80703ed0 | 119 | // THE FOLLWOING CODE BLOCK IS THE MULITIPLEXED ISR FOR BOTH ACTIVITY & INACTIVITY INTERRUPT |
bala0x07 | 0:e36d80703ed0 | 120 | |
bala0x07 | 0:e36d80703ed0 | 121 | void interrupt_activity_inactivity() |
bala0x07 | 0:e36d80703ed0 | 122 | { |
bala0x07 | 0:e36d80703ed0 | 123 | char count; |
bala0x07 | 0:e36d80703ed0 | 124 | |
bala0x07 | 0:e36d80703ed0 | 125 | // The following statement disables all interrupts since no other interrupts must disturb at this point |
bala0x07 | 0:e36d80703ed0 | 126 | |
bala0x07 | 0:e36d80703ed0 | 127 | i2c.write(slave_address_acc, all_interrupt_clear_command, 2); |
bala0x07 | 0:e36d80703ed0 | 128 | |
bala0x07 | 0:e36d80703ed0 | 129 | i2c.write(slave_address_acc, intr_source_address, 1); |
bala0x07 | 0:e36d80703ed0 | 130 | i2c.read(slave_address_acc, interrupt_source, 1); |
bala0x07 | 0:e36d80703ed0 | 131 | |
bala0x07 | 0:e36d80703ed0 | 132 | char_to_int(interrupt_source[0]); // Coverts intr_source(char) to int & stores in intr_source_d |
bala0x07 | 0:e36d80703ed0 | 133 | |
bala0x07 | 0:e36d80703ed0 | 134 | pc.printf("INT Source = "); |
bala0x07 | 0:e36d80703ed0 | 135 | print_data_bits((interrupt_source_duplicate)); |
bala0x07 | 0:e36d80703ed0 | 136 | |
bala0x07 | 0:e36d80703ed0 | 137 | //-------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 138 | |
bala0x07 | 0:e36d80703ed0 | 139 | /* VERIFY WHETHER THE INTERRUPT IS BECAUSE OF ACTIVITY */ |
bala0x07 | 0:e36d80703ed0 | 140 | |
bala0x07 | 0:e36d80703ed0 | 141 | //if((((int)intr_source) & 0x10) == 0x10) |
bala0x07 | 0:e36d80703ed0 | 142 | if(interrupt_source_duplicate & 0x10) |
bala0x07 | 0:e36d80703ed0 | 143 | { |
bala0x07 | 0:e36d80703ed0 | 144 | /* THE FOLLOWING BLOCK IS USED JUST FOR VERIFICATION PURPOSE AND ARE NOT MANDATORY */ |
bala0x07 | 0:e36d80703ed0 | 145 | |
bala0x07 | 0:e36d80703ed0 | 146 | pc.printf("ENTERED ACTIVITY CONDITION\r\n\r\n"); |
bala0x07 | 0:e36d80703ed0 | 147 | for(count = 0; count < 10; count++) |
bala0x07 | 0:e36d80703ed0 | 148 | { |
bala0x07 | 0:e36d80703ed0 | 149 | led = !led; |
bala0x07 | 0:e36d80703ed0 | 150 | wait(0.05); |
bala0x07 | 0:e36d80703ed0 | 151 | } |
bala0x07 | 0:e36d80703ed0 | 152 | /* |
bala0x07 | 0:e36d80703ed0 | 153 | fetch_vehicle_speed(); |
bala0x07 | 0:e36d80703ed0 | 154 | previous_speed = vehicle_speed; |
bala0x07 | 0:e36d80703ed0 | 155 | wait(5); |
bala0x07 | 0:e36d80703ed0 | 156 | fetch_vehicle_speed(); |
bala0x07 | 0:e36d80703ed0 | 157 | current_speed = vehicle_speed; |
bala0x07 | 0:e36d80703ed0 | 158 | |
bala0x07 | 0:e36d80703ed0 | 159 | //if((current_speed > previous_speed) && (current_speed > speed_threshold)) // Decision making regarding vehicle's current state |
bala0x07 | 0:e36d80703ed0 | 160 | if(current_speed == 79) |
bala0x07 | 0:e36d80703ed0 | 161 | { |
bala0x07 | 0:e36d80703ed0 | 162 | i2c.write(slave_address_acc, activity_interrupt_disable_command, 2); // Disables Activity interrupt & enables Inactivity interrupt |
bala0x07 | 0:e36d80703ed0 | 163 | pc.printf("\r\n>>> VEHICLE HAS STARTED FROM STOP <<<"); |
bala0x07 | 0:e36d80703ed0 | 164 | } |
bala0x07 | 0:e36d80703ed0 | 165 | */ |
bala0x07 | 0:e36d80703ed0 | 166 | } |
bala0x07 | 0:e36d80703ed0 | 167 | |
bala0x07 | 0:e36d80703ed0 | 168 | //-------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 169 | |
bala0x07 | 0:e36d80703ed0 | 170 | /* VERIFY WHETHER THE INTERRUPT IS BECAUSE OF INACTIVITY */ |
bala0x07 | 0:e36d80703ed0 | 171 | |
bala0x07 | 0:e36d80703ed0 | 172 | //if((((int)intr_source) & 0x08) == 0x08) // Verify whether it is inactivity interrupt |
bala0x07 | 0:e36d80703ed0 | 173 | if(interrupt_source_duplicate & 0x08) |
bala0x07 | 0:e36d80703ed0 | 174 | { |
bala0x07 | 0:e36d80703ed0 | 175 | /* THE FOLLOWING BLOCK IS USED JUST FOR VERIFICATION PURPOSE AND ARE NOT MANDATORY */ |
bala0x07 | 0:e36d80703ed0 | 176 | |
bala0x07 | 0:e36d80703ed0 | 177 | pc.printf("ENTERED INACTIVITY CONDITION \r\n\r\n"); |
bala0x07 | 0:e36d80703ed0 | 178 | for(count = 0; count < 10; count++) |
bala0x07 | 0:e36d80703ed0 | 179 | { |
bala0x07 | 0:e36d80703ed0 | 180 | led = !led; |
bala0x07 | 0:e36d80703ed0 | 181 | wait(0.2); |
bala0x07 | 0:e36d80703ed0 | 182 | } |
bala0x07 | 0:e36d80703ed0 | 183 | /* |
bala0x07 | 0:e36d80703ed0 | 184 | fetch_vehicle_speed(); |
bala0x07 | 0:e36d80703ed0 | 185 | |
bala0x07 | 0:e36d80703ed0 | 186 | if(vehicle_speed == 0) // Decision making regarding vehicle's current state |
bala0x07 | 0:e36d80703ed0 | 187 | { |
bala0x07 | 0:e36d80703ed0 | 188 | i2c.write(slave_address_acc, inactivity_interrupt_disable_command, 2); // Disables Inactivity interrupt & enables Activity interrupt |
bala0x07 | 0:e36d80703ed0 | 189 | pc.printf("\r\n>>> VEHICLE HAS STOPPED FROM START <<<"); |
bala0x07 | 0:e36d80703ed0 | 190 | } |
bala0x07 | 0:e36d80703ed0 | 191 | */ |
bala0x07 | 0:e36d80703ed0 | 192 | } |
bala0x07 | 0:e36d80703ed0 | 193 | |
bala0x07 | 0:e36d80703ed0 | 194 | } |
bala0x07 | 0:e36d80703ed0 | 195 | |
bala0x07 | 0:e36d80703ed0 | 196 | //********************************************************************************************************* |
bala0x07 | 0:e36d80703ed0 | 197 | |
bala0x07 | 0:e36d80703ed0 | 198 | void initialize_accelerometer() |
bala0x07 | 0:e36d80703ed0 | 199 | { |
bala0x07 | 0:e36d80703ed0 | 200 | inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge |
bala0x07 | 0:e36d80703ed0 | 201 | double_tap.rise(interrupt_sudden_jerk); |
bala0x07 | 0:e36d80703ed0 | 202 | pc.baud(38400); |
bala0x07 | 0:e36d80703ed0 | 203 | |
bala0x07 | 0:e36d80703ed0 | 204 | char cmd[2], cmd2[2], cmd3[5], cmd4[8], cmd5[3], cmd6[2], cmd7[2], cmd8[2]; |
bala0x07 | 0:e36d80703ed0 | 205 | |
bala0x07 | 0:e36d80703ed0 | 206 | |
bala0x07 | 0:e36d80703ed0 | 207 | |
bala0x07 | 0:e36d80703ed0 | 208 | /* THE FOLLOWING GROUP OF COMMAND VARIABLES STORES THE CONFIGURATION VALUES TO BE WRITTEN TO THE ADXL345 ACCELEROMETER */ |
bala0x07 | 0:e36d80703ed0 | 209 | |
bala0x07 | 0:e36d80703ed0 | 210 | cmd[0] = 0x2D; // Post the Register address of the slave (Have to write this into slave) |
bala0x07 | 0:e36d80703ed0 | 211 | cmd[1] = 0x08; // Turn ON the Measure Bit |
bala0x07 | 0:e36d80703ed0 | 212 | |
bala0x07 | 0:e36d80703ed0 | 213 | cmd3[0] = 0x1D; // Threshold Tap Register address |
bala0x07 | 0:e36d80703ed0 | 214 | cmd3[1] = 100; // Threshold tap Register value |
bala0x07 | 0:e36d80703ed0 | 215 | cmd3[2] = 0x7F; // Offset - X axis |
bala0x07 | 0:e36d80703ed0 | 216 | cmd3[3] = 0x7F; // Offset - Y axis |
bala0x07 | 0:e36d80703ed0 | 217 | cmd3[4] = 0x05; // Offset - Z axis |
bala0x07 | 0:e36d80703ed0 | 218 | |
bala0x07 | 0:e36d80703ed0 | 219 | |
bala0x07 | 0:e36d80703ed0 | 220 | cmd4[0] = 0x21; // DUR Register address |
bala0x07 | 0:e36d80703ed0 | 221 | cmd4[1] = 0x15; // DUR Register value providing maximum time to be held to generate an interrupt |
bala0x07 | 0:e36d80703ed0 | 222 | cmd4[2] = 0x15; // Latent |
bala0x07 | 0:e36d80703ed0 | 223 | cmd4[3] = 0x45; // Window Time |
bala0x07 | 0:e36d80703ed0 | 224 | cmd4[4] = 64; // THRES_ACT register value 62.5mg/LSB , therfore value 32 indicates 2g activity |
bala0x07 | 0:e36d80703ed0 | 225 | cmd4[5] = 50; // THRES_INACT Register |
bala0x07 | 0:e36d80703ed0 | 226 | cmd4[6] = 5; // TIME_INACT Register, making inactivity detection time = 5 secs |
bala0x07 | 0:e36d80703ed0 | 227 | cmd4[7] = 0x77; // Activity, Inactivity detection enabled for all axis |
bala0x07 | 0:e36d80703ed0 | 228 | |
bala0x07 | 0:e36d80703ed0 | 229 | cmd5[0] = 0x2E; // INT Enable Register address |
bala0x07 | 0:e36d80703ed0 | 230 | //cmd5[1] = 0x74; // INT Enable Register value enabling Single Tap, Double Tap, Activity and Free Fall detection |
bala0x07 | 0:e36d80703ed0 | 231 | //cmd5[2] = 0x00; // INT Map Register value mapping Single Tap event to INT1 |
bala0x07 | 0:e36d80703ed0 | 232 | //cmd5[1] = 0x20; // Enabling only the double tap interrupt |
bala0x07 | 0:e36d80703ed0 | 233 | //cmd5[2] = 0x20; // Mapping the double tap interrupt to INT2 pin |
bala0x07 | 0:e36d80703ed0 | 234 | //cmd5[1] = 0x10; // Enabling only the activity interrupt |
bala0x07 | 0:e36d80703ed0 | 235 | //cmd5[2] = 0x10; // Mapping the sctivity interrupt to the INT2 pin |
bala0x07 | 0:e36d80703ed0 | 236 | //cmd5[1] = 0x08; // Enabling only the inactivity interrupt |
bala0x07 | 0:e36d80703ed0 | 237 | //cmd5[2] = 0x08; // Mapping the Inactivity interrupt to the INT2 pin |
bala0x07 | 0:e36d80703ed0 | 238 | cmd5[1] = 0x38; // Enabling Activity & inactivity interrupt |
bala0x07 | 0:e36d80703ed0 | 239 | cmd5[2] = 0xDF; // Activity--->INT1 & Inactivity--->INT2 |
bala0x07 | 0:e36d80703ed0 | 240 | |
bala0x07 | 0:e36d80703ed0 | 241 | |
bala0x07 | 0:e36d80703ed0 | 242 | cmd6[0] = 0x2A; // Address of the TAP_AXES Register |
bala0x07 | 0:e36d80703ed0 | 243 | cmd6[1] = 0x06; // X & Y axis participate in tap detection |
bala0x07 | 0:e36d80703ed0 | 244 | |
bala0x07 | 0:e36d80703ed0 | 245 | cmd7[0] = 0x28; // Address of the Threshold register for Free Fall detection |
bala0x07 | 0:e36d80703ed0 | 246 | cmd7[1] = 0x07; // Recommeded value : 0x05 to 0x09 Refer datasheet |
bala0x07 | 0:e36d80703ed0 | 247 | |
bala0x07 | 0:e36d80703ed0 | 248 | cmd8[0] = 0x2C; // Address of the BW RATE register |
bala0x07 | 0:e36d80703ed0 | 249 | cmd8[1] = 0x0D; // Increased the data rate to 800Hz, default is 0x0A indicating 100Hz |
bala0x07 | 0:e36d80703ed0 | 250 | |
bala0x07 | 0:e36d80703ed0 | 251 | cmd2[0] = 0x31; // Data format register address |
bala0x07 | 0:e36d80703ed0 | 252 | cmd2[1] = 0x04; // Making the acceleration data as left justified |
bala0x07 | 0:e36d80703ed0 | 253 | |
bala0x07 | 0:e36d80703ed0 | 254 | axis_data_start_address[0] = 0x32; |
bala0x07 | 0:e36d80703ed0 | 255 | |
bala0x07 | 0:e36d80703ed0 | 256 | |
bala0x07 | 0:e36d80703ed0 | 257 | |
bala0x07 | 0:e36d80703ed0 | 258 | i2c.write(slave_address_acc, cmd, 2); |
bala0x07 | 0:e36d80703ed0 | 259 | i2c.write(slave_address_acc, cmd3, 5); |
bala0x07 | 0:e36d80703ed0 | 260 | i2c.write(slave_address_acc, cmd4, 8); |
bala0x07 | 0:e36d80703ed0 | 261 | i2c.write(slave_address_acc, cmd5, 3); |
bala0x07 | 0:e36d80703ed0 | 262 | i2c.write(slave_address_acc, cmd6, 2); |
bala0x07 | 0:e36d80703ed0 | 263 | i2c.write(slave_address_acc, cmd7, 2); |
bala0x07 | 0:e36d80703ed0 | 264 | i2c.write(slave_address_acc, cmd8, 2); |
bala0x07 | 0:e36d80703ed0 | 265 | i2c.write(slave_address_acc, cmd2, 2); |
bala0x07 | 0:e36d80703ed0 | 266 | |
bala0x07 | 0:e36d80703ed0 | 267 | |
bala0x07 | 0:e36d80703ed0 | 268 | //char dev_add[2] = {0x00,0}; |
bala0x07 | 0:e36d80703ed0 | 269 | //i2c.write(slave_address_acc, dev_add, 1); |
bala0x07 | 0:e36d80703ed0 | 270 | //i2c.read(slave_address_acc, dev_add, 1); |
bala0x07 | 0:e36d80703ed0 | 271 | //print_data_bits(dev_add[0]); |
bala0x07 | 0:e36d80703ed0 | 272 | |
bala0x07 | 0:e36d80703ed0 | 273 | pc.printf(" ACCELEROMETER DATA LOG \r\n\r\n"); |
bala0x07 | 0:e36d80703ed0 | 274 | |
bala0x07 | 0:e36d80703ed0 | 275 | while (1); |
bala0x07 | 0:e36d80703ed0 | 276 | |
bala0x07 | 0:e36d80703ed0 | 277 | pc.printf("\r\n CAME HERE \r\n"); |
bala0x07 | 0:e36d80703ed0 | 278 | |
bala0x07 | 0:e36d80703ed0 | 279 | // pc.printf(" ACCELEROMETER DATA LOG \r\n\r\n"); |
bala0x07 | 0:e36d80703ed0 | 280 | |
bala0x07 | 0:e36d80703ed0 | 281 | // while (1) { |
bala0x07 | 0:e36d80703ed0 | 282 | /*-------------------------------------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 283 | wait(1.0); |
bala0x07 | 0:e36d80703ed0 | 284 | pc.printf("STILL IN WHILE LOOP\r\n\r\n"); |
bala0x07 | 0:e36d80703ed0 | 285 | --------------------------------------------------------------------------------------------------------------------------------------*/ |
bala0x07 | 0:e36d80703ed0 | 286 | // wait(0.25); |
bala0x07 | 0:e36d80703ed0 | 287 | //-------------------------------------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 288 | |
bala0x07 | 0:e36d80703ed0 | 289 | // USE THE FOLLOWING BLOCK TO READ THE DATA IN X-AXIS, Y-AXIS & Z-AXIS |
bala0x07 | 0:e36d80703ed0 | 290 | /* |
bala0x07 | 0:e36d80703ed0 | 291 | i2c.write(slave_address_acc, axis_data_start_address, 1); |
bala0x07 | 0:e36d80703ed0 | 292 | i2c.read(slave_address_acc, axis_data, 6); |
bala0x07 | 0:e36d80703ed0 | 293 | |
bala0x07 | 0:e36d80703ed0 | 294 | x_axis = axis_data[1]; // Puts MSB data into respective axes |
bala0x07 | 0:e36d80703ed0 | 295 | y_axis = axis_data[3]; |
bala0x07 | 0:e36d80703ed0 | 296 | z_axis = axis_data[5]; |
bala0x07 | 0:e36d80703ed0 | 297 | |
bala0x07 | 0:e36d80703ed0 | 298 | if(x_axis & 0x80) // Testing the signess of the x-axis data |
bala0x07 | 0:e36d80703ed0 | 299 | pc.printf("X-axis_1 = %d\r\n", (((~x_axis)+1))); // Converts 2's complement data into decimal |
bala0x07 | 0:e36d80703ed0 | 300 | else |
bala0x07 | 0:e36d80703ed0 | 301 | pc.printf("X-axis_0 = %d\r\n", x_axis); |
bala0x07 | 0:e36d80703ed0 | 302 | |
bala0x07 | 0:e36d80703ed0 | 303 | if(y_axis & 0x80) // Testing the signess of the y-axis data |
bala0x07 | 0:e36d80703ed0 | 304 | pc.printf("Y-axis_1 = %d\r\n", (((~y_axis)+1))); // Converts 2's complement data into decimal |
bala0x07 | 0:e36d80703ed0 | 305 | else |
bala0x07 | 0:e36d80703ed0 | 306 | pc.printf("Y-axis_0 = %d\r\n", y_axis); |
bala0x07 | 0:e36d80703ed0 | 307 | |
bala0x07 | 0:e36d80703ed0 | 308 | if(z_axis & 0x80) // Testing the signess of the y-axis data |
bala0x07 | 0:e36d80703ed0 | 309 | pc.printf("Z-axis_1 = %d\r\n\r\n", (((~z_axis)+1))); // Converts 2's complement data into decimal |
bala0x07 | 0:e36d80703ed0 | 310 | else |
bala0x07 | 0:e36d80703ed0 | 311 | pc.printf("Z-axis_0 = %d\r\n\r\n",z_axis); |
bala0x07 | 0:e36d80703ed0 | 312 | */ |
bala0x07 | 0:e36d80703ed0 | 313 | /*-------------------------------------------------------------------------------------------------------------------------------------- |
bala0x07 | 0:e36d80703ed0 | 314 | |
bala0x07 | 0:e36d80703ed0 | 315 | // THIS CAN BE USED WHEN THERE IS A NEED OF VERY HIGH LEVEL ACCURACY & USE INT16_T DATA TYPE |
bala0x07 | 0:e36d80703ed0 | 316 | |
bala0x07 | 0:e36d80703ed0 | 317 | x_axis = (int)axis_data[1] << 8 | (int)axis_data[0]; |
bala0x07 | 0:e36d80703ed0 | 318 | y_axis = (int)axis_data[3] << 8 | (int)axis_data[2]; |
bala0x07 | 0:e36d80703ed0 | 319 | z_axis = (int)axis_data[5] << 8 | (int)axis_data[4]; |
bala0x07 | 0:e36d80703ed0 | 320 | |
bala0x07 | 0:e36d80703ed0 | 321 | --------------------------------------------------------------------------------------------------------------------------------------*/ |
bala0x07 | 0:e36d80703ed0 | 322 | // } |
bala0x07 | 0:e36d80703ed0 | 323 | } |
bala0x07 | 0:e36d80703ed0 | 324 | |
bala0x07 | 0:e36d80703ed0 | 325 |