程序是参考官方例程的
卡在while(!I2C_StateDetect( I2C1 , I2C_PROGRAMMINGMODE_MASTER_SBSEND))
各位大牛帮忙分析一下,感谢!~
void IIC_Init(void)
{
/* Enable GPIOB clock */
RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOB, ENABLE );
/* Enable I2C1 APB1 clock */
RCC_APB1PeriphClock_Enable( RCC_APB1PERIPH_I2C1, ENABLE );
/* Enable DMA1 clock */
RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_DMA1, ENABLE);
printf("I2C 1\r\n");
/* Cofigure the GPIO */
{
GPIO_InitPara GPIO_InitStructure;
GPIO_PinAFConfig( GPIOB ,GPIO_PINSOURCE6,GPIO_AF_1 );
GPIO_PinAFConfig( GPIOB ,GPIO_PINSOURCE7,GPIO_AF_1 );
/* I2C1 GPIO */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_6 | // I2C1 SCL
GPIO_PIN_7; // I2C1 SDA
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;//OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
GPIO_Init( GPIOB , &GPIO_InitStructure );
}
/* Cofigure the I2C1 interface */
{
I2C_InitPara I2C_InitStructure;
I2C_InitStructure.I2C_Protocol = I2C_PROTOCOL_I2C;//I2C_PROTOCOL_SMBUSHOST;
I2C_InitStructure.I2C_DutyCycle = I2C_DUTYCYCLE_2;
I2C_InitStructure.I2C_BitRate = 100000;
I2C_InitStructure.I2C_AddressingMode = I2C_ADDRESSING_MODE_7BIT;
I2C_InitStructure.I2C_DeviceAddress = 0x08;
I2C_Init(I2C1, &I2C_InitStructure);
}
// I2C1->CTLR2|=0x04;
/* Enable I2C1 */
I2C_Enable( I2C1, ENABLE );
/* Enable Acknowledge */
I2C_Acknowledge_Enable( I2C1 , ENABLE );
/* Enable the I2C1 DMA requests */
I2C_DMA_Enable( I2C1 , ENABLE );
printf("I2C bus init success...\r\n");
}
u8 IICwriteBytes(u8 dev, u8 ReadAddr, u8 NumByteToWrite, uint8_t* pBuffer)
{
/*!< While the bus is busy */
while(I2C_GetBitState( I2C1 , I2C_FLAG_I2CBSY))
{
printf("IIC busy\r\n");
}
/*!< Send START condition */
I2C_StartOnBus_Enable( I2C1 , ENABLE );
/*!< Test on EV5 and clear it */
while(!I2C_StateDetect( I2C1 , I2C_PROGRAMMINGMODE_MASTER_SBSEND))///卡在此处
{
printf("Test on EV5\r\n");
}
/*!< Send MPU6050 address for write */
I2C_AddressingDevice_7bit( I2C1 , dev, I2C_DIRECTION_TRANSMITTER);////////////////////////
/*!< Test on EV6 and clear it */
while(!I2C_StateDetect( I2C1 , I2C_PROGRAMMINGMODE_MASTER_TRANSMITTER_ADDSEND))
{
printf("Test on EV6\r\n");
}
/*!< Send the MPU6050's internal address to write to : only one byte Address */
I2C_SendData( I2C1 , ReadAddr);
/*!< Test on EV8 and clear it */
while(!I2C_StateDetect( I2C1 , I2C_PROGRAMMINGMODE_MASTER_BYTE_TRANSMITTED))
{
printf("Test on EV8r\n");
}
/* Configure the DMA Tx Channel */
{
DMA_InitPara DMA_InitStruc;
/* Configure the DMA Tx Channel with the buffer address and the buffer size */
DMA_DeInit( DMA1_CHANNEL2 );
DMA_InitStruc.DMA_PeripheralBaseAddr = (uint32_t)&I2C1->DTR ;
DMA_InitStruc.DMA_MemoryBaseAddr = (uint32_t)pBuffer;
DMA_InitStruc.DMA_DIR = DMA_DIR_PERIPHERALDST;
DMA_InitStruc.DMA_BufferSize = (uint32_t)NumByteToWrite;
DMA_InitStruc.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE;
DMA_InitStruc.DMA_MemoryInc = DMA_MEMORYINC_ENABLE;
DMA_InitStruc.DMA_PeripheralDataSize = DMA_PERIPHERALDATASIZE_BYTE;
DMA_InitStruc.DMA_MemoryDataSize = DMA_MEMORYDATASIZE_BYTE;
DMA_InitStruc.DMA_Mode = DMA_MODE_NORMAL ;
DMA_InitStruc.DMA_Priority = DMA_PRIORITY_VERYHIGH ;
DMA_InitStruc.DMA_MTOM = DMA_MEMTOMEM_DISABLE;
DMA_Init( DMA1_CHANNEL2 , &DMA_InitStruc );
}
/* Inform the DMA that the next End Of Transfer Signal will be the last one */
I2C_DMALastTransfer_Enable( I2C1 , ENABLE );
/* Enable the I2C1 DMA Tx Channel */
DMA_Enable( DMA1_CHANNEL2 , ENABLE );
/* Wait until I2C1 Rx DMA1 Channel Transfer Complete */
while ( DMA_GetBitState( DMA1_FLAG_TC2 ) == RESET)
{
printf("IIC RX...\r\n");
}
/* Wait until the last data byte is received into the shift register */
while(!I2C_GetBitState(I2C1, I2C_FLAG_BTC))
{
}
/* Clear ADDR Flag by reading STR1 then STR2 registers (STR1 have already
been read) */
(void)I2C1->STR2;
printf("IIC RX doen...\r\n");
/* Send a stop condition */
I2C_StopOnBus_Enable( I2C1, ENABLE );
return 1;
}
|