problems with I2c

Hi,

I'm having some problems using i2c to write to an eeprom, a 24lc515. Sometimes it'll write to it without any problems, and sometimes it won't write at all. The code I am using is this:

void EE_wr_byte(int EE_address, char data) {

unsigned char transfer; unsigned int temp; unsigned char control = 0xA0; P4OUT |= SDA + SCL; //Stop P4DIR |= SDA + SCL; //Port as output P4OUT &= ~SDA; //Start EE_ser_out(control); //Control-byte temp = (EE_address >> 8); //High byte to send transfer = temp; EE_ser_out(transfer); transfer = EE_address; EE_ser_out(transfer); EE_ser_out(data);

P4OUT |= SCL; //Stop P4OUT |= SDA;

}

void EE_ser_out(char data) { unsigned int a;

P4OUT &= ~SCL; //SCL = 0 for(a=0;a

Reply to
ddshore
Loading thread data ...

It is probably a delay issue. Writing 1 bytes takes like 10 msec because it has to do some erasing internally. Writing multiple bytes in the same page also takes 10 msec. Read the part about aknowledge polling in the datasheet. If you want to verify this is your problem, add like 10 or 20 msec of delay after writing 1 byte.

Patrick

Reply to
Patrick van Gelder

It seems that EE_ser_out leaves the SDA line high or low depending on the last bit sent i.e. LSB. That's why you can only make a valid STOP when the previous byte's LSB was a 0.

Reply to
Viktor

Thanks a lot, that was it!

Reply to
ddshore

Glad to help. I'd just like to add that that when I see bit-banging code (like yours is) written in C, not assembler, I get an urge to bite through my mouse cord. Try doing it in asm, then link it in as a C function. It'll be easier to write, read and debug.

Viktor

Reply to
Viktor

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.