cancel
Showing results for 
Search instead for 
Did you mean: 

Send And Receive Data with I2C

mahmoud boroumand
Associate III

Hello

I Have stm32f107vct6 And I Want to send data from master to slave I2C Protocol . I Wrote Some code But it didn't work.

Do you Have complete Example for i2c?

this is my code

Master:

while(1){
	while(!__HAL_I2C_GET_FLAG(&hi2c1,I2C_FLAG_BUSY))
			HAL_I2C_Master_Transmit(&hi2c1, 0x80, buffer,20, 10); 
	
	
		HAL_Delay(500);
	
	
}

Slave:

	 hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 400000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0x80;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
	
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
	
	HAL_I2C_EnableListen_IT(&hi2c1);
 
 
void I2C1_EV_IRQHandler(void)
{
  /* USER CODE BEGIN I2C1_EV_IRQn 0 */
		HAL_I2C_Slave_Receive(&hi2c1,buffer,20,10000);
   
  /* USER CODE END I2C1_EV_IRQn 0 */
  HAL_I2C_EV_IRQHandler(&hi2c1);
  /* USER CODE BEGIN I2C1_EV_IRQn 1 */
 
		
  /* USER CODE END I2C1_EV_IRQn 1 */
}
 
 

on my code I2C1_EV_IRQHandler call success but data doesn't receive anything.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

There are several examples in the CubeMX repository. Here is one of them:

https://github.com/STMicroelectronics/STM32CubeF1/blob/441b2cbdc25aa50437a59c4bffe22b88e78942c9/Projects/STM3210E_EVAL/Examples/I2C/I2C_TwoBoards_ComPolling/readme.txt

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

1 REPLY 1
TDK
Guru

There are several examples in the CubeMX repository. Here is one of them:

https://github.com/STMicroelectronics/STM32CubeF1/blob/441b2cbdc25aa50437a59c4bffe22b88e78942c9/Projects/STM3210E_EVAL/Examples/I2C/I2C_TwoBoards_ComPolling/readme.txt

If you feel a post has answered your question, please click "Accept as Solution".