在线时间0 小时
UID2041843
ST金币0
蝴蝶豆0
注册时间2013-9-14
新手上路
- 最后登录
- 1970-1-1
|
a0a.1 0b0c
我使用硬體I2C去做24C32燒寫
正在測試I2C READ BYTE跟WRITE BYTE時發現了一些問題如下,
I2C READ如果加上回傳會出現讀出來的數據錯誤
WRITE程式如下
void I2C_EE_ByteWrite(uint8_t pBuffer,uint16_t WriteAddr)
{
uint8_t at24C02_to_16_addr;
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
#ifdef AT24C32_64
I2C_Send7bitAddress(I2C1,(uint8_t)EEPROM_ADDRESS,I2C_Direction_Transmitter);//EEPROM_ADDRESS
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,(uint8_t)((WriteAddr&0xFF00)>>8)); //MSB
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING)); // TXOK?
I2C_SendData(I2C1,(uint8_t)(WriteAddr&0x00FF)); //LSB
#else //AT24C02 to 16
at24C02_to_16_addr=(uint8_t)((WriteAddr&0x0E00)>>7);
I2C_Send7bitAddress(I2C1,EEPROM_ADDRESS|at24C02_to_16_addr,I2C_Direction_Transmitter);//EEPROM_ADDRESS &MSB
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,(uint8_t)(WriteAddr&0x00FF)); //LSB
#endif
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING)); // TXOK?
I2C_SendData(I2C1,pBuffer);
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); // TXOK?
I2C_GenerateSTOP(I2C1,ENABLE);
delay(EEPROM_DLY);
}
READ如下
uint8_t I2C_EE_ByteRead(uint16_t WriteAddr)
{
uint8_t at24C02_to_16_addr,read_data;
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
#ifdef AT24C32_64
I2C_Send7bitAddress(I2C1,(uint8_t)EEPROM_ADDRESS,I2C_Direction_Transmitter);//EEPROM_ADDRESS
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_ClearFlag(I2C1,I2C_FLAG_ADDR);
I2C_SendData(I2C1,(uint8_t)((WriteAddr&0xFF00)>>8)); //MSB
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING)); // TXOK?
I2C_SendData(I2C1,(uint8_t)(WriteAddr&0x00FF)); //LSB
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING)); // TXOK?
I2C_GenerateSTART(I2C1,ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,(uint8_t)EEPROM_ADDRESS,I2C_Direction_Receiver); //RECEIVER
#else
at24C02_to_16_addr=(uint8_t)((WriteAddr>>7)&0x0E);
at24C02_to_16_addr=at24C02_to_16_addr|EEPROM_ADDRESS;
I2C_Send7bitAddress(I2C1,at24C02_to_16_addr,I2C_Direction_Transmitter);//EEPROM_ADDRESS &MSB
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,(uint8_t)(WriteAddr)); //LSB
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING)); // TXOK?
I2C_GenerateSTART(I2C1,ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,(uint8_t)at24C02_to_16_addr,I2C_Direction_Receiver); //RECEIVER
#endif
while (!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); //RX FLAG
read_data=I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1,ENABLE); //NONE ACK
I2C_GenerateSTOP(I2C1,ENABLE);
//return read_data;
}
拿掉return read_data;收下的資料居然正確,加上RETURN會不正確
麻煩幫小弟看一下,謝謝 |
|