CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

mpu6050 problems
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Jan 03, 2013 8:06 pm     Reply with quote

Hi andys,

As wired (AD0 = GND) your slave address should be 0x68. You should try
this address and see what happens. It's troubling that the I2C diagnostic
program returns another value. This suggests you have a hardware issue?
Unfortunately, I can't test this with real hardware myself because I don't
use the PIC33 series devices.....

I would double-check everything on your board, and make sure it's wired the
way you think it is!

Do you have a schematic for the breakout board that you posted?

John
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Fri Jan 04, 2013 1:53 am     Reply with quote

Er. No......

Take a deep breath, and step back. There are two different ways of representing I2C addresses. Originally, Texas, gave addresses as the 7bit value, that needs to be sent in the top seven bits of the address byte. This 7bit address was then followed by a 0 or a 1, to signify a write or read. So a device with a 7bit address of 0x68 (1101000 in binary), needs to be sent 11010000 to write, or 11010001 to read. Bytes of 0xD0, and 0xD1. Historically, most manufacturers switched to not talking about the 7bit address, but instead using the address 'bytes', so 0xD0, and 0xD1 in this case. This particular device data sheet, is using the 7bit address format.
Hence the bytes that need to be sent when working with the PIC, are 0xD0, and 0xD1. All correct.
The '0' response, means the device is also supporting GCA. The zero address is reserved for a 'general call' function, which can be implemented to allow things like software reprogramming of the device address, or special configuration options. Unless you know what 'extras' the device implements on this address, ignore it.

Best Wishes
andys



Joined: 23 Oct 2006
Posts: 175

View user's profile Send private message

mpu6050 problems
PostPosted: Fri Jan 04, 2013 5:05 am     Reply with quote

Dear Ttelmah,

From your answer i expect my code to work properly but this is not happen.

What is going wrong ???

Furthermore where can i found document to learn more tips (like 0XD0) regarding i2c communication ?
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Fri Jan 04, 2013 8:11 am     Reply with quote

Well starting with the first operation you do. Write to register 0x6B, and send a zero, with the comment that this is to reset the device. Wrong. To reset the device, you need to _set_ bit seven in this register. It clears itself to zero a few moments later. So your very first operation, does not do what you say. I stopped looking here.....

You need to actually study the data sheet.

Best Wishes
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Fri Jan 04, 2013 1:10 pm     Reply with quote

Hi andys,

Sorry for the confusion! I'm traveling now, so I can't check my working code
for confirmation on this, but it appears that Ttelmah is correct. I remember
pulling my hair out over this when I was working on my project, and only
solving it when I took a look at some Arduino code for this chip, and then
the light dawned Smile

Did you get it working?

John
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Fri Jan 04, 2013 3:22 pm     Reply with quote

Unlikely. He has several other problems. On the next operation, he tries to talk to the device, without addressing it....

Looking at some of the basic examples for I2C, and understanding what the bus requires, followed by sitting down with a piece of paper and writing down what needs to be sent, to get the device to work, is needed here.

Best Wishes
andys



Joined: 23 Oct 2006
Posts: 175

View user's profile Send private message

mpu6050 problems
PostPosted: Sat Jan 05, 2013 9:08 pm     Reply with quote

Guys i am completely confused.

Firstly i think the code (before while) is right.
Code:

i2c_start();
i2c_write(0xd0);
i2c_write(0x6b);
i2c_write(0x00);
 i2c_stop();
delay_ms(10);


(you said "To reset the device, you need to _set_ bit seven in this register")

But this is happen with the i2c_write(0xd0);.

Also to the next part of the code (which is presented bellow) you said : "Unlikely. He has several other problems. On the next operation, he tries to talk to the device, without addressing it...."

But with i2c_write(0xd0); I think i addressing the code.
Code:

