Laboratory 6

Dependencies:   ADXL362 mbed

main.cpp

Committer:
fanbsun
Date:
2018-02-22
Revision:
1:737fa48a7c0c
Parent:
0:1ca806626aba

File content as of revision 1:737fa48a7c0c:

#include "mbed.h"
#include "ADXL362.h"
#include <stdlib.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);

int adxl362_reg_print(int start, int length);

int main()
{
    adxl362.reset();
    wait_ms(600); // we need to wait at least 500ms after ADXL362 reset
    adxl362.set_mode(ADXL362::MEASUREMENT);
    
    adxl362_reg_print(0, 3);
    
    adx1362_knock_detect();
    
    
    
    
    
    
    
    return(0);
}



int adxl362_reg_print(int start, int length)
{
    int i;
    if(start >= 0x00 && start <= 0x2E && length >= 0x00 /*&& (adxl362.read_reg(adxl362.DEVID_AD) == 0xAD)*/)
    {
        for(i = start; i <= start + length; i++)
        {
            pc.printf("\n\r0x%x: Test=%d\n\r", i, adxl362.read_reg());   
        }
    }
    else
    {
        pc.printf("Error");
        return(-1);  
    }    
}

void adx1362_knock_detect()
{
    int8_t x1,y1,z1,x2,y2,z2,x,y,z; 
    while(1)
    {
        
        while(1) 
        {
        x1=adxl362.scanx_u8();
        y1=adxl362.scany_u8();
        z1=adxl362.scanz_u8();
        wait_ms(100);
        x2=adxl362.scanx_u8();
        y2=adxl362.scany_u8();
        z2=adxl362.scanz_u8();
        
        x=abs(x1 - x2);
        y=abs(y1 - y2);
        z=abs(z1 - z2);
        
        if (x>10 || y>10 || z>10)
        break;

        printf("x = %d y = %d z = %d\r\n",x,y,z);
        wait_ms(100);
        }
        
        myled = 1;
        wait(2);
        
     }