In this lab, you will: Construct a prototype

Dependencies:   ADXL362 mbed

Committer:
csinders
Date:
Thu Mar 01 16:06:44 2018 +0000
Revision:
5:7f4dc37352d7
Parent:
4:7616d6fd81d8
making sure is published

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csinders 0:d5c236ffbd5e 1 #include "mbed.h"
csinders 0:d5c236ffbd5e 2 #include "ADXL362.h"
csinders 0:d5c236ffbd5e 3
csinders 0:d5c236ffbd5e 4 // Interface pulled from ADXL362.cpp
csinders 0:d5c236ffbd5e 5 // ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) :
csinders 0:d5c236ffbd5e 6 ADXL362 adxl362(PA_0,PA_7,PA_6,PA_1);
csinders 0:d5c236ffbd5e 7 Serial pc(USBTX, USBRX);
csinders 2:b56010953f54 8 DigitalOut myled(LED1);
csinders 0:d5c236ffbd5e 9
csinders 1:fedb70ea0eaa 10 int adxl362_reg_print(int start, int length);
csinders 4:7616d6fd81d8 11 void adxl362_get_average(int16_t avg[]);
csinders 1:fedb70ea0eaa 12
csinders 0:d5c236ffbd5e 13 int main() {
csinders 3:33cca9e98fbb 14 pc.printf("Starting program\r\n");
csinders 0:d5c236ffbd5e 15 adxl362.reset();
csinders 0:d5c236ffbd5e 16 wait_ms(600); // we need to wait at least 500ms after ADXL362 reset
csinders 0:d5c236ffbd5e 17 adxl362.set_mode(ADXL362::MEASUREMENT);
csinders 4:7616d6fd81d8 18 int16_t x,y,z;
csinders 4:7616d6fd81d8 19 int16_t avg[3]; // Avg values of x,y,z. x = avg[0], y = avg[1] z = avg[2]
csinders 3:33cca9e98fbb 20 int countOverThreshHold = 0;
csinders 3:33cca9e98fbb 21 adxl362_reg_print(1,0); // Test of axdl_reg_print
csinders 2:b56010953f54 22 wait_ms(1000); // wait so that values can be seen
csinders 5:7f4dc37352d7 23 int threshHoldRange = 30; // threshhold difference between avg and read values
csinders 2:b56010953f54 24 adxl362_get_average(avg);// Generate an average of what x,y,z are.
csinders 2:b56010953f54 25 //pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
csinders 0:d5c236ffbd5e 26 while(1) {
csinders 4:7616d6fd81d8 27 x=adxl362.scanx();
csinders 4:7616d6fd81d8 28 y=adxl362.scany();
csinders 4:7616d6fd81d8 29 z=adxl362.scanz();
csinders 3:33cca9e98fbb 30
csinders 2:b56010953f54 31 if (x > (avg[0] + threshHoldRange) || x < (avg[0] - threshHoldRange)
csinders 2:b56010953f54 32 || y > (avg[1] + threshHoldRange) || y < (avg[1] - threshHoldRange)
csinders 3:33cca9e98fbb 33 || z > (avg[2] + threshHoldRange) || z < (avg[2] - threshHoldRange)) {
csinders 2:b56010953f54 34 // commented out z as it gives weird values
csinders 3:33cca9e98fbb 35 // ) {
csinders 2:b56010953f54 36 /* print cases used to test threshhold range
csinders 2:b56010953f54 37 pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
csinders 2:b56010953f54 38 pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
csinders 2:b56010953f54 39 pc.printf("Threshold range is = %d\r\n", threshHoldRange);
csinders 2:b56010953f54 40 */
csinders 3:33cca9e98fbb 41 //pc.printf("outside of threshold range\n\r");
csinders 3:33cca9e98fbb 42 countOverThreshHold++;
csinders 3:33cca9e98fbb 43 pc.printf("Gone over threshhold %d times\n\r", countOverThreshHold);
csinders 2:b56010953f54 44 myled = 1; // LED is ON
csinders 2:b56010953f54 45 wait_ms(1900); // Wait 1.9s. Last .1 sec is done while getting average
csinders 2:b56010953f54 46 adxl362_get_average(avg); // Generate new average
csinders 2:b56010953f54 47 myled = 0; // LED is OFF
csinders 2:b56010953f54 48 }
csinders 3:33cca9e98fbb 49
csinders 0:d5c236ffbd5e 50 pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
csinders 0:d5c236ffbd5e 51 wait_ms(100);
csinders 0:d5c236ffbd5e 52 }
csinders 2:b56010953f54 53 }
csinders 2:b56010953f54 54
csinders 4:7616d6fd81d8 55 void adxl362_get_average(int16_t avg[]) {
csinders 2:b56010953f54 56 // Generate an average of what x,y,z are.
csinders 5:7f4dc37352d7 57 int numberOfPolls = 2;
csinders 2:b56010953f54 58 // reset average back to zero for each
csinders 2:b56010953f54 59 int tmp[3]; // required because avg was having integer overflow
csinders 2:b56010953f54 60 tmp[0] = 0;
csinders 2:b56010953f54 61 tmp[1] = 0;
csinders 2:b56010953f54 62 tmp[2] = 0;
csinders 2:b56010953f54 63 avg[0] = 0;
csinders 2:b56010953f54 64 avg[1] = 0;
csinders 2:b56010953f54 65 avg[2] = 0;
csinders 4:7616d6fd81d8 66 int16_t x,y,z;
csinders 2:b56010953f54 67 // Poll each value 10 times
csinders 2:b56010953f54 68 for (int i = 0; i < numberOfPolls; i++) {
csinders 2:b56010953f54 69 //pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
csinders 4:7616d6fd81d8 70 x = adxl362.scanx();
csinders 2:b56010953f54 71 tmp[0]+= x;
csinders 4:7616d6fd81d8 72 y = adxl362.scany();
csinders 2:b56010953f54 73 tmp[1]+= y;
csinders 4:7616d6fd81d8 74 z=adxl362.scanz();
csinders 2:b56010953f54 75 tmp[2]+= z;
csinders 2:b56010953f54 76 wait_ms(10);
csinders 2:b56010953f54 77 //pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",tmp[0],tmp[1],tmp[2]);
csinders 2:b56010953f54 78
csinders 2:b56010953f54 79 }
csinders 2:b56010953f54 80 // Divide each value by the number of polls to get the average
csinders 2:b56010953f54 81 tmp[0] = tmp[0] / numberOfPolls;
csinders 2:b56010953f54 82 tmp[1] = tmp[1] / numberOfPolls;
csinders 2:b56010953f54 83 tmp[2] = tmp[2] / numberOfPolls;
csinders 2:b56010953f54 84 // store values from the tmp to the passed in array
csinders 2:b56010953f54 85 avg[0] = tmp[0];
csinders 2:b56010953f54 86 avg[1] = tmp[1];
csinders 2:b56010953f54 87 avg[2] = tmp[2];
csinders 2:b56010953f54 88 pc.printf("New Average: xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
csinders 2:b56010953f54 89 wait_ms(5000);
csinders 0:d5c236ffbd5e 90 }
csinders 0:d5c236ffbd5e 91
csinders 0:d5c236ffbd5e 92 int adxl362_reg_print(int start, int length) {
csinders 3:33cca9e98fbb 93 int end = 0x2E;
csinders 3:33cca9e98fbb 94 if ((start + length) < end) {
csinders 3:33cca9e98fbb 95 end = (start + length); // so it only goes to the length
csinders 3:33cca9e98fbb 96 }
csinders 3:33cca9e98fbb 97 if (length == 0) {
csinders 3:33cca9e98fbb 98 end = 0x2E; // so it prints till the end
csinders 3:33cca9e98fbb 99 }
csinders 3:33cca9e98fbb 100
csinders 3:33cca9e98fbb 101
csinders 0:d5c236ffbd5e 102 // Check if start is within registry
csinders 0:d5c236ffbd5e 103 if (start < 0 || start > 0x2E) {
csinders 0:d5c236ffbd5e 104 pc.printf("Error: start value passed to adxl362_reg_print outside of range of registry\n\r");
csinders 0:d5c236ffbd5e 105 return -1;
csinders 0:d5c236ffbd5e 106 }
csinders 0:d5c236ffbd5e 107 // check if length is negative
csinders 0:d5c236ffbd5e 108 if (length < 0) {
csinders 0:d5c236ffbd5e 109 pc.printf("Error: length passed to adxl362_reg_print is negative\n\r");
csinders 0:d5c236ffbd5e 110 return -1;
csinders 0:d5c236ffbd5e 111 }
csinders 0:d5c236ffbd5e 112
csinders 0:d5c236ffbd5e 113 // check if valid communication with device going
csinders 2:b56010953f54 114 if (adxl362.read_reg(adxl362.DEVID_AD) != 0xAD) {
csinders 0:d5c236ffbd5e 115 pc.printf("Error: Unable to read from DEVID_AD register\n\r");
csinders 1:fedb70ea0eaa 116 return -1;
csinders 3:33cca9e98fbb 117 }
csinders 0:d5c236ffbd5e 118 // String array with all of the names of the different registers in order
csinders 3:33cca9e98fbb 119 char regNames [40][20] = {
csinders 0:d5c236ffbd5e 120 "DEVID_AD", "DEVID_MST", "PARTID",
csinders 0:d5c236ffbd5e 121 "REVID", "XDATA", "YDATA",
csinders 0:d5c236ffbd5e 122 "ZDATA", "STATUS", "FIFO_ENTRIES_L",
csinders 0:d5c236ffbd5e 123 "FIFO_ENTRIES_H", "XDATA_L", "XDATA_H",
csinders 0:d5c236ffbd5e 124 "YDATA_L", "YDATA_H", "ZDATA_L",
csinders 0:d5c236ffbd5e 125 "ZDATA_H", "TEMP_L", "TEMP_H",
csinders 0:d5c236ffbd5e 126 "RESERVED", "RESERVED", "SOFT_RESET",
csinders 0:d5c236ffbd5e 127 "THRESH_ACT_L", "THRESH_ACT_H", "TIME_INACT_L",
csinders 0:d5c236ffbd5e 128 "TIME_ACT", "THRESH_INACT_L", "THRESH_INACT_H",
csinders 0:d5c236ffbd5e 129 "TIME_INACT_L", "TIME_INACT_H", "ACT_INACT_CTL",
csinders 0:d5c236ffbd5e 130 "FIFO_CONTROL", "FIFO_SAMPLES", "INTMAP1",
csinders 0:d5c236ffbd5e 131 "INTMATP2", "FILTER_CTL", "POWER_CTL",
csinders 0:d5c236ffbd5e 132 "SELF_TEST"};
csinders 3:33cca9e98fbb 133
csinders 3:33cca9e98fbb 134
csinders 3:33cca9e98fbb 135 for (int hexValue = 0; hexValue < end; hexValue++) {
csinders 2:b56010953f54 136 uint8_t tmp = 0; // hex value location of the register
csinders 3:33cca9e98fbb 137 uint8_t arrayAddress = 0; // Hex value of the register
csinders 1:fedb70ea0eaa 138 // May need to increment the registers inside of the if, since the values
csinders 1:fedb70ea0eaa 139 // are not always sequential
csinders 1:fedb70ea0eaa 140 // You can find registers inside of ADXL362.h file in ADXL362 folder
csinders 1:fedb70ea0eaa 141 // they are the ADXL362_register_t
csinders 3:33cca9e98fbb 142 if (hexValue == 0) {
csinders 1:fedb70ea0eaa 143 tmp = adxl362.read_reg(adxl362.DEVID_AD);
csinders 3:33cca9e98fbb 144 arrayAddress = 0;
csinders 3:33cca9e98fbb 145 } else if (hexValue == 1) {
csinders 1:fedb70ea0eaa 146 tmp = adxl362.read_reg(adxl362.DEVID_MST);
csinders 3:33cca9e98fbb 147 arrayAddress = 1;
csinders 3:33cca9e98fbb 148 } else if (hexValue == 2) {
csinders 3:33cca9e98fbb 149 tmp = adxl362.read_reg(adxl362.PARTID);
csinders 3:33cca9e98fbb 150 arrayAddress = 2;
csinders 3:33cca9e98fbb 151 } else if (hexValue == 3) {
csinders 3:33cca9e98fbb 152 tmp = adxl362.read_reg(adxl362.REVID);
csinders 3:33cca9e98fbb 153 arrayAddress = 3;
csinders 3:33cca9e98fbb 154 } else if (hexValue == 8) {
csinders 3:33cca9e98fbb 155 tmp = adxl362.read_reg(adxl362.XDATA);
csinders 3:33cca9e98fbb 156 arrayAddress = 4;
csinders 3:33cca9e98fbb 157 } else if (hexValue == 9) {
csinders 3:33cca9e98fbb 158 tmp = adxl362.read_reg(adxl362.YDATA);
csinders 3:33cca9e98fbb 159 arrayAddress = 5;
csinders 3:33cca9e98fbb 160 } else if (hexValue == 0x0A) {
csinders 3:33cca9e98fbb 161 tmp = adxl362.read_reg(adxl362.ZDATA);
csinders 3:33cca9e98fbb 162 arrayAddress = 6;
csinders 3:33cca9e98fbb 163 } else if (hexValue == 0x0B) {
csinders 3:33cca9e98fbb 164 tmp = adxl362.read_reg(adxl362.STATUS);
csinders 3:33cca9e98fbb 165 arrayAddress = 7;
csinders 3:33cca9e98fbb 166 } else if (hexValue == 0x0C) {
csinders 3:33cca9e98fbb 167 tmp = adxl362.read_reg(adxl362.FIFO_ENTRIES_L);
csinders 3:33cca9e98fbb 168 arrayAddress = 8;
csinders 3:33cca9e98fbb 169 } else if (hexValue == 0x0D) {
csinders 3:33cca9e98fbb 170 tmp = adxl362.read_reg(adxl362.FIFO_ENTRIES_H);
csinders 3:33cca9e98fbb 171 arrayAddress = 9;
csinders 3:33cca9e98fbb 172 } else if (hexValue == 0x0E) {
csinders 3:33cca9e98fbb 173 tmp = adxl362.read_reg(adxl362.XDATA_L);
csinders 3:33cca9e98fbb 174 arrayAddress = 9;
csinders 3:33cca9e98fbb 175 } else if (hexValue == 0x0F) {
csinders 3:33cca9e98fbb 176 tmp = adxl362.read_reg(adxl362.XDATA_H);
csinders 3:33cca9e98fbb 177 arrayAddress = 10;
csinders 3:33cca9e98fbb 178 } else if (hexValue == 0x10) {
csinders 3:33cca9e98fbb 179 tmp = adxl362.read_reg(adxl362.YDATA_L);
csinders 3:33cca9e98fbb 180 arrayAddress = 11;
csinders 3:33cca9e98fbb 181 } else if (hexValue == 0x11) {
csinders 3:33cca9e98fbb 182 tmp = adxl362.read_reg(adxl362.YDATA_H);
csinders 3:33cca9e98fbb 183 arrayAddress = 12;
csinders 3:33cca9e98fbb 184 } else if (hexValue == 0x12) {
csinders 3:33cca9e98fbb 185 tmp = adxl362.read_reg(adxl362.YDATA_H);
csinders 3:33cca9e98fbb 186 arrayAddress = 13;
csinders 3:33cca9e98fbb 187 } else if (hexValue == 0x13) {
csinders 3:33cca9e98fbb 188 tmp = adxl362.read_reg(adxl362.ZDATA_L);
csinders 3:33cca9e98fbb 189 arrayAddress = 14;
csinders 3:33cca9e98fbb 190 } else if (hexValue == 0x14) {
csinders 3:33cca9e98fbb 191 tmp = adxl362.read_reg(adxl362.ZDATA_H);
csinders 3:33cca9e98fbb 192 arrayAddress = 15;
csinders 3:33cca9e98fbb 193 } else if (hexValue == 0x15) {
csinders 3:33cca9e98fbb 194 // RESERVED
csinders 3:33cca9e98fbb 195 } else if (hexValue == 0x16) {
csinders 3:33cca9e98fbb 196 // RESERVED
csinders 3:33cca9e98fbb 197 } else if (hexValue == 0x1F) {
csinders 3:33cca9e98fbb 198 tmp = adxl362.read_reg(adxl362.SOFT_RESET);
csinders 3:33cca9e98fbb 199 arrayAddress = 18;
csinders 3:33cca9e98fbb 200 } else if (hexValue == 0x20) {
csinders 3:33cca9e98fbb 201 tmp = adxl362.read_reg(adxl362.THRESH_ACT_L);
csinders 3:33cca9e98fbb 202 arrayAddress = 19;
csinders 3:33cca9e98fbb 203 } else if (hexValue == 0x21) {
csinders 3:33cca9e98fbb 204 tmp = adxl362.read_reg(adxl362.THRESH_ACT_H);
csinders 3:33cca9e98fbb 205 arrayAddress = 20;
csinders 3:33cca9e98fbb 206 } else if (hexValue == 0x22) {
csinders 3:33cca9e98fbb 207 tmp = adxl362.read_reg(adxl362.TIME_ACT);
csinders 3:33cca9e98fbb 208 arrayAddress = 21;
csinders 3:33cca9e98fbb 209 } else if (hexValue == 0x23) {
csinders 3:33cca9e98fbb 210 tmp = adxl362.read_reg(adxl362.THRESH_INACT_L);
csinders 3:33cca9e98fbb 211 arrayAddress = 22;
csinders 3:33cca9e98fbb 212 } else if (hexValue == 0x24) {
csinders 3:33cca9e98fbb 213 tmp = adxl362.read_reg(adxl362.THRESH_INACT_H);
csinders 3:33cca9e98fbb 214 arrayAddress = 23;
csinders 3:33cca9e98fbb 215 } else if (hexValue == 0x25) {
csinders 3:33cca9e98fbb 216 tmp = adxl362.read_reg(adxl362.TIME_INACT_L);
csinders 3:33cca9e98fbb 217 arrayAddress = 24;
csinders 3:33cca9e98fbb 218 } else if (hexValue == 0x26) {
csinders 3:33cca9e98fbb 219 tmp = adxl362.read_reg(adxl362.TIME_INACT_H);
csinders 3:33cca9e98fbb 220 arrayAddress = 25;
csinders 3:33cca9e98fbb 221 } else if (hexValue == 0x27) {
csinders 3:33cca9e98fbb 222 tmp = adxl362.read_reg(adxl362.ACT_INACT_CTL);
csinders 3:33cca9e98fbb 223 arrayAddress = 26;
csinders 3:33cca9e98fbb 224 } else if (hexValue == 0x28) {
csinders 3:33cca9e98fbb 225 tmp = adxl362.read_reg(adxl362.FIFO_CONTROL);
csinders 3:33cca9e98fbb 226 arrayAddress = 27;
csinders 3:33cca9e98fbb 227 } else if (hexValue == 0x29) {
csinders 3:33cca9e98fbb 228 tmp = adxl362.read_reg(adxl362.FIFO_SAMPLES);
csinders 3:33cca9e98fbb 229 arrayAddress = 28;
csinders 3:33cca9e98fbb 230 } else if (hexValue == 0x2A) {
csinders 3:33cca9e98fbb 231 tmp = adxl362.read_reg(adxl362.INTMAP1);
csinders 3:33cca9e98fbb 232 arrayAddress = 29;
csinders 3:33cca9e98fbb 233 } else if (hexValue == 0x2B) {
csinders 3:33cca9e98fbb 234 tmp = adxl362.read_reg(adxl362.INTMAP2);
csinders 3:33cca9e98fbb 235 arrayAddress = 30;
csinders 3:33cca9e98fbb 236 } else if (hexValue == 0x2C) {
csinders 3:33cca9e98fbb 237 tmp = adxl362.read_reg(adxl362.FILTER_CTL);
csinders 3:33cca9e98fbb 238 arrayAddress = 31;
csinders 3:33cca9e98fbb 239 } else if (hexValue == 0x2D) {
csinders 3:33cca9e98fbb 240 tmp = adxl362.read_reg(adxl362.POWER_CTL);
csinders 3:33cca9e98fbb 241 arrayAddress = 32;
csinders 3:33cca9e98fbb 242 } else if (hexValue == 0x2E) {
csinders 3:33cca9e98fbb 243 tmp = adxl362.read_reg(adxl362.SELF_TEST);
csinders 3:33cca9e98fbb 244 arrayAddress = 33;
csinders 1:fedb70ea0eaa 245 }
csinders 3:33cca9e98fbb 246 if (hexValue != 0 && tmp != 0) {
csinders 3:33cca9e98fbb 247 pc.printf("%#04x: %s=%#04x\n\r", hexValue, regNames[arrayAddress], tmp); // Print register
csinders 3:33cca9e98fbb 248 // set both to zero so they don't show again
csinders 3:33cca9e98fbb 249 arrayAddress = 0;
csinders 3:33cca9e98fbb 250 tmp = 0;
csinders 3:33cca9e98fbb 251 }
csinders 1:fedb70ea0eaa 252 }
csinders 2:b56010953f54 253 return 0;
csinders 0:d5c236ffbd5e 254
csinders 0:d5c236ffbd5e 255 // below is github with data for ADXL362 methods
csinders 0:d5c236ffbd5e 256 //https://github.com/analogdevicesinc/mbed-adi/blob/master/libraries/ADXL362/ADXL362.cpp
csinders 0:d5c236ffbd5e 257 }