我初始化IIC以后再读取传感器设备的寄存器,但是代码会在
I2C_GenerateSTART( I2C1, ENABLE );
之后的
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );
这一句无限循环,看起来开始信号并没有发出去
这是完整代码
GPIO_InitTypeDef GPIO_InitStructure={0}; I2C_InitTypeDef I2C_InitTSturcture={0}; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE ); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_PinRemapConfig(GPIO_PartialRemap3_I2C1, ENABLE);//C17-SCL C16-SDA GPIO_InitStructure.GPIO_Pin = GPIO_Pin_17;// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOC, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_16;// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOC, &GPIO_InitStructure ); I2C_InitTSturcture.I2C_ClockSpeed = bound; I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C; I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_16_9; I2C_InitTSturcture.I2C_OwnAddress1 = address; I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable; I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init( I2C1, &I2C_InitTSturcture ); I2C_Cmd( I2C1, ENABLE ); I2C_AcknowledgeConfig( I2C1, ENABLE );
while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET ); I2C_GenerateSTART( I2C1, ENABLE );//send start while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) ); I2C_Send7bitAddress( I2C1, 0x18, I2C_Direction_Transmitter ); while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) ); while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING ) ); I2C_SendData( I2C1, 0x0f); while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) ); I2C_GenerateSTART(I2C1, ENABLE); while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) ); I2C_Send7bitAddress( I2C1, 0x18, I2C_Direction_Receiver ); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); while( I2C_GetFlagStatus( I2C1, I2C_FLAG_RXNE ) == RESET ) I2C_AcknowledgeConfig( I2C1, DISABLE ); printf("%x\r\n",I2C_ReceiveData(I2C1)); I2C_GenerateSTOP( I2C1, ENABLE );
热门产品 :
CH32V317: 互联型青稞RISC-V MCU