lab 6

Dependencies:   ADXL362 mbed MPL3115A2

Committer:
htdoughe
Date:
Fri Feb 23 16:47:22 2018 +0000
Revision:
16:993757c8b398
Parent:
15:5ce9c63e7da2
Child:
17:6a08357dbb3f
moving code around

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