GolamMostafa:
@chucktodd
1. As a learner, I simply wanted to translate label-by-label the tasks, contained in the Flow Chart of Fig-1 of the OP, in terms of register level instructions. While testing the codes (shown in OP) in a real situation of the following setup (Fig-1), I discovered that the program segment does not work with R-W/ bit set to LL (Write Mode) as prescribed in the Flow Chart of OP; but, it works with R-W/ bit set to LH (Read Mode). I decided to post it in the Forum to hear others' opinions to resolve this apparent contradiction!
2. For one byte write operation, we could comfortably insert delay(5) to complete the 'write cycle'. When we are going to make a page (128 byte) write operation, should we insert delay(5*128) or let the system take as much time as required to complete the 'write cycle'? This is the situation where ACK Polling is a desirable option to detect the end of 'write cycle'.
3. Interestingly, the following Wire Library based instructions (agrees with Flow Chart, Fig-1 of OP) work to sense the end of 'write cycle'.
do
{
Wire.beginTransmission(deviceAddress); //START command and deviceAddress queued
Wire.write(0x00); //dummy data and direction mode (R-W/=0) bit queued
byte stscode = Wire.endTransmission(); //Transmission of queued data and STOP command
}
while (stscode != 0x00); //Transmission success status code as per Wire Library
**4.**
Wire.endTransmission() method transmits queued Control Byte (deviceAddress + W/) and then asserts STOP command. In the quoted instructions, Wire.beginTransmission(adr) contains 7-bit (deviceAddress) in the argument; from where the missing bit (W/) is coming to have a place at the end of deviceAddress for the formation of an 8-bit control byte?
We have tested your program codes, and they have worked well (program file is attached as a reference). Your program segment contains a 'timeout' feature based on millis(), which has made it very attractive. Thanks a lot for the contribution.
#4, the Wire.h library takes the Address you provide, Left shifts it one bit, appends the R/W bit. Then hands that byte to the TwoWire hardware. The TwoWire hardware create a START signal on the Bus, then clocks out the 'control' byte (leftShifted address + r/w).
#3, your example would send 2 bytes the first one a 'control' byte (leftShifted address +'w' bit) and then a second byte of 8 zero bits. The 'dummy' byte is an error. If the Device was ready, it would accept that byte of zero.
#2, no, Most EEPROM devices write a 'page' of data at a time, the 24C512 has a 256 byte page size. When you send a write command:
Wire.beginTransmission(0x51);
Wire.write((uint8_t)highByte(address));
Wire.write((uint8_t)lowByte(address));
Wire.write((uint8_t)1);
Wire.write((uint8_t)2);
Wire.write((uint8_t)4);
Wire.write((uint8_t)5);
Wire.write((uint8_t)61);
Wire.write((uint8_t)1;
Wire.write((uint8_t)61);
Wire.write((uint8_t)1);
Wire.write((uint8_t)61);
Wire.write((uint8_t)1);
Wire.write((uint8_t)61);
Wire.write((uint8_t)1);
Wire.endTransmission();
The chip sees the write command, takes the first two bytes from the I2C bus, then uses that address to copy the current content of the addressed page into a temporary write buffer. Then it overwrites matching bytes in that temporary write buffer with the data it receives from the I2C bus. after the transmission is completed, it starts the program cycle:
First it erases the 'page' all 256 bytes in it's eeprom array,
Then it programs each byte to match the content of the temporary write buffer.
This sequence can take a maximum of 5ms. But is usually shorter.
Any single Write transmission will take at most 5ms.
Chuck.