Couple of sketches I made as I learned about the 24LC256.
This one overwrites all the data with FF, or whatever value you choose to substitite. it does it in (2) 30 packet strings, and one (4) packet string. This puts the Internal Address Pointer at the beginning of a Page each time the cycle is complete.
EDIT I discovered at the end of the process, after the "OVERWRITE COMPLETE" message, the 'startAddress' value was 1 not 0. I could find no reason for this in the code. I put a workaround in , I set the value of 'startAddress' to (-1) and things work fine. I am open to reasons why, however **
New Sketch:
// Some code to overwrite FF (or other chosen value) in all memory locations in a 24LC256.
// mashup from Volkemon 7/2010
#include <Wire.h>
char chipAdress=0x50; // 24LC256 address on the bus, pins a0,a1 and a2 tied to ground. Will not compile as a byte value, only char??
int startAddress = 0; //overall counter for address location. 16 bit value, will be split into MSB and LSB
byte startaddrMSB = 0x00; // Most Signifigant Byte, 1/2 of the address in chip memory. Max val 0x7F
byte startaddrLSB = 0x00; // Least Signifigant Byte, other 1/2 of the address in chip memory. Max val 0xFF
void setup()
{
Wire.begin(); // enable the i2c bus
Serial.begin(19200); // Make sure you set the serial monitor to match! :)
}
void loop()
{
Serial.print("Writing starting at bite/slot "); Serial.print(startAddress, DEC); // info sent to serial monitor for debugging.
Wire.beginTransmission(chipAdress); // chip address on the TWI bus, not chip memory address.
//Set up the chip Internal Address Pointer to the desired location
startaddrMSB = highByte(startAddress);
startaddrLSB = lowByte(startAddress);
Wire.send(startaddrMSB); //First 7 bits of the address
Wire.send(startaddrLSB); //Second 8 bits of the address
for (byte ct=0x0; ct<30; ct++) // loop where we write info to Wire library buffer.30 byte max. first two are used for address.
{
Wire.send(0xF1);
startAddress ++;
if (startAddress >= 0x7FFF){
Serial.print("OVERWRITE COMPLETE");
startAddress = (-1); // if set to zero, address is off by 1 after 1st overwrite. WHY???
delay(5000);
}
}
Wire.endTransmission(); // commands write of data from Wire library buffer to chip , 'closes' chip
Serial.print(" Done1.");Serial.print(startAddress);
Wire.beginTransmission(chipAdress); // chip address on the TWI bus, not chip memory address.
//Set up the chip Internal Address Pointer to the desired location
startaddrMSB = highByte(startAddress);
startaddrLSB = lowByte(startAddress);
Wire.send(startaddrMSB); //First 7 bits of the address
Wire.send(startaddrLSB); //Second 8 bits of the address
for (char ct=0x0; ct<30; ct++) // loop where we write info to Wire library buffer.30 byte max.first two used by address.
{
Wire.send(0xF2); // Sends the value 0xFF to Wire library buffer.
startAddress++;
if (startAddress >= 0x7FFF){
Serial.print("OVERWRITE COMPLETE");
startAddress = (-1); // if set to zero, address is off by 1 after 1st overwrite. WHY???
delay(5000);
}
}
Wire.endTransmission(); // commands write of data from Wire library buffer to chip , 'closes' chip
Serial.print(" Done2.");Serial.print(startAddress);
// The first two wire.send loops sent 60 packets, leaving 4 open in the page. This cleans them up, and gets the Internal Address Pointer to an page beginning.
Wire.beginTransmission(chipAdress); // chip address on the TWI bus, not chip memory address.
//Set up the chip Internal Address Pointer to the desired location
startaddrMSB = highByte(startAddress);
startaddrLSB = lowByte(startAddress);
Wire.send(startaddrMSB); //First 7 bits of the address
Wire.send(startaddrLSB); //Second 8 bits of the address
for (char ct=0x0; ct<4; ct++) // loop where we write info to Wire library buffer.4 to clean up the page.
{
Wire.send(0xF3); // Sends the value 0xFF to Wire library buffer.
startAddress++;
if (startAddress >= 0x7FFF){
Serial.print("OVERWRITE COMPLETE");
startAddress = (-1); // if set to zero, address is off by 1 after 1st overwrite. WHY???
delay(5000);
}
}
Wire.endTransmission(); // commands write of data from Wire library buffer to chip , 'closes' chip
Serial.print(" Done3."); Serial.println(startAddress);
}
This one reads all the data off the card. The data can be read only in 32 byte strings due to limitations in the wire library. starts at the beginning, and goes to the end, actually beyond..
EDIT I had forgotten to put code in the setup loop to put the Internal Address Pointer back to '0'.... :
// sketch to read entire chip 32 bytes at a time, display on serial monitor with progress indicated.
// July2010- Volkemon tweaked and commented.
// _ _
// A0-|oU |-Vcc
// A1-| |-WP
// A2-| |-SCL
// Vss-| |-SDA
// ---
//
// SDA goes to Arduino 4
// SCL goes to Arduino 5
// WP goes to ground for now. Can be put to Vcc if write protection is needed.
// Vcc goes to arduino Vcc
// Vss goes to arduino ground
// A2, A1, A0 go to ground for now.
//
// They can be also put to either ground or Vcc to control up to 8 memory chips (2^3), giving you 2MBit of memory (8*2^15).
// A2 A1 A0 Binary Address Hex Address
// 0 0 0 0b1010000 0×50
// 0 0 1 0b1010001 0×51
// 0 1 0 0b1010010 0×52
// 0 1 1 0b1010011 0×53
// 1 0 0 0b1010100 0×54
// 1 0 1 0b1010101 0×55
// 1 1 0 0b1010110 0×56
// 1 1 1 0b1010111 0×57
#include <Wire.h>
char chipAdress=0x50; // Binary 10100000 . Three bits after 1010 (currently 000) are used to specify to what A2, A1, A0 are connected to, ground or vcc. See comment above.
// Last bit specifies the opertation - 0 for write, 1 for read. This is controlled for you by the Wire library. :)
int block = 0;
void setup()
{
Wire.begin(); // enable the i2c bus
Serial.begin(19200); // Did you set your serial monitor to 19200?
Wire.beginTransmission(chipAdress); // chip address on the TWI bus, not chip memory address.
//Set up the chip Internal Address Pointer to the beginning of memory
Wire.send(0x00);
Wire.send(0x00);
Wire.endTransmission(); // sends Wire buffer to chip, sets Internal Address Pointer to '0'
}
void loop()
{
Serial.println("");
Serial.print(block);
Wire.requestFrom(chipAdress, 32); // requests 32 Bytes of data in a packet, maximum string size.
while(Wire.available()){ // 'while loop' start, Checks to see if data is waiting
Serial.print(" "); // space to format packets on serial monitor
Serial.print(Wire.receive(), HEX); // print the values received on the serial monitor
} // end bracket for 'while loop'
Wire.endTransmission(); // when 'while loop' is finished (no more data available) 'closes' chip
//Serial.println("");
block += 1;
if (block > 1024){
Serial.println("End Of Memory");
delay(5000);
}
delay(100);
}
Hope it helps someone... I learned a lot about these little chips today!!