Skip to main content
uartist
Associate II
June 17, 2022
Solved

I2C interfacing not working while using SPI.

Hello.

I have been working on a project where the I2C based sensor I have chosen works by itself. When I enable a SPI based module, the I2C sensor does not seem to send any data.

The I2C lines don't even have any conflicts with other pins.

I have included this main code used during testing.

int main(void)
{
 /* USER CODE BEGIN 1 */
	int i2c, test;
	char out[70] = {"\0"};
 /* USER CODE END 1 */
 
 /* MCU Configuration--------------------------------------------------------*/
 
 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();
 
 /* USER CODE BEGIN Init */
 
 /* USER CODE END Init */
 
 /* Configure the system clock */
 SystemClock_Config();
 
 /* USER CODE BEGIN SysInit */
 
 /* USER CODE END SysInit */
 
 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_I2C1_Init();
 MX_LPUART1_UART_Init();
 MX_SPI1_Init();
 MX_ADC_Init();
 /* USER CODE BEGIN 2 */
 
 
 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1);
 sprintf(out, "This is an ADC Test\r\n");
 HAL_UART_Transmit(&hlpuart1, (uint8_t*)out, sizeof(out), 200);
 HAL_Delay(2000);
 SPIMod_Reset();
 
 
 /* USER CODE END 2 */
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */
 
	 sprintf(out, "I2C Reading: %d, Test O/P: %d\n", i2c, test);
	 HAL_UART_Transmit(&hlpuart1, (uint8_t*)out, sizeof(out), 100);
	 i2c = Read_i2c();
	 test = interpolate_func(i2c);
 
	 if(DAC_out <= UnderRange)
	 {
	 SPIMod_Set_Out_Value_uA(4000);
	 }
	 
	 else if(DAC_out >= OverRange)
	 {
	 SPIMod_Set_Out_Value_uA(20000);
	 }
	 
	 else
	 {
	 SPIMod_Set_Out_Value_uA(DAC_out);
	 }
 
	 HAL_Delay(150);
 /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */
}

This topic has been closed for replies.
waclawek.jan
waclawek.janBest answer
Super User
June 17, 2022

The problem is probably in Read_i2c() or SPIMod_Set_Out_Value_uA().

JW

4 replies

waclawek.jan
waclawek.janBest answer
Super User
June 17, 2022

The problem is probably in Read_i2c() or SPIMod_Set_Out_Value_uA().

JW

Andrew Neil
Super User
June 17, 2022

Have you used an oscilloscope or analyser to see what's actually happening on the wires?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
uartist
uartistAuthor
Associate II
June 20, 2022

I found the problem. There was some initialization issue on my side.

Thanks for solution suggestions.

Andrew Neil
Super User
June 20, 2022

@uartist​ "I found the problem"

thanks for feeding back - now please mark the solution:

0693W000008y9fZQAQ.png

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.