#include <Wire.h>
#define EEPROM_ADR 0x54
#define MAX_I2C_WRITE 16
byte tempStore[MAX_I2C_WRITE];
void setup()
{
//Start the I2C Library
Wire.begin();
Wire.setClock(400000);
//Start the serial port
Serial.begin(19200);
//Here is where we'll keep track of where in the memory we're writing
long currentSpot = 0;
long timerReset = 0;
byte counter = 0;
//Here we listen for bytes on the serial port and increment
//the counter as we store them in our tempStore variable
while (1)
{
while (Serial.available())
{
tempStore[counter++] = Serial.read(); //Read this byte into the array
if (counter == MAX_I2C_WRITE)
{
//Once we've collected a page worth, go ahead and do
//a page write operation
writeEEPROMPage(currentSpot);
counter = 0; //Reset
currentSpot += MAX_I2C_WRITE;
}
timerReset = millis();
}
if (millis() - timerReset > 2000)
{
Serial.println(currentSpot);
timerReset = millis();
}
}
}
void loop()
{
// Don't do anything here
}
/* This is the 3 step memory writing procedure that
we talked about. First we send the MSB of the address.
Then we send the LSB of the address. Then we send the
data that we want to store. */
void writeEEPROMPage(long eeAddress)
{
Wire.beginTransmission(EEPROM_ADR);
// were should be the MSB part
Wire.write((int)(eeAddress & 0xFF)); // LSB
//Write bytes to EEPROM
for (byte x = 0 ; x < MAX_I2C_WRITE ; x++)
Wire.write(tempStore[x]); //Write the data
Wire.endTransmission(); //Send stop condition
}
I'm new at coding and I really would like to learn more. Thanks, I really need help in this.
When I look at the Sparkfun code on the page you link it is not suitable for you EEPROM as it write the MSB/LSB address after the wire.begin but for your 24LC08 EEPROM you write the upper 2 bits of the MSB address in the wire.begin and then the LSB address directly after.
The library code here seems to handle the write address correctly (though I have not tested it)
After some hours I cracked it, I've seen another forum post (this one How to connect / use ATMEL 24C16 EEPROM chip? - Storage - Arduino Forum ) about connecting to a similar working chip (24lc16) and I saw to lines of code from "davekw7x" that handles the MSB part of the code to this family of chips.
So i've changed the void for writting in the eeprom memory adding a changeable address for the chip and that did the trick. Also I've seen with other code (I2CScanner) that the eeprom gave more than one address depending on the memory that it's got. As an example I had a 24lc04 lying around and it gave me 2 locations and the 24lc08 gave 4. That means that by each 256 byte it gives one location.
So I hope that this could help anyone that had the same problem that I got.
Riva thank you for your answer, before I posted in the forum I tried to work with that library. But because of me being a noob, or because of the ship I couldn't write or read when using it. I knew the code wasn't suitable, but the main parts where so I started with it. I didn't knew how to interact with the eeprom so I needed to start somewhere. The addressing part was easy, basically you can connect anyway you want the eeprom, you only need to be careful when using more than one. But the way the memory addressing works in this ancient chips I think you can't connect more than one (depending on what memory size you work with).
But right now the car stereo is working like a champ after being store for about 20 years. Yeah!