If you want to use a Raspberry PI instead of an ATMega, see this post.
At first, I wrote two functions which can read from a register respectively write to a register:
void TWIM_WriteRegister(char reg, char value)
{
TWIM_Start(addr, TWIM_WRITE); // set device address and write mode
TWIM_Write(reg);
TWIM_Write(value);
TWIM_Stop();
}
char TWIM_ReadRegister(char reg)
{
TWIM_Start(addr, TWIM_WRITE);
TWIM_Write(reg);
TWIM_Stop();
TWIM_Start(addr, TWIM_READ); // set device address and read mode
char ret = TWIM_ReadNack();
TWIM_Stop();
return ret;
}
As a second step, I wrote two functions which read the acceleration and gyro data from the Sensor. Please note that you have to disable the sleep mode, this can be done by using
TWIM_WriteRegister(107, 0). double MPU6050_ReadAccel(int axis)//x = 0; y = 1; z = 2
{
char reg = axis * 2 + 59;
char AFS_SEL = TWIM_ReadRegister(28);
double factor = 1<<AFS_SEL;
factor = 16384/factor;
int val = 0;
double double_val = 0;
char ret = 0;
ret = TWIM_ReadRegister(reg);
val = ret << 8;
ret = TWIM_ReadRegister(reg+1);
val += ret;
if (val & 1<<15)
val -= 1<<16; double_val = val;
double_val = double_val / factor;
return double_val;
}
double MPU6050_ReadGyro(int axis)//x = 0; y = 1; z = 2
{
char reg = axis * 2 + 67;
char FS_SEL = TWIM_ReadRegister(27);
double factor = 1<<FS_SEL;
factor = 131/factor;
int val = 0;
double double_val = 0;
char ret = 0;
ret = TWIM_ReadRegister(reg);
val = ret << 8;
ret = TWIM_ReadRegister(reg+1);
val += ret;
if (val & 1<<15)
val -= 1<<16; double_val = val;
double_val = double_val / factor;
return double_val;
}
The values for the gyrometer are in degrees per second and in units of g for the accelerometer, further information is provided in the Register Map, especially at the explaination of FS_SEL and AFS_SEL: http://www.invensense.com/mems/gyro/documents/RM-MPU-6000A.pdf





