0
\$\begingroup\$

I'm using MPU6050 with PIC18F45 protocol I2C. On EasyPIC v6 board its just send me back anything that I put in this WRITE command I2C1_Wr(0xD3);. I replaced the sensor with New One too. Its no good. I'm using MikroC Pro for Programming. My supervisor is forcing me to drop this project can someone tell me the problem with this code please. Here is the Code:

void MPU6050_Activ()
{    I2C1_Init(100000);
     I2C1_Start();
     I2C1_Wr(0x23);   //AD0 TO HIGH NOW ADD IS 0x69 to Write 0xD2 to Read 0xD3
     I2C1_Wr(0x6B); //PWR_MGMT REG ADDRESS
     I2C1_Wr(0x00);  // PWR_MNGMT Reg set to Intenal Clock no Sleep no Reset no Cycle
     I2C1_Stop();

     Delay_ms(70);

     I2C1_Start();
     I2C1_Wr(0x22);
     I2C1_Wr( 0x1B);   //GYRO_CONFIG  Reg
     I2C1_Wr( 0x00 ); //gyro_config, +-250 °/s
     I2C1_Stop();


}

    char text_output[13];
    int data_out;
    unsigned short MPU6050_Read(unsigned short rAddr)
  {
      unsigned short reslt;
      I2C1_Start();
      I2C1_Wr( 0x22 );
      I2C1_Wr(rAddr);  //66
      I2C1_Repeated_Start();
      I2C1_Wr( 0x23 );
      reslt = I2C1_Rd(0u);      // Read the data (NO acknowledge)
      while (!I2C1_Is_Idle()) asm nop;               // Wait for the read cycle to finish
      I2C1_Stop();
      return reslt;
}




void main()
{

     unsigned short read_out;
     TRISC = 0;
     Lcd_Init();                        // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR);               // Clear display
   //Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

     Lcd_Out(2,1,"LED_ON");
     Delay_ms(1000);
     MPU6050_Activ();
     Lcd_Out( 2, 12, "LCDX" );
      while(1){
       Delay_ms(500);
       read_out=MPU6050_Read(MPU6050_RA_GYRO_XOUT_H);
       IntToStr(read_out, text_output );
       Lcd_Out( 1, 7, text_output );
       Lcd_Out( 1, 5, "K" );

      }
}
\$\endgroup\$
  • \$\begingroup\$ Bruce Abbott has already correctly answered your question... but as as an aside I think the "while (!I2C1_Is_Idle()) asm nop;" is unnecessary. When I2C1_Rd() returns you will have your byte. All you need to do next is I2C1_Stop() and the bus will be free. \$\endgroup\$ – aja Nov 16 '14 at 3:57
2
\$\begingroup\$

To select the correct I2C address you should send 0xd2 for writing and 0xd3 for reading (assuming AD0 is pulled high).

" I2C1_Wr(0xD3); " does not appear anywhere in your code. You are sending 0x22 and 0x23, which is not addressing the MPU6050!

\$\endgroup\$
  • \$\begingroup\$ Sorry but I'm sending 0x23 purposely. It still replies the 0x23 back in read_out variable. I tried to communicate with MPU6050 with right combination of AD0 and D3 D2 and read 0x43 register and many others but it still gives back the value I use as Address \$\endgroup\$ – Ahmad Nov 15 '14 at 20:42
  • \$\begingroup\$ I'm not using any level converter but 10K PullUp resistors cause I have seen some tutorials with the same config. But what is confusing that code never gives any wrong value just the Address \$\endgroup\$ – Ahmad Nov 15 '14 at 20:45
  • 1
    \$\begingroup\$ You definitely need pull-ups. The EasyPIC v6 has DIP switches and a jumper for enabling pull-ups on the I2C lines (RC3 and RC4). The reason you are reading back the same address you sent is that only one buffer register is used for both transmission and reception. If no data is received over the bus then the buffer will contain the last byte that was transmitted. \$\endgroup\$ – Bruce Abbott Nov 16 '14 at 1:20
  • \$\begingroup\$ But the last thing I'm sending out is D3 for read which is 211 just before the I2C1_Rd(0u) command which should be in the buffer but it still gives me out D2 = 210 the Address in Write command \$\endgroup\$ – Ahmad Nov 16 '14 at 9:44
  • \$\begingroup\$ Your code:- "I2C1_Wr( 0x23 ); reslt = I2C1_Rd(0u);". On rereading your second comment I see that you are using pullups, but no level converter? The MPU6050 is not 5V tolerant, so you should use a level converter (with pullups on both sides) when interfacing it to a 5V MCU. \$\endgroup\$ – Bruce Abbott Nov 16 '14 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.