Pages: [1]   Go Down
Author Topic: 24LC256 overwrite and read all sketches  (Read 2741 times)
0 Members and 1 Guest are viewing this topic.
Melbourne, Fla, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 22
MAKE + Arduino = ROCK!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley **

New Sketch:
Code:
// 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.. smiley

**EDIT** I had forgotten to put code in the setup loop to put the Internal Address Pointer back to '0'.... :smiley

Code:

// 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!!
« Last Edit: July 26, 2010, 09:36:37 am by Volkemon » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17261
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your posting. I did use a 24LC256 chip in my last project to save script patterns for a LED cube. However I only performed single byte reads and writes. I think it would be cool to build a shield for eight of these puppies mounted on it. Been looking for a bargain price of a bunch of them on E-bay, but so far haven't found a listing I like.

Lefty
Logged

Melbourne, Fla, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 22
MAKE + Arduino = ROCK!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found the 24LC512 for $1.40 each from Newark, same footprint and pinout, but twice the memory for your shield.

http://www.newark.com/jsp/search/productdetail.jsp?SKU=62K0581&CMP=AFC-GB100000001  

I am sure there are others, I just happened to start here.

Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17261
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow that's a great price and Newark doesn't charge me shipping! I just ordered eight of them.

Thanks for the info

Lefty
Logged

Melbourne, Fla, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 22
MAKE + Arduino = ROCK!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hm...no shipping?!? I may have to look into that. I just googled it.

Well, the 'page wrap' during writes had been the major problem in reliable data logging for me using the 24LC256.

Using the two sketches above, and writing values such as F1,F2,F3 and observing the patterns shown by reading it, have proved valueable in writing information properly to the chip.

This may free me from using a SD card that is WAY too big for my purpose. SWEET!

Now... to transmit it via XBee to a roving monitor... smiley  
Logged

SF Bay Area
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Bit wrangling since '68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Volkemon...

I have implemented your overwrite sketch.  It was a good mechanism for learning how to work with the 24LC256.  You had a couple of problems:

1) Can't get "chipAdress" to compile using "byte", need "char"...
    Mine works fine with "byte", I don't know why... (uint8_t would
    probably be better.)

2) Your off-by-one problem:
    a) Change "int startAddress = 0" to "uint16_t startAddress = 0"
        (Signed integer changed to unsigned.)

    b) Change (3) tests to "if (startAddress > 0x7FFF) {"
        (address 0x7FFF didn't get written until next pass using -1,
         I think. This is probably the root of the problem.)

    c) Change (3) reset stmts to "startAddress = 0;"

    Anyhow, it works now...

Hope you sleep better tonight!

73,
  Gary (Garitron)
Logged

Pages: [1]   Go Up
Jump to: