代码如下:
int main(void)
{
int i;
//uint32_t sr2_1;
RCC_Configuration();
RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_I2C1RST, ENABLE);
RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_I2C1RST, DISABLE);
GPIO_Configuration();
I2C_Configuration();
/* Enable I2C1 */
I2C_Enable(I2C1,ENABLE);
I2C_Acknowledge_Enable(I2C1,ENABLE);
/* The software must wait until I2C Bus is idle */
while(I2C_GetBitState(I2C1,I2C_FLAG_I2CBSY));
/* Send a start condition to I2C bus */
I2C_StartOnBus_Enable(I2C1,ENABLE);
/* The software must wait until SBSEND bit is set */
while(!I2C_StateDetect(I2C1,I2C_PROGRAMMINGMODE_MASTER_SBSEND));
/* Send slave address to I2C bus */
I2C_AddressingDevice_7bit(I2C1,I2C_ADDR,I2C_DIRECTION_TRANSMITTER);
//for(;;)
//{
//I2C_SendData(I2C1,I2C1_Buf_Write[0]);
//}
/* The software must wait until ADDSEND bit is set */
while(!I2C_StateDetect(I2C1,I2C_PROGRAMMINGMODE_MASTER_TRANSMITTER_ADDSEND));
/* Wait until the transmit data buffer is empty */
while(!I2C_GetBitState(I2C1,I2C_FLAG_TBE));
for(i=0;i<5;i++)
{
/* Data transmission */
I2C_SendData(I2C1,I2C1_Buf_Write[i]);
while(!I2C_StateDetect(I2C1, I2C_PROGRAMMINGMODE_MASTER_BYTE_TRANSMITTED));
}
/* Send a stop condition to I2C bus */
I2C_StopOnBus_Enable(I2C1,ENABLE);
while(I2C1->CTLR1&0x0200);
/*read*/
I2C_Enable(I2C1,ENABLE);
I2C_Acknowledge_Enable(I2C1,ENABLE);
while(I2C_GetBitState(I2C1, I2C_FLAG_I2CBSY));
I2C_StartOnBus_Enable(I2C1, ENABLE);
while(!I2C_StateDetect(I2C1, I2C_PROGRAMMINGMODE_MASTER_SBSEND));
I2C_AddressingDevice_7bit(I2C1,I2C_ADDR,I2C_DIRECTION_TRANSMITTER);
while(!I2C_StateDetect(I2C1, I2C_PROGRAMMINGMODE_MASTER_RECEIVER_ADDSEND));
I2C_StartOnBus_Enable(I2C1, ENABLE);
while(!I2C_StateDetect(I2C1, I2C_PROGRAMMINGMODE_MASTER_SBSEND));
for(i=0;i<2;i++)
{
I2C_SendData(I2C1,addr[i]);
while(!I2C_StateDetect(I2C1, I2C_PROGRAMMINGMODE_MASTER_BYTE_TRANSMITTED));
}
I2C_AddressingDevice_7bit(I2C1, I2C_ADDR, I2C_DIRECTION_RECEIVER);
while(!I2C_StateDetect(I2C1, I2C_PROGRAMMINGMODE_MASTER_RECEIVER_ADDSEND));
for(i=0;i<3;i++)
{
if(i==2)
{
/* Wait until the second last data byte is received into the shift register */
while(!I2C_GetBitState(I2C1,I2C_FLAG_BTC));
/* Send a NACK for the last data byte */
I2C_Acknowledge_Enable(I2C1, DISABLE);
}
/* Wait until the RBNE bit is set */
while(!I2C_StateDetect(I2C1, I2C_PROGRAMMINGMODE_MASTER_BYTE_RECEIVED));
/* Read a data from I2C_DTR */
I2C1_Buf_Read[i] = I2C_ReceiveData(I2C1);
}
I2C_StopOnBus_Enable(I2C1,ENABLE);
while(I2C1->CTLR1&0x0200);
I2C_Acknowledge_Enable(I2C1, ENABLE);
/* Infinite loop */
while(1);
} |