这次试用I2C读写外部EEP,采用AT24C,但是调试已进入循环,已哭晕在厕所。请高人指点一下。采用CUBE,生成初始化代码;
void MX_I2C2_Init(void)
{
hi2c2.Instance = I2C2;
hi2c2.Init.clockSpeed = 10000;
hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
HAL_I2C_Init(&hi2c2);
}
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hi2c->Instance==I2C2)
{
/* USER CODE BEGIN I2C2_MspInit 0 */
/* USER CODE END I2C2_MspInit 0 */
/**I2C2 GPIO Configuration
PB10 ------> I2C2_SCL
PB11 ------> I2C2_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C2_CLK_ENABLE();
/* USER CODE BEGIN I2C2_MspInit 1 */
/* USER CODE END I2C2_MspInit 1 */
}
}
采用前辈给的测试方式(看图片);
http://www.stmcu.org/module/foru ... xtra=page%3D&page=1
但是初始化之后,busy位一直都是高(看图片),进入前辈写的测试函数后
void TestIIC(void)
{
unsigned int i=0,tmp12;
uint8_t IIC_DAT[100]={'1','2'};
uint8_t err='1';
tmp12= HAL_I2C_IsDeviceReady(&hi2c2,0xa4,3000,1000); //检查器件准备好
for(i=0;i<100;i++)
EEPROM_WriteByte(i,'e');
for(i=0;i<100;i++)
{
IIC_DAT=EEPROM_ReadByte(i);
if(IIC_DAT!='e')
err++;
}
while( HAL_BUSY == HAL_UART_Transmit(&huart2,&err,1,1)){};
while( HAL_BUSY == HAL_UART_Transmit(&huart2,IIC_DAT,100,1)){};
while(1);
}
之后一直进入HAL_I2C_IsDeviceReady(&hi2c2,0xa4,3000,1000); //检查器件准备
/* Wait until BUSY flag is reset */
I2C_WaitOnFlagUntilTimeout()
中的I2C_WaitOnFlagUntilTimeout()函数,等待busy位变低。但是外部引脚通过示波器测试都处于拉高状态,理应不应当处于BUSY状态。但就跳不出等待查询BUSY位函数,一直超时等待。
连读写程序都无法进入。
还请搞过这个东西的前辈指点迷津啊。
|