CPS-Lab1
/
Lab6
In this lab, you will: Construct a prototype
main.cpp@0:d5c236ffbd5e, 2018-02-20 (annotated)
- Committer:
- csinders
- Date:
- Tue Feb 20 15:54:29 2018 +0000
- Revision:
- 0:d5c236ffbd5e
- Child:
- 1:fedb70ea0eaa
Minor alterations to program. Switched with board is being used.
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 | 0:d5c236ffbd5e | 9 | int main() { |
csinders | 0:d5c236ffbd5e | 10 | pc.printf("Starting program\n\r"); |
csinders | 0:d5c236ffbd5e | 11 | adxl362.reset(); |
csinders | 0:d5c236ffbd5e | 12 | wait_ms(600); // we need to wait at least 500ms after ADXL362 reset |
csinders | 0:d5c236ffbd5e | 13 | adxl362.set_mode(ADXL362::MEASUREMENT); |
csinders | 0:d5c236ffbd5e | 14 | int8_t x,y,z; |
csinders | 0:d5c236ffbd5e | 15 | |
csinders | 0:d5c236ffbd5e | 16 | while(1) { |
csinders | 0:d5c236ffbd5e | 17 | x=adxl362.scanx_u8(); |
csinders | 0:d5c236ffbd5e | 18 | y=adxl362.scany_u8(); |
csinders | 0:d5c236ffbd5e | 19 | z=adxl362.scanz_u8(); |
csinders | 0:d5c236ffbd5e | 20 | pc.printf("x = %d y = %d z = %d\r\n",x,y,z); |
csinders | 0:d5c236ffbd5e | 21 | wait_ms(100); |
csinders | 0:d5c236ffbd5e | 22 | } |
csinders | 0:d5c236ffbd5e | 23 | } |
csinders | 0:d5c236ffbd5e | 24 | |
csinders | 0:d5c236ffbd5e | 25 | int adxl362_reg_print(int start, int length) { |
csinders | 0:d5c236ffbd5e | 26 | // Check if start is within registry |
csinders | 0:d5c236ffbd5e | 27 | if (start < 0 || start > 0x2E) { |
csinders | 0:d5c236ffbd5e | 28 | pc.printf("Error: start value passed to adxl362_reg_print outside of range of registry\n\r"); |
csinders | 0:d5c236ffbd5e | 29 | return -1; |
csinders | 0:d5c236ffbd5e | 30 | } |
csinders | 0:d5c236ffbd5e | 31 | // check if length is negative |
csinders | 0:d5c236ffbd5e | 32 | if (length < 0) { |
csinders | 0:d5c236ffbd5e | 33 | pc.printf("Error: length passed to adxl362_reg_print is negative\n\r"); |
csinders | 0:d5c236ffbd5e | 34 | return -1; |
csinders | 0:d5c236ffbd5e | 35 | } |
csinders | 0:d5c236ffbd5e | 36 | |
csinders | 0:d5c236ffbd5e | 37 | // check if valid communication with device going |
csinders | 0:d5c236ffbd5e | 38 | if (adxl362.read_reg(adxl362.DEVID_AD) != 0xAD) { |
csinders | 0:d5c236ffbd5e | 39 | pc.printf("Error: Unable to read from DEVID_AD register\n\r"); |
csinders | 0:d5c236ffbd5e | 40 | } |
csinders | 0:d5c236ffbd5e | 41 | |
csinders | 0:d5c236ffbd5e | 42 | |
csinders | 0:d5c236ffbd5e | 43 | // String array with all of the names of the different registers in order |
csinders | 0:d5c236ffbd5e | 44 | char regNames [0x2E][20] = { |
csinders | 0:d5c236ffbd5e | 45 | "DEVID_AD", "DEVID_MST", "PARTID", |
csinders | 0:d5c236ffbd5e | 46 | "REVID", "XDATA", "YDATA", |
csinders | 0:d5c236ffbd5e | 47 | "ZDATA", "STATUS", "FIFO_ENTRIES_L", |
csinders | 0:d5c236ffbd5e | 48 | "FIFO_ENTRIES_H", "XDATA_L", "XDATA_H", |
csinders | 0:d5c236ffbd5e | 49 | "YDATA_L", "YDATA_H", "ZDATA_L", |
csinders | 0:d5c236ffbd5e | 50 | "ZDATA_H", "TEMP_L", "TEMP_H", |
csinders | 0:d5c236ffbd5e | 51 | "RESERVED", "RESERVED", "SOFT_RESET", |
csinders | 0:d5c236ffbd5e | 52 | "THRESH_ACT_L", "THRESH_ACT_H", "TIME_INACT_L", |
csinders | 0:d5c236ffbd5e | 53 | "TIME_ACT", "THRESH_INACT_L", "THRESH_INACT_H", |
csinders | 0:d5c236ffbd5e | 54 | "TIME_INACT_L", "TIME_INACT_H", "ACT_INACT_CTL", |
csinders | 0:d5c236ffbd5e | 55 | "FIFO_CONTROL", "FIFO_SAMPLES", "INTMAP1", |
csinders | 0:d5c236ffbd5e | 56 | "INTMATP2", "FILTER_CTL", "POWER_CTL", |
csinders | 0:d5c236ffbd5e | 57 | "SELF_TEST"}; |
csinders | 0:d5c236ffbd5e | 58 | |
csinders | 0:d5c236ffbd5e | 59 | // below is github with data for ADXL362 methods |
csinders | 0:d5c236ffbd5e | 60 | //https://github.com/analogdevicesinc/mbed-adi/blob/master/libraries/ADXL362/ADXL362.cpp |
csinders | 0:d5c236ffbd5e | 61 | } |