cancel
Showing results for 
Search instead for 
Did you mean: 

I2C doesn't work after uploading (only sometimes)

Senax
Associate II

I wrote a little program that reads I2C values with interrupts: The Problem is that sometimes it just doesn't work after uploading. I just have to run it so many times till it works (not changing anything in the code). It seems like its just coincidence if it works or not.

Here are the Details:

Sensor: HMC 5983 or MPU-6050 (same problem)

Board: STM32F103RB (Nucleo 64)

Code (without the generated Cubemx stuff):

int main(void) {
    /*/////////////////////Setup/////////////////////////*/
	buf[0] = 0x00;
	buf[1] = 0x78;
	buf[2] = 0x80;
	buf[3] = 0x00;
	HAL_I2C_Master_Transmit(&hi2c1, Kompass_ADDR, buf, 4, 100);
	/*//////////////////////Start of IT Routine//////////////////*/
	buf[0] = 0x03;
	HAL_I2C_Master_Transmit_IT(&hi2c1, Kompass_ADDR, buf, 1);
 
	/* USER CODE END 2 */
 
	/* Infinite loop */
	/* USER CODE BEGIN WHILE */
	while (1) {
		/*/////////////////////printing the value////////////////////////*/
	    sprintf((char*) Serial_buffer, " X %i \r\n", DataX);
		HAL_UART_Transmit(&huart2, (uint8_t*) Serial_buffer, strlen((char*) Serial_buffer), 100);
		HAL_Delay(500);
		/* USER CODE END WHILE */
 
		/* USER CODE BEGIN 3 */
	}
	/* USER CODE END 3 */
}
 
 
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c) {
	HAL_I2C_Master_Receive_IT(&hi2c1, Kompass_ADDR, buf, 6);
}
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c) {
	DataX = ((int16_t) buf[0]) << 8 | buf[1];
	buf[0] = 0x03;
	HAL_I2C_Master_Transmit_IT(&hi2c1, Kompass_ADDR, buf, 1);
}
////////////////////////////I2C Init Function////////////////////////
static void MX_I2C1_Init(void) {
 
	hi2c1.Instance = I2C1;
	hi2c1.Init.ClockSpeed = 100000;
	hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
	hi2c1.Init.OwnAddress1 = 0;
	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();
	}
}
 

Thank you in Advance 🙂

14 REPLIES 14
Senax
Associate II

So I also tried to use the sensor on an Arduino UNO, what just worked fine. I could upload the program a thousand times, and it worked every time.

Senax
Associate II

So this sounds interesting: when the MPU-6050 is connected to the board, I2C works fine, but as soon as I plug the other I2C Sensor in it stops working. Is it possible that one sensor blocks the other one?

Senax
Associate II

So I think I may have found the cause of the problem (please correct me if I'm wrong):

My I2C Logic is 5v and all of my sensors are connected through the same 5v. So according to the data sheet the minimum resistance must be 1.53 kΩ for it to work.

HMC 5983 Resistance: 1.57 kΩ

MPU-6050 Resistance: 1.85 kΩ

But as soon as I connect them together the Resistance drops to around 1.45 kΩ

I will try to build a pull-up resistor into the circuit.

Senax
Associate II

Nope, that didn't help.

LuanaQ
Associate

Hey there! I'm having the exact same issue, did you manage to find a solution? Thanks