View previous topic :: View next topic |
Author |
Message |
Mahdi
Joined: 21 Aug 2012 Posts: 47
|
how to connect to mpu6050 gyro sensor-i2c? |
Posted: Tue Aug 21, 2012 3:33 pm |
|
|
Hi.
I want to connect to mpu6050 gyro and accel sensor with i2c protocol.
Please help me.
Please give me a example code.
I want to access and write to register of sensor.
How to do addressing of the i2c sensor address and register address with ccs?
This is mpu6050 datasheet:
http://www.invensense.com/mems/gyro/documents/PS-MPU-6000A.pdf
tx |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Tue Aug 21, 2012 4:36 pm |
|
|
Can't give any code as we don't know which PIC you're using !!!
I'm assuming you're using a 3 volt PIC(?) as the sensor is a 3 volt device ???
Also be sure to use the correct I2C buss pullups.
Also download PCM programmer's I2C diagnostic program from this forum, it'll confirm you CAN talk to the sensor, have it wired up correctly, etc.
hth
jay |
|
|
Mahdi
Joined: 21 Aug 2012 Posts: 47
|
|
Posted: Wed Aug 22, 2012 12:40 am |
|
|
temtronic wrote: | Can't give any code as we don't know which PIC you're using !!!
I'm assuming you're using a 3 volt PIC(?) as the sensor is a 3 volt device ???
Also be sure to use the correct I2C buss pullups.
Also download PCM programmer's I2C diagnostic program from this forum, it'll confirm you CAN talk to the sensor, have it wired up correctly, etc.
hth
jay |
I'm using 16f877a.
I'm not writing into the register of sensor.
For example I want to write 0 in bit6 of 0x3b address register.
I need example of code.
pls |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Aug 22, 2012 1:15 am |
|
|
Thing is that the sensor is a 3v device, and the PIC you have is a 5v device. You need level translation hardware between them.....
Best Wishes |
|
|
Mahdi
Joined: 21 Aug 2012 Posts: 47
|
|
Posted: Wed Aug 22, 2012 4:14 am |
|
|
thank u so much temtronic and Ttelmah for your answer |
|
|
Mahdi
Joined: 21 Aug 2012 Posts: 47
|
|
Posted: Wed Aug 22, 2012 7:00 am |
|
|
Ttelmah wrote: | Thing is that the sensor is a 3v device, and the PIC you have is a 5v device. You need level translation hardware between them.....
Best Wishes |
The sensor and the pic16f877a vcc is 3 volt.
When i want to read from any register the lcd show 255.
This is my code:
Code: |
void main()
{
set_tris_b(0x00); //set port b as outputs
lcd_init();
long input;
while(true)
{
i2c_start();
i2c_write(0x68);// device address
input=i2c_read(0x3b);// register address(X accel)
i2c_stop();
delay_ms(10);
lcd_gotoxy(1,1);
printf(lcd_putc,"%Lu",input);
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Aug 22, 2012 7:38 am |
|
|
You can't just make a chip work of 3v, by attaching 3v....
The PIC16F877A, _requires_ 4v _minimum_. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Aug 22, 2012 7:46 am |
|
|
You are also misunderstanding I2C.
You don't read and write in one instruction.
Sequence is:
START
Write device address, with bottom bit clear (we are writing).
Write register address (write register number).
START (repeated start)
Write device address with bottom bit _set_ (says we want to read)
read reply
stop
Look at the I2C bus scanner from PCM programmer (already mentioned). It shows 'how' to scan a bus for replies, and as such how the transactions work.
The 'repeated start', is how you turn the bus around, after sending the register address, before reading. |
|
|
Mahdi
Joined: 21 Aug 2012 Posts: 47
|
|
Posted: Wed Aug 22, 2012 8:02 am |
|
|
Ttelmah wrote: | You can't just make a chip work of 3v, by attaching 3v....
The PIC16F877A, _requires_ 4v _minimum_. |
but the datasheet of 16f877a said:
Wide operating voltage range: 2.0V to 5.5V
Last edited by Mahdi on Wed Aug 22, 2012 8:12 am; edited 1 time in total |
|
|
Mahdi
Joined: 21 Aug 2012 Posts: 47
|
|
Posted: Wed Aug 22, 2012 8:11 am |
|
|
I have no experience with the ccs.
can you give me a sample code of following the address
i want to read from device
device address:0x68
register address: 0x3b
thank you so much |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 22, 2012 2:45 pm |
|
|
Quote: | Wide operating voltage range: 2.0V to 5.5V |
That wide range is only for the "LF" version of the 877A.
The part number is PIC16LF877A.
But, you can use an 3v i2c slave with a 5v PIC, if you use software i2c
on PortB, which is a TTL-compatible i/o port.
In software i2c, the i2c bits are sent and received by "bit banging" with
the i/o port pins. On PortB, the Vih (high level input voltage) is 2.0 volts.
This is true even if the Vdd for the PIC is 5.0 volts.
So, you can use pull-ups on the i2c bus that pull-up to +3.0v, and it works
perfectly well with the TTL-compatible inputs on PortB. You can use
3.3K ohm resistors for the pull-ups on the SCL and SDA lines.
This technique does not work with hardware i2c, where the PIC is running
at +5v for Vdd. It requires software i2c. The CCS #use i2c() library
supports software i2c. Software i2c is the default case for that library.
So, it's no problem to do it.
Quote: | device address:0x68
|
That's the slave address in 7-bit Philips format. But CCS doesn't use that
format. CCS uses 8-bit format. To convert to CCS format, just shift
the Philips address left by 1 bit position. So, the correct addresses to
use with the i2c_write() function are:
CCS i2c slave write address = 0xd0
CCS i2c slave read address = 0xd1
This assumes that you have connected pin 9 on the MPU-6050 to ground.
Here is a link to the i2c bus scanner program. Modify the first few lines
of the program (#include, #fuses, #use delay, #use rs232) to fit your
PIC and your board:
http://www.ccsinfo.com/forum/viewtopic.php?t=47707&start=21
This program will tell you if the PIC can find the MPU-6050 on the i2c bus. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Thu Aug 23, 2012 2:06 am |
|
|
As a further comment, the F877, will probably run at 3.3v, _but will not be reliable_. Get the LF variant.
This is down to 'read the data sheet, not just the header'. The sheet covers both chip versions, and _between them_, the two chip types have the 'wide operating voltage range' mentioned.....
Best Wishes |
|
|
Mahdi
Joined: 21 Aug 2012 Posts: 47
|
|
Posted: Thu Aug 23, 2012 6:42 am |
|
|
thanks alot pcm programer
Ttelmah wrote: | As a further comment, the F877, will probably run at 3.3v, _but will not be reliable_. Get the LF variant.
This is down to 'read the data sheet, not just the header'. The sheet covers both chip versions, and _between them_, the two chip types have the 'wide operating voltage range' mentioned.....
Best Wishes |
Ttelmah your answer is right.but the LF frequency is low.until to 10Mhz
and it receive alot of noise.There is no way that I use the 16f?
for example use the resistor in scl and sda line or use 3.0v Zener diode?
i dont understand this part
Quote: |
But, you can use an 3v i2c slave with a 5v PIC, if you use software i2c
on PortB, which is a TTL-compatible i/o port. |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Thu Aug 23, 2012 7:30 am |
|
|
This is down to input threshold levels.
A 'TTL' port pin, is considered to be 'high', if it is above 2.4v. A Schmitt port pin, is only considered 'high', when it gets to 4v (with a PIC running at 5v).
The hardware I2C input, uses a Schmitt buffer.
So a 3.3v device connected to a 5v PIC, using the hardware I2C, won't work. However you can connect such a device to a software I2C port provided you choose pins that have TTL input buffers (data sheet).
Fortunately, CCS provides software I2C just as easily as hardware.
Since I2C is an 'open collector' bus (the chips only drive downwards), the outputs from the PIC will drive the lines fine.
Wiring then has to be:
Run the PIC off 5v
Choose PIC pins with TTL input buffers.
Select these in #use I2C
Connect the PIC I2C pins to the 3v device.
Have the I2C pullups, go to the 3v rail (remember that lower rail voltage means smaller resistors will be needed - typically 4K7 is quite OK for short busses at 5v, but at 3v, you need 2K7 to give the same bus drive).
Best Wishes |
|
|
Mahdi
Joined: 21 Aug 2012 Posts: 47
|
|
Posted: Thu Aug 23, 2012 8:19 am |
|
|
This could can use? with 2.2 and 3.3 resistor the out put of micro has 3 volt |
|
|
|