Hi People,
I copied a piece of code of which I cannot get my head around.
I'm planning to store integers of data to be examined later.
Therefore I connected an AT24C256 EEPROM data chip on the I2C line which works fine.
At least... The EEprom is detected in the I2C line, due to an I2C test sketch.
Programming the chip is the next step.
I saw an informative clip on Youtube, but I'm missing out on certain explanations.
// Function to write to EEPROOM
void writeEEPROM(int address, byte val, int i2c_address)
If I understand this part correctly, this is a function with the name writeEEPROM.
Whenever this name is called upon, this function will start.
I do not get why, in this matter, the function starts with a void, while in the next part,
a similar part starts with byte (somewhere below).
{
// Begin transmission to I2C EEPROM
Wire.beginTransmission(i2c_address);
// Send memory address as two 8-bit bytes
Wire.write((int)(address >> 8)); // MSB
Wire.write((int)(address & 0xFF)); // LSB
For instance, this part above...
I guess the "Wire.write (...)" are codewords from the "wire.h" Library, correct?
Secondly, I do not exactly get what is going on above.
What I guess is: An Integer is 16 bits. Writing to a memory address can only be done in bytes,
so the integer is split. It shifts 8 positions, but I do not know how...
And it seems the second part is masked with which HEX FF, which is binary 1111 1111.
// Send data to be stored
Wire.write(val);
// End the transmission
Wire.endTransmission();
// Add 5ms delay for EEPROM
delay(5);
}
Here below I do not get why it is a variable (byte) "readEEPROM"
First of all: I guess because the program needs that byte of data to work with.
I just did not know a byte could be a function.
Secondly: Why or how does this fit? There was an integer split in half to get it fixed in 2 bytes.
But here it seems to be stored in 1 byte...
// Function to read from EEPROM
byte readEEPROM(int address, int i2c_address)
{
// Define byte for received data
byte rcvData = 0xFF;
// Begin transmission to I2C EEPROM
Wire.beginTransmission(i2c_address);
Here I'm lost... (above) "Begin transmission to I2C EEPROM"
The EEprom needed to send data, when I need it, right?
// Send memory address as two 8-bit bytes
Wire.write((int)(address >> 8)); // MSB
Wire.write((int)(address & 0xFF)); // LSB
Or is it to command (as a master) the EEprom, beeing a slave, to send stored data?
How does the EEprom determine which data to send??
// End the transmission
Wire.endTransmission();
// Request one byte of data at current memory address
Wire.requestFrom(i2c_address, 1);
// Read the data and assign to variable
rcvData = Wire.read();
And how come that the transmission is ended, but there is still data requested 1 line below.
// Return the data as function output
return rcvData;
}
And I again, assume the requested data is loaded in the rvcData, for further use?
Hard to crack this one in my mind...