2020-02-13 08:17 AM
Hi,
I'm using an STM32F429 MCU and trying to connect to a sensor through I2C. However, I measured both I2C pins with an oscilloscope and they were not showing any clock pulse or a signal. Here's my code for initiation:
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();
printf("I2C Initiation Failed!\r\n");
}
}
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(i2cHandle->Instance==I2C1)
{
__GPIOB_CLK_ENABLE();
/* I2C1 clock enable */
__I2C1_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB8 ------> I2C1_SCL
PB9 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
}
And the following code for testing:
while (1)
{
unsigned char buf;
HAL_I2C_Mem_Read(&hi2c1, 0x29, 4, I2C_MEMADD_SIZE_8BIT, buf, 1, 10000);
printf("Testing!\r\n");
HAL_Delay(1000);
}
I believe I measured the correct pins(PB8 and PB9), but I was not even seeing an SCL signal. The slave device was not connected yet. Does it have to be connected to see a clock signal? Or is there anything wrong with the code? Thanks.
Solved! Go to Solution.
2020-02-13 08:33 AM
Problem solved after connecting the sensor to the MCU.
2020-02-13 08:33 AM
Problem solved after connecting the sensor to the MCU.