cancel
Showing results for 
Search instead for 
Did you mean: 

I2C read from memory problem

larsahl
Associate II
Posted on July 09, 2008 at 13:51

I2C read from memory problem

4 REPLIES 4
larsahl
Associate II
Posted on May 17, 2011 at 12:25

Hello I dont get my code to work for reading from the M24 series memory. I got a 64kb version.

This is my write code and I think its working but I dont know, becuse I cant read from the memory to verify that.

void I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr)

{

/* Send STRAT condition */

I2C_GenerateSTART(I2C1, ENABLE);

/* Test on EV5 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

/* Send EEPROM address for write */

I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);

/* Test on EV6 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

// send adress 2 times manual says so

/* Send the EEPROM's internal address to write to */

I2C_SendData(I2C1, WriteAddr);

/* Test on EV8 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/* Send the EEPROM's internal address to write to */

I2C_SendData(I2C1, WriteAddr);

/* Test on EV8 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/* Send the byte to be written */

I2C_SendData(I2C1, *pBuffer);

/* Test on EV8 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/* Send STOP condition */

I2C_GenerateSTOP(I2C1, ENABLE);

}

For reading from the memory I dont really have a clue, but this is my code

and it doesnt work. Trying to read 1 byte, I want to read more bytes later but im trying just to read one byte right now.

/* ByteRead code */

void I2C_EE_ByteRead(u8* pBuffer, u8 ReadAddr)

{

// Check busy flag (dont know if I need that but just in case maybe)

I2C_CheckEvent(I2C1, I2C_FLAG_BUSY);

// Start

I2C_GenerateSTART(I2C1, ENABLE);

// Test EV5

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

// Send device adress should it be A0 here or A1? I have no cables to the

chip enable E0,E1,E2 ports.

I2C_Send7bitAddress(I2C1, 0xA0, I2C_Direction_Transmitter);

// Test EV6

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

// Clear EV6 by setting again the PE bit

I2C_Cmd(I2C1, ENABLE);

// Send address for reading

I2C_SendData(I2C1, 0x1);

// Test EV8

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

// Send address for reading

I2C_SendData(I2C1, 0x1);

// Test EV8

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

// Start

I2C_GenerateSTART(I2C1, ENABLE);

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

I2C_Send7bitAddress(I2C1, 0xA1, I2C_Direction_Receiver);

// Inactive ACK

I2C_AcknowledgeConfig(I2C1, DISABLE);

// I doesnt go in there, what should I do here?

if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))

{

*pBuffer = I2C_ReceiveData(I2C1);

}

// Stop

I2C_GenerateSTOP(I2C1, ENABLE);

I2C_AcknowledgeConfig(I2C1, DISABLE);

}

Please help me im getting gray hair on this one :-[

larsahl
Associate II
Posted on May 17, 2011 at 12:25

hello ppl!

Now it works fine, I found some errors in the code ;)

morphiq
Associate II
Posted on May 17, 2011 at 12:25

Can u post corrected code?

alexcarrillo
Associate II
Posted on May 17, 2011 at 12:25

Quote:

On 26-02-2008 at 16:57, larsahl wrote:

// send adress 2 times manual says so

/* Send the EEPROM's internal address to write to */

I2C_SendData(I2C1, WriteAddr);

/* Test on EV8 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/* Send the EEPROM's internal address to write to */

I2C_SendData(I2C1, WriteAddr);

/* Test on EV8 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

Where in the manual it said that you have to sennd the address twice???