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 Interfacing via I2C

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
z1rqdym



Joined: 14 Dec 2014
Posts: 24

View user's profile Send private message Send e-mail MSN Messenger

MPU6050 Interfacing via I2C
PostPosted: Sun Dec 14, 2014 11:18 am     Reply with quote

Hello everyone...

I'm making of quadcopter controller card and software with PIC18F4431 and GY-521 ( MPU6050 ) 3axis gyro + accel.

I have a problem about MPU6050 sensor.
I read a lot of things about it on this forum and in google...

so i am confused.

i learned I2C protocol well. my problem is not about that.

what is the mpu6050 device adress ? is it 0x68? 0x69? (these ones writing on the RM of the mpu6050 pdf.)

but on this forum i see that the device adress is 0xD0 or 0xD1.

in binary 0x68 = 01101000
0xD0 = 11010000

i see that the 0xD0 is shifted to left. why?

i2c use 7bit or 10bit data tarnsfer.

if its about 7bit data transfering maybe i could understand why 0x68 was shifted to left 1 bit.

if its that do like this should i do shifting to left all the data that i get from mpu6050? example for accel_x_high etc... to getting the right data from the sensor?

i write some code about to getting the gyro and accel data .



Code:
int16 accel_x, gyro_x;

// reg_L is the adress for ACCEL_XOUT_L or GYRO_XOUT_L
// reg_H is the adress for ACCEL_XOUT_L or GYRO_XOUT_H


int16 MPU6050_READ_ACCEL_X(int reg_L, int reg_H)
{
  unsigned int accel_x_h, accel_x_l;
  i2c_start();
  i2c_write(0x68);
  i2c_write(reg_H);
  i2c_start();
  i2c_write(0x69);
  accel_x_h=i2c_read(0);
  i2c_stop();
  delay_ms(50);
  i2c_start();
  i2c_write(0x68);
  i2c_write(reg_L);
  i2c_start();
  i2c_write(0x69);
  accel_x_l=i2c_read(0);
  i2c_stop();
  accel_x=make16(accel_x_h,accel_x_l);
  return accel_x;
}

int16 MPU6050_READ_GYRO_X(int reg_L, int reg_H)
{
 int gyro_x_h, gyro_x_l;
 i2c_start();
 i2c_write(0x68);
 i2c_write(reg_H);
 i2c_start();
 i2c_write(0x69);
 gyro_x_h=i2c_read(0);
 i2c_stop();
 delay_ms(50);
 i2c_start();
 i2c_write(0x68);
 i2c_write(reg_L);
 i2c_start();
 i2c_write(0x69);
 gyro_x_l=i2c_read(0);
 i2c_stop();
 gyro_x=make16(gyro_x_h,gyro_x_l);
 return gyro_x;
}
so is there any mistakes?

i can't write in english well but i can understand well Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 14, 2014 11:36 am     Reply with quote

Quote:

what is the mpu6050 device adress ? is it 0x68? 0x69?
but on this forum i see that the device adress is 0xD0 or 0xD1.

Read the 2nd paragraph in this section of the link below:
I2C Device Addressing -
http://www.robot-electronics.co.uk/acatalog/I2C_Tutorial.html
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Dec 14, 2014 11:56 am     Reply with quote

also be sure to use proper logic level translators if you're running the PIC at 5 V ! As teh sensor is a 3V only device !!


it's always a great idea to run PCM Ps' 'i2C scanner program( on this forum) as a 'base line' test to confirm your PIC WILL access any I2C device.


jay
z1rqdym



Joined: 14 Dec 2014
Posts: 24

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Sun Dec 14, 2014 12:14 pm     Reply with quote

well, i read it and i understand it.

in the RegisterMap PDF of MPU6050 there write the DevAdress is 0x68.

that's mean in binary (MSB 0bX1101000 LSB) , the X is unimportent.

in i2c data transfering we send first the MSB to LSB . the last bit is R/W bit to say to slave IC to "I will read on you!" or "I will write to you!" is it right? Smile

so we will shift left 1 bit to get first 7 bit MSB to LSB and then we have all ready the last bit to R/W too.

#define MPU6050_ADRESS 0x68



Code:

#define MPU6050_ADRESS 0x68

int16 accel_x, gyro_x;

// reg_L is the adress for ACCEL_XOUT_L or GYRO_XOUT_L
// reg_H is the adress for ACCEL_XOUT_L or GYRO_XOUT_H

int8 ADRESS_SHIFT_I2C_W(int8 adrss)         
{
 adress = adrss << 1;
 return adress;
}

int8 ADRESS_SHIFT_I2C_R(int8 adrss)
{
 adress = adrss << 1;
 bit_set(adress,0);
 return adress;
}

int16 MPU6050_READ_ACCEL_X(int reg_L, int reg_H)
{
  unsigned int accel_x_h, accel_x_l;
  i2c_start();
  i2c_write(ADRESS_SHIFT(MPU6050_ADRESS));
  i2c_write(reg_H);
  i2c_start();
  i2c_write(ADRESS_SHIFT_I2C_R(MPU6050_ADRESS));
  accel_x_h=i2c_read(0);
  i2c_stop();
  delay_ms(50);
  i2c_start();
  i2c_write(ADRESS_SHIFT_I2C_W(MPU6050_ADRESS));
  i2c_write(reg_L);
  i2c_start();
  i2c_write(ADRESS_SHIFT_I2C_R(MPU6050_ADRESS));
  accel_x_l=i2c_read(0);
  i2c_stop();
  accel_x=make16(accel_x_h,accel_x_l);
  return accel_x;
}

