lab 6

Dependencies:   ADXL362 mbed

Committer:
davmalon
Date:
Fri Mar 02 16:43:08 2018 +0000
Revision:
1:32862341b795
Parent:
0:3b7e393206f1
added lab report

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davmalon 0:3b7e393206f1 1 #include "mbed.h"
davmalon 0:3b7e393206f1 2 #include "ADXL362.h"
davmalon 0:3b7e393206f1 3 #include <stdlib.h>
davmalon 0:3b7e393206f1 4
davmalon 0:3b7e393206f1 5
davmalon 0:3b7e393206f1 6
davmalon 0:3b7e393206f1 7 /* David Malone davmalon@indiana.edu
davmalon 0:3b7e393206f1 8 lab 6
davmalon 0:3b7e393206f1 9 please note that when testing my values, the values for x and y were averaginf round 253-254 and would rop upon being bumped or moved.
davmalon 0:3b7e393206f1 10
davmalon 0:3b7e393206f1 11 */
davmalon 0:3b7e393206f1 12
davmalon 0:3b7e393206f1 13 // Interface pulled from ADXL362.cpp
davmalon 0:3b7e393206f1 14 // ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) :
davmalon 0:3b7e393206f1 15 ADXL362 adxl362(PA_0,PA_7,PA_6,PA_1);
davmalon 0:3b7e393206f1 16
davmalon 0:3b7e393206f1 17 DigitalOut led1(LED1);//led setup
davmalon 0:3b7e393206f1 18
davmalon 0:3b7e393206f1 19 int adxl362_reg_print(int start, int length); //function prototype for lab 6 functionality
davmalon 0:3b7e393206f1 20
davmalon 0:3b7e393206f1 21 int main() {
davmalon 0:3b7e393206f1 22 adxl362.reset();
davmalon 0:3b7e393206f1 23 wait_ms(600); // we need to wait at least 500ms after ADXL362 reset
davmalon 0:3b7e393206f1 24 adxl362.set_mode(ADXL362::MEASUREMENT);
davmalon 0:3b7e393206f1 25 long unsigned int count = 0;
davmalon 0:3b7e393206f1 26 int x,y,z,test;
davmalon 0:3b7e393206f1 27 wait_ms(1000);
davmalon 0:3b7e393206f1 28 x=adxl362.scanx();
davmalon 0:3b7e393206f1 29 y=adxl362.scany();
davmalon 0:3b7e393206f1 30 z=adxl362.scanz();
davmalon 0:3b7e393206f1 31 float average = (253 + 253 + 73) / 3.0;
davmalon 0:3b7e393206f1 32 float currentReading = 0.0;
davmalon 0:3b7e393206f1 33 int numKnocks =0;
davmalon 0:3b7e393206f1 34 float movingAverage = average;
davmalon 0:3b7e393206f1 35 float temp;
davmalon 0:3b7e393206f1 36 float swap;
davmalon 0:3b7e393206f1 37 float sum;
davmalon 0:3b7e393206f1 38 float numbers[] = {average, average, average, average, average, average, average, average, average, average};
davmalon 0:3b7e393206f1 39 printf("x = %d y = %d z = %d \r\n",x,y,z);
davmalon 0:3b7e393206f1 40 while(1) {
davmalon 0:3b7e393206f1 41 x=adxl362.scanx_u8();
davmalon 0:3b7e393206f1 42 y=adxl362.scany_u8();
davmalon 0:3b7e393206f1 43 z=adxl362.scanz_u8();
davmalon 0:3b7e393206f1 44 adxl362_reg_print(0,46); //print all the registers
davmalon 0:3b7e393206f1 45
davmalon 0:3b7e393206f1 46 /*
davmalon 0:3b7e393206f1 47
davmalon 0:3b7e393206f1 48 //test = adxl362.read_reg(adxl362.DEVID_AD); //testing the device id register
davmalon 0:3b7e393206f1 49 //printf("x = %d y = %d z = %d Test = %0x\r\n",x,y,z,test);
davmalon 0:3b7e393206f1 50 //int i = adxl362_reg_print(0,-1);
davmalon 0:3b7e393206f1 51 //knock reader
davmalon 0:3b7e393206f1 52 sum = 0;
davmalon 0:3b7e393206f1 53 for(int j = 0; j < 10; j++) {
davmalon 0:3b7e393206f1 54 sum = sum + numbers[j];
davmalon 0:3b7e393206f1 55 }//gets sum of samples for moving average
davmalon 0:3b7e393206f1 56
davmalon 0:3b7e393206f1 57 movingAverage = sum / 10.0 ; //moving average based upon 10 most recent samplings
davmalon 0:3b7e393206f1 58
davmalon 0:3b7e393206f1 59 currentReading = (abs(x) + abs(y) + abs(z)) / 3.0;
davmalon 0:3b7e393206f1 60 if(currentReading * 1.5 < movingAverage) { //regitered a knock, lets get led and delay
davmalon 0:3b7e393206f1 61 numKnocks +=1;
davmalon 0:3b7e393206f1 62 led1 = 1;
davmalon 0:3b7e393206f1 63 printf("Number of knocks since reset: %d \r\n", numKnocks);
davmalon 0:3b7e393206f1 64 wait_ms(2000); //wait 2 seconds
davmalon 0:3b7e393206f1 65 led1 = 0;
davmalon 0:3b7e393206f1 66 }//end if
davmalon 0:3b7e393206f1 67 //swap moving array
davmalon 0:3b7e393206f1 68 temp = numbers[9];
davmalon 0:3b7e393206f1 69 numbers[9] = currentReading;
davmalon 0:3b7e393206f1 70 swap = numbers[8];
davmalon 0:3b7e393206f1 71 numbers[8] = temp;
davmalon 0:3b7e393206f1 72 temp = numbers[7];
davmalon 0:3b7e393206f1 73 numbers[7] = swap;
davmalon 0:3b7e393206f1 74 swap = numbers[6];
davmalon 0:3b7e393206f1 75 numbers[6] = temp;
davmalon 0:3b7e393206f1 76 temp = numbers[5];
davmalon 0:3b7e393206f1 77 numbers[5] = swap;
davmalon 0:3b7e393206f1 78 swap = numbers[4];
davmalon 0:3b7e393206f1 79 numbers[4] = temp;
davmalon 0:3b7e393206f1 80 temp = numbers[3];
davmalon 0:3b7e393206f1 81 numbers[3] = swap;
davmalon 0:3b7e393206f1 82 swap = numbers[2];
davmalon 0:3b7e393206f1 83 numbers[2] = temp;
davmalon 0:3b7e393206f1 84 temp = numbers[1];
davmalon 0:3b7e393206f1 85 numbers[1] = swap;
davmalon 0:3b7e393206f1 86 numbers[0] = temp;
davmalon 0:3b7e393206f1 87 //this swaps the 10 array elements so that the new reading can be contained in the past 10 readings for a moving average;
davmalon 0:3b7e393206f1 88
davmalon 0:3b7e393206f1 89 count +=1;
davmalon 0:3b7e393206f1 90 */
davmalon 0:3b7e393206f1 91
davmalon 0:3b7e393206f1 92 wait_ms(100);
davmalon 0:3b7e393206f1 93 //printf("x = %d y = %d z = %d \r\n",x,y,z);
davmalon 0:3b7e393206f1 94 }
davmalon 0:3b7e393206f1 95 }//end main
davmalon 0:3b7e393206f1 96
davmalon 0:3b7e393206f1 97 int adxl362_reg_print(int start, int length) {
davmalon 0:3b7e393206f1 98 //check if start or length go beyond addr window
davmalon 0:3b7e393206f1 99 if(start < 0x00 || start > 0x2E) { //check starting value
davmalon 0:3b7e393206f1 100 printf("recieved invalid starting point. range is 0-46 \n\r");
davmalon 0:3b7e393206f1 101 return -1;
davmalon 0:3b7e393206f1 102 }
davmalon 0:3b7e393206f1 103 else if((start + length) > 0x2E) { //check input range
davmalon 0:3b7e393206f1 104 printf("length too long. range is 0-46 \n\r");
davmalon 0:3b7e393206f1 105 return -1;
davmalon 0:3b7e393206f1 106 }
davmalon 0:3b7e393206f1 107 else if(length < 0) { //cheack the length
davmalon 0:3b7e393206f1 108 printf("invalid length. Length must be positive \n\r");
davmalon 0:3b7e393206f1 109 return -1;
davmalon 0:3b7e393206f1 110 }
davmalon 0:3b7e393206f1 111 else if(adxl362.read_reg(adxl362.DEVID_AD) != 0xAD) { //check the device id register
davmalon 0:3b7e393206f1 112 printf("Device id register has incorrect value of: %d \n\r", adxl362.read_reg(adxl362.DEVID_AD));
davmalon 0:3b7e393206f1 113 return -1;
davmalon 0:3b7e393206f1 114 }
davmalon 0:3b7e393206f1 115 else {//checks passed, output registers
davmalon 0:3b7e393206f1 116 char *rtn = "";
davmalon 0:3b7e393206f1 117 for(int i = start; i < length -1; i++) {
davmalon 0:3b7e393206f1 118 printf("%02x :", i); //print address
davmalon 0:3b7e393206f1 119 switch(i) {
davmalon 0:3b7e393206f1 120 case 0:
davmalon 0:3b7e393206f1 121 rtn = "DEVID_AD";
davmalon 0:3b7e393206f1 122 printf("%s = %0x \n\r", rtn, adxl362.DEVID_AD);
davmalon 0:3b7e393206f1 123 break;
davmalon 0:3b7e393206f1 124 case 1:
davmalon 0:3b7e393206f1 125 rtn = "DEVID_MST";
davmalon 0:3b7e393206f1 126 printf("%s = %0x \n\r", rtn, adxl362.DEVID_MST);
davmalon 0:3b7e393206f1 127 break;
davmalon 0:3b7e393206f1 128 case 2:
davmalon 0:3b7e393206f1 129 rtn = "PARTID";
davmalon 0:3b7e393206f1 130 printf("%s = %0x \n\r", rtn, adxl362.PARTID);
davmalon 0:3b7e393206f1 131 case 3:
davmalon 0:3b7e393206f1 132 rtn = "REVID";
davmalon 0:3b7e393206f1 133 printf("%s = %0x \n\r", rtn, adxl362.read_reg(adxl362.REVID));
davmalon 0:3b7e393206f1 134 break;
davmalon 0:3b7e393206f1 135 case 4: // no opp
davmalon 0:3b7e393206f1 136 rtn = "";
davmalon 0:3b7e393206f1 137 break;
davmalon 0:3b7e393206f1 138 case 5: //no opp
davmalon 0:3b7e393206f1 139 break;
davmalon 0:3b7e393206f1 140 case 6: //no opp
davmalon 0:3b7e393206f1 141 break;
davmalon 0:3b7e393206f1 142 case 7: //mo opp
davmalon 0:3b7e393206f1 143 break;
davmalon 0:3b7e393206f1 144 case 8:
davmalon 0:3b7e393206f1 145 rtn = "XDATA";
davmalon 0:3b7e393206f1 146 printf("%s = %0x \n\r", rtn, adxl362.XDATA);
davmalon 0:3b7e393206f1 147 break;
davmalon 0:3b7e393206f1 148 case 9:
davmalon 0:3b7e393206f1 149 rtn = "YDATA";
davmalon 0:3b7e393206f1 150 printf("%s = %0x \n\r", rtn, adxl362.YDATA);
davmalon 0:3b7e393206f1 151 break;
davmalon 0:3b7e393206f1 152 case 10:
davmalon 0:3b7e393206f1 153 rtn = "ZDATA";
davmalon 0:3b7e393206f1 154 printf("%s = %0x \n\r", rtn, adxl362.ZDATA);
davmalon 0:3b7e393206f1 155 break;
davmalon 0:3b7e393206f1 156 case 11:
davmalon 0:3b7e393206f1 157 rtn = "STATUS";
davmalon 0:3b7e393206f1 158 printf("%s = %0x \n\r", rtn, adxl362.STATUS);
davmalon 0:3b7e393206f1 159 break;
davmalon 0:3b7e393206f1 160 case 12:
davmalon 0:3b7e393206f1 161 rtn = "FIFO_ENTRIES_L";
davmalon 0:3b7e393206f1 162 printf("%s = %0x \n\r", rtn, adxl362.FIFO_ENTRIES_L);
davmalon 0:3b7e393206f1 163 break;
davmalon 0:3b7e393206f1 164 case 13:
davmalon 0:3b7e393206f1 165 rtn = "FIFO_ENTRIES_H";
davmalon 0:3b7e393206f1 166 printf("%s = %0x \n\r", rtn, adxl362.FIFO_ENTRIES_H);
davmalon 0:3b7e393206f1 167 break;
davmalon 0:3b7e393206f1 168 case 14:
davmalon 0:3b7e393206f1 169 rtn = "XDATA_L";
davmalon 0:3b7e393206f1 170 printf("%s = %0x \n\r", rtn, adxl362.XDATA_L);
davmalon 0:3b7e393206f1 171 break;
davmalon 0:3b7e393206f1 172 case 15:
davmalon 0:3b7e393206f1 173 rtn = "XDATA_H";
davmalon 0:3b7e393206f1 174 printf("%s = %0x \n\r", rtn, adxl362.XDATA_H);
davmalon 0:3b7e393206f1 175 break;
davmalon 0:3b7e393206f1 176 case 16:
davmalon 0:3b7e393206f1 177 rtn = "YDATA_L";
davmalon 0:3b7e393206f1 178 printf("%s = %0x \n\r", rtn, adxl362.YDATA_L);
davmalon 0:3b7e393206f1 179 break;
davmalon 0:3b7e393206f1 180 case 17:
davmalon 0:3b7e393206f1 181 rtn = "YDATA_H";
davmalon 0:3b7e393206f1 182 printf("%s = %0x \n\r", rtn, adxl362.YDATA_H);
davmalon 0:3b7e393206f1 183 break;
davmalon 0:3b7e393206f1 184 case 18:
davmalon 0:3b7e393206f1 185 rtn = "ZDATA_L";
davmalon 0:3b7e393206f1 186 printf("%s = %0x \n\r", rtn, adxl362.ZDATA_L);
davmalon 0:3b7e393206f1 187 break;
davmalon 0:3b7e393206f1 188 case 19:
davmalon 0:3b7e393206f1 189 rtn = "ZDATA_H";
davmalon 0:3b7e393206f1 190 printf("%s = %0x \n\r", rtn, adxl362.ZDATA_H);
davmalon 0:3b7e393206f1 191 break;
davmalon 0:3b7e393206f1 192 case 20:
davmalon 0:3b7e393206f1 193 rtn = "TEMP_L";
davmalon 0:3b7e393206f1 194 printf("%s = %0x \n\r", rtn, adxl362.TEMP_L);
davmalon 0:3b7e393206f1 195 break;
davmalon 0:3b7e393206f1 196 case 21:
davmalon 0:3b7e393206f1 197 rtn = "TEMP_H";
davmalon 0:3b7e393206f1 198 printf("%s = %0x \n\r", rtn, adxl362.TEMP_H);
davmalon 0:3b7e393206f1 199 break;
davmalon 0:3b7e393206f1 200 case 31:
davmalon 0:3b7e393206f1 201 rtn = "SOFT_RESET";
davmalon 0:3b7e393206f1 202 printf("%s = %0x \n\r", rtn, adxl362.SOFT_RESET);
davmalon 0:3b7e393206f1 203 break;
davmalon 0:3b7e393206f1 204 case 32:
davmalon 0:3b7e393206f1 205 rtn = "THRESH_ACT_L";
davmalon 0:3b7e393206f1 206 printf("%s = %0x \n\r", rtn, adxl362.THRESH_ACT_L);
davmalon 0:3b7e393206f1 207 break;
davmalon 0:3b7e393206f1 208 case 33:
davmalon 0:3b7e393206f1 209 rtn = "THRESH_ACT_H";
davmalon 0:3b7e393206f1 210 printf("%s = %0x \n\r", rtn, adxl362.THRESH_ACT_H);
davmalon 0:3b7e393206f1 211 break;
davmalon 0:3b7e393206f1 212 case 34:
davmalon 0:3b7e393206f1 213 rtn = "TIME_ACT";
davmalon 0:3b7e393206f1 214 printf("%s = %0x \n\r", rtn, adxl362.TIME_ACT);
davmalon 0:3b7e393206f1 215 break;
davmalon 0:3b7e393206f1 216 case 35:
davmalon 0:3b7e393206f1 217 rtn = "THRESH_INACT_L";
davmalon 0:3b7e393206f1 218 printf("%s = %0x \n\r", rtn, adxl362.THRESH_INACT_L);
davmalon 0:3b7e393206f1 219 break;
davmalon 0:3b7e393206f1 220 case 36:
davmalon 0:3b7e393206f1 221 rtn = "THRESH_INACT_H";
davmalon 0:3b7e393206f1 222 printf("%s = %0x \n\r", rtn, adxl362.THRESH_INACT_H);
davmalon 0:3b7e393206f1 223 break;
davmalon 0:3b7e393206f1 224 case 37:
davmalon 0:3b7e393206f1 225 rtn = "TIME_INACT_L";
davmalon 0:3b7e393206f1 226 printf("%s = %0x \n\r", rtn, adxl362.TIME_INACT_L);
davmalon 0:3b7e393206f1 227 break;
davmalon 0:3b7e393206f1 228 case 38:
davmalon 0:3b7e393206f1 229 rtn = "TIME_INACT_H";
davmalon 0:3b7e393206f1 230 printf("%s = %0x \n\r", rtn, adxl362.TIME_INACT_H);
davmalon 0:3b7e393206f1 231 break;
davmalon 0:3b7e393206f1 232 case 39:
davmalon 0:3b7e393206f1 233 rtn = "ACT_INACT_CTL";
davmalon 0:3b7e393206f1 234 printf("%s = %0x \n\r", rtn, adxl362.ACT_INACT_CTL);
davmalon 0:3b7e393206f1 235 break;
davmalon 0:3b7e393206f1 236 case 40:
davmalon 0:3b7e393206f1 237 rtn = "FIFO_CONTROL";
davmalon 0:3b7e393206f1 238 printf("%s = %0x \n\r", rtn, adxl362.FIFO_CONTROL);
davmalon 0:3b7e393206f1 239 break;
davmalon 0:3b7e393206f1 240 case 41:
davmalon 0:3b7e393206f1 241 rtn = "FIFO_SAMPLES";
davmalon 0:3b7e393206f1 242 printf("%s = %0x \n\r", rtn, adxl362.FIFO_SAMPLES);
davmalon 0:3b7e393206f1 243 break;
davmalon 0:3b7e393206f1 244 case 42:
davmalon 0:3b7e393206f1 245 rtn = "INTMAP1";
davmalon 0:3b7e393206f1 246 printf("%s = %0x \n\r", rtn, adxl362.INTMAP1);
davmalon 0:3b7e393206f1 247 break;
davmalon 0:3b7e393206f1 248 case 43:
davmalon 0:3b7e393206f1 249 rtn = "INTMAP2";
davmalon 0:3b7e393206f1 250 printf("%s = %0x \n\r", rtn, adxl362.INTMAP2);
davmalon 0:3b7e393206f1 251 break;
davmalon 0:3b7e393206f1 252 case 44:
davmalon 0:3b7e393206f1 253 rtn = "FILTER_CTL";
davmalon 0:3b7e393206f1 254 printf("%s = %0x \n\r", rtn, adxl362.FILTER_CTL);
davmalon 0:3b7e393206f1 255 break;
davmalon 0:3b7e393206f1 256 case 45:
davmalon 0:3b7e393206f1 257 rtn = "POWER_CTL";
davmalon 0:3b7e393206f1 258 printf("%s = %0x \n\r", rtn, adxl362.POWER_CTL);
davmalon 0:3b7e393206f1 259 break;
davmalon 0:3b7e393206f1 260 case 46:
davmalon 0:3b7e393206f1 261 rtn = "SELF_TEST";
davmalon 0:3b7e393206f1 262 printf("%s = %0x \n\r", rtn, adxl362.SELF_TEST);
davmalon 0:3b7e393206f1 263 break;
davmalon 0:3b7e393206f1 264 default:
davmalon 0:3b7e393206f1 265 rtn = "";
davmalon 0:3b7e393206f1 266 break;
davmalon 0:3b7e393206f1 267
davmalon 0:3b7e393206f1 268 }//end switch
davmalon 0:3b7e393206f1 269 }//end loop
davmalon 0:3b7e393206f1 270 return 1; //sucess
davmalon 0:3b7e393206f1 271 }//
davmalon 0:3b7e393206f1 272 return 0;//undetected failure
davmalon 0:3b7e393206f1 273
davmalon 0:3b7e393206f1 274 }//end reg_print function
davmalon 0:3b7e393206f1 275
davmalon 0:3b7e393206f1 276
davmalon 0:3b7e393206f1 277 /* getRegName()
davmalon 0:3b7e393206f1 278 paramater int :: address of register
davmalon 0:3b7e393206f1 279 return String :: name of register
davmalon 0:3b7e393206f1 280 register list
davmalon 0:3b7e393206f1 281 DEVID_AD = 0x00,
davmalon 0:3b7e393206f1 282 DEVID_MST = 0x01,
davmalon 0:3b7e393206f1 283 PARTID = 0x02,
davmalon 0:3b7e393206f1 284 REVID = 0x03,
davmalon 0:3b7e393206f1 285 XDATA = 0x08,
davmalon 0:3b7e393206f1 286 YDATA = 0x09,
davmalon 0:3b7e393206f1 287 ZDATA = 0x0A,
davmalon 0:3b7e393206f1 288 STATUS = 0x0B,
davmalon 0:3b7e393206f1 289 FIFO_ENTRIES_L = 0x0C,
davmalon 0:3b7e393206f1 290 FIFO_ENTRIES_H = 0x0D,
davmalon 0:3b7e393206f1 291 XDATA_L = 0x0E,
davmalon 0:3b7e393206f1 292 XDATA_H = 0x0F,
davmalon 0:3b7e393206f1 293 YDATA_L = 0x10,
davmalon 0:3b7e393206f1 294 YDATA_H = 0x11,
davmalon 0:3b7e393206f1 295 ZDATA_L = 0x12,
davmalon 0:3b7e393206f1 296 ZDATA_H = 0x13,
davmalon 0:3b7e393206f1 297 TEMP_L = 0x14,
davmalon 0:3b7e393206f1 298 TEMP_H = 0x15,
davmalon 0:3b7e393206f1 299 // Reserved = 0x16;
davmalon 0:3b7e393206f1 300 // Reserved = 0x17;
davmalon 0:3b7e393206f1 301 SOFT_RESET = 0x1F,
davmalon 0:3b7e393206f1 302 THRESH_ACT_L = 0x20,
davmalon 0:3b7e393206f1 303 THRESH_ACT_H = 0x21,
davmalon 0:3b7e393206f1 304 TIME_ACT = 0x22,
davmalon 0:3b7e393206f1 305 THRESH_INACT_L = 0x23,
davmalon 0:3b7e393206f1 306 THRESH_INACT_H = 0x24,
davmalon 0:3b7e393206f1 307 TIME_INACT_L = 0x25,
davmalon 0:3b7e393206f1 308 TIME_INACT_H = 0x26,
davmalon 0:3b7e393206f1 309 ACT_INACT_CTL = 0x27,
davmalon 0:3b7e393206f1 310 FIFO_CONTROL = 0x28,
davmalon 0:3b7e393206f1 311 FIFO_SAMPLES = 0x29,
davmalon 0:3b7e393206f1 312 INTMAP1 = 0x2A,
davmalon 0:3b7e393206f1 313 INTMAP2 = 0x2B,
davmalon 0:3b7e393206f1 314 FILTER_CTL = 0x2C,
davmalon 0:3b7e393206f1 315 POWER_CTL = 0x2D,
davmalon 0:3b7e393206f1 316 SELF_TEST = 0x2E,
davmalon 0:3b7e393206f1 317 */
davmalon 0:3b7e393206f1 318
davmalon 0:3b7e393206f1 319
davmalon 0:3b7e393206f1 320
davmalon 0:3b7e393206f1 321 /* End of file */