我用stm32f103 自带的库函数i2c读写at24c04,写数据没有问题,但是读数据不知道是哪个地方设置不对??只能读一次,读数据之后不能继续读和写了。请帮忙看下是读数据函数哪个地方有问题呢???,程序如下:
//i2c1写数据,在芯片的0x01写入值为4的数据
void I2C1H_Send_data()
{
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)==SET);
I2C_GenerateSTART(I2C1, ENABLE); //
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //
I2C_Send7bitAddress(I2C1,0xa0,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//
I2C_SendData(I2C1, 0x01); //
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING));//
I2C_SendData(I2C1, 4);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE); //
}
//i2c1读数据,在芯片的0x01读出数据
u8 I2C1H_Read_data()
{
u8 buf;
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)==SET);
I2C_GenerateSTART(I2C1, ENABLE);
while(SUCCESS != I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));//
I2C_Send7bitAddress(I2C1,0xa0,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, 0x01);
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,0xa0,I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_AcknowledgeConfig(I2C1,DISABLE);//¹Ø±ÕÏìÓ¦
while(! I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED));//EV7
buf = I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1,ENABLE);
I2C_AcknowledgeConfig(I2C1,ENABLE);
return buf;
}
|