CPS-Lab1
/
Lab6
In this lab, you will: Construct a prototype
Diff: main.cpp
- Revision:
- 3:33cca9e98fbb
- Parent:
- 2:b56010953f54
- Child:
- 4:7616d6fd81d8
--- a/main.cpp Wed Feb 21 21:06:45 2018 +0000 +++ b/main.cpp Thu Feb 22 18:04:28 2018 +0000 @@ -11,38 +11,42 @@ void adxl362_get_average(int8_t avg[]); int main() { - pc.printf("Starting program\n\r"); + pc.printf("Starting program\r\n"); adxl362.reset(); wait_ms(600); // we need to wait at least 500ms after ADXL362 reset adxl362.set_mode(ADXL362::MEASUREMENT); int8_t x,y,z; int8_t avg[3]; // Avg values of x,y,z. x = avg[0], y = avg[1] z = avg[2] - - adxl362_reg_print(0,0); // Test of axdl_reg_print + int countOverThreshHold = 0; + adxl362_reg_print(1,0); // Test of axdl_reg_print wait_ms(1000); // wait so that values can be seen int threshHoldRange = 10; // threshhold difference between avg and read values adxl362_get_average(avg);// Generate an average of what x,y,z are. //pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]); while(1) { x=adxl362.scanx_u8(); - y=adxl362.scany_u8(); + y=adxl362.scany_u8(); z=adxl362.scanz_u8(); + if (x > (avg[0] + threshHoldRange) || x < (avg[0] - threshHoldRange) || y > (avg[1] + threshHoldRange) || y < (avg[1] - threshHoldRange) - //|| z > (zAvg + threshHoldRange) || z < (zAvg - threshHoldRange)) { + || z > (avg[2] + threshHoldRange) || z < (avg[2] - threshHoldRange)) { // commented out z as it gives weird values - ) { + // ) { /* print cases used to test threshhold range pc.printf("x = %d y = %d z = %d\r\n",x,y,z); pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]); pc.printf("Threshold range is = %d\r\n", threshHoldRange); */ - pc.printf("outside of threshold range\n\r"); + //pc.printf("outside of threshold range\n\r"); + countOverThreshHold++; + pc.printf("Gone over threshhold %d times\n\r", countOverThreshHold); myled = 1; // LED is ON wait_ms(1900); // Wait 1.9s. Last .1 sec is done while getting average adxl362_get_average(avg); // Generate new average myled = 0; // LED is OFF } + pc.printf("x = %d y = %d z = %d\r\n",x,y,z); wait_ms(100); } @@ -86,6 +90,15 @@ } int adxl362_reg_print(int start, int length) { + int end = 0x2E; + if ((start + length) < end) { + end = (start + length); // so it only goes to the length + } + if (length == 0) { + end = 0x2E; // so it prints till the end + } + + // Check if start is within registry if (start < 0 || start > 0x2E) { pc.printf("Error: start value passed to adxl362_reg_print outside of range of registry\n\r"); @@ -98,14 +111,12 @@ } // check if valid communication with device going - // For some reason getting 0xAC instead of 0xAD? - // Printing out what value we are getting if (adxl362.read_reg(adxl362.DEVID_AD) != 0xAD) { pc.printf("Error: Unable to read from DEVID_AD register\n\r"); return -1; - } + } // String array with all of the names of the different registers in order - char regNames [37][20] = { + char regNames [40][20] = { "DEVID_AD", "DEVID_MST", "PARTID", "REVID", "XDATA", "YDATA", "ZDATA", "STATUS", "FIFO_ENTRIES_L", @@ -119,23 +130,125 @@ "FIFO_CONTROL", "FIFO_SAMPLES", "INTMAP1", "INTMATP2", "FILTER_CTL", "POWER_CTL", "SELF_TEST"}; - // Example for first two registers - // I think their is a better way to do this, but unsure of how. - for (int i = 0; i < 2; i++) { + + + for (int hexValue = 0; hexValue < end; hexValue++) { uint8_t tmp = 0; // hex value location of the register - uint8_t hexValue = 0; // Hex value of the register + uint8_t arrayAddress = 0; // Hex value of the register // May need to increment the registers inside of the if, since the values // are not always sequential // You can find registers inside of ADXL362.h file in ADXL362 folder // they are the ADXL362_register_t - if (i == 0) { + if (hexValue == 0) { tmp = adxl362.read_reg(adxl362.DEVID_AD); - hexValue = 0x00; - } else if (i == 1) { + arrayAddress = 0; + } else if (hexValue == 1) { tmp = adxl362.read_reg(adxl362.DEVID_MST); - hexValue = 0x01; + arrayAddress = 1; + } else if (hexValue == 2) { + tmp = adxl362.read_reg(adxl362.PARTID); + arrayAddress = 2; + } else if (hexValue == 3) { + tmp = adxl362.read_reg(adxl362.REVID); + arrayAddress = 3; + } else if (hexValue == 8) { + tmp = adxl362.read_reg(adxl362.XDATA); + arrayAddress = 4; + } else if (hexValue == 9) { + tmp = adxl362.read_reg(adxl362.YDATA); + arrayAddress = 5; + } else if (hexValue == 0x0A) { + tmp = adxl362.read_reg(adxl362.ZDATA); + arrayAddress = 6; + } else if (hexValue == 0x0B) { + tmp = adxl362.read_reg(adxl362.STATUS); + arrayAddress = 7; + } else if (hexValue == 0x0C) { + tmp = adxl362.read_reg(adxl362.FIFO_ENTRIES_L); + arrayAddress = 8; + } else if (hexValue == 0x0D) { + tmp = adxl362.read_reg(adxl362.FIFO_ENTRIES_H); + arrayAddress = 9; + } else if (hexValue == 0x0E) { + tmp = adxl362.read_reg(adxl362.XDATA_L); + arrayAddress = 9; + } else if (hexValue == 0x0F) { + tmp = adxl362.read_reg(adxl362.XDATA_H); + arrayAddress = 10; + } else if (hexValue == 0x10) { + tmp = adxl362.read_reg(adxl362.YDATA_L); + arrayAddress = 11; + } else if (hexValue == 0x11) { + tmp = adxl362.read_reg(adxl362.YDATA_H); + arrayAddress = 12; + } else if (hexValue == 0x12) { + tmp = adxl362.read_reg(adxl362.YDATA_H); + arrayAddress = 13; + } else if (hexValue == 0x13) { + tmp = adxl362.read_reg(adxl362.ZDATA_L); + arrayAddress = 14; + } else if (hexValue == 0x14) { + tmp = adxl362.read_reg(adxl362.ZDATA_H); + arrayAddress = 15; + } else if (hexValue == 0x15) { + // RESERVED + } else if (hexValue == 0x16) { + // RESERVED + } else if (hexValue == 0x1F) { + tmp = adxl362.read_reg(adxl362.SOFT_RESET); + arrayAddress = 18; + } else if (hexValue == 0x20) { + tmp = adxl362.read_reg(adxl362.THRESH_ACT_L); + arrayAddress = 19; + } else if (hexValue == 0x21) { + tmp = adxl362.read_reg(adxl362.THRESH_ACT_H); + arrayAddress = 20; + } else if (hexValue == 0x22) { + tmp = adxl362.read_reg(adxl362.TIME_ACT); + arrayAddress = 21; + } else if (hexValue == 0x23) { + tmp = adxl362.read_reg(adxl362.THRESH_INACT_L); + arrayAddress = 22; + } else if (hexValue == 0x24) { + tmp = adxl362.read_reg(adxl362.THRESH_INACT_H); + arrayAddress = 23; + } else if (hexValue == 0x25) { + tmp = adxl362.read_reg(adxl362.TIME_INACT_L); + arrayAddress = 24; + } else if (hexValue == 0x26) { + tmp = adxl362.read_reg(adxl362.TIME_INACT_H); + arrayAddress = 25; + } else if (hexValue == 0x27) { + tmp = adxl362.read_reg(adxl362.ACT_INACT_CTL); + arrayAddress = 26; + } else if (hexValue == 0x28) { + tmp = adxl362.read_reg(adxl362.FIFO_CONTROL); + arrayAddress = 27; + } else if (hexValue == 0x29) { + tmp = adxl362.read_reg(adxl362.FIFO_SAMPLES); + arrayAddress = 28; + } else if (hexValue == 0x2A) { + tmp = adxl362.read_reg(adxl362.INTMAP1); + arrayAddress = 29; + } else if (hexValue == 0x2B) { + tmp = adxl362.read_reg(adxl362.INTMAP2); + arrayAddress = 30; + } else if (hexValue == 0x2C) { + tmp = adxl362.read_reg(adxl362.FILTER_CTL); + arrayAddress = 31; + } else if (hexValue == 0x2D) { + tmp = adxl362.read_reg(adxl362.POWER_CTL); + arrayAddress = 32; + } else if (hexValue == 0x2E) { + tmp = adxl362.read_reg(adxl362.SELF_TEST); + arrayAddress = 33; } - pc.printf("%#04x: %s=%#04x\n\r", hexValue, regNames[i], tmp); // Print register + if (hexValue != 0 && tmp != 0) { + pc.printf("%#04x: %s=%#04x\n\r", hexValue, regNames[arrayAddress], tmp); // Print register + // set both to zero so they don't show again + arrayAddress = 0; + tmp = 0; + } } return 0;