Compass Working

Dependencies:   mbed

Revision:
1:ef74b3428716
Child:
2:ef6778c2438e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Tap.cpp	Wed Jan 13 22:31:54 2016 +0000
@@ -0,0 +1,84 @@
+#include "Tap.h"
+
+I2C acc(p9,p10);
+DigitalIn interrupt(p20);
+BusOut led(LED1,LED2,LED3,LED4);
+
+
+
+char Tap() 
+{
+    
+    
+    
+    cs=0;
+    acc.write(0x31); // data format
+    acc.write(0x0B);  
+    cs=1;
+    
+    cs=0;
+    acc.write(0x2D);  // power control
+    acc.write(0x08);  // measure mode
+    cs=1;
+    
+    cs=0;
+    acc.write(0x1D); // tap threshold
+    acc.write(0x50);  // set to 5g 
+    cs=1;
+    
+    cs=0;
+    acc.write(0x21); // tap duration
+    acc.write(0x10);  // set to 0.01
+    cs=1;
+    
+    cs=0;
+    acc.write(0x22); // double tap gap
+    acc.write(0x05);  // set to aprox .006
+    cs=1;
+    
+    cs=0;
+    acc.write(0x23); // double tap window
+    acc.write(0xFF);  // Set to .38175
+    cs=1;
+    
+    cs=0;
+    acc.write(0x2A); // tap axis
+    acc.write(0x07);  // set to enable x,y, and z (00000111) 
+    cs=1;
+    
+    cs=0;
+    acc.write(0x2E); // interupt enable
+    acc.write(0x60);  // set to single and double tap (01100000)
+    cs=1;
+    
+    cs=0;
+    acc.write(0x2F); // map interupt
+    acc.write(0x60);  // set to 00000111
+    cs=1;
+    
+    char Inbyte;
+    
+    while(1)
+    {
+        wait(0.01);
+        Inbyte =0;
+        
+        if(interrupt==1)  // Check for interupt signal
+        {
+            cs=0;
+            acc.write(0x80|0x30);  // Connect to device
+            Inbyte = acc.write(0x00);  // Get interupt information
+            if(Inbyte & 0x40)  // Turn on one LED for one tap
+            {
+                led =1;
+                return 1;  
+            }
+            if(Inbyte & 0x20)  // Turn on two LEDs for two taps
+            {
+                led =3;
+                return 2;
+            }
+            led =0;  // Turn off LED
+        }     
+    }   
+}
\ No newline at end of file