while(1)
{
//Xacce
i2c_start();
i2c_write(0xd0);  /// sensor address
i2c_write(0x3b);  ///ACCEL_XOUT_H
i2c_start();
i2c_write(0xd1);   
 xacc=i2c_read(0);
printf("xacc:%fl:\n",xacc);
i2c_stop();
i2c_stop();

//Yacce
i2c_start();
i2c_write(0xd0);  /// sensor address
i2c_write(0x3d);  ///ACCEL_YOUT_H
i2c_start();
i2c_write(0xd1);   
Yacc=i2c_read(0);
printf("Yacc:%fl:\n",Yacc);
i2c_stop();
i2c_stop();
}

Can you help me to make it more clear ?
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Sun Jan 06, 2013 2:48 am     Reply with quote

No.

The 'd0', is the _device address_. The sequence for each I2C transaction needs to be:

Start
Address device -select 'write'
Select register (in some cases this can be omitted but not often)
Optionally send data to register(s). (most devices auto-increment)
optionally 'restart'
if restart, address device - select 'read'
if restart Read data from register(s). (same comment auto-increment in some cases)
stop

So the sequence to write 0x80 to register 0x6B is:
Code:

i2c_start(); //start transaction
i2c_write(0xd0); //select device - write
i2c_write(0x6b); //write register address
i2c_write(0x80); //Write 0x80 to register 6B
i2c_stop(); //stop transaction


The code to read the byte from the same register is:
Code:

i2c_start(); //start transaction
i2c_write(0xd0); //select device - write
i2c_write(0x6b); //write register address
i2c_start(); //trigger a restart to re-address device
i2c_write(0xD1); //select device _read_
val=i2c_read(0); //read register 6B - last read should flag with '0'
i2c_stop(); //stop transaction.


Seriously, I'd suggest you 'encapsulate' these as routines. So:
Code:

#define DEVICE_WRITE_ADDRESS 0xD0
#define DEVICE_READ_ADDRESS 0xD1

void write_byte_to_register(int8 register,int8 date_byte) {
   i2c_start(); //start transaction
   i2c_write(DEVICE_WRITE_ADDRESS); //select device - write
   i2c_write(register); //write register address
   i2c_write(data_byte); //Write 0x80 to register 6B
   i2c_stop(); //stop transaction
}

void read_block(int8 register, int8 * data_block, int8 count) {
   int8 loop_count;
   i2c_start(); //start transaction
   i2c_write(DEVICE_WRITE_ADDRESS); //select device - write
   i2c_write(register); //write register address
   i2c_start(); //trigger a restart to re-address device
   i2c_write(DEVICE_READ_ADDRESS); //select device _read_
   for (loop_count=0;loop_count<(count-1);loop_count++) {
       data_block[loop_count]=i2c_read(); //read count-1 bytes
   }
   data_block[loop_count]=i2c_read(0); last byte acknowledge
   i2c_stop(); //stop transaction.
}

//Then to set bit 7 in register 6B, call
   write_byte_to_register(0x6B,0x80); //send 128, setting bit 7

//while to read the 6bytes from the gyros, is:
   int8 gyro_bytes[6];

   read_block(0x43,gyro_bytes,6);


You need to get your head round the whole 'i2c transaction' idea. A 'start', _must_ always be followed by a device address (hence my comment about your later code). Also when you read from a register, the register address automatically increments, so if you read again, you get the next byte. This is a 'block read', and needs to be done for things like the gyro registers. The chip latches the whole 'block' of registers, when an I2C transaction starts, so all six registers have 'same moment' values. If you read them separately this doesn't apply.....

Best Wishes
andys



Joined: 23 Oct 2006
Posts: 175

View user's profile Send private message

mpu6050 problems
PostPosted: Sun Jan 06, 2013 3:35 pm     Reply with quote

What is happened when the 0x80 is written to register 6B ????????

i2c_write(0x80); //Write 0x80 to register 6B

I can find 0x80 value in datasheet
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Sun Jan 06, 2013 7:26 pm     Reply with quote

H andys,

Do a Google search on "MPU6050 Register Map", and download this document. It's required reading, along with the MPU6050 datasheet, to fully understand the operation of this device.

John
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group