Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "ADXL362.h" 00003 00004 // Interface pulled from ADXL362.cpp 00005 // ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) : 00006 ADXL362 adxl362(PA_0,PA_7,PA_6,PA_1); 00007 Serial pc(USBTX, USBRX); 00008 DigitalOut myled(LED1); 00009 00010 int adxl362_reg_print(int start, int length); 00011 void adxl362_get_average(int16_t avg[]); 00012 00013 int main() { 00014 pc.printf("Starting program\r\n"); 00015 adxl362.reset(); 00016 wait_ms(600); // we need to wait at least 500ms after ADXL362 reset 00017 adxl362.set_mode(ADXL362::MEASUREMENT); 00018 int16_t x,y,z; 00019 int16_t avg[3]; // Avg values of x,y,z. x = avg[0], y = avg[1] z = avg[2] 00020 int countOverThreshHold = 0; 00021 adxl362_reg_print(1,0); // Test of axdl_reg_print 00022 wait_ms(1000); // wait so that values can be seen 00023 int threshHoldRange = 30; // threshhold difference between avg and read values 00024 adxl362_get_average(avg);// Generate an average of what x,y,z are. 00025 //pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]); 00026 while(1) { 00027 x=adxl362.scanx(); 00028 y=adxl362.scany(); 00029 z=adxl362.scanz(); 00030 00031 if (x > (avg[0] + threshHoldRange) || x < (avg[0] - threshHoldRange) 00032 || y > (avg[1] + threshHoldRange) || y < (avg[1] - threshHoldRange) 00033 || z > (avg[2] + threshHoldRange) || z < (avg[2] - threshHoldRange)) { 00034 // commented out z as it gives weird values 00035 // ) { 00036 /* print cases used to test threshhold range 00037 pc.printf("x = %d y = %d z = %d\r\n",x,y,z); 00038 pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]); 00039 pc.printf("Threshold range is = %d\r\n", threshHoldRange); 00040 */ 00041 //pc.printf("outside of threshold range\n\r"); 00042 countOverThreshHold++; 00043 pc.printf("Gone over threshhold %d times\n\r", countOverThreshHold); 00044 myled = 1; // LED is ON 00045 wait_ms(1900); // Wait 1.9s. Last .1 sec is done while getting average 00046 adxl362_get_average(avg); // Generate new average 00047 myled = 0; // LED is OFF 00048 } 00049 00050 pc.printf("x = %d y = %d z = %d\r\n",x,y,z); 00051 wait_ms(100); 00052 } 00053 } 00054 00055 void adxl362_get_average(int16_t avg[]) { 00056 // Generate an average of what x,y,z are. 00057 int numberOfPolls = 2; 00058 // reset average back to zero for each 00059 int tmp[3]; // required because avg was having integer overflow 00060 tmp[0] = 0; 00061 tmp[1] = 0; 00062 tmp[2] = 0; 00063 avg[0] = 0; 00064 avg[1] = 0; 00065 avg[2] = 0; 00066 int16_t x,y,z; 00067 // Poll each value 10 times 00068 for (int i = 0; i < numberOfPolls; i++) { 00069 //pc.printf("x = %d y = %d z = %d\r\n",x,y,z); 00070 x = adxl362.scanx(); 00071 tmp[0]+= x; 00072 y = adxl362.scany(); 00073 tmp[1]+= y; 00074 z=adxl362.scanz(); 00075 tmp[2]+= z; 00076 wait_ms(10); 00077 //pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",tmp[0],tmp[1],tmp[2]); 00078 00079 } 00080 // Divide each value by the number of polls to get the average 00081 tmp[0] = tmp[0] / numberOfPolls; 00082 tmp[1] = tmp[1] / numberOfPolls; 00083 tmp[2] = tmp[2] / numberOfPolls; 00084 // store values from the tmp to the passed in array 00085 avg[0] = tmp[0]; 00086 avg[1] = tmp[1]; 00087 avg[2] = tmp[2]; 00088 pc.printf("New Average: xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]); 00089 wait_ms(5000); 00090 } 00091 00092 int adxl362_reg_print(int start, int length) { 00093 int end = 0x2E; 00094 if ((start + length) < end) { 00095 end = (start + length); // so it only goes to the length 00096 } 00097 if (length == 0) { 00098 end = 0x2E; // so it prints till the end 00099 } 00100 00101 00102 // Check if start is within registry 00103 if (start < 0 || start > 0x2E) { 00104 pc.printf("Error: start value passed to adxl362_reg_print outside of range of registry\n\r"); 00105 return -1; 00106 } 00107 // check if length is negative 00108 if (length < 0) { 00109 pc.printf("Error: length passed to adxl362_reg_print is negative\n\r"); 00110 return -1; 00111 } 00112 00113 // check if valid communication with device going 00114 if (adxl362.read_reg(adxl362.DEVID_AD) != 0xAD) { 00115 pc.printf("Error: Unable to read from DEVID_AD register\n\r"); 00116 return -1; 00117 } 00118 // String array with all of the names of the different registers in order 00119 char regNames [40][20] = { 00120 "DEVID_AD", "DEVID_MST", "PARTID", 00121 "REVID", "XDATA", "YDATA", 00122 "ZDATA", "STATUS", "FIFO_ENTRIES_L", 00123 "FIFO_ENTRIES_H", "XDATA_L", "XDATA_H", 00124 "YDATA_L", "YDATA_H", "ZDATA_L", 00125 "ZDATA_H", "TEMP_L", "TEMP_H", 00126 "RESERVED", "RESERVED", "SOFT_RESET", 00127 "THRESH_ACT_L", "THRESH_ACT_H", "TIME_INACT_L", 00128 "TIME_ACT", "THRESH_INACT_L", "THRESH_INACT_H", 00129 "TIME_INACT_L", "TIME_INACT_H", "ACT_INACT_CTL", 00130 "FIFO_CONTROL", "FIFO_SAMPLES", "INTMAP1", 00131 "INTMATP2", "FILTER_CTL", "POWER_CTL", 00132 "SELF_TEST"}; 00133 00134 00135 for (int hexValue = 0; hexValue < end; hexValue++) { 00136 uint8_t tmp = 0; // hex value location of the register 00137 uint8_t arrayAddress = 0; // Hex value of the register 00138 // May need to increment the registers inside of the if, since the values 00139 // are not always sequential 00140 // You can find registers inside of ADXL362.h file in ADXL362 folder 00141 // they are the ADXL362_register_t 00142 if (hexValue == 0) { 00143 tmp = adxl362.read_reg(adxl362.DEVID_AD); 00144 arrayAddress = 0; 00145 } else if (hexValue == 1) { 00146 tmp = adxl362.read_reg(adxl362.DEVID_MST); 00147 arrayAddress = 1; 00148 } else if (hexValue == 2) { 00149 tmp = adxl362.read_reg(adxl362.PARTID); 00150 arrayAddress = 2; 00151 } else if (hexValue == 3) { 00152 tmp = adxl362.read_reg(adxl362.REVID); 00153 arrayAddress = 3; 00154 } else if (hexValue == 8) { 00155 tmp = adxl362.read_reg(adxl362.XDATA); 00156 arrayAddress = 4; 00157 } else if (hexValue == 9) { 00158 tmp = adxl362.read_reg(adxl362.YDATA); 00159 arrayAddress = 5; 00160 } else if (hexValue == 0x0A) { 00161 tmp = adxl362.read_reg(adxl362.ZDATA); 00162 arrayAddress = 6; 00163 } else if (hexValue == 0x0B) { 00164 tmp = adxl362.read_reg(adxl362.STATUS); 00165 arrayAddress = 7; 00166 } else if (hexValue == 0x0C) { 00167 tmp = adxl362.read_reg(adxl362.FIFO_ENTRIES_L); 00168 arrayAddress = 8; 00169 } else if (hexValue == 0x0D) { 00170 tmp = adxl362.read_reg(adxl362.FIFO_ENTRIES_H); 00171 arrayAddress = 9; 00172 } else if (hexValue == 0x0E) { 00173 tmp = adxl362.read_reg(adxl362.XDATA_L); 00174 arrayAddress = 9; 00175 } else if (hexValue == 0x0F) { 00176 tmp = adxl362.read_reg(adxl362.XDATA_H); 00177 arrayAddress = 10; 00178 } else if (hexValue == 0x10) { 00179 tmp = adxl362.read_reg(adxl362.YDATA_L); 00180 arrayAddress = 11; 00181 } else if (hexValue == 0x11) { 00182 tmp = adxl362.read_reg(adxl362.YDATA_H); 00183 arrayAddress = 12; 00184 } else if (hexValue == 0x12) { 00185 tmp = adxl362.read_reg(adxl362.YDATA_H); 00186 arrayAddress = 13; 00187 } else if (hexValue == 0x13) { 00188 tmp = adxl362.read_reg(adxl362.ZDATA_L); 00189 arrayAddress = 14; 00190 } else if (hexValue == 0x14) { 00191 tmp = adxl362.read_reg(adxl362.ZDATA_H); 00192 arrayAddress = 15; 00193 } else if (hexValue == 0x15) { 00194 // RESERVED 00195 } else if (hexValue == 0x16) { 00196 // RESERVED 00197 } else if (hexValue == 0x1F) { 00198 tmp = adxl362.read_reg(adxl362.SOFT_RESET); 00199 arrayAddress = 18; 00200 } else if (hexValue == 0x20) { 00201 tmp = adxl362.read_reg(adxl362.THRESH_ACT_L); 00202 arrayAddress = 19; 00203 } else if (hexValue == 0x21) { 00204 tmp = adxl362.read_reg(adxl362.THRESH_ACT_H); 00205 arrayAddress = 20; 00206 } else if (hexValue == 0x22) { 00207 tmp = adxl362.read_reg(adxl362.TIME_ACT); 00208 arrayAddress = 21; 00209 } else if (hexValue == 0x23) { 00210 tmp = adxl362.read_reg(adxl362.THRESH_INACT_L); 00211 arrayAddress = 22; 00212 } else if (hexValue == 0x24) { 00213 tmp = adxl362.read_reg(adxl362.THRESH_INACT_H); 00214 arrayAddress = 23; 00215 } else if (hexValue == 0x25) { 00216 tmp = adxl362.read_reg(adxl362.TIME_INACT_L); 00217 arrayAddress = 24; 00218 } else if (hexValue == 0x26) { 00219 tmp = adxl362.read_reg(adxl362.TIME_INACT_H); 00220 arrayAddress = 25; 00221 } else if (hexValue == 0x27) { 00222 tmp = adxl362.read_reg(adxl362.ACT_INACT_CTL); 00223 arrayAddress = 26; 00224 } else if (hexValue == 0x28) { 00225 tmp = adxl362.read_reg(adxl362.FIFO_CONTROL); 00226 arrayAddress = 27; 00227 } else if (hexValue == 0x29) { 00228 tmp = adxl362.read_reg(adxl362.FIFO_SAMPLES); 00229 arrayAddress = 28; 00230 } else if (hexValue == 0x2A) { 00231 tmp = adxl362.read_reg(adxl362.INTMAP1); 00232 arrayAddress = 29; 00233 } else if (hexValue == 0x2B) { 00234 tmp = adxl362.read_reg(adxl362.INTMAP2); 00235 arrayAddress = 30; 00236 } else if (hexValue == 0x2C) { 00237 tmp = adxl362.read_reg(adxl362.FILTER_CTL); 00238 arrayAddress = 31; 00239 } else if (hexValue == 0x2D) { 00240 tmp = adxl362.read_reg(adxl362.POWER_CTL); 00241 arrayAddress = 32; 00242 } else if (hexValue == 0x2E) { 00243 tmp = adxl362.read_reg(adxl362.SELF_TEST); 00244 arrayAddress = 33; 00245 } 00246 if (hexValue != 0 && tmp != 0) { 00247 pc.printf("%#04x: %s=%#04x\n\r", hexValue, regNames[arrayAddress], tmp); // Print register 00248 // set both to zero so they don't show again 00249 arrayAddress = 0; 00250 tmp = 0; 00251 } 00252 } 00253 return 0; 00254 00255 // below is github with data for ADXL362 methods 00256 //https://github.com/analogdevicesinc/mbed-adi/blob/master/libraries/ADXL362/ADXL362.cpp 00257 }
Generated on Wed Jul 13 2022 17:19:42 by
1.7.2