ashad
Full Member level 6
- Joined
- Mar 28, 2006
- Messages
- 393
- Helped
- 40
- Reputation
- 80
- Reaction score
- 28
- Trophy points
- 1,308
- Location
- Trento, Italy
- Activity points
- 3,296
Hi all,
I am facing problem while interfacing the MPU6000 Gyro/ Accelerometer chip. I am communicating with 345Khz I2C clk and trying to read the Orientation data but it always give me last command address which I wrote in TWDR register
I appreciate if anyone can share his experience.
I2C initialization Code
MPU6000 command set
I am facing problem while interfacing the MPU6000 Gyro/ Accelerometer chip. I am communicating with 345Khz I2C clk and trying to read the Orientation data but it always give me last command address which I wrote in TWDR register
I appreciate if anyone can share his experience.
I2C initialization Code
Code:
void iniTWI(void)
{
TWBR = 0x08; /* 0x06 with 11.0592MHz genertes 395KHz with TWPS1:0 =0
0x08 with 11.0592MHz genertes 345KHz with TWPS1:0 =0 */
// R/W6543210 8th bit is the read(1) or write(0)
// TWAR = 0b01101000; // This is used in case Uc in slave mode LSB A0 is grounded =0
TWDR = 0x00; // TWI data register
TWSR = 0x00;
// 76543210
TWCR = 0b00000100; // Control register
// TWCR = (1<<TWEN);
}//void iniTWI(void)
void TWI_stop(void)
{
TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
// TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); // Sending Stop bit
_delay_ms(1);
}//void TWI_stop(void)
void TWI_start(void)
{
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // Sending Start bit
while (!(TWCR & (1<<TWINT)));
// TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
// while ((TWCR & (1<<TWINT)) == 0);
}//void TWI_start(void)
void TWI_write(unsigned char TWI_data)
{
TWDR = TWI_data;
TWCR = (1<<TWINT) | (1<<TWEN); // Sending Start bit
while (!(TWCR & (1<<TWINT)));
// TWCR = (1<<TWINT)|(1<<TWEN);
// while ((TWCR & (1<<TWINT)) == 0);
}//void TWI_start(void)
// Read without Ack
unsigned char TWI_readNACK(void)
{
TWCR = (1<<TWINT)|(1<<TWEN);
// while ((TWCR & (1<<TWINT)) == 0);
while (!(TWCR & (1<<TWINT)));
return TWDR;
}//unsigned char TWI_read(void)
// Read with Ack
unsigned char TWI_readACK(void)
{
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
while ((TWCR & (1<<TWINT)) == 0);
return TWDR;
}//unsigned char TWI_readACK(void)
unsigned char TWIGetStatus(void)
{
unsigned char TWI_status = 0x00;
//mask status the prescalar bits
TWI_status = TWSR & 0xF8;
return TWI_status;
}
MPU6000 command set
Code:
void mpu_data(void)
{
TWI_start();
if (TWIGetStatus() != 0x08) error_msg();
TWI_write(SLA_Wrt);
if (TWIGetStatus() != 0x18) error_msg();
TWI_write(0x3B); //Address of Accelerometer X out Highbyte
if (TWIGetStatus() != 0x28) error_msg();
TWI_start();
if (TWIGetStatus() != 0x08) error_msg();
TWI_write(SLA_Rd);
if (TWIGetStatus() != 0x18) error_msg();
Acc_Xout[1]= TWI_readACK();
Acc_Xout[0]= TWI_readACK();
Acc_Yout[1]= TWI_readACK();
Acc_Yout[0]= TWI_readACK();
Acc_Zout[1]= TWI_readACK();
Acc_Zout[0]= TWI_readACK();
Tempout[1]= TWI_readACK();
Tempout[0]= TWI_readACK();
Gyro_Xout[1]= TWI_readACK();
Gyro_Xout[0]= TWI_readACK();
Gyro_Yout[1]= TWI_readACK();
Gyro_Yout[0]= TWI_readACK();
Gyro_Zout[1]= TWI_readACK();
Gyro_Zout[0]= TWI_readNACK();
TWI_stop();
}//void mpu_data(void)