I'm trying to erase an 24LC256, but it's taking a really long time... like more than 10 seconds per byte.
Here's my (abbreviated) code, which is inside a loop:
Using some serial debugging, I figured out that the the long delay comes from endTransmission(). My code seems fine, and my connection's alright... Where could this delay be coming from?
Update: I have found that this code works in my main program, but when copied and pasted over here, I takes ~1 minute per loop (I put a serial.println() at the end of the loop). Why would copy&pasted code work as fast as it should in the main program, but not here?
baum:
Using some serial debugging, I figured out that the the long delay comes from endTransmission().
endTransmission is the only thing that does any transmitting. The other stuff just puts things into a buffer. So if there is a delay, that's where it would be. Perhaps paste more code? Like the whole thing.
When doing a write of less than 64 bytes the data in the rest of the page is refreshed along with the data bytes being written. This will force the entire page to endure a write cycle, for this reason endurance is specified per page.
You may want to redesign so that you erase 64 bytes at a time. You are effectively making the chip do 64 lots of writes when it need only do one. In other words, you are reducing the chip life by a factor of 64 unnecessarily.
So I (tried) to incorporate in a page write, and now it won't erase. Odd thing is that the writeArray function is copied from another program, where it works.
/* Science Research Timer Erase
* Erases both the internal and
* external 24LC256 EEPROMs.
*/
#include <Wire.h>
#include <EEPROM.h>
#define EEPROM_ADDRESS 0x50
#define EXTERNAL_EEPROM_SIZE 32768L //size of 24LC
#define L_PIN 3
void setup() {
unsigned long memPointer = 0;
Serial.begin(19200);
Wire.begin();
pinMode(L_PIN, OUTPUT);
/* First clear the internal. */
for (int i = 0; i <= E2END; i++) {
EEPROM.write(i, 0);
}
blink(L_PIN);
/* And now the external EEPROM. */
char eraseArray[BUFFER_LENGTH]; //32 bytes for standard Wire library
for(int i = 0; i < BUFFER_LENGTH; i++) {
eraseArray[i] = 0xFF;
}
//Now eraseArray[] is filled with 0xFF.
while(memPointer < EXTERNAL_EEPROM_SIZE) {
writeArray(eraseArray, BUFFER_LENGTH, memPointer);
memPointer += BUFFER_LENGTH; //increase the address.
Serial.println(memPointer);
}
digitalWrite(L_PIN, HIGH);
} //end setup
void loop() {
}
void blink(byte pin) {
digitalWrite(pin, HIGH);
delay(200);
digitalWrite(pin, LOW);
return;
}
/* writes an Array to the 24LC56.*/
void writeArray(char data[], int length, unsigned int address) {
if((length + 2) > BUFFER_LENGTH) { //too big for Wire buffer!
return;
}
unsigned int i;
Wire.beginTransmission(EEPROM_ADDRESS);
Wire.write(highByte(address));
Wire.write(lowByte(address));
/* Now do a page write */
for(i = 0; i < length; i++) {
Wire.write(data[i]);
}
Wire.endTransmission(); //send the data.
return;
}