Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to read data by i2c interface?

Status
Not open for further replies.

hamidkavianathar

Member level 5
Joined
Mar 6, 2016
Messages
89
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
804
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:
[LIST=1]
[*]    /* While the bus is busy */   
[*]  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));   
[*]     
[*]  /* Send START condition */   
[*]  I2C_GenerateSTART(I2C1, ENABLE);   
[*]     
[*]  /* Test on EV5 and clear it */   
[*]  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));   
[*]      
[*]  /* Send 9250 address for write */   
[*]  I2C_Send7bitAddress(I2C1, MPU9250_ADDRESS , I2C_Direction_Transmitter);   
[*]   
[*]  /* Test on EV6 and clear it */   
[*]  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));   
[*]     
[*]  /* Clear EV6 by setting again the PE bit */   
[*]  //I2C_Cmd(I2C1, ENABLE);   
[*]   
[*]  /* Send the 9250's internal address to write to */   
[*]  I2C_SendData(I2C1, ReadAddr);     
[*]   
[*]  /* Test on EV8 and clear it */   
[*]  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));   
[*]     
[*]  /* Send STRAT condition a second time */     
[*]  I2C_GenerateSTART(I2C1, ENABLE);   
[*]     
[*]  /* Test on EV5 and clear it */   
[*]  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));   
[*]     
[*]  /* Send 9250 address for read */   
[*]  I2C_Send7bitAddress(I2C1, MPU9250_ADDRESS , I2C_Direction_Receiver);   
[*]     
[*]  /* Test on EV6 and clear it */   
[*]  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));   
[*]     
[*]  /* While there is data to be read */   
[*]  while(NumByteToRead)     
[*]  {   
[*]    if(NumByteToRead == 1)   
[*]    {   
[*]      /* Disable Acknowledgement */   
[*]      I2C_AcknowledgeConfig(I2C1, DISABLE);   
[*]         
[*]      /* Send STOP Condition */   
[*]      I2C_GenerateSTOP(I2C1, ENABLE);   
[*]    }   
[*]   
[*]    /* Test on EV7 and clear it */   
[*]    if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))     
[*]    {         
[*]      /* Read a byte from the 9250 */   
[*]      *pBuffer = I2C_ReceiveData(I2C1);   
[*]   
[*]      /* Point to the next location where the byte read will be saved */   
[*]      pBuffer++;    
[*]         
[*]      /* Decrement the read bytes counter */   
[*]      NumByteToRead--;           
[*]    }      
[*]  }   
[*]   
[*]  /* Enable Acknowledgement to be ready for another reception */   
[*]  I2C_AcknowledgeConfig(I2C1, ENABLE);   
[*]}   
[*] 
[/LIST]
 

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);
 

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
 

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.
 

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.
 
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.
 
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.
https://www.lucidarme.me/?p=5057
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top