Compass Working

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Tap.cpp Source File

Tap.cpp

00001 #include "Tap.h"
00002 I2C acc(p9,p10);
00003 DigitalIn int2(p20);
00004 InterruptIn check(p12);
00005 BusOut led(LED1,LED2,LED3,LED4);
00006 char acc_config[2];
00007 const int addr=(0x53<<1);
00008 char data1[1];
00009 char ack;
00010 
00011 void Tap_init() 
00012     {
00013     acc.frequency(100000);   
00014     acc_config[0]=0x31; acc_config[1]=0x0B;         //data format
00015     ack = acc.write(addr,acc_config, 2);
00016     wait(0.1);
00017       
00018     acc_config[0]=0x2D; acc_config[1]=0x08;         //power control->measure mode
00019     acc.write(addr,acc_config, 2);   
00020     wait(0.1);
00021     
00022     acc_config[0]=0x1D; acc_config[1]=0x50;         //Tap Threshold ->5g 
00023     acc.write(addr,acc_config, 2); 
00024     wait(0.1);
00025     
00026     acc_config[0]=0x21; acc_config[1]=0x10;         //Tap duration -> 0.01
00027     acc.write(addr,acc_config, 2);    
00028     wait(0.1);
00029     
00030     acc_config[0]=0x22; acc_config[1]=0x05;         //Tap Gap ~0.006
00031     acc.write(addr,acc_config, 2); 
00032     wait(0.1);
00033     
00034     acc_config[0]=0x23; acc_config[1]=0xFF;         //Tap window ->~0.38175
00035     acc.write(addr,acc_config, 2);
00036     wait(0.1);
00037     
00038     acc_config[0]=0x2A; acc_config[1]=0x07;         //Axis -> x&y&z
00039     acc.write(addr,acc_config, 2);
00040     wait(0.1);
00041     
00042     acc_config[0]=0x2E; acc_config[1]=0x60;         //enable interupt -> single and double tap
00043     acc.write(addr,acc_config, 2);
00044     wait(0.1);
00045     
00046     acc_config[0]=0x2F; acc_config[1]=0x60;         //map interupt ->int2
00047     acc.write(addr,acc_config, 2);
00048     wait(0.1);
00049     }
00050     float Tap()
00051     {    
00052         int2.rise(&Tap_return)
00053         while(1)
00054             {
00055             wait(0.01);
00056             
00057             if(int2.read()==1)  // Check for interupt signal
00058             {
00059                
00060                 acc_config[0]=0x30;
00061                 acc.write(addr,acc_config,1);             
00062                 acc.read(addr,data1,1);  // Get interupt information
00063                 
00064                 if(data1[0] & 0x40)  // Turn on one LED for one tap
00065                 {
00066                     led =1; 
00067                 }
00068                 if(data1[0] & 0x20)  // Turn on two LEDs for two taps
00069                 {
00070                     led =2;
00071                 }
00072                 wait(1);
00073                  // Turn off LED
00074                  if(led==1){return 1;}
00075                  if(led ==2){return 2;}
00076                 
00077             }     
00078         }   
00079     }
00080     
00081     void Tap_return()
00082     {
00083             
00084     }