this is sample source of MMA8452

Dependencies:   mbed

Fork of MMA8452 by Hiroaki Matsuda

Committer:
matsu
Date:
Wed Nov 28 17:05:31 2012 +0000
Revision:
1:0c1dd02be97a
Parent:
0:904e837fccc5
ver1:get analog data from MMA8452

Who changed what in which revision?

UserRevisionLine numberNew contents of line
matsu 0:904e837fccc5 1 #include "mbed.h"
matsu 0:904e837fccc5 2
matsu 0:904e837fccc5 3 I2C i2c(p9,p10);
matsu 0:904e837fccc5 4 Serial pc(USBTX, USBRX);
matsu 0:904e837fccc5 5 DigitalOut led1(LED1);
matsu 0:904e837fccc5 6 DigitalOut led2(LED2);
matsu 0:904e837fccc5 7 DigitalOut led3(LED3);
matsu 0:904e837fccc5 8
matsu 0:904e837fccc5 9 int main() {
matsu 0:904e837fccc5 10 led1 = 0;
matsu 0:904e837fccc5 11 led2 = 0;
matsu 0:904e837fccc5 12 led3 = 0;
matsu 0:904e837fccc5 13 char cmd[6];
matsu 0:904e837fccc5 14 char add[1];
matsu 0:904e837fccc5 15 char init[2];
matsu 0:904e837fccc5 16 add[0] = 0x01;
matsu 0:904e837fccc5 17 init[0] = 0x2A;
matsu 0:904e837fccc5 18 init[1] = 0x01;
matsu 0:904e837fccc5 19 int number=0;
matsu 0:904e837fccc5 20 i2c.frequency(40000);
matsu 0:904e837fccc5 21
matsu 0:904e837fccc5 22 wait(0.5);
matsu 0:904e837fccc5 23 //init
matsu 0:904e837fccc5 24 //set active mode
matsu 0:904e837fccc5 25 while(i2c.write(0x3A,init,2));
matsu 0:904e837fccc5 26 led1 = 1;
matsu 0:904e837fccc5 27 led2 = 1;
matsu 0:904e837fccc5 28 led3 = 1;
matsu 0:904e837fccc5 29
matsu 0:904e837fccc5 30 //get analog data
matsu 0:904e837fccc5 31 while(1) {
matsu 0:904e837fccc5 32 //send procedure
matsu 0:904e837fccc5 33 wait(0.07);
matsu 0:904e837fccc5 34 switch(number){
matsu 0:904e837fccc5 35 case 0:
matsu 0:904e837fccc5 36 if(i2c.write(0x3A,add,1) == 0){
matsu 0:904e837fccc5 37 number++;
matsu 0:904e837fccc5 38 led1 = 1;
matsu 0:904e837fccc5 39 }else{
matsu 0:904e837fccc5 40 number=0;
matsu 0:904e837fccc5 41 led1 = 0;
matsu 0:904e837fccc5 42 led2 = 0;
matsu 0:904e837fccc5 43 led3 = 1;
matsu 0:904e837fccc5 44 }
matsu 0:904e837fccc5 45 break;
matsu 0:904e837fccc5 46 case 1:
matsu 0:904e837fccc5 47 if(i2c.read(0x3A,cmd,6) == 0){
matsu 0:904e837fccc5 48 number++;
matsu 0:904e837fccc5 49 led2 = 1;
matsu 0:904e837fccc5 50 }else{
matsu 0:904e837fccc5 51 number=0;
matsu 0:904e837fccc5 52 led1 = 0;
matsu 0:904e837fccc5 53 led2 = 0;
matsu 0:904e837fccc5 54 led3 = 0;
matsu 0:904e837fccc5 55 }
matsu 0:904e837fccc5 56 break;
matsu 0:904e837fccc5 57 case 2:
matsu 1:0c1dd02be97a 58 //get analog data
matsu 0:904e837fccc5 59 pc.printf("\r X = %d,Y = %d,Z = %d ",cmd[1],cmd[3],cmd[5]);
matsu 0:904e837fccc5 60 led3 = 1;
matsu 0:904e837fccc5 61 wait(0.5);
matsu 0:904e837fccc5 62 led1 = 0;
matsu 0:904e837fccc5 63 led2 = 0;
matsu 0:904e837fccc5 64 led3 = 0;
matsu 0:904e837fccc5 65 number=0;
matsu 0:904e837fccc5 66 break;
matsu 0:904e837fccc5 67 }
matsu 0:904e837fccc5 68 }
matsu 0:904e837fccc5 69 }