我初始化IIC以后再讀取傳感器設(shè)備的寄存器,但是代碼會在
I2C_GenerateSTART(?I2C1,?ENABLE?);
之后的
while(?!I2C_CheckEvent(?I2C1,?I2C_EVENT_MASTER_MODE_SELECT?)?);
這一句無限循環(huán),看起來開始信號并沒有發(fā)出去
這是完整代碼
????????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?);
熱門產(chǎn)品 :
CH592: RISC-V內(nèi)核BLE5.4無線MCU