Hello people!
I'm trying to log sensor data continously to EEPROM (24AA1025)!
The datasheet says that the page buffer of the EEPROM is 128byte and only can be written continously within a physical page size.
So i implemented following methods for continous write to the EEPROM:
unsigned long startWriteAddressEEPROM;
unsigned long writtenBytesEEPROM;
unsigned long currentPageStartAddress;
unsigned long nextPageStartAddress;
unsigned long pageSize = 128;
void startContinousWriteToEEPROM(unsigned long address) {
Wire.beginTransmission(0b1010000 | (byte)((address>>14)&0b0000100));
Wire.send((byte)((address >> 8) & 0xFF)); // MSB
Wire.send((byte)(address & 0xFF)); // LSB
startWriteAddressEEPROM = address;
writtenBytesEEPROM = 0;
currentPageStartAddress = (address/pageSize)*pageSize;
nextPageStartAddress = currentPageStartAddress + pageSize;
}
void writeContinousByteToEEPROM(byte data) {
Wire.send(data);
writtenBytesEEPROM++;
unsigned long absoluteAddress = startWriteAddressEEPROM + writtenBytesEEPROM;
if(absoluteAddress >= nextPageStartAddress) {
endContinousWriteToEEPROM();
startContinousWriteToEEPROM(absoluteAddress);
}
}
void writeContinousDoubleByteToEEPROM(unsigned int data) {
writeContinousByteToEEPROM((byte)(data>>8));
writeContinousByteToEEPROM((byte)data);
}
void writeContinousTripleByteToEEPROM(unsigned long data) {
writeContinousByteToEEPROM((byte)(data>>16));
writeContinousByteToEEPROM((byte)(data>>8));
writeContinousByteToEEPROM((byte)data);
}
void endContinousWriteToEEPROM() {
Wire.endTransmission();
delay(5); // also tried delay(500);
}
The problem with the code is, that only the first 30 bytes of the page buffer are written to the EEPROMs memory.
If i set the pageSize to 30, the continous writing also do not seems to work correctly - some bytes in the EEPROM will not get written.
The only way to write continous without failures was setting the pagesize<16 ???
Here is the code of my testing sketch for writing the numbers 1,2,3,4,...127,128 to the first 10 pages, and readig them back to the serial for debugging:
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
Serial.println("START FORMATTING");
for(long i=0; i<10*128; i++) {
writeByteToEEPROM(i,0xFF);
}
Serial.println("FINISHED FORMATTING");
Serial.println("START WRITING");
startContinousWriteToEEPROM(0);
for(long i=0; i<10; i++) {
for(long j=0;j<128;j++) {
writeContinousByteToEEPROM(j);
}
}
endContinousWriteToEEPROM();
Serial.println("FINISHED WRITING");
Serial.println("START READING");
for(long i=0; i<10*128; i++) {
Serial.print("Read from ");
Serial.print(i);
Serial.print(" - value = ");
Serial.print(readByteFromEEPROM(i), DEC);
Serial.println("");
}
Serial.println("FINISHED READING");
}
void loop() {}
I'm working on this for about a week and can't find my fault.
Please can anybody help me finding the bug?
Thanks!