I2C 初始化代码- 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(__FILE__, __LINE__);
- }
- }
- void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- //GPIO_InitTypeDef GPIO_InitStruct;
- if(i2cHandle->Instance==I2C1)
- {
- /* USER CODE BEGIN I2C1_MspInit 0 */
- __HAL_RCC_I2C1_CLK_ENABLE();
-
-
- GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7; //????
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; //GPIO?????
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; //???
- HAL_GPIO_Init(GPIOB,&GPIO_InitStruct);
-
- HAL_GPIO_WritePin(GPIOB, 6, GPIO_PIN_SET); //??SCL
- HAL_GPIO_WritePin(GPIOB, 7, GPIO_PIN_SET); //??SDA
-
-
- HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
-
- i2cHandle->Instance->CR1= I2C_CR1_SWRST; //??I2C???
- i2cHandle->Instance->CR1= 0; //????(??????)
- /* USER CODE END I2C1_MspInit 0 */
-
- /**I2C1 GPIO Configuration
- PB6 ------> I2C1_SCL
- PB7 ------> I2C1_SDA
- */
- GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /* I2C1 clock enable */
- //__HAL_RCC_I2C1_CLK_ENABLE();
- /* USER CODE BEGIN I2C1_MspInit 1 */
- /* USER CODE END I2C1_MspInit 1 */
- }
- }
复制代码 测试发现返回为超时错误。逻辑分析仪抓出数据还没有发送从地址
|
评分
查看全部评分
楼主设的是低速模式,换成高速模式看一下。
在CubeMx中将I2C的改为高速。
HAL_I2C_Read和write的timeout参数设大一点,或者干脆设为最大值,看看通讯是否成功。
评分
查看全部评分