In this lab, you will: Construct a prototype

Dependencies:   ADXL362 mbed

main.cpp

Committer:
csinders
Date:
2018-02-21
Revision:
2:b56010953f54
Parent:
1:fedb70ea0eaa
Child:
3:33cca9e98fbb

File content as of revision 2:b56010953f54:

#include "mbed.h"
#include "ADXL362.h"
 
// Interface pulled from ADXL362.cpp
// ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) :
ADXL362 adxl362(PA_0,PA_7,PA_6,PA_1);
Serial pc(USBTX, USBRX);
DigitalOut myled(LED1);

int adxl362_reg_print(int start, int length);
void adxl362_get_average(int8_t avg[]);

int main() {
    pc.printf("Starting program\n\r");
    adxl362.reset();
    wait_ms(600); // we need to wait at least 500ms after ADXL362 reset
    adxl362.set_mode(ADXL362::MEASUREMENT);
    int8_t x,y,z; 
    int8_t avg[3]; // Avg values of x,y,z. x = avg[0], y = avg[1] z = avg[2]
    
    adxl362_reg_print(0,0); // Test of axdl_reg_print
    wait_ms(1000); // wait so that values can be seen
    int threshHoldRange = 10; // threshhold difference between avg and read values
    adxl362_get_average(avg);// Generate an average of what x,y,z are.
    //pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
    while(1) {
        x=adxl362.scanx_u8();
        y=adxl362.scany_u8();
        z=adxl362.scanz_u8();
        if (x > (avg[0] + threshHoldRange) || x < (avg[0] - threshHoldRange)
            || y > (avg[1] + threshHoldRange) || y < (avg[1] - threshHoldRange) 
            //|| z > (zAvg + threshHoldRange) || z < (zAvg - threshHoldRange)) {
            // commented out z as it gives weird values
            ) {
            /* print cases used to test threshhold range
            pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
            pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
            pc.printf("Threshold range is = %d\r\n", threshHoldRange);
            */
            pc.printf("outside of threshold range\n\r");
            myled = 1; // LED is ON
            wait_ms(1900); // Wait 1.9s.  Last .1 sec is done while getting average
            adxl362_get_average(avg); // Generate new average
            myled = 0; // LED is OFF
        }
        pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
        wait_ms(100);
    }
}

void adxl362_get_average(int8_t avg[]) {
     // Generate an average of what x,y,z are.
    int numberOfPolls = 10;
    // reset average back to zero for each
    int tmp[3]; // required because avg was having integer overflow
    tmp[0] = 0;
    tmp[1] = 0;
    tmp[2] = 0;
    avg[0] = 0;
    avg[1] = 0;
    avg[2] = 0;
    int8_t x,y,z; 
    // Poll each value 10 times
    for (int i = 0; i < numberOfPolls; i++) {
        //pc.printf("x = %d y = %d z = %d\r\n",x,y,z);
        x = adxl362.scanx_u8();
        tmp[0]+= x;
        y = adxl362.scany_u8();
        tmp[1]+= y;
        z=adxl362.scanz_u8();
        tmp[2]+= z; 
        wait_ms(10);
        //pc.printf("xAvg = %d yAvg = %d zAvg = %d\r\n",tmp[0],tmp[1],tmp[2]);

    } 
    // Divide each value by the number of polls to get the average
    tmp[0] = tmp[0] / numberOfPolls;
    tmp[1] = tmp[1] / numberOfPolls;
    tmp[2] = tmp[2] / numberOfPolls;
    // store values from the tmp to the passed in array
    avg[0] = tmp[0];
    avg[1] = tmp[1];
    avg[2] = tmp[2];
    pc.printf("New Average: xAvg = %d yAvg = %d zAvg = %d\r\n",avg[0],avg[1],avg[2]);
    wait_ms(5000);
}

int adxl362_reg_print(int start, int length) {
    // Check if start is within registry
    if (start < 0 || start > 0x2E) {
        pc.printf("Error: start value passed to adxl362_reg_print outside of range of registry\n\r");
        return -1;
    }
    // check if length is negative
    if (length < 0) {
        pc.printf("Error: length passed to adxl362_reg_print is negative\n\r");
        return -1;
    }
    
    // check if valid communication with device going
    // For some reason getting 0xAC instead of 0xAD?
    // Printing out what value we are getting    
    if (adxl362.read_reg(adxl362.DEVID_AD) != 0xAD) {
        pc.printf("Error: Unable to read from DEVID_AD register\n\r");
        return -1;
    }    
    // String array with all of the names of the different registers in order
    char regNames [37][20]  = {
         "DEVID_AD", "DEVID_MST", "PARTID",
         "REVID", "XDATA", "YDATA",
         "ZDATA", "STATUS", "FIFO_ENTRIES_L", 
         "FIFO_ENTRIES_H", "XDATA_L", "XDATA_H",
         "YDATA_L", "YDATA_H", "ZDATA_L",
         "ZDATA_H", "TEMP_L", "TEMP_H",
         "RESERVED", "RESERVED", "SOFT_RESET",
         "THRESH_ACT_L", "THRESH_ACT_H", "TIME_INACT_L",
         "TIME_ACT", "THRESH_INACT_L", "THRESH_INACT_H",
         "TIME_INACT_L", "TIME_INACT_H", "ACT_INACT_CTL",
         "FIFO_CONTROL", "FIFO_SAMPLES", "INTMAP1",
         "INTMATP2", "FILTER_CTL", "POWER_CTL",
         "SELF_TEST"};
    // Example for first two registers 
    // I think their is a better way to do this, but unsure of how.
    for (int i = 0; i < 2; i++) { 
        uint8_t tmp = 0; // hex value location of the register
        uint8_t hexValue = 0; // Hex value of the register
        // May need to increment the registers inside of the if, since the values 
        // are not always sequential
        // You can find registers inside of ADXL362.h file in ADXL362 folder
        // they are the ADXL362_register_t
        if (i == 0) { 
            tmp = adxl362.read_reg(adxl362.DEVID_AD);
            hexValue = 0x00;
        } else if (i == 1) { 
            tmp = adxl362.read_reg(adxl362.DEVID_MST);
            hexValue = 0x01;
        }
        pc.printf("%#04x: %s=%#04x\n\r", hexValue, regNames[i], tmp); // Print register
    }
    return 0;
    
// below is github with data for ADXL362 methods
 //https://github.com/analogdevicesinc/mbed-adi/blob/master/libraries/ADXL362/ADXL362.cpp   
}