lab 6

Dependencies:   ADXL362 mbed MPL3115A2

Committer:
htdoughe
Date:
Thu Feb 22 21:13:54 2018 +0000
Revision:
10:8dcc46dd53bf
Parent:
9:a14593af628d
Child:
11:5aba4ea23082
Child:
12:c6b056ad171e
declared variables

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rmneubau 0:0f663d16e8a2 1 #include "mbed.h"
rmneubau 0:0f663d16e8a2 2 #include "ADXL362.h"
htdoughe 4:8065ca897f8b 3 #include "string.h"
htdoughe 9:a14593af628d 4
htdoughe 9:a14593af628d 5 DigitalOut led1(LD3);
htdoughe 4:8065ca897f8b 6
htdoughe 4:8065ca897f8b 7 //map of register values and names
htdoughe 5:02aa71487c01 8 int regids[34];
htdoughe 5:02aa71487c01 9 char* regnames[34];
htdoughe 4:8065ca897f8b 10 //populating the map
htdoughe 4:8065ca897f8b 11 void initmap() {
htdoughe 5:02aa71487c01 12 regids[0] = 0x00;
htdoughe 5:02aa71487c01 13 regids[1] = 0x01;
htdoughe 5:02aa71487c01 14 regids[2] = 0x02;
htdoughe 5:02aa71487c01 15 regids[3] = 0x03;
htdoughe 5:02aa71487c01 16 regids[4] = 0x08;
htdoughe 5:02aa71487c01 17 regids[5] = 0x09;
htdoughe 5:02aa71487c01 18 regids[6] = 0x0A;
htdoughe 5:02aa71487c01 19 regids[7] = 0x0B;
htdoughe 5:02aa71487c01 20 regids[8] = 0x0c;
htdoughe 5:02aa71487c01 21 regids[9] = 0x0D;
htdoughe 5:02aa71487c01 22 regids[10] = 0x0E;
htdoughe 5:02aa71487c01 23 regids[11] = 0x0F;
htdoughe 5:02aa71487c01 24 regids[12] = 0x10;
htdoughe 5:02aa71487c01 25 regids[13] = 0x11;
htdoughe 5:02aa71487c01 26 regids[14] = 0x12;
htdoughe 5:02aa71487c01 27 regids[15] = 0x13;
htdoughe 5:02aa71487c01 28 regids[16] = 0x14;
htdoughe 5:02aa71487c01 29 regids[17] = 0x15;
htdoughe 5:02aa71487c01 30 regids[18] = 0x1F;
htdoughe 5:02aa71487c01 31 regids[19] = 0x20;
htdoughe 5:02aa71487c01 32 regids[20] = 0x21;
htdoughe 5:02aa71487c01 33 regids[21] = 0x22;
htdoughe 5:02aa71487c01 34 regids[22] = 0x23;
htdoughe 5:02aa71487c01 35 regids[23] = 0x24;
htdoughe 5:02aa71487c01 36 regids[24] = 0x25;
htdoughe 5:02aa71487c01 37 regids[25] = 0x26;
htdoughe 5:02aa71487c01 38 regids[26] = 0x27;
htdoughe 5:02aa71487c01 39 regids[27] = 0x28;
htdoughe 5:02aa71487c01 40 regids[28] = 0x29;
htdoughe 5:02aa71487c01 41 regids[29] = 0x2A;
htdoughe 5:02aa71487c01 42 regids[30] = 0x2B;
htdoughe 5:02aa71487c01 43 regids[31] = 0x2C;
htdoughe 5:02aa71487c01 44 regids[32] = 0x2D;
htdoughe 5:02aa71487c01 45 regids[33] = 0x2E;
htdoughe 5:02aa71487c01 46
htdoughe 5:02aa71487c01 47 regnames[0] = "DEVID_AD";
htdoughe 5:02aa71487c01 48 regnames[1] = "DEVID_MST";
htdoughe 5:02aa71487c01 49 regnames[2] = "PARTID";
htdoughe 5:02aa71487c01 50 regnames[3] = "REVID";
htdoughe 5:02aa71487c01 51 regnames[4] = "XDATA";
htdoughe 5:02aa71487c01 52 regnames[5] = "YDATA";
htdoughe 5:02aa71487c01 53 regnames[6] = "ZDATA";
htdoughe 5:02aa71487c01 54 regnames[7] = "STATUS";
htdoughe 5:02aa71487c01 55 regnames[8] = "FIFO_ENTRIES_L";
htdoughe 5:02aa71487c01 56 regnames[9] = "FIFO_ENTRIES_H";
htdoughe 5:02aa71487c01 57 regnames[10] = "XDATA_L";
htdoughe 5:02aa71487c01 58 regnames[11] = "XDATA_H";
htdoughe 5:02aa71487c01 59 regnames[12] = "YDATA_L";
htdoughe 5:02aa71487c01 60 regnames[13] = "YDATA_H";
htdoughe 5:02aa71487c01 61 regnames[14] = "ZDATA_L";
htdoughe 5:02aa71487c01 62 regnames[15] = "ZDATA_H";
htdoughe 5:02aa71487c01 63 regnames[16] = "TEMP_L";
htdoughe 5:02aa71487c01 64 regnames[17] = "TEMP_H";
htdoughe 5:02aa71487c01 65 regnames[18] = "SOFT_RESET";
htdoughe 5:02aa71487c01 66 regnames[19] = "THRESH_ACT_L";
htdoughe 5:02aa71487c01 67 regnames[20] = "THRESH_ACT_H";
htdoughe 5:02aa71487c01 68 regnames[21] = "TIME_ACT";
htdoughe 5:02aa71487c01 69 regnames[22] = "THRESH_INACT_L";
htdoughe 5:02aa71487c01 70 regnames[23] = "THRESH_INACT_H";
htdoughe 5:02aa71487c01 71 regnames[24] = "TIME_INACT_L";
htdoughe 5:02aa71487c01 72 regnames[25] = "TIME_INACT_H";
htdoughe 5:02aa71487c01 73 regnames[26] = "ACT_INACT_CTL";
htdoughe 5:02aa71487c01 74 regnames[27] = "FIFO_CONTROL";
htdoughe 5:02aa71487c01 75 regnames[28] = "FIFO_SAMPLES";
htdoughe 5:02aa71487c01 76 regnames[29] = "INTMAP1";
htdoughe 5:02aa71487c01 77 regnames[30] = "INTMAP2";
htdoughe 5:02aa71487c01 78 regnames[31] = "FILTER_CTL";
htdoughe 5:02aa71487c01 79 regnames[32] = "POWER_CTL";
htdoughe 5:02aa71487c01 80 regnames[33] = "SELF_TEST";
htdoughe 4:8065ca897f8b 81 }
htdoughe 4:8065ca897f8b 82
rmneubau 0:0f663d16e8a2 83 // Interface pulled from ADXL362.cpp
rmneubau 0:0f663d16e8a2 84 // ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) :
rmneubau 0:0f663d16e8a2 85 ADXL362 adxl362(PA_0,PA_7,PA_6,PA_1);
rmneubau 0:0f663d16e8a2 86
rmneubau 0:0f663d16e8a2 87 int adxl362_reg_print(int s, int l){
htdoughe 4:8065ca897f8b 88 //check s is between 0x00 and 0x2E
rmneubau 0:0f663d16e8a2 89 if(s > 0x2E || s < 0x00){
htdoughe 1:b8716571ecac 90 printf("adx: requires start between 0x00 and 0x2E; given %d (0x%01x)\n", s,s);
htdoughe 4:8065ca897f8b 91 return -1;
rmneubau 0:0f663d16e8a2 92 }else if(l < 0){
htdoughe 4:8065ca897f8b 93 //check length > 0
htdoughe 1:b8716571ecac 94 printf("adx: requires length >= 0, given %d\n", l);
htdoughe 4:8065ca897f8b 95 return -1;
rmneubau 0:0f663d16e8a2 96 }else{
rmneubau 0:0f663d16e8a2 97 // start sending stuff
rmneubau 0:0f663d16e8a2 98 //first check DEVID_AD == 0xAD
htdoughe 7:446c8e17a945 99 uint8_t devid = adxl362.read_reg(ADXL362::DEVID_AD);
htdoughe 6:f05c72e636aa 100 printf("devid = %d\n", devid);
htdoughe 5:02aa71487c01 101 if(devid != 0xAD){
htdoughe 2:496a662be854 102 printf("error: DEVID_AD is not 0xAD; exiting\n");
htdoughe 4:8065ca897f8b 103 return -1;
rmneubau 0:0f663d16e8a2 104 }else{
rmneubau 0:0f663d16e8a2 105 //now do stuff
htdoughe 1:b8716571ecac 106 //if length == 0, send everything from s to end of registers
htdoughe 4:8065ca897f8b 107 //else print from s to l
htdoughe 4:8065ca897f8b 108 // formula : range = end - start - length
htdoughe 4:8065ca897f8b 109 // area = start + range
htdoughe 4:8065ca897f8b 110 int stop = 0;
htdoughe 1:b8716571ecac 111 if(l == 0){
htdoughe 4:8065ca897f8b 112 stop = 34;
htdoughe 3:813694e51f72 113 }else{
htdoughe 4:8065ca897f8b 114 stop = s + l;
htdoughe 4:8065ca897f8b 115 }
htdoughe 4:8065ca897f8b 116 int in = s;
htdoughe 4:8065ca897f8b 117 while(in < stop){
htdoughe 4:8065ca897f8b 118 //get register name
htdoughe 5:02aa71487c01 119 char * name = regnames[in]; //GET NAME
htdoughe 5:02aa71487c01 120 ADXL362::ADXL362_register_t reg = (ADXL362::ADXL362_register_t) regids[in];
htdoughe 8:9c9d58ca2b79 121 uint8_t val = adxl362.read_reg(reg); //GET REG VAL
htdoughe 4:8065ca897f8b 122 //print val
htdoughe 5:02aa71487c01 123 printf("0x%01x: %s=0x%01x\r\n", regids[in], name, val);
htdoughe 4:8065ca897f8b 124 in++;
htdoughe 1:b8716571ecac 125 }
rmneubau 0:0f663d16e8a2 126 }
rmneubau 0:0f663d16e8a2 127 }
htdoughe 5:02aa71487c01 128 return 0;
htdoughe 5:02aa71487c01 129 }
htdoughe 5:02aa71487c01 130
htdoughe 5:02aa71487c01 131 int main() {
htdoughe 5:02aa71487c01 132 initmap();
htdoughe 5:02aa71487c01 133 adxl362.reset();
htdoughe 5:02aa71487c01 134 wait_ms(600); // we need to wait at least 500ms after ADXL362 reset
htdoughe 5:02aa71487c01 135 adxl362.set_mode(ADXL362::MEASUREMENT);
htdoughe 10:8dcc46dd53bf 136 int8_t x,y,z, initx, inity, intiz;
htdoughe 5:02aa71487c01 137 int start, length;
htdoughe 5:02aa71487c01 138 start = 0;
htdoughe 8:9c9d58ca2b79 139 length = 5;
htdoughe 5:02aa71487c01 140 adxl362_reg_print(start, length);
htdoughe 10:8dcc46dd53bf 141
htdoughe 10:8dcc46dd53bf 142 //initial values of x, y, and z.
htdoughe 10:8dcc46dd53bf 143 //These values are the baseline for the accelerometer; knocks are detected
htdoughe 10:8dcc46dd53bf 144 // in relation to these initial values
htdoughe 9:a14593af628d 145 initx=adxl362.scanx_u8();
htdoughe 9:a14593af628d 146 inity=adxl362.scany_u8();
htdoughe 9:a14593af628d 147 initz=adxl362.scanz_u8();
rmneubau 0:0f663d16e8a2 148
htdoughe 5:02aa71487c01 149 while(1) {
htdoughe 10:8dcc46dd53bf 150 //get the values of x, y, and z to test for a knock
htdoughe 5:02aa71487c01 151 x=adxl362.scanx_u8();
htdoughe 5:02aa71487c01 152 y=adxl362.scany_u8();
htdoughe 5:02aa71487c01 153 z=adxl362.scanz_u8();
htdoughe 9:a14593af628d 154 if(x > initx+2 || y > inity+2 || z > initz+2){
htdoughe 9:a14593af628d 155 led1 = 1;
htdoughe 9:a14593af628d 156 wait_ms(200);
htdoughe 9:a14593af628d 157 led1 = 0;
htdoughe 9:a14593af628d 158 }
htdoughe 5:02aa71487c01 159 printf("x = %d y = %d z = %d\r\n",x,y,z);
htdoughe 5:02aa71487c01 160 wait_ms(100);
htdoughe 5:02aa71487c01 161 }
htdoughe 1:b8716571ecac 162 }