研究生
最后登录1970-1-1
在线时间 小时
注册时间2021-12-18
|
//从eeprom中读取数据
int I2C_EE_ReadData(uint8_t addr, uint8_t *Data,uint8_t numRead)
{
I2CTimeout = I2CT_FLAG_TIMEOUT;
I2C_GenerateSTART(I2C1, ENABLE);//起始信号
while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT )))
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(4);
}
//ev5被检测到,发送设备地址
I2C_Send7bitAddress(I2C1,0xA0, I2C_Direction_Transmitter);
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(5);
}
I2C_Cmd(I2C1,ENABLE);
//ev6被检测到,发送要操作的寄存器单元地址
I2C_SendData(I2C1, addr);
I2CTimeout = I2CT_FLAG_TIMEOUT;
//检测ev8
while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING)))
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(6);
}
//产生第二次起始信号
I2C_GenerateSTART(I2C1, ENABLE);//起始信号
while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT )));
//ev5被检测到,发送接收设备地址
I2C_Send7bitAddress(I2C1,0xA0, I2C_Direction_Receiver);
while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)));
//ev6被检测到,表示有从机应答,向主机发送数据
while(numRead--){
while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)));
//ev6被检测到,表示有从机应答,向主机发送数据
while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)));
//ev7被检测到,即数据寄存器有新的的数据
*Data = I2C_ReceiveData(I2C1);
if (numRead == 1)
{
I2C_AcknowledgeConfig(I2C1,DISABLE);
while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)));
//ev6被检测到
*Data = I2C_ReceiveData(I2C1);
}
}
I2C_GenerateSTOP(I2C1, ENABLE);
I2C_AcknowledgeConfig(I2C1,ENABLE);//方便下次通信
return 1;
}
|
|