+ Post New Thread
Results 1 to 10 of 10
  1. #1
    Member level 3
    Points: 507, Level: 4

    Join Date
    Mar 2016
    Posts
    61
    Helped
    0 / 0
    Points
    507
    Level
    4

    how to read data by i2c interface?

    hi guys
    I want to read data from mpu 9250. I've written this code. but it's hanging at line 24.
    Code:
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    could you please tell me, what should I do?
    thanks.

    Code:
    1. /* While the bus is busy */
    2. while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
    3. /* Send START condition */
    4. I2C_GenerateSTART(I2C1, ENABLE);
    5. /* Test on EV5 and clear it */
    6. while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
    7. /* Send 9250 address for write */
    8. I2C_Send7bitAddress(I2C1, MPU9250_ADDRESS , I2C_Direction_Transmitter);
    9. /* Test on EV6 and clear it */
    10. while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    11. /* Clear EV6 by setting again the PE bit */
    12. //I2C_Cmd(I2C1, ENABLE);
    13. /* Send the 9250's internal address to write to */
    14. I2C_SendData(I2C1, ReadAddr);
    15. /* Test on EV8 and clear it */
    16. while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    17. /* Send STRAT condition a second time */
    18. I2C_GenerateSTART(I2C1, ENABLE);
    19. /* Test on EV5 and clear it */
    20. while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
    21. /* Send 9250 address for read */
    22. I2C_Send7bitAddress(I2C1, MPU9250_ADDRESS , I2C_Direction_Receiver);
    23. /* Test on EV6 and clear it */
    24. while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    25. /* While there is data to be read */
    26. while(NumByteToRead)
    27. {
    28. if(NumByteToRead == 1)
    29. {
    30. /* Disable Acknowledgement */
    31. I2C_AcknowledgeConfig(I2C1, DISABLE);
    32. /* Send STOP Condition */
    33. I2C_GenerateSTOP(I2C1, ENABLE);
    34. }
    35. /* Test on EV7 and clear it */
    36. if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
    37. {
    38. /* Read a byte from the 9250 */
    39. *pBuffer = I2C_ReceiveData(I2C1);
    40. /* Point to the next location where the byte read will be saved */
    41. pBuffer++;
    42. /* Decrement the read bytes counter */
    43. NumByteToRead--;
    44. }
    45. }
    46. /* Enable Acknowledgement to be ready for another reception */
    47. I2C_AcknowledgeConfig(I2C1, ENABLE);
    48. }

  2. #2
    Member level 5
    Points: 549, Level: 4

    Join Date
    May 2016
    Posts
    81
    Helped
    2 / 2
    Points
    549
    Level
    4

    Re: how to read data by i2c interface?

    which controller you are using?


    1 members found this post helpful.

    •   Alt18th May 2016, 12:30

      advertising

        
       

  3. #3
    Member level 3
    Points: 507, Level: 4

    Join Date
    Mar 2016
    Posts
    61
    Helped
    0 / 0
    Points
    507
    Level
    4

    Re: how to read data by i2c interface?

    Quote Originally Posted by akshaybhavsar View Post
    which controller you are using?
    thanks for the reply.
    I'm using stm32f103ret. and I use IAR ide.



    •   Alt18th May 2016, 12:37

      advertising

        
       

  4. #4
    Member level 5
    Points: 549, Level: 4

    Join Date
    May 2016
    Posts
    81
    Helped
    2 / 2
    Points
    549
    Level
    4

    Re: how to read data by i2c interface?

    might be problem with I2C initialization


    1 members found this post helpful.

    •   Alt18th May 2016, 12:47

      advertising

        
       

  5. #5
    Member level 3
    Points: 507, Level: 4

    Join Date
    Mar 2016
    Posts
    61
    Helped
    0 / 0
    Points
    507
    Level
    4

    Re: how to read data by i2c interface?

    Quote Originally Posted by akshaybhavsar View Post
    might be problem with I2C initialization
    I have initialized it by these command:

    Code:
      I2C_InitTypeDef  I2C_InitStructure;    
       
      /* I2C configuration */   
      I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;   
      I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;   
      I2C_InitStructure.I2C_OwnAddress1 = 0x00;   
      I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;   
      I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;   
      I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;   
         
      /* I2C Peripheral Enable */   
      I2C_Cmd(I2C1, ENABLE);   
      /* Apply I2C configuration after enabling it */   
      I2C_Init(I2C1, &I2C_InitStructure);



  6. #6
    Member level 5
    Points: 549, Level: 4

    Join Date
    May 2016
    Posts
    81
    Helped
    2 / 2
    Points
    549
    Level
    4

    Re: how to read data by i2c interface?

    Code:
    void I2C1init(void) 
    {        
    GPIO_InitTypeDef          GPIO_InitStructure;
    I2C_InitTypeDef   I2C_InitStructure;
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); 
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,  ENABLE);
    
      GPIOB->BSRRL = BIT_6 | BIT_7;                            // SDA, SCL -> hi
      GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6 | GPIO_Pin_7; // SDA, SCL def
      GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;            // alternate function
      GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;           // use open drain !
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
      GPIO_Init(GPIOB, &GPIO_InitStructure);   
    
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);  // PB6:I2C1_SCL
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);  // PB7:I2C1_SDA
      
      I2C_InitStructure.I2C_ClockSpeed      = 100000;
      I2C_InitStructure.I2C_Mode            = I2C_Mode_I2C;
      I2C_InitStructure.I2C_DutyCycle       = I2C_DutyCycle_16_9;
      I2C_InitStructure.I2C_OwnAddress1      = 0;
      I2C_InitStructure.I2C_Ack             = I2C_Ack_Disable;
      I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
      I2C_Init(I2C1, &I2C_InitStructure);
      I2C_Cmd(I2C1, ENABLE);
    }
    - - - Updated - - -

    Please make use of this initialization function

    - - - Updated - - -

    if it works



  7. #7
    Member level 3
    Points: 507, Level: 4

    Join Date
    Mar 2016
    Posts
    61
    Helped
    0 / 0
    Points
    507
    Level
    4

    Re: how to read data by i2c interface?

    thanks. but I'm sure that I have initialized it correctly, because I have used it before for a bmp180 sensor. my problem is at line 24 of above code not in its beginning.



    •   Alt18th May 2016, 13:59

      advertising

        
       

  8. #8
    Advanced Member level 5
    Points: 11,414, Level: 25
    schmitt trigger's Avatar
    Join Date
    Apr 2013
    Location
    C4E4DEEEDCE6ECD2
    Posts
    1,931
    Helped
    631 / 631
    Points
    11,414
    Level
    25

    Re: how to read data by i2c interface?

    If you want to perform serious work with logic busses, you will require, sooner or later, a logic analyzer.

    Look on Ebay. There are some pretty cheap ones.

    Other than that, I would look with a dual trace scope, and least check that the SDA and SCL signals are there, and meet the proper logic levels.
    My batteries are recharged by "Helpful Post" ratings.
    If you feel that I've helped you, please indicate it as a Helpful Post


    1 members found this post helpful.

  9. #9
    Super Moderator
    Points: 22,487, Level: 36
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Portugal
    Posts
    6,862
    Helped
    835 / 835
    Points
    22,487
    Level
    36
    Blog Entries
    5

    Re: how to read data by i2c interface?

    Do you have other I2C devices physically connected to the same communication bus ? If yes, just to make a simple check if there could be something wrong in the programming side, remove them and try again.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )


    1 members found this post helpful.

  10. #10
    Member level 3
    Points: 507, Level: 4

    Join Date
    Mar 2016
    Posts
    61
    Helped
    0 / 0
    Points
    507
    Level
    4

    Re: how to read data by i2c interface?

    thanks for the comment. I've test it with bmp180 sensor. it works properly.

    - - - Updated - - -

    I find the solution. the sensor must be initialized. I hadn't seen any sensor like that. thank you all.
    this page helped me a lot.
    http://www.lucidarme.me/?p=5057



--[[ ]]--