Qmax / Mbed 2 deprecated LIS_Accelerometer_WIP

Dependencies:   mbed

Committer:
bala0x07
Date:
Thu Aug 24 08:54:43 2017 +0000
Revision:
0:491bc4ee502e
Child:
1:ecd469d7323d
Child:
2:c4fb968de7d3
ACC_24_8_17

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bala0x07 0:491bc4ee502e 1
bala0x07 0:491bc4ee502e 2 /*
bala0x07 0:491bc4ee502e 3 _____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 4 _____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 5
bala0x07 0:491bc4ee502e 6 // ACCELEROMETER : LIS2DE12 //
bala0x07 0:491bc4ee502e 7 _____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 8 _____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 9
bala0x07 0:491bc4ee502e 10
bala0x07 0:491bc4ee502e 11 >>>>>>>>>>>>>>>>>>>>>>>>>>>>> THIS CODE IS SUBJECTED TO MODIFICATION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
bala0x07 0:491bc4ee502e 12
bala0x07 0:491bc4ee502e 13 Updates :
bala0x07 0:491bc4ee502e 14 --------
bala0x07 0:491bc4ee502e 15 >>> Communication established with SA0 HIGH with internal pullup (Hence Slave Address : 0x32) //
bala0x07 0:491bc4ee502e 16 >>> I2C Communciation verified
bala0x07 0:491bc4ee502e 17 >>> Interrupt trigger verified for INT1 (configurent for low event) but later trigger not working
bala0x07 0:491bc4ee502e 18 >>> Interrupt source register was read successfully
bala0x07 0:491bc4ee502e 19 >>> Interrupt source register reflects the immediate changes of the High and Low event of the axes
bala0x07 0:491bc4ee502e 20 >>> IA bits goes high only when an interrupt is triggered
bala0x07 0:491bc4ee502e 21 >>> 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
bala0x07 0:491bc4ee502e 22 >>> INTERRUPT SOURCE REGISTER 2 has a;so been checked and the corressponding event data has been reflected in that
bala0x07 0:491bc4ee502e 23 >>> INTERRUPT ACTIVE nit is not getting set int the INTERRUPT SRC 2 REG, and have to be resolved
bala0x07 0:491bc4ee502e 24 >>> INT2 pin toggling verified
bala0x07 0:491bc4ee502e 25
bala0x07 0:491bc4ee502e 26 Issues to be Resolved :
bala0x07 0:491bc4ee502e 27 ----------------------
bala0x07 0:491bc4ee502e 28 >>> Acceleration data is not changing and always read as '0'
bala0x07 0:491bc4ee502e 29 >>> If the INT_SRC register is not continuosly read, after the interrupt, reading INT_SRC inside the ISR returns 0x00 as value
bala0x07 0:491bc4ee502e 30 >>> If the INT_SRC register is continuosly read inside a while loop, reading the INT_SRC inside the ISR returns correct value
bala0x07 0:491bc4ee502e 31
bala0x07 0:491bc4ee502e 32 To dos:
bala0x07 0:491bc4ee502e 33 ------
bala0x07 0:491bc4ee502e 34 >>> ACTIVITY & INACTIVITY Interrupts to be configured on INT1 pin
bala0x07 0:491bc4ee502e 35 >>> CLICK & DOUBLE CLICK Interrupt to be configured on INT2 pin
bala0x07 0:491bc4ee502e 36
bala0x07 0:491bc4ee502e 37 NOTE :
bala0x07 0:491bc4ee502e 38 -----
bala0x07 0:491bc4ee502e 39 Leave the SA0 pin as it is and fix the slave address 32h
bala0x07 0:491bc4ee502e 40 __________29/7/17
bala0x07 0:491bc4ee502e 41 now via interrupt we can read SRC
bala0x07 0:491bc4ee502e 42
bala0x07 0:491bc4ee502e 43 _____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 44
bala0x07 0:491bc4ee502e 45 LAST UPDATED : 15-JUN-2017 (WEDNESDAY)
bala0x07 0:491bc4ee502e 46
bala0x07 0:491bc4ee502e 47 ____________________
bala0x07 0:491bc4ee502e 48
bala0x07 0:491bc4ee502e 49 Author : >> BALA <<
bala0x07 0:491bc4ee502e 50 ____________________
bala0x07 0:491bc4ee502e 51 _____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 52
bala0x07 0:491bc4ee502e 53 */
bala0x07 0:491bc4ee502e 54
bala0x07 0:491bc4ee502e 55 /*
bala0x07 0:491bc4ee502e 56 *****************************************************************************************************
bala0x07 0:491bc4ee502e 57 REFRENCES :
bala0x07 0:491bc4ee502e 58 ----------
bala0x07 0:491bc4ee502e 59 >>> SLAVE ADDRESS : 1001 A2 A1 A0 R/W : Page : 7 (Datasheet)
bala0x07 0:491bc4ee502e 60 >>> TEMPERATURE OVERSHOOT SHUTDOWN REG CONFIGURATION : Page : 10 (Datasheet)
bala0x07 0:491bc4ee502e 61
bala0x07 0:491bc4ee502e 62 *****************************************************************************************************
bala0x07 0:491bc4ee502e 63 */
bala0x07 0:491bc4ee502e 64
bala0x07 0:491bc4ee502e 65
bala0x07 0:491bc4ee502e 66 #include "mbed.h"
bala0x07 0:491bc4ee502e 67 #include "main.h"
bala0x07 0:491bc4ee502e 68
bala0x07 0:491bc4ee502e 69
bala0x07 0:491bc4ee502e 70 I2C i2c(PB_14, PB_13);
bala0x07 0:491bc4ee502e 71 Serial pc(PC_0, NC,115200);
bala0x07 0:491bc4ee502e 72 InterruptIn change(PC_6); // INT1
bala0x07 0:491bc4ee502e 73 InterruptIn jerk(PB_15); //INT2
bala0x07 0:491bc4ee502e 74 DigitalOut ACC_LED(PC_10);
bala0x07 0:491bc4ee502e 75
bala0x07 0:491bc4ee502e 76 #define acc_address 0x32
bala0x07 0:491bc4ee502e 77
bala0x07 0:491bc4ee502e 78 int i=0,j=0,Steady_cnt=0;
bala0x07 0:491bc4ee502e 79 float TIMING=0;
bala0x07 0:491bc4ee502e 80 char src_data[1];
bala0x07 0:491bc4ee502e 81 unsigned int int1_src_data,cnt=0;
bala0x07 0:491bc4ee502e 82 char acc_reg[1] = {0x29};
bala0x07 0:491bc4ee502e 83 char acc_data[6];
bala0x07 0:491bc4ee502e 84 unsigned int x, y, z;
bala0x07 0:491bc4ee502e 85 float X, Y, Z;
bala0x07 0:491bc4ee502e 86 char int1_dur[2] = {0x33, 70};
bala0x07 0:491bc4ee502e 87 char JERK_FLAG=0,MOVEMENT_FLAG=0,MOTION=0;
bala0x07 0:491bc4ee502e 88 //char first_event = 1;
bala0x07 0:491bc4ee502e 89
bala0x07 0:491bc4ee502e 90 //____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 91
bala0x07 0:491bc4ee502e 92
bala0x07 0:491bc4ee502e 93
bala0x07 0:491bc4ee502e 94 //____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 95
bala0x07 0:491bc4ee502e 96 void movement_inertia()
bala0x07 0:491bc4ee502e 97 {
bala0x07 0:491bc4ee502e 98
bala0x07 0:491bc4ee502e 99
bala0x07 0:491bc4ee502e 100 //-------------------------------------------------------------------------------
bala0x07 0:491bc4ee502e 101 // pc.printf("\r\n\r\nINTERRUPT SOURCE REGISTER 1: ");
bala0x07 0:491bc4ee502e 102 // print_data_bits(src_data[0]);
bala0x07 0:491bc4ee502e 103
bala0x07 0:491bc4ee502e 104 MOVEMENT_FLAG=1;
bala0x07 0:491bc4ee502e 105
bala0x07 0:491bc4ee502e 106
bala0x07 0:491bc4ee502e 107 }
bala0x07 0:491bc4ee502e 108
bala0x07 0:491bc4ee502e 109 //____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 110
bala0x07 0:491bc4ee502e 111 void sudden_jerk()
bala0x07 0:491bc4ee502e 112 {
bala0x07 0:491bc4ee502e 113 JERK_FLAG=1;
bala0x07 0:491bc4ee502e 114 }
bala0x07 0:491bc4ee502e 115
bala0x07 0:491bc4ee502e 116 //____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 117
bala0x07 0:491bc4ee502e 118
bala0x07 0:491bc4ee502e 119 void print_data_bits(char data_fetched)
bala0x07 0:491bc4ee502e 120 {
bala0x07 0:491bc4ee502e 121 unsigned int shifter;
bala0x07 0:491bc4ee502e 122
bala0x07 0:491bc4ee502e 123 for(shifter = 0; shifter < 8; shifter++)
bala0x07 0:491bc4ee502e 124 {
bala0x07 0:491bc4ee502e 125 pc.printf("%d",((data_fetched&0x80)>>7));
bala0x07 0:491bc4ee502e 126 data_fetched = data_fetched << 1;
bala0x07 0:491bc4ee502e 127 }
bala0x07 0:491bc4ee502e 128 pc.printf("\r\n");
bala0x07 0:491bc4ee502e 129 }
bala0x07 0:491bc4ee502e 130
bala0x07 0:491bc4ee502e 131 //____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 132
bala0x07 0:491bc4ee502e 133 void read_acceleration()
bala0x07 0:491bc4ee502e 134 {
bala0x07 0:491bc4ee502e 135
bala0x07 0:491bc4ee502e 136 i2c.write(acc_address, acc_reg, 1);
bala0x07 0:491bc4ee502e 137 i2c.read(acc_address, acc_data, 6);
bala0x07 0:491bc4ee502e 138
bala0x07 0:491bc4ee502e 139 x = (acc_data[0] << 6) | (acc_data[1]);
bala0x07 0:491bc4ee502e 140 y = (acc_data[2] << 6) | (acc_data[3]);
bala0x07 0:491bc4ee502e 141 z = (acc_data[4] << 6) | (acc_data[5]);
bala0x07 0:491bc4ee502e 142
bala0x07 0:491bc4ee502e 143 // x = acc_data[2];
bala0x07 0:491bc4ee502e 144 // y = acc_data[4];
bala0x07 0:491bc4ee502e 145 // z = acc_data[6];
bala0x07 0:491bc4ee502e 146
bala0x07 0:491bc4ee502e 147 X = x;
bala0x07 0:491bc4ee502e 148 Y = y;
bala0x07 0:491bc4ee502e 149 Z = z;
bala0x07 0:491bc4ee502e 150
bala0x07 0:491bc4ee502e 151
bala0x07 0:491bc4ee502e 152 // X = x / 15974.4;
bala0x07 0:491bc4ee502e 153 // Y = y / 15974.4;
bala0x07 0:491bc4ee502e 154 // Z = z / 15974.4;
bala0x07 0:491bc4ee502e 155
bala0x07 0:491bc4ee502e 156
bala0x07 0:491bc4ee502e 157
bala0x07 0:491bc4ee502e 158 //-------------------------------------------------------------------------------
bala0x07 0:491bc4ee502e 159
bala0x07 0:491bc4ee502e 160 // i2c.write(acc_address, int1_src, 1);
bala0x07 0:491bc4ee502e 161 // i2c.read(acc_address, src_data, 1);
bala0x07 0:491bc4ee502e 162 //
bala0x07 0:491bc4ee502e 163 // int1_src_data = (unsigned int)src_data[0];
bala0x07 0:491bc4ee502e 164
bala0x07 0:491bc4ee502e 165 // pc.printf("\r\nINTERRUPT SOURCE 2 : ");
bala0x07 0:491bc4ee502e 166 // print_data_bits(src_data[0]);
bala0x07 0:491bc4ee502e 167
bala0x07 0:491bc4ee502e 168
bala0x07 0:491bc4ee502e 169 // if(!(int1_src_data & 0x40) && first_event)
bala0x07 0:491bc4ee502e 170 // {
bala0x07 0:491bc4ee502e 171 // first_event = 0;
bala0x07 0:491bc4ee502e 172 // pc.printf("\r\n\r\n>>> MOTION OBSERVED");
bala0x07 0:491bc4ee502e 173 // }
bala0x07 0:491bc4ee502e 174 //-------------------------------------------------------------------------------
bala0x07 0:491bc4ee502e 175 }
bala0x07 0:491bc4ee502e 176
bala0x07 0:491bc4ee502e 177
bala0x07 0:491bc4ee502e 178 //____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 179
bala0x07 0:491bc4ee502e 180 void initialize_internal_temperature_sensor()
bala0x07 0:491bc4ee502e 181 {
bala0x07 0:491bc4ee502e 182 char temperature_config_1[2] = {0x1F, 12};
bala0x07 0:491bc4ee502e 183 char temperature_config_2[2] = {0x23, 0x80};
bala0x07 0:491bc4ee502e 184 char temperature_reg[1] = {0x0C};
bala0x07 0:491bc4ee502e 185 char temp_value[2];
bala0x07 0:491bc4ee502e 186 float temperature;
bala0x07 0:491bc4ee502e 187
bala0x07 0:491bc4ee502e 188 i2c.write(acc_address, temperature_config_1, 2); // Internal Temperature Sensor is enabled
bala0x07 0:491bc4ee502e 189 i2c.write(acc_address, temperature_config_2, 2);
bala0x07 0:491bc4ee502e 190
bala0x07 0:491bc4ee502e 191 i2c.write(acc_address, temperature_reg, 1);
bala0x07 0:491bc4ee502e 192 i2c.read(acc_address, temp_value, 2);
bala0x07 0:491bc4ee502e 193
bala0x07 0:491bc4ee502e 194 temperature = ((temp_value[1] << 8) | (temp_value[0]));
bala0x07 0:491bc4ee502e 195
bala0x07 0:491bc4ee502e 196 pc.printf("\r\nTEMPERATURE : %d deg celsius", temperature);
bala0x07 0:491bc4ee502e 197 }
bala0x07 0:491bc4ee502e 198
bala0x07 0:491bc4ee502e 199 //____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 200
bala0x07 0:491bc4ee502e 201
bala0x07 0:491bc4ee502e 202 void configure_accelerometer()
bala0x07 0:491bc4ee502e 203 {
bala0x07 0:491bc4ee502e 204 /*
bala0x07 0:491bc4ee502e 205 char ctrl_reg_0[2] = {0x1E, 0x10}; // SA0 Internal pullup Enabled
bala0x07 0:491bc4ee502e 206 char ctrl_reg_1[2] = {0x20, 0x2F}; // ODR -> 10Hz, X,Y,Z axis Enabled
bala0x07 0:491bc4ee502e 207 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
bala0x07 0:491bc4ee502e 208 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.....
bala0x07 0:491bc4ee502e 209 char ctrl_reg_4[2] = {0x23, 0x80}; // BDU enabled, 2g full scale selection, Self test diabled
bala0x07 0:491bc4ee502e 210 char ctrl_reg_5[2] = {0x24, 0x40}; // Reboot memory content disabled, FIFO enabled, Interrupt request not latched on INT1 & INT2, 4D detection disbaled
bala0x07 0:491bc4ee502e 211 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
bala0x07 0:491bc4ee502e 212 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)
bala0x07 0:491bc4ee502e 213 char int1_cfg[2] = {0x30, 0x1F}; // OR combination of interrupts, Interrupts enabled for all X/Y/Z high and low except Z-high event
bala0x07 0:491bc4ee502e 214 char int1_ths[2] = {0x32, 7}; // For 2g Full scale, 1LSB = 16mg, Aim : Threshold : 100mg, 100/16 = 6.25, rounded to '7'
bala0x07 0:491bc4ee502e 215 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)
bala0x07 0:491bc4ee502e 216
bala0x07 0:491bc4ee502e 217 i2c.write(acc_address, ctrl_reg_0, 2);
bala0x07 0:491bc4ee502e 218 i2c.write(acc_address, ctrl_reg_1, 2);
bala0x07 0:491bc4ee502e 219 i2c.write(acc_address, ctrl_reg_2, 2);
bala0x07 0:491bc4ee502e 220 i2c.write(acc_address, ctrl_reg_3, 2);
bala0x07 0:491bc4ee502e 221 i2c.write(acc_address, ctrl_reg_4, 2);
bala0x07 0:491bc4ee502e 222 i2c.write(acc_address, ctrl_reg_5, 2);
bala0x07 0:491bc4ee502e 223 i2c.write(acc_address, ctrl_reg_6, 2);
bala0x07 0:491bc4ee502e 224 i2c.write(acc_address, fifo_ctrl_reg, 2);
bala0x07 0:491bc4ee502e 225 i2c.write(acc_address, int1_cfg, 2);
bala0x07 0:491bc4ee502e 226 i2c.write(acc_address, int1_ths, 2);
bala0x07 0:491bc4ee502e 227 i2c.write(acc_address, int1_dur, 2);
bala0x07 0:491bc4ee502e 228
bala0x07 0:491bc4ee502e 229 char ver_data[1];
bala0x07 0:491bc4ee502e 230 i2c.write(acc_address, ctrl_reg_1, 1);
bala0x07 0:491bc4ee502e 231 i2c.read(acc_address, ver_data, 1);
bala0x07 0:491bc4ee502e 232 pc.printf("\r\n\r\n Verification data : ");
bala0x07 0:491bc4ee502e 233 print_data_bits(ver_data[0]);
bala0x07 0:491bc4ee502e 234 */
bala0x07 0:491bc4ee502e 235 ///*
bala0x07 0:491bc4ee502e 236 char ctrl_reg_0[2] = {0x1E, 0x10}; //added additionally this line
bala0x07 0:491bc4ee502e 237 char ctrl_reg_1[2] = {0x20, 0x27}; // Modified point (worked) changed from 0x27
bala0x07 0:491bc4ee502e 238 char ctrl_reg_2[2] = {0x21, 0x03}; // Modified to Auto Reset on Interrupt event CHANGE FROM 0X01
bala0x07 0:491bc4ee502e 239 char ctrl_reg_3[2] = {0x22, 0x40}; // INT1 on INT1,also attach CLK_INT //#C0
bala0x07 0:491bc4ee502e 240 char ctrl_reg_4[2] = {0x23, 0x80}; // Modified changed from 0x08 now block update
bala0x07 0:491bc4ee502e 241 char ctrl_reg_5[2] = {0x24, 0x00}; // Modified to FIFO Enabled changed from 00
bala0x07 0:491bc4ee502e 242 char ctrl_reg_6[2] = {0x25, 0x28}; // IA2 Interrupt on INT2 pin & activity also on INT2 //activityy int also enabled
bala0x07 0:491bc4ee502e 243 //fifo bypass mode
bala0x07 0:491bc4ee502e 244 /////////char fifo_ctrl_reg[2] = {0x2E, 40};
bala0x07 0:491bc4ee502e 245
bala0x07 0:491bc4ee502e 246 char int1_cfg[2] = {0x30, 0x43}; // 6D direction detection enabled ,XH & XL enabled
bala0x07 0:491bc4ee502e 247 char int1_ths[2] = {0x32, 0x01}; //INT1 threshold value
bala0x07 0:491bc4ee502e 248 char int1_dur[2] = {0x33, 0x00};
bala0x07 0:491bc4ee502e 249
bala0x07 0:491bc4ee502e 250 char int2_cfg[2] = {0x34, 0x3F}; //INT2 is Enabled when XHIE
bala0x07 0:491bc4ee502e 251 char int2_ths[2] = {0x36, 0x02}; //changed from A8 to 28
bala0x07 0:491bc4ee502e 252 char int2_dur[2] = {0x37, 0x01};
bala0x07 0:491bc4ee502e 253
bala0x07 0:491bc4ee502e 254 char act_ths[2] = {0x3E, 00};
bala0x07 0:491bc4ee502e 255 char act_dur[2] = {0x3F, 00};
bala0x07 0:491bc4ee502e 256
bala0x07 0:491bc4ee502e 257 //32 33 30
bala0x07 0:491bc4ee502e 258 //36 37 34
bala0x07 0:491bc4ee502e 259 i2c.write(acc_address, ctrl_reg_0, 2);
bala0x07 0:491bc4ee502e 260 i2c.write(acc_address, ctrl_reg_1, 2);
bala0x07 0:491bc4ee502e 261 i2c.write(acc_address, ctrl_reg_2, 2);
bala0x07 0:491bc4ee502e 262 i2c.write(acc_address, ctrl_reg_3, 2);
bala0x07 0:491bc4ee502e 263 i2c.write(acc_address, ctrl_reg_4, 2);
bala0x07 0:491bc4ee502e 264 i2c.write(acc_address, ctrl_reg_5, 2);
bala0x07 0:491bc4ee502e 265 i2c.write(acc_address, ctrl_reg_6, 2);
bala0x07 0:491bc4ee502e 266
bala0x07 0:491bc4ee502e 267 i2c.write(acc_address, int1_cfg, 2);
bala0x07 0:491bc4ee502e 268 i2c.write(acc_address, int1_ths, 2);
bala0x07 0:491bc4ee502e 269 i2c.write(acc_address, int1_dur, 2);
bala0x07 0:491bc4ee502e 270
bala0x07 0:491bc4ee502e 271
bala0x07 0:491bc4ee502e 272 i2c.write(acc_address, int2_ths, 2);
bala0x07 0:491bc4ee502e 273 i2c.write(acc_address, int2_dur, 2);
bala0x07 0:491bc4ee502e 274 i2c.write(acc_address, int2_cfg, 2);
bala0x07 0:491bc4ee502e 275
bala0x07 0:491bc4ee502e 276 i2c.write(acc_address, act_ths, 2);
bala0x07 0:491bc4ee502e 277 i2c.write(acc_address, act_dur, 2);
bala0x07 0:491bc4ee502e 278 i2c.write(acc_address, int1_dur, 2);
bala0x07 0:491bc4ee502e 279 //////i2c.write(acc_address, fifo_ctrl_reg, 2);
bala0x07 0:491bc4ee502e 280 //*/
bala0x07 0:491bc4ee502e 281 }
bala0x07 0:491bc4ee502e 282
bala0x07 0:491bc4ee502e 283
bala0x07 0:491bc4ee502e 284 //____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 285
bala0x07 0:491bc4ee502e 286 char initialize_accelerometer()
bala0x07 0:491bc4ee502e 287 {
bala0x07 0:491bc4ee502e 288 char dev_id_address[2] = {0x0F};
bala0x07 0:491bc4ee502e 289 char dev_id[1];
bala0x07 0:491bc4ee502e 290
bala0x07 0:491bc4ee502e 291 //SA0 = 0; // I2C LSB Address
bala0x07 0:491bc4ee502e 292
bala0x07 0:491bc4ee502e 293 i2c.write(acc_address, dev_id_address, 1);
bala0x07 0:491bc4ee502e 294 wait(0.1);
bala0x07 0:491bc4ee502e 295 i2c.read(acc_address, dev_id, 1);
bala0x07 0:491bc4ee502e 296 wait(2);
bala0x07 0:491bc4ee502e 297
bala0x07 0:491bc4ee502e 298 pc.printf("\r\n\r\nDEVICE ID : ");
bala0x07 0:491bc4ee502e 299 print_data_bits(dev_id[0]);
bala0x07 0:491bc4ee502e 300
bala0x07 0:491bc4ee502e 301 if(dev_id[0] == 0x33)//00110011(who am i)
bala0x07 0:491bc4ee502e 302 {
bala0x07 0:491bc4ee502e 303 pc.printf("\r\nAccelerometer Initialized Successfully");
bala0x07 0:491bc4ee502e 304 return 1;
bala0x07 0:491bc4ee502e 305 }
bala0x07 0:491bc4ee502e 306 else
bala0x07 0:491bc4ee502e 307 {
bala0x07 0:491bc4ee502e 308 pc.printf("\r\nAccelerometer Initialization Failed..... Can't communicate with the device");
bala0x07 0:491bc4ee502e 309 return 0;
bala0x07 0:491bc4ee502e 310 }
bala0x07 0:491bc4ee502e 311 }
bala0x07 0:491bc4ee502e 312
bala0x07 0:491bc4ee502e 313 //____________________________________________________________________________________________________
bala0x07 0:491bc4ee502e 314 Timer t;
bala0x07 0:491bc4ee502e 315 int main()
bala0x07 0:491bc4ee502e 316 {
bala0x07 0:491bc4ee502e 317 char retry_attempt = 0;
bala0x07 0:491bc4ee502e 318
bala0x07 0:491bc4ee502e 319 change.rise(&movement_inertia);
bala0x07 0:491bc4ee502e 320 jerk.rise(&sudden_jerk);
bala0x07 0:491bc4ee502e 321
bala0x07 0:491bc4ee502e 322 i2c.frequency(100000);
bala0x07 0:491bc4ee502e 323 //SA0 = 0;
bala0x07 0:491bc4ee502e 324 // i2c.write(acc_address, int1_dur, 2);
bala0x07 0:491bc4ee502e 325 pc.printf("\r\n\r\n\r\n>>> ACCELEROMETER UNDER TEST <<<");
bala0x07 0:491bc4ee502e 326
bala0x07 0:491bc4ee502e 327 retry:
bala0x07 0:491bc4ee502e 328 if(initialize_accelerometer())
bala0x07 0:491bc4ee502e 329 {
bala0x07 0:491bc4ee502e 330 configure_accelerometer();
bala0x07 0:491bc4ee502e 331
bala0x07 0:491bc4ee502e 332 while(1)
bala0x07 0:491bc4ee502e 333 {
bala0x07 0:491bc4ee502e 334 //read_acceleration();
bala0x07 0:491bc4ee502e 335 //pc.printf("\r\n\r\nX : %d", x);
bala0x07 0:491bc4ee502e 336 //pc.printf("\r\nY : %d", y);
bala0x07 0:491bc4ee502e 337 // pc.printf("\r\nZ : %d", z);
bala0x07 0:491bc4ee502e 338 //movement_inertia();
bala0x07 0:491bc4ee502e 339 //sudden_jerk();
bala0x07 0:491bc4ee502e 340
bala0x07 0:491bc4ee502e 341 // pc.printf("\n(%d)",cnt);
bala0x07 0:491bc4ee502e 342 // cnt++;
bala0x07 0:491bc4ee502e 343 // if(cnt>100)
bala0x07 0:491bc4ee502e 344 // cnt=0;
bala0x07 0:491bc4ee502e 345
bala0x07 0:491bc4ee502e 346
bala0x07 0:491bc4ee502e 347 //t.stop();
bala0x07 0:491bc4ee502e 348 TIMING=t.read();
bala0x07 0:491bc4ee502e 349 pc.printf("The time taken was %f seconds\n", t.read());
bala0x07 0:491bc4ee502e 350 if(TIMING>3)
bala0x07 0:491bc4ee502e 351 {
bala0x07 0:491bc4ee502e 352 pc.printf("MOOOVINGGGG....");
bala0x07 0:491bc4ee502e 353 MOTION=1;
bala0x07 0:491bc4ee502e 354 //TIMING=0;
bala0x07 0:491bc4ee502e 355 //t.stop();
bala0x07 0:491bc4ee502e 356 /// t.reset();
bala0x07 0:491bc4ee502e 357 ACC_LED=1;
bala0x07 0:491bc4ee502e 358 Steady_cnt=0;
bala0x07 0:491bc4ee502e 359 }
bala0x07 0:491bc4ee502e 360 else
bala0x07 0:491bc4ee502e 361 {
bala0x07 0:491bc4ee502e 362 if(Steady_cnt>3)
bala0x07 0:491bc4ee502e 363 {
bala0x07 0:491bc4ee502e 364 ACC_LED=0;
bala0x07 0:491bc4ee502e 365 Steady_cnt=0;
bala0x07 0:491bc4ee502e 366 pc.printf("REST....");
bala0x07 0:491bc4ee502e 367 MOTION=0;
bala0x07 0:491bc4ee502e 368 }
bala0x07 0:491bc4ee502e 369 }
bala0x07 0:491bc4ee502e 370 wait(1);
bala0x07 0:491bc4ee502e 371 // if(JERK_FLAG)
bala0x07 0:491bc4ee502e 372 // {
bala0x07 0:491bc4ee502e 373 // JERK_FLAG=0;
bala0x07 0:491bc4ee502e 374 // i2c.write(acc_address, int2_src, 1);
bala0x07 0:491bc4ee502e 375 // i2c.read(acc_address, src_data, 1);
bala0x07 0:491bc4ee502e 376 // pc.printf("\rSRC2 : ");
bala0x07 0:491bc4ee502e 377 // print_data_bits(src_data[0]);
bala0x07 0:491bc4ee502e 378 // src_data[0]=src_data[0]&0x42;
bala0x07 0:491bc4ee502e 379 // // if( src_data[0]==0x42)
bala0x07 0:491bc4ee502e 380 // {
bala0x07 0:491bc4ee502e 381 // pc.printf(" **********SUDDEN JERK********** ");
bala0x07 0:491bc4ee502e 382 // }
bala0x07 0:491bc4ee502e 383 //
bala0x07 0:491bc4ee502e 384 // }
bala0x07 0:491bc4ee502e 385
bala0x07 0:491bc4ee502e 386 if(MOVEMENT_FLAG)
bala0x07 0:491bc4ee502e 387 {
bala0x07 0:491bc4ee502e 388 Steady_cnt++;
bala0x07 0:491bc4ee502e 389 MOVEMENT_FLAG=0;
bala0x07 0:491bc4ee502e 390 t.reset();
bala0x07 0:491bc4ee502e 391 t.start();
bala0x07 0:491bc4ee502e 392
bala0x07 0:491bc4ee502e 393
bala0x07 0:491bc4ee502e 394 ///////////////////////////////////////////////////
bala0x07 0:491bc4ee502e 395 // i2c.write(acc_address, int1_src, 1);//acc_address=0x32 ..INITIALISE COMMN
bala0x07 0:491bc4ee502e 396 // i2c.read(acc_address, src_data, 1);//READING FROM INT1 STATUS REG
bala0x07 0:491bc4ee502e 397 // pc.printf("\rSRC1 : ");
bala0x07 0:491bc4ee502e 398 // print_data_bits(src_data[0]);
bala0x07 0:491bc4ee502e 399 // int1_src_data = (unsigned int)src_data[0];//READ DATA STORED
bala0x07 0:491bc4ee502e 400 // if(i>100)
bala0x07 0:491bc4ee502e 401 // i=0;
bala0x07 0:491bc4ee502e 402 // if(j>100)
bala0x07 0:491bc4ee502e 403 // j=0;
bala0x07 0:491bc4ee502e 404 // if(int1_src_data & 0x42) //changed from (0x2A)
bala0x07 0:491bc4ee502e 405 // {
bala0x07 0:491bc4ee502e 406 // i++;
bala0x07 0:491bc4ee502e 407 // pc.printf(" %d=X_INCREMENT",i);
bala0x07 0:491bc4ee502e 408 // //char int1_dur[2] = {0x33, 50};
bala0x07 0:491bc4ee502e 409 // //i2c.write(acc_address, int1_dur, 2);
bala0x07 0:491bc4ee502e 410 // }
bala0x07 0:491bc4ee502e 411 //
bala0x07 0:491bc4ee502e 412 //
bala0x07 0:491bc4ee502e 413 // else if(int1_src_data & 0x41)
bala0x07 0:491bc4ee502e 414 // {
bala0x07 0:491bc4ee502e 415 // j++;
bala0x07 0:491bc4ee502e 416 // pc.printf(" %d=X_decrement",j);
bala0x07 0:491bc4ee502e 417 // // char int1_dur[2] = {0x33, 3};
bala0x07 0:491bc4ee502e 418 // //i2c.write(acc_address, int1_dur, 2);
bala0x07 0:491bc4ee502e 419 // }
bala0x07 0:491bc4ee502e 420
bala0x07 0:491bc4ee502e 421 }
bala0x07 0:491bc4ee502e 422 }
bala0x07 0:491bc4ee502e 423 }
bala0x07 0:491bc4ee502e 424
bala0x07 0:491bc4ee502e 425 else
bala0x07 0:491bc4ee502e 426 {
bala0x07 0:491bc4ee502e 427 if(retry_attempt < 100)
bala0x07 0:491bc4ee502e 428 {
bala0x07 0:491bc4ee502e 429 retry_attempt++;
bala0x07 0:491bc4ee502e 430 goto retry;
bala0x07 0:491bc4ee502e 431 }
bala0x07 0:491bc4ee502e 432 }
bala0x07 0:491bc4ee502e 433
bala0x07 0:491bc4ee502e 434
bala0x07 0:491bc4ee502e 435 return 0;
bala0x07 0:491bc4ee502e 436 }
bala0x07 0:491bc4ee502e 437
bala0x07 0:491bc4ee502e 438 //____________________________________________________________________________________________________