First

Dependencies:   UniGraphic mbed

Revision:
5:98ad3701ec24
Parent:
3:824764571657
Child:
6:459d1eb2cc23
--- a/acc.cpp	Thu Jan 14 21:36:29 2016 +0000
+++ b/acc.cpp	Thu Jan 14 22:03:54 2016 +0000
@@ -5,13 +5,13 @@
 #include "mbed.h"
 
 SPI acc(p11,p12,p13);           //Set up SPI MOSI, MISO, SCL
-DigitalOut cs(p14);             //set up chip select
+DigitalOut cs(p10);             //set up chip select
 Serial pc(USBTX, USBRX);
 char buffer[6];                 //raw filler data
 int16_t data[3];                //16bit twos-compliment integer data
 float x,y,z;                    //set up floats for values to display
 
-int main(){
+void acc_init(void){
     cs=1;                       //initially off
     acc.format(8,3);            //8bit, mode 3
     acc.frequency(2000000);     //2MHz
@@ -23,18 +23,20 @@
     acc.write(0x2D);            //power mode = measure
     acc.write(0x08);
     cs=1;
-    
+}
+ 
+float acc_sense(char axis){
+    cs=0;
+    acc.write(0x80|0x40|0x32);      //RW bit high, MB bit high, address to ask for a reading
+    for (int i=0;i<=5;i++){buffer[i]=acc.write(0x00);}           //read 6bytes
+    cs=1;
+    data[0]=buffer[1]<<8|buffer[0];         //x    
+    data[1]=buffer[3]<<8|buffer[2];         //y         combine MSB and LSB
+    data[2]=buffer[5]<<8|buffer[4];         //z
     
-    while(1){
-        wait(0.2);
-        cs=0;
-        acc.write(0x80|0x40|0x32);      //RW bit high, MB bit high, address
-        for (int i=0;i<=5;i++){buffer[i]=acc.write(0x00);}           //read 6bytes
-        cs=1;
-        data[0]=buffer[1]<<8|buffer[0];             
-        data[1]=buffer[3]<<8|buffer[2];         //combine MSB and LSB
-        data[2]=buffer[5]<<8|buffer[4];
-        x=0.004*data[0];    y=0.004*data[1];    z=0.004*data[2];        //convert to float, actual g value
-        pc.printf("x = %+1.2fg\t y = %+1.2fg\t z = %+1.2fg\n\r", x,y,z);
-    }
+    x=atan(data[0],data[2]);
+    y=atan(data[1],data[2]);
+    
+    if(axis==1) return x;       //if asked for x, give x
+    if(axis==2) return y;       //if asked for y, give y
 }