#include <Wire.h>
#include <stdio.h>
#define MMA7455_I2C_ADDRESS 0x1D
//MMA7455 Registers
#define MMA7455_XOUTL 0x00
#define MMA7455_XOUTH 0x01
#define MMA7455_YOUTL 0x02
#define MMA7455_YOUTH 0x03
#define MMA7455_ZOUTL 0x04
#define MMA7455_ZOUTH 0x05
#define MMA7455_XOUT8 0x06
#define MMA7455_YOUT8 0x07
#define MMA7455_ZOUT8 0x08
#define MMA7455_STATUS 0x09
#define MMA7455_DETSRC 0x0A
#define MMA7455_TOUT 0x0B
#define MMA7455_I2CAD 0x0D
#define MMA7455_USRINF 0x0E
#define MMA7455_WHOAMI 0x0F
#define MMA7455_XOFFL 0x10
#define MMA7455_XOFFH 0x11
#define MMA7455_YOFFL 0x12
#define MMA7455_YOFFH 0x13
#define MMA7455_ZOFFL 0x14
#define MMA7455_ZOFFH 0x15
#define MMA7455_MCTL 0x16
#define MMA7455_INTRST 0x17
#define MMA7455_CTL1 0x18
#define MMA7455_CTL2 0x19
#define MMA7455_LDTH 0x1A
#define MMA7455_PDTH 0x1B
#define MMA7455_PW 0x1C
#define MMA7455_LT 0x1D
#define MMA7455_TW 0x1E
static FILE uartout = {0} ;
static int uart_putchar (char c, FILE *stream)
{
Serial.write(c) ;
return 0 ;
}
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
// The uart is the standard output device STDOUT.
stdout = &uartout ;
mma7455WriteReg(MMA7455_MCTL, 0x01);
}
unsigned char mma7455ReadReg(unsigned char reg)
{
//Write register address first
Wire.beginTransmission(MMA7455_I2C_ADDRESS);
Wire.write(reg);
Wire.endTransmission();
//Then read the data back
Wire.requestFrom(MMA7455_I2C_ADDRESS, 1);
if(Wire.available())
{
return Wire.read();
}
//Return zero if no data available
return 0;
}
void mma7455WriteReg(unsigned char reg, unsigned char value)
{
//Write register address first
Wire.beginTransmission(MMA7455_I2C_ADDRESS);
Wire.write(reg);
//And then write data
Wire.write(value);
Wire.endTransmission();
}
void loop()
{
printf("X = %u\n\r", mma7455ReadReg
(MMA7455_XOUT8
)); printf("Y = %u\n\r", mma7455ReadReg
(MMA7455_YOUT8
)); printf("Z = %u\n\r", mma7455ReadReg
(MMA7455_ZOUT8
));
delay(500);
}
Comments
what port you connect it?
what port you connect it?
This chip supports both SPI
This chip supports both SPI and I2C protocol.The Arduino code above is for I2C though. More details about using I2C with Arduino can be found here at This board needs to be connected to the I2c pins of Arduino. Details about Arduino I2C port is available at http://www.arduino.cc/en/Reference/Wire
Problem
I connect and run but,Why x y z out 0 0 0 all time?
Re:Problem
Please check if CS is conencted to VCC. Please go through the datasheet carefully, the progrram may not work if the connections aren't right.
Re:Problem
OK. I can x y z is out 255 251 19 right.
It number of g or number anything?
thank you
Re:Problem
The sample program above will just print x,y and z only. Please see datasheet for more features.
hi dear i can read numbers in
hi dear
i can read numbers in hex from mma7455 but i can not relate between number and acceleration
please help me
sincerly yours