Electronic – I2C delay needed and c18

c18eepromi2cpic

I am using I2C and I have earlier posted doubts about I2C here. I am using PIC controllers and using their compiler C18. I used their libraries to build a function to write data in EEPROM via I2C, and when I crosschecked the SCL lines they were fluctuating.

I have made up a function to write to EEPROM but I have missed out on delays as the datasheet says "minimum 5ms delay for each write operation in to I2C is required".

I trust it is causing the troubles. The problem is SCL does not stay at 100kHz and fluctuates between 54kHz and 100kHz but never beyond that. Is that because Control byte,address and data is sent along the function?

float ee_write_float(unsigned char ee_addr, float f)
{
     void i2c_init(); //initialize I2c
     unsigned char *p = (unsigned char *)&f;
     unsigned char  i;

   for (i = sizeof f; i != 0; --i)
   {
      EEByteWrite(EE_I2C_ADDR, ee_addr++, *p++);
   }

} 

How important is the dela for I2C write operations?

Could that be the cause of problem?

Should I put a delay after the EEByteWrite function to compensate?

Noted: the SCL becomes somewhat stable after the baud rate is reduced to 20kHz
then it fluctuates between 17kHz and 19kHz. I would be extremely happy if you found out any reasonable cause to fix this trouble..or any valuable suggestions will be welcomed whole heartily.

Best Answer

In I2C, the clock only toggles as necessary to transmit or receive data. There's no need for a continuous clock signal so variation in the observed frequency should be expected. The 100kHz specification is the maximum clock rate rather than the average or continuous clock rate.

Non-volatile memories like Flash and EEPROM need a relatively long period of time to write data into the memory, and 5ms sounds about right. You must not try to execute multiple write operations without allowing for this delay between them. Because of this, you cannot possibly execute more than 200 write operations per second and still allow 5ms per operation.

Once you have everything working correctly, you should expect to see a burst of 100kHz clock pulses every 5ms or so.