资源描述
Arduino/maple驱动MMA7660例程
2011-07-30 10:15
Arduino/maple驱动MMA7660例程
【整理者】
【提供者】yuyyy
【详细说明】Arduino/maple驱动MMA7660例程
我们是深圳本土最受客户青睐、成长最快、最关注增值服务的MID电子元件提供商之一。我们深圳的长期大量现货型号有:MMA7660, MMA7455, MMA8452QR1, MXC6225XU, MMC3140MS, TS1003,MPU-3050, ALC5621, ALC5625, WM8988, ES8388,联系热线:13714686590,杨先生
//SDA (data line) is on pin 30, and SCL (clock line) is on pin 29
#include <Wire.h>
#define I2C_ADDRESS 0x4C
#define I2C_ADDRESS_W 0x99 //MMA7660 Address + 1 bit (Write) pag. 21
#define I2C_ADDRESS_R 0x98 //MMA7660 Address + 0 bit (Read) pag 21
#define TILT_STATUS 0x03
#define SRST 0x04
#define SPCNT 0x05
#define INTSU 0x06
#define MODE 0x07
#define SR 0x08
#define PDET 0x09
#define PD 0x0A
#define POWER_UP_DELAY 10
#define X_OUT 0x00
#define Y_OUT 0x01
#define Z_OUT 0x02
void setup()
{
SerialUSB.println("Setting up I2C protocol...");
Wire.begin(30,29);
SLAVE_I2C_INIT();
pinMode(13, OUTPUT);
}
void loop()
{
char x = SLAVE_I2C_READ(X_OUT);
char y = SLAVE_I2C_READ(Y_OUT);
char z = SLAVE_I2C_READ(Z_OUT);
char mode = SLAVE_I2C_READ(0x07);
// char tilt = SLAVE_I2C_READ(0x03);
char sample = SLAVE_I2C_READ(0x04);
// char fac = SLAVE_I2C_READ(0x0B);
SerialUSB.print("X=");
SerialUSB.print(x, DEC);
SerialUSB.print(", Y=");
SerialUSB.print(y, DEC);
SerialUSB.print(", Z=");
SerialUSB.print(z, DEC);
SerialUSB.print(", Mode=");
SerialUSB.print(mode,DEC);
//Serial.print(", Tilt=");
// Serial.print(tilt,DEC);
SerialUSB.print(", sample=");
SerialUSB.print(sample,DEC);
//Serial.print(", fac=");
//Serial.print(fac,DEC);
SerialUSB.println(".");
delay(100);
}
void SLAVE_I2C_INIT()
{
SerialUSB.println("Powering up SLAVE interface...");
delay(POWER_UP_DELAY);
SLAVE_I2C_SEND(MODE,0x00); // Setting up MODE to Stand by to set SR
delay(100);
SLAVE_I2C_SEND(SR,0x00); // Setting up SR register to 120 samples active and auto sleep mode
delay(100);
SLAVE_I2C_SEND(MODE,0x01); //Setting up MODE Active to START measures
delay(100);
}
void SLAVE_I2C_SEND(unsigned char REG_ADDRESS, unsigned char DATA) //SEND data to MMA7660
{
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(REG_ADDRESS);
Wire.send(DATA);
Wire.endTransmission();
}
unsigned char SLAVE_I2C_READ(unsigned char REG_ADDRESS) //READ MMA7660 data
{
unsigned char R_VALUE;
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(REG_ADDRESS); // register to read
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 1); // read a byte
while(!Wire.available())
{
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
R_VALUE = Wire.receive();
return R_VALUE;
链接地址
展开阅读全文