PIC/MPU 6050 I2C communication

Discussion in 'Programmer's Corner' started by rwallace, Jan 16, 2013.

  1. rwallace

    Thread Starter New Member

    Jan 16, 2013
    3
    0
    PIC 18f4520
    MPU 6050
    I2C Pull Up: 1.8k

    MPlabX with C18 compiler

    I have been trying to communicate with the MPU through an I2C bus for quite a while and can't seem to get an acknowledgement from the MPU. I am fairly new to the PIC and MPU so it could be an obvious problem but I have searched every forum and tried everything I could think of but still no communication.

    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <p18f4520.h>
    5. #include <i2c.h>
    6.  
    7. //Configuration bits
    8. #pragma config WDT = OFF
    9. #pragma config OSC = INTIO67
    10. //#pragma config PWRT + ON
    11. #pragma config LVP = OFF
    12. #pragma config MCLRE = ON
    13. #pragma config BOREN = ON
    14. //=============================================================
    15. void main(void) {
    16.     unsigned char data=0;
    17.     OSCCON = 0b01100000;   //set to 4Mhz Internal clock
    18.     while(OSCCONbits.IOFS != 1);    //wait for oscillator to stabilize
    19.     TRISC = 0x00;
    20.     PORTC = 0x00;
    21.     SSPADD=0x09;         //Sets Baud Rate to 100kHz after equation//
    22.  
    23.     OpenI2C (MASTER,SLEW_OFF);
    24.     if (PIR1bits.SSPIF == 0)    //Test I2C master mode initiated//
    25.     {LATDbits.LATD2=1;}
    26.    
    27.    StartI2C ();
    28.    IdleI2C();
    29.    WriteI2C (0xD0);
    30.    IdleI2C();
    31.    WriteI2C (0x75);
    32.    IdleI2C ();
    33.    WriteI2C (0b00000000);
    34.    IdleI2C ();
    35.    StopI2C ();
    36.    
    37.    StartI2C();
    38.    IdleI2C ();
    39.    WriteI2C (0xD0);
    40.     IdleI2C ();
    41.    WriteI2C (0x75);
    42.    IdleI2C ();
    43.    StartI2C ();
    44.    WriteI2C (0xD1);
    45.    IdleI2C ();
    46.    data = ReadI2C();
    47.    IdleI2C ();
    48.    StopI2C ();
    49. }
    50.  
    As shown I was trying to "wake" it up through the power management register and also reading from the who_am_i register.

    I have also posted a picture of my setup:
    http://i.imgur.com/nnPVj.jpg

    Any help would be greatly appreciated.
     
    Last edited by a moderator: Jan 17, 2013
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    That is always the first thing to check with I2C: get that ACK from the slave.

    One thing that never seems standard is the addresses! If 0xD0 is the write address then 0xD1 should be the read address. See if the data sheet confirms this.
     
Loading...