Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

i2c eeprom

Status
Not open for further replies.

pawnda

New Member
Hi there. I am trying to write data to an eeprom (24c32a) through i2c. I am using the C18 library. I went through the steps in the datasheet and I can't figure out what im doing wrong.

datasheet: https://www.electro-tech-online.com/custompdfs/2009/09/21163E.pdf

what im trying to do is write the value 0xAA to the address 0x0000 and then read it back on the LCD.

Code:
void main(void)
{
                OSCCON = 0b01110010;
	        TRISCbits.TRISC3 = 1;
	        TRISCbits.TRISC4 = 1;
		OpenI2C(MASTER, SLEW_OFF);
		StartI2C();
		WriteI2C(0xA0);

		WriteI2C(0x00);
		WriteI2C(0x00);
		WriteI2C(0xAA);
		StopI2C();
		
		StartI2C();
		WriteI2C(0xA1);
		WriteI2C(0x00);
		WriteI2C(0x00);
		write = ReadI2C();
		NotAckI2C();
		StopI2C();
		prnt(write);
	while (1);
}

thanks
 
Last edited:
This is how I read a byte from an EEPROM,
Code:
    StartI2C();
    WriteI2C(0xa0);
    WriteI2C(0x00);
    WriteI2C(0x00);
    [COLOR="Red"]RestartI2C();[/COLOR]
    WriteI2C(0xa1);
    byte=ReadI2C();
    NotAckI2C();
    StopI2C();
Note that I write the address and then do a ReStart before the read.

Mike.
 
Still doesn't work. None of the WriteI2C functions are returning a 1 which means they are successful. In the datasheet on page 7 at the bottom, doesn't figure 6-1 imply that I could read the current address with:

Code:
	StartI2C();
    WriteI2C(0xA1);
    write = ReadI2C();
    NotAckI2C();
    StopI2C();
 
I don't know why it's not working but when you write to a location it auto increments to the next location. So, your above code was reading location 1.

Have you grounded all the address pins?

Mike.
 
Have you tried disabling the acknowledges? I sometimes find I2C works better if I dont wait for an acknowledge.....

OK, this is not what you should do in your final production code, but it might get your application running when otherwise it will just sit and sulk at you...

Simon
 
Got it. I needed a delay after the first stop to allow the eeprom time to write.

Code:
	OpenI2C(MASTER, SLEW_OFF);
		StartI2C();
		WriteI2C(0xA0)
		WriteI2C(0x00);
		WriteI2C(0x01);
		WriteI2C('d');
		StopI2C();
		delay5ms();
   		StartI2C();
    	        WriteI2C(0xa0);
    	        WriteI2C(0x00);
    	        WriteI2C(0x01);
    	        RestartI2C();
		WriteI2C(0xa1);
   	        write=ReadI2C();
   	        NotAckI2C();
    	        StopI2C();
		prnt(write);
		while (1);
 
Last edited:
The way I get around this problem is by waiting for an ack,
Code:
//Sends the start and device address.
//If the device is busy then it resends until accepted.
void SendID(char DeviceID){
    SendStart();
    if(SendByte(DeviceID)==1)
        return;
    do{
        SendRestart();
    }while(SendByte(DeviceID)==0);
}

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top