I2C写寄存器
//发送开始信号
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
//发送从设备地址-写
I2C_Send7bitAddress(I2C1,DevAddress,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//发送寄存器地址
I2C_SendData(I2C1,D_MenAddress);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING));
//重新发送开始信号
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
//发送从设备地址-读
I2C_Send7bitAddress(I2C1,DevAddress,I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
//循环读取数据
while(bufferSize)
{
bufferSize--;
if(bufferSize == 0)
{
//关闭自动应答
I2C_AcknowledgeConfig(I2C1, DISABLE);
}
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
*pBuffer = I2C_ReceiveData(I2C1);
pBuffer++;
}
//发送停止信号
I2C_GenerateSTOP(I2C1,ENABLE);
//重新打开自动应答
I2C_AcknowledgeConfig(I2C1, ENABLE);
|