Jackie Youngs
/
Lab6
Lab6_
main.cpp@2:e84a46c82e7a, 2018-02-22 (annotated)
- Committer:
- youngs2
- Date:
- Thu Feb 22 20:06:51 2018 +0000
- Revision:
- 2:e84a46c82e7a
- Parent:
- 1:308da76c50c5
Lab6_
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
youngs2 | 0:36f6b91a8ff8 | 1 | #include "mbed.h" |
youngs2 | 0:36f6b91a8ff8 | 2 | #include "ADXL362.h" |
youngs2 | 0:36f6b91a8ff8 | 3 | |
youngs2 | 0:36f6b91a8ff8 | 4 | // Interface pulled from ADXL362.cpp |
youngs2 | 0:36f6b91a8ff8 | 5 | // ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) : |
youngs2 | 0:36f6b91a8ff8 | 6 | ADXL362 adxl362(PA_0,PA_7,PA_6,PA_1); |
youngs2 | 1:308da76c50c5 | 7 | DigitalOut led1(LED1); |
youngs2 | 0:36f6b91a8ff8 | 8 | |
youngs2 | 0:36f6b91a8ff8 | 9 | |
youngs2 | 0:36f6b91a8ff8 | 10 | int adxl362_reg_print(int start, int length) { |
youngs2 | 0:36f6b91a8ff8 | 11 | if(length == 0) |
youngs2 | 1:308da76c50c5 | 12 | length = 46; |
youngs2 | 0:36f6b91a8ff8 | 13 | int i = start, end = start + length; |
youngs2 | 0:36f6b91a8ff8 | 14 | if(start > 0x2E || start < 0x00) |
youngs2 | 0:36f6b91a8ff8 | 15 | return(-1); |
youngs2 | 0:36f6b91a8ff8 | 16 | if(length < 0) |
youngs2 | 0:36f6b91a8ff8 | 17 | return(-1); |
youngs2 | 0:36f6b91a8ff8 | 18 | if (adxl362.read_reg((ADXL362::ADXL362_register_t) 0x00) != 0xAD) |
youngs2 | 0:36f6b91a8ff8 | 19 | return(-1); |
youngs2 | 1:308da76c50c5 | 20 | if (end > 47) |
youngs2 | 1:308da76c50c5 | 21 | return(-1); |
youngs2 | 0:36f6b91a8ff8 | 22 | for( i = start; i <= end; i++) { |
youngs2 | 0:36f6b91a8ff8 | 23 | //0x16 -> 0x1E not used |
youngs2 | 0:36f6b91a8ff8 | 24 | //0x04 -> 0x07 not used |
youngs2 | 0:36f6b91a8ff8 | 25 | if (i == 0x04) i = 0x08; |
youngs2 | 0:36f6b91a8ff8 | 26 | if (i == 0x16) i = 0x1F; |
youngs2 | 0:36f6b91a8ff8 | 27 | char name[25]; |
youngs2 | 0:36f6b91a8ff8 | 28 | uint8_t regval = adxl362.read_reg((ADXL362::ADXL362_register_t) i); |
youngs2 | 0:36f6b91a8ff8 | 29 | switch (i) { |
youngs2 | 0:36f6b91a8ff8 | 30 | case 0x00: |
youngs2 | 0:36f6b91a8ff8 | 31 | strcpy(name,"DEVID_A"); |
youngs2 | 0:36f6b91a8ff8 | 32 | break; |
youngs2 | 0:36f6b91a8ff8 | 33 | case 0x01: |
youngs2 | 0:36f6b91a8ff8 | 34 | strcpy(name,"DEVID_MST"); |
youngs2 | 0:36f6b91a8ff8 | 35 | break; |
youngs2 | 0:36f6b91a8ff8 | 36 | case 0x02: |
youngs2 | 0:36f6b91a8ff8 | 37 | strcpy(name, "PARTID"); |
youngs2 | 0:36f6b91a8ff8 | 38 | break; |
youngs2 | 0:36f6b91a8ff8 | 39 | case 0x03: |
youngs2 | 0:36f6b91a8ff8 | 40 | strcpy(name,"REVID"); |
youngs2 | 0:36f6b91a8ff8 | 41 | break; |
youngs2 | 0:36f6b91a8ff8 | 42 | case 0x08: |
youngs2 | 0:36f6b91a8ff8 | 43 | strcpy(name,"XDATA"); |
youngs2 | 0:36f6b91a8ff8 | 44 | break; |
youngs2 | 0:36f6b91a8ff8 | 45 | case 0x09: |
youngs2 | 0:36f6b91a8ff8 | 46 | strcpy(name,"YDATA"); |
youngs2 | 0:36f6b91a8ff8 | 47 | break; |
youngs2 | 0:36f6b91a8ff8 | 48 | case 0x0A: |
youngs2 | 0:36f6b91a8ff8 | 49 | strcpy(name,"ZDATA"); |
youngs2 | 0:36f6b91a8ff8 | 50 | break; |
youngs2 | 0:36f6b91a8ff8 | 51 | case 0x0B: |
youngs2 | 0:36f6b91a8ff8 | 52 | strcpy(name,"STATUS"); |
youngs2 | 0:36f6b91a8ff8 | 53 | break; |
youngs2 | 0:36f6b91a8ff8 | 54 | case 0x0C: |
youngs2 | 0:36f6b91a8ff8 | 55 | strcpy(name,"FIFO_ENTRIES_L"); |
youngs2 | 0:36f6b91a8ff8 | 56 | break; |
youngs2 | 0:36f6b91a8ff8 | 57 | case 0x0D: |
youngs2 | 0:36f6b91a8ff8 | 58 | strcpy(name,"FIFO_ENTRIES_H"); |
youngs2 | 0:36f6b91a8ff8 | 59 | break; |
youngs2 | 0:36f6b91a8ff8 | 60 | case 0x0E: |
youngs2 | 0:36f6b91a8ff8 | 61 | strcpy(name,"XDATA_L"); |
youngs2 | 0:36f6b91a8ff8 | 62 | break; |
youngs2 | 0:36f6b91a8ff8 | 63 | case 0x0F: |
youngs2 | 0:36f6b91a8ff8 | 64 | strcpy(name,"XDATA_H"); |
youngs2 | 0:36f6b91a8ff8 | 65 | break; |
youngs2 | 0:36f6b91a8ff8 | 66 | case 0x10: |
youngs2 | 0:36f6b91a8ff8 | 67 | strcpy(name,"YDATA_L"); |
youngs2 | 0:36f6b91a8ff8 | 68 | break; |
youngs2 | 0:36f6b91a8ff8 | 69 | case 0x11: |
youngs2 | 0:36f6b91a8ff8 | 70 | strcpy(name,"YDATA_H"); |
youngs2 | 0:36f6b91a8ff8 | 71 | break; |
youngs2 | 0:36f6b91a8ff8 | 72 | case 0x12: |
youngs2 | 0:36f6b91a8ff8 | 73 | strcpy(name,"ZDATA_L"); |
youngs2 | 0:36f6b91a8ff8 | 74 | break; |
youngs2 | 0:36f6b91a8ff8 | 75 | case 0x13: |
youngs2 | 0:36f6b91a8ff8 | 76 | strcpy(name,"ZDATA_H"); |
youngs2 | 0:36f6b91a8ff8 | 77 | break; |
youngs2 | 0:36f6b91a8ff8 | 78 | case 0x14: |
youngs2 | 0:36f6b91a8ff8 | 79 | strcpy(name,"TEMP_L"); |
youngs2 | 0:36f6b91a8ff8 | 80 | break; |
youngs2 | 0:36f6b91a8ff8 | 81 | case 0x15: |
youngs2 | 0:36f6b91a8ff8 | 82 | strcpy(name,"TEMP_H"); |
youngs2 | 0:36f6b91a8ff8 | 83 | break; |
youngs2 | 0:36f6b91a8ff8 | 84 | case 0x1F: |
youngs2 | 0:36f6b91a8ff8 | 85 | strcpy(name,"SOFT_RESET"); |
youngs2 | 0:36f6b91a8ff8 | 86 | break; |
youngs2 | 0:36f6b91a8ff8 | 87 | case 0x20: |
youngs2 | 0:36f6b91a8ff8 | 88 | strcpy(name,"THRESH_ACT_L"); |
youngs2 | 0:36f6b91a8ff8 | 89 | break; |
youngs2 | 0:36f6b91a8ff8 | 90 | case 0x21: |
youngs2 | 0:36f6b91a8ff8 | 91 | strcpy(name,"THRESH_ACT_H"); |
youngs2 | 0:36f6b91a8ff8 | 92 | break; |
youngs2 | 0:36f6b91a8ff8 | 93 | case 0x22: |
youngs2 | 0:36f6b91a8ff8 | 94 | strcpy(name,"TIME_ACT"); |
youngs2 | 0:36f6b91a8ff8 | 95 | break; |
youngs2 | 0:36f6b91a8ff8 | 96 | case 0x23: |
youngs2 | 0:36f6b91a8ff8 | 97 | strcpy(name,"THRES_INACT_L"); |
youngs2 | 0:36f6b91a8ff8 | 98 | break; |
youngs2 | 0:36f6b91a8ff8 | 99 | case 0x24: |
youngs2 | 0:36f6b91a8ff8 | 100 | strcpy(name,"THRESH_INACT_H"); |
youngs2 | 0:36f6b91a8ff8 | 101 | break; |
youngs2 | 0:36f6b91a8ff8 | 102 | case 0x25: |
youngs2 | 0:36f6b91a8ff8 | 103 | strcpy(name,"TIME_INACT_L"); |
youngs2 | 0:36f6b91a8ff8 | 104 | break; |
youngs2 | 0:36f6b91a8ff8 | 105 | case 0x26: |
youngs2 | 0:36f6b91a8ff8 | 106 | strcpy(name,"TIME_INACT_H"); |
youngs2 | 0:36f6b91a8ff8 | 107 | break; |
youngs2 | 0:36f6b91a8ff8 | 108 | case 0x27: |
youngs2 | 0:36f6b91a8ff8 | 109 | strcpy(name,"ACT_INACT_CTL"); |
youngs2 | 0:36f6b91a8ff8 | 110 | break; |
youngs2 | 0:36f6b91a8ff8 | 111 | case 0x28: |
youngs2 | 0:36f6b91a8ff8 | 112 | strcpy(name,"FIFO_CONTROL"); |
youngs2 | 0:36f6b91a8ff8 | 113 | break; |
youngs2 | 0:36f6b91a8ff8 | 114 | case 0x29: |
youngs2 | 0:36f6b91a8ff8 | 115 | strcpy(name,"FIFO_SAMPLES"); |
youngs2 | 0:36f6b91a8ff8 | 116 | break; |
youngs2 | 0:36f6b91a8ff8 | 117 | case 0x2A: |
youngs2 | 0:36f6b91a8ff8 | 118 | strcpy(name,"INTMAP1"); |
youngs2 | 0:36f6b91a8ff8 | 119 | break; |
youngs2 | 0:36f6b91a8ff8 | 120 | case 0x2B: |
youngs2 | 0:36f6b91a8ff8 | 121 | strcpy(name,"INTMAP2"); |
youngs2 | 0:36f6b91a8ff8 | 122 | break; |
youngs2 | 0:36f6b91a8ff8 | 123 | case 0x2C: |
youngs2 | 0:36f6b91a8ff8 | 124 | strcpy(name,"FILTER_CTL"); |
youngs2 | 0:36f6b91a8ff8 | 125 | break; |
youngs2 | 0:36f6b91a8ff8 | 126 | case 0x2D: |
youngs2 | 0:36f6b91a8ff8 | 127 | strcpy(name,"POWER_CTL"); |
youngs2 | 0:36f6b91a8ff8 | 128 | break; |
youngs2 | 0:36f6b91a8ff8 | 129 | case 0x2E: |
youngs2 | 0:36f6b91a8ff8 | 130 | strcpy(name,"SELF_TEST"); |
youngs2 | 0:36f6b91a8ff8 | 131 | break; |
youngs2 | 0:36f6b91a8ff8 | 132 | default: |
youngs2 | 0:36f6b91a8ff8 | 133 | printf("ERR\n\r"); |
youngs2 | 0:36f6b91a8ff8 | 134 | } |
youngs2 | 1:308da76c50c5 | 135 | printf("0x%02X: %s = 0x%02X\n\r", i, name, regval); |
youngs2 | 0:36f6b91a8ff8 | 136 | |
youngs2 | 0:36f6b91a8ff8 | 137 | } |
youngs2 | 0:36f6b91a8ff8 | 138 | return(0); |
youngs2 | 0:36f6b91a8ff8 | 139 | } |
youngs2 | 0:36f6b91a8ff8 | 140 | |
youngs2 | 0:36f6b91a8ff8 | 141 | |
youngs2 | 0:36f6b91a8ff8 | 142 | |
youngs2 | 0:36f6b91a8ff8 | 143 | int main() { |
youngs2 | 0:36f6b91a8ff8 | 144 | adxl362.reset(); |
youngs2 | 0:36f6b91a8ff8 | 145 | wait_ms(600); // we need to wait at least 500ms after ADXL362 reset |
youngs2 | 0:36f6b91a8ff8 | 146 | adxl362.set_mode(ADXL362::MEASUREMENT); |
youngs2 | 1:308da76c50c5 | 147 | int return_val, start, length; |
youngs2 | 1:308da76c50c5 | 148 | int8_t x,y,z,a,b,c, t=4; |
youngs2 | 1:308da76c50c5 | 149 | printf("Enter start: \n\r"); |
youngs2 | 1:308da76c50c5 | 150 | scanf("%d", &start); |
youngs2 | 1:308da76c50c5 | 151 | printf("Enter length: \n\r"); |
youngs2 | 1:308da76c50c5 | 152 | scanf("%d", &length); |
youngs2 | 1:308da76c50c5 | 153 | return_val = adxl362_reg_print(start, length); |
youngs2 | 1:308da76c50c5 | 154 | printf("Return Val Read Reg: %d \n\r", return_val); |
youngs2 | 1:308da76c50c5 | 155 | wait_ms(5000); |
youngs2 | 0:36f6b91a8ff8 | 156 | while(1) { |
youngs2 | 0:36f6b91a8ff8 | 157 | x=adxl362.scanx_u8(); |
youngs2 | 0:36f6b91a8ff8 | 158 | y=adxl362.scany_u8(); |
youngs2 | 0:36f6b91a8ff8 | 159 | z=adxl362.scanz_u8(); |
youngs2 | 1:308da76c50c5 | 160 | wait_ms(10); |
youngs2 | 1:308da76c50c5 | 161 | a=adxl362.scanx_u8(); |
youngs2 | 1:308da76c50c5 | 162 | b=adxl362.scany_u8(); |
youngs2 | 1:308da76c50c5 | 163 | c=adxl362.scanz_u8(); |
youngs2 | 1:308da76c50c5 | 164 | if (a >= x+t || a <= x-t || |
youngs2 | 1:308da76c50c5 | 165 | b >= y+t || b <= y-t || |
youngs2 | 1:308da76c50c5 | 166 | c >= z+t || c <= z-t) { |
youngs2 | 1:308da76c50c5 | 167 | printf("Knock\n\r"); |
youngs2 | 1:308da76c50c5 | 168 | led1 = 1; |
youngs2 | 1:308da76c50c5 | 169 | wait_ms(2000); |
youngs2 | 1:308da76c50c5 | 170 | led1 = 0; |
youngs2 | 1:308da76c50c5 | 171 | |
youngs2 | 1:308da76c50c5 | 172 | } |
youngs2 | 1:308da76c50c5 | 173 | |
youngs2 | 1:308da76c50c5 | 174 | } |
youngs2 | 0:36f6b91a8ff8 | 175 | } |