int16 MPU6050_READ_GYRO_X(int reg_L, int reg_H)
{
 int gyro_x_h, gyro_x_l;
 i2c_start();
 i2c_write(ADRESS_SHIFT_I2C_W(MPU6050_ADRESS));
 i2c_write(reg_H);
 i2c_start();
 i2c_write(ADRESS_SHIFT_I2C_R(MPU6050_ADRESS));
 gyro_x_h=i2c_read(0);
 i2c_stop();
 delay_ms(50);
 i2c_start();
 i2c_write(ADRESS_SHIFT_I2C_W(MPU6050_ADRESS));
 i2c_write(reg_L);
 i2c_start();
 i2c_write(ADRESS_SHIFT_I2C_R(MPU6050_ADRESS));
 gyro_x_l=i2c_read(0);
 i2c_stop();
 gyro_x=make16(gyro_x_h,gyro_x_l);
 return gyro_x;
}



so i correct my code, is there now any mistakes?
z1rqdym



Joined: 14 Dec 2014
Posts: 24

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Sun Dec 14, 2014 12:22 pm     Reply with quote

I use 5V for my PIC18F4431. GY-521 has a low dropout regulator for 5V to 3,3V. and the sensors SDA and SCL pins are pull-up to 3,3V with 2,2kOhm resistors.

i read about this discussion too. some one makes it work without change anything. someone can't get ACK. maybe i should use 3,3V dsPIC.

but i haven't a programmer for dsPIC.

or i should use software I2C, with TTL buffered ports on the pic like portB.

if i will use software I2C, my quadcopters' responsiblity may be getting poor.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 14, 2014 12:26 pm     Reply with quote

Quote:
some one makes it work without change anything

There is a large thread here on the MPU-6050 which tells you how to do it.
http://www.ccsinfo.com/forum/viewtopic.php?t=52866

Post a link to the website for your MPU-6050 board.
z1rqdym



Joined: 14 Dec 2014
Posts: 24

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Sun Dec 14, 2014 12:45 pm     Reply with quote

thanks a lot to PCM Programmer and thanks to you to temtronic for your responses Smile

when i working my quadcopter i will take a video there Smile
z1rqdym



Joined: 14 Dec 2014
Posts: 24

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Tue Dec 16, 2014 6:06 am     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=52866&postdays=0&postorder=asc&start=15

the last thread on page2 that posted from PCM programmer,

I examine all the code and compare all of with the InvenSens MPU6000/6050 PDFs'.

So, I have new questions...

the first is, whats that mean

Code:
 
//-----------------------------------------
void Get_Accel_Angles()
{
accel_xangle = 57.295*atan((float)accel_yout/ sqrt(pow((float)accel_zout,2)+pow((float)accel_xout,2)));
accel_yangle = 57.295*atan((float)-accel_xout/ sqrt(pow((float)accel_zout,2)+pow((float)accel_yout,2)));
}   

//--------------------------------------------------



In this code I see to find the yangel by accelerometer. But why we are multiply the atan with 57.295?

accel_xout, accel_yout, accel_zout values are already g forces in hex/dec etc...

why we need to multiply 57,295 with atan?



and the second question is...

that question is about my level of c programming.

Code:
gyro_xrate = (float)gyro_xout/gyro_xsensitivity;

Code:
gyro_xrate = gyro_xout/gyro_xsensitivity;


whats the different in these codes?
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Dec 16, 2014 6:20 am     Reply with quote

I'll take a try..


1) the CCS 'math' library is coded for RADIANS not DEGREES...so you need to convert from radians to degrees.the 'magical' 57 number is the hint.

2) the first one 'casts'( forces) the result to be a floating point number regardless of the 'type' of the variables on the right side.


Jay
z1rqdym



Joined: 14 Dec 2014
Posts: 24

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Tue Dec 16, 2014 6:33 am     Reply with quote

Thanks for answering my first question.

"1radian = 180/Pi degrees" and 180/Pi = 57.295 thanks again Smile

But I'm so sorry but your second answer. I didn't understand it :/

Could you tell a bit more detail, please?
z1rqdym



Joined: 14 Dec 2014
Posts: 24

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Tue Dec 16, 2014 8:06 am     Reply with quote

i thought about "(float)x/y;"

(float) makes the solution a float ? is it right?
z1rqdym



Joined: 14 Dec 2014
Posts: 24

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Tue Dec 16, 2014 9:21 am     Reply with quote

please help please :(

why are we multiply 1/400 with "gyro_xrate"

Quote:

dt=0,0025; //0,0025=1/400

gyro_xangle += gyro_xrate * dt;


is gyro_xrate not already radian/s ?
if it's true, why don't we multiply with 57,295 as like for accel?
if it's false, how and where are we found the dt value 1/400=0,0025?
i think there is a mistake...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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