CPS-Lab1
/
Lab6
In this lab, you will: Construct a prototype
main.cpp@1:fedb70ea0eaa, 2018-02-20 (annotated)
- Committer:
- csinders
- Date:
- Tue Feb 20 20:36:42 2018 +0000
- Revision:
- 1:fedb70ea0eaa
- Parent:
- 0:d5c236ffbd5e
- Child:
- 2:b56010953f54
Still working on ADXL_reg_print; confirmed that accelerometer is working.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:d5c236ffbd5e | 8 | |
csinders | 1:fedb70ea0eaa | 9 | int adxl362_reg_print(int start, int length); |
csinders | 1:fedb70ea0eaa | 10 | |
csinders | 0:d5c236ffbd5e | 11 | int main() { |
csinders | 0:d5c236ffbd5e | 12 | pc.printf("Starting program\n\r"); |
csinders | 0:d5c236ffbd5e | 13 | adxl362.reset(); |
csinders | 0:d5c236ffbd5e | 14 | wait_ms(600); // we need to wait at least 500ms after ADXL362 reset |
csinders | 0:d5c236ffbd5e | 15 | adxl362.set_mode(ADXL362::MEASUREMENT); |
csinders | 0:d5c236ffbd5e | 16 | int8_t x,y,z; |
csinders | 0:d5c236ffbd5e | 17 | |
csinders | 1:fedb70ea0eaa | 18 | adxl362_reg_print(0,1); |
csinders | 1:fedb70ea0eaa | 19 | wait_ms(1000); |
csinders | 1:fedb70ea0eaa | 20 | /* Commented out while working on adxl362_reg_print |
csinders | 0:d5c236ffbd5e | 21 | while(1) { |
csinders | 0:d5c236ffbd5e | 22 | x=adxl362.scanx_u8(); |
csinders | 0:d5c236ffbd5e | 23 | y=adxl362.scany_u8(); |
csinders | 0:d5c236ffbd5e | 24 | z=adxl362.scanz_u8(); |
csinders | 0:d5c236ffbd5e | 25 | pc.printf("x = %d y = %d z = %d\r\n",x,y,z); |
csinders | 0:d5c236ffbd5e | 26 | wait_ms(100); |
csinders | 0:d5c236ffbd5e | 27 | } |
csinders | 1:fedb70ea0eaa | 28 | */ |
csinders | 0:d5c236ffbd5e | 29 | } |
csinders | 0:d5c236ffbd5e | 30 | |
csinders | 0:d5c236ffbd5e | 31 | int adxl362_reg_print(int start, int length) { |
csinders | 0:d5c236ffbd5e | 32 | // Check if start is within registry |
csinders | 0:d5c236ffbd5e | 33 | if (start < 0 || start > 0x2E) { |
csinders | 0:d5c236ffbd5e | 34 | pc.printf("Error: start value passed to adxl362_reg_print outside of range of registry\n\r"); |
csinders | 0:d5c236ffbd5e | 35 | return -1; |
csinders | 0:d5c236ffbd5e | 36 | } |
csinders | 0:d5c236ffbd5e | 37 | // check if length is negative |
csinders | 0:d5c236ffbd5e | 38 | if (length < 0) { |
csinders | 0:d5c236ffbd5e | 39 | pc.printf("Error: length passed to adxl362_reg_print is negative\n\r"); |
csinders | 0:d5c236ffbd5e | 40 | return -1; |
csinders | 0:d5c236ffbd5e | 41 | } |
csinders | 0:d5c236ffbd5e | 42 | |
csinders | 0:d5c236ffbd5e | 43 | // check if valid communication with device going |
csinders | 1:fedb70ea0eaa | 44 | // For some reason getting 0xAC instead of 0xAD? |
csinders | 1:fedb70ea0eaa | 45 | // Printing out what value we are getting |
csinders | 1:fedb70ea0eaa | 46 | int ableToRead = adxl362.read_reg(adxl362.DEVID_AD); |
csinders | 1:fedb70ea0eaa | 47 | pc.printf("ableToRead = %d\n\r", ableToRead); |
csinders | 1:fedb70ea0eaa | 48 | /* TEMPORARILY COMMENTED OUT WHILE IT IS READING INCORRECTLY |
csinders | 1:fedb70ea0eaa | 49 | if (ableToRead != 0xAD) { |
csinders | 0:d5c236ffbd5e | 50 | pc.printf("Error: Unable to read from DEVID_AD register\n\r"); |
csinders | 1:fedb70ea0eaa | 51 | return -1; |
csinders | 0:d5c236ffbd5e | 52 | } |
csinders | 1:fedb70ea0eaa | 53 | */ |
csinders | 0:d5c236ffbd5e | 54 | // String array with all of the names of the different registers in order |
csinders | 1:fedb70ea0eaa | 55 | char regNames [37][20] = { |
csinders | 0:d5c236ffbd5e | 56 | "DEVID_AD", "DEVID_MST", "PARTID", |
csinders | 0:d5c236ffbd5e | 57 | "REVID", "XDATA", "YDATA", |
csinders | 0:d5c236ffbd5e | 58 | "ZDATA", "STATUS", "FIFO_ENTRIES_L", |
csinders | 0:d5c236ffbd5e | 59 | "FIFO_ENTRIES_H", "XDATA_L", "XDATA_H", |
csinders | 0:d5c236ffbd5e | 60 | "YDATA_L", "YDATA_H", "ZDATA_L", |
csinders | 0:d5c236ffbd5e | 61 | "ZDATA_H", "TEMP_L", "TEMP_H", |
csinders | 0:d5c236ffbd5e | 62 | "RESERVED", "RESERVED", "SOFT_RESET", |
csinders | 0:d5c236ffbd5e | 63 | "THRESH_ACT_L", "THRESH_ACT_H", "TIME_INACT_L", |
csinders | 0:d5c236ffbd5e | 64 | "TIME_ACT", "THRESH_INACT_L", "THRESH_INACT_H", |
csinders | 0:d5c236ffbd5e | 65 | "TIME_INACT_L", "TIME_INACT_H", "ACT_INACT_CTL", |
csinders | 0:d5c236ffbd5e | 66 | "FIFO_CONTROL", "FIFO_SAMPLES", "INTMAP1", |
csinders | 0:d5c236ffbd5e | 67 | "INTMATP2", "FILTER_CTL", "POWER_CTL", |
csinders | 0:d5c236ffbd5e | 68 | "SELF_TEST"}; |
csinders | 1:fedb70ea0eaa | 69 | // Example for first two registers |
csinders | 1:fedb70ea0eaa | 70 | // I think their is a better way to do this, but unsure of how. |
csinders | 1:fedb70ea0eaa | 71 | for (int i = 0; i < 2; i++) { |
csinders | 1:fedb70ea0eaa | 72 | uint8_t tmp; // hex value location of the register |
csinders | 1:fedb70ea0eaa | 73 | uint8_t hexValue; // Hex value of the register |
csinders | 1:fedb70ea0eaa | 74 | // May need to increment the registers inside of the if, since the values |
csinders | 1:fedb70ea0eaa | 75 | // are not always sequential |
csinders | 1:fedb70ea0eaa | 76 | // You can find registers inside of ADXL362.h file in ADXL362 folder |
csinders | 1:fedb70ea0eaa | 77 | // they are the ADXL362_register_t |
csinders | 1:fedb70ea0eaa | 78 | if (i == 0) { |
csinders | 1:fedb70ea0eaa | 79 | tmp = adxl362.read_reg(adxl362.DEVID_AD); |
csinders | 1:fedb70ea0eaa | 80 | hexValue = 0x00; |
csinders | 1:fedb70ea0eaa | 81 | } else if (i == 1) { |
csinders | 1:fedb70ea0eaa | 82 | tmp = adxl362.read_reg(adxl362.DEVID_MST); |
csinders | 1:fedb70ea0eaa | 83 | hexValue = 0x01; |
csinders | 1:fedb70ea0eaa | 84 | } |
csinders | 1:fedb70ea0eaa | 85 | pc.printf("%#04x: %s=%#04x\n\r", hexValue, regNames[i], tmp); // Print register |
csinders | 1:fedb70ea0eaa | 86 | } |
csinders | 1:fedb70ea0eaa | 87 | return 1; |
csinders | 0:d5c236ffbd5e | 88 | |
csinders | 0:d5c236ffbd5e | 89 | // below is github with data for ADXL362 methods |
csinders | 0:d5c236ffbd5e | 90 | //https://github.com/analogdevicesinc/mbed-adi/blob/master/libraries/ADXL362/ADXL362.cpp |
csinders | 0:d5c236ffbd5e | 91 | } |