[SOLVED] MPU6000 Interface with Microcontroller

Status
Not open for further replies.

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
www.elabsp.com
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
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)
 

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
How do you know that the output that you are getting is not the orientation data,but the command address?
And what output device are you using to view the output....is it the terminal or an LCD?
 

In the command set,it looks like you did not terminate the if statement(s) in these functions: TWI_start(), TWIGetStatus(), TWI_write(SLA_Wrt), TWIGetStatus(),TWI_write(SLA_Rd).Complete the if statement(s) and report on whether that solves the problem or not.
 

U mean ... I need to insert brackets with the if{} statement ????
No....I mean,try ending the if statements with endif statement if possible.If that does not work,try using curly braces to end the if statements and report on what happens.
 

No....I mean,try ending the if statements with endif statement if possible.If that does not work,try using curly braces to end the if statements and report on what happens.

I have checked by running code, this problem is not arising because of Braces ... Suggest me any other problem if you will find in my code or Anyone can share his experience regarding MPU6000
 

Hi all,

Now I am getting Temperature and Accelemeter data but I tried several time to read the Gyro data from MPU6000, but I am getting 0x0000 0x0000 0x0000 for all 3 axis.

I appreciate if anyone can share his experience with me.
 
Last edited:

look like your sensor is a gyroscopic rate sensor it will give readings only when in a rotary motion.





try a rotation likes this.
 
Last edited:

Thanks for reply,

I am moving Flexible PCB up and down while reading the sensor data, however, I am getting Zero Zero Zero but Accelerometer reading is changing accordingly.
 

but you have to rotate though, hold the maximum value received from the sensor.

- - - Updated - - -

might be it was momentry
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…