Mini Project 10: Displaying stuff from day 7

Dependencies:   DmTouch_UniGraphic UniGraphic mbed

acceler_sensor.cpp

Committer:
swescott17
Date:
2017-01-18
Revision:
16:995c9920f14e
Parent:
15:565da8e90073

File content as of revision 16:995c9920f14e:

#include "acceler_sensor.h"
DigitalOut led3(LED3);
const int address = 0xA6;

void configure_acceleration(void){
    
  // set 'address' to    
    i2c_port.start();
    i2c_port.write(address);
    i2c_port.write(0x1D);   //to set the Tap Threshold
    i2c_port.write(0x50 << 2); 
    i2c_port.stop();
    wait(0.01);
    
    i2c_port.start();
    i2c_port.write(address);
    i2c_port.write(0x21);//set Tap Duration
    i2c_port.write(0x0A); 
    i2c_port.stop();
    wait(0.01);
     
    i2c_port.start();
    i2c_port.write(address);
    int temp[2] = {0x22, 0x05};
    i2c_port.write(temp[0]);//set Tap Gap
    i2c_port.write(temp[1]);
    i2c_port.stop();
    wait(0.01);
    
    i2c_port.start();
    i2c_port.write(address);
    i2c_port.write(0x23);//set Tap Window
    i2c_port.write(0xFF);
    i2c_port.stop();
    wait(0.01);
 
    i2c_port.start();
    i2c_port.write(address);
    i2c_port.write(0x2A);//set Tap Axis
    i2c_port.write(0x07);
    i2c_port.stop();
    wait(0.01);

    i2c_port.start();
    i2c_port.write(address);
    i2c_port.write(0x2E);//set interupt Enable
    i2c_port.write(0x20|0x40);
    i2c_port.stop();
    wait(0.01);
  
    i2c_port.start();
    i2c_port.write(address);
    i2c_port.write(0x2F);//set interupt map
    i2c_port.write(0x20|0x40); 
    i2c_port.stop();
    wait(0.01);             //all the above should confige tap functions for accel sensor, so it's unneccesary

    i2c_port.start();
    i2c_port.write(address);
    i2c_port.frequency(2000000);
    i2c_port.write(0x2D);//power control register
    i2c_port.write(0x08);//measure mode 
    i2c_port.stop();
}

void getacceleration (float* data){
    char buffer[6];
    char idontknow[1];
    idontknow[0] = 0x32;
        int16_t rawdata[3];
        i2c_port.write(address,idontknow,1);
        i2c_port.read(address,buffer,6);
        /*i2c_port.write(address);
        i2c_port.write(0x80|0x40|0x32);
          for(int i=0; i<= 5; i++)
            {
                buffer[i]=i2c_port.write(0x00);
                //pc.printf("x = %+1.2fg\t", buffer[i]);
            }*/
        rawdata[0] = buffer[1]<<8 | buffer[0];
        rawdata[1] = buffer[3]<<8 | buffer[2];
        rawdata[2] = buffer[5]<<8 | buffer[4];
        data[0] = rawdata[0]*0.004; 
        data[1] = rawdata[1]*0.004;


        pc.printf("x = %+1.2fg\t y = %+1.2fg\n\r", data[0],data[1]); //Just grabbed from SPI code. Not formatted right
        
    
    }