#include <Wire.h>
const char addr = 0x1d; //the mma7455 address
//char modeselect = 1;//0:Serial Test,1:VitualColorMixer,2:distancemeasure
//byte x1=0;
float x,y,z,x_measure,y_measure,z_measure,easing=0.1;
void accewrite(byte reg,byte data){
Wire.beginTransmission(addr); // transmit todevice
Wire.send(reg);// sets register pointer to the command register
Wire.send(data);// send the data
Wire.endTransmission();// end the transmission
}
byte acceread(byte reg,char command){ //command 1 display:theregister 0x??(reg) value: ??(reading) with"/n"
//command 2 display: ??(reading)
//the other no display
byte reading= 0;
Wire.beginTransmission(addr);// transmit to device
Wire.send(reg);// sets register pointer to the command register
Wire.endTransmission();// end the transmission
Wire.requestFrom(addr,1);// request 1 bytes from slave device
if(1<=Wire.available()){// if 1 byte was received ( ACK )
reading = Wire.receive(); //receive the register data from slave
// reading = Wire.receive(); // receive the next register data fromslave
}
returnreading;
}
void acceinit(){
accewrite(0x16,0x04);//set theacce to standy mode In standby mode the device can read
// and write to the registers with the I2C/SPI available,
// but no new measurements can be taken in this mode as allcurrent
//consuming parts are off.
accewrite(0x10,0x08); // set x offset value ,x =-1 ,offset = 2
accewrite(0x11,0x00); //the ture x is negative,offset must be positive
accewrite(0x12,0x26); // the same as x
accewrite(0x13,0x00);
// accewrite(0x14,0x28); //Idon't know why z can't offset
// accewrite(0x15,0x04);
accewrite(0x16,0x05); // setthe acce to measure mode : +/- 2g mode, 64 LSB/gOR 0.015625 g/LSB
}
void setup(){
x = y = z =x_measure = y_measure = z_measure = 0;
Wire.begin();// join i2c bus (address optional for master)
Serial.begin(9600); // start serial communicationat 9600bps
acceinit();
}
void loop(){
//test withprocessing:VirtualColorMixer
x_measure= acceread(0x06,0); //read x axis value
y_measure= acceread(0x07,0); //read y axis value
z_measure= acceread(0x08,0); //read z axis value
// x += (x_measure - x ) * easing;
// y += (y_measure - y ) * easing;
// z += ( z_measure - z ) *easing;
Serial.print(x_measure);
Serial.print(",");
Serial.println(y_measure);
//Serial.print(",");
//Serial.println(z);
delay(20);
}
改来改去的,一会要传给Processing,一会自己测试,最后删了一些,留下基本的数据读取。