Hi all,
I'm an author of the AN3291.
Thank you for your important feedback. I apologize for that mistake - you are right.
I have been tested the WRITE and READ routines from my AN3291. One mistake (mentioned previously) was in the write routine and I found another one in the read routine too. Now both routines are correct and they are in follow:
void I2C_write_byte (word addr,byte data)
{
IICS_IICIF = 1;
EE_Addr = addr; // address input;
temp = (EE_Addr_H & 0x07) << 1;
IICC_TX = 1; // Set transmit mode;
//-------start of transmit first byte to IIC bus-----
IICC_MST = 1; // Start transfer - Master bit = 1;
IICD = sl_addr | temp; // send "Write" + high address;
while (!(IICS_IICIF));
IICS_IICIF = 1;
while (IICS_RXAK); // wait for ACK from slave;
//-----Slave ACK occured------------
IICD = EE_Addr_L; // send low address byte;
while (!(IICS_IICIF)); // wait till data transfered;
IICS_IICIF = 1;
while (IICS_RXAK); // wait for ACK from slave;
//-----Slave ACK occured------------
IICD = data; // write data byte into EEPROM;
while (!(IICS_IICIF)); // wait till data transfered;
IICS_IICIF = 1; // - ADDED;
while (IICS_RXAK); // wait for ACK from slave;
IICC_MST = 0; // STOP bit;
}
byte I2C_read_byte(word addr)
{
IICS_IICIF = 1;
EE_Addr = addr; // address input;
temp = (EE_Addr_H & 0x07) << 1;
IICC_TX = 1; // Set transmit mode;
//-------start of transmit first byte to IIC bus-----
IICC_MST = 1; // Start transfer - Master bit = 1;
IICD = sl_addr | temp; // send "Write" + high address;
while (!(IICS_IICIF));
IICS_IICIF = 1;
while (IICS_RXAK); // wait for ACK from slave;
//-----Slave ACK occured------------
IICD = EE_Addr_L; // send low address byte;
while (!(IICS_IICIF));
IICS_IICIF = 1;
while (IICS_RXAK); // wait for ACK from slave;
//-----Slave ACK occured------------
IICC_RSTA = 1; // Generate repeat START condition;
IICD = sl_addr | temp | 1; // Send combined address & "Read" command;
while (!(IICS_IICIF));
IICS_IICIF = 1;
while (IICS_RXAK); // wait for ACK from slave;
//-----Slave ACK occured------------
IICC_TX = 0; // Set receive mode;
IICC_TXAK = 1; // Last byte will be read; - ADDED;
temp = IICD; // read IIC data
while (!(IICS_IICIF));
IICS_IICIF = 1;
IICC_MST = 0; // STOP bit;
rd_data = IICD;
return rd_data;
}
I made tests on my 9S08QG8 MCU with the 24LC16B serial EEPROM. The code in the AN3291 will be corrected as soon as possible.
I will make tests with the mentioned EEPROMs 24FC512 and QG8 MCU after I receive this type of EEPROM. I guess it could be finished next week - then I will place the message to this forum.
Sorry once again for this mistake.
Stano.