SRAM class using 23K256

I had a need in a project for a fast easy external memory.
Microchip’s 23K256 is ideal.

I looked at Spi Library and Spi RAM Library from the arduino playground.

What I really wanted was to have the ram accessible continually from my code.
I did not want to pass address and length each time I stored or retrieved data.

My class has four functions:

writestream: Setup the SRAM in sequential write mode starting from the passed address.
Data can then be written one byte at a time using RWdata(byte data).
Each byte is stored in the next location and it wraps around 32767.

readstream: Setup the SRAM in sequential read mode starting from the passed address.
Data can then be read one byte at a time using byte RWdata(0).The passed data is irrelavent.
Each byte is read from the next location and it wraps around 32767.

RWdata: Write or read the data from the SRAM.
If the writesteam is open the passed data will be written to the current address.
If the readstream is open the data from the current address will be returned.

closeRWstream: Use to close the open read or write stream.
Dont need when changing between read/write.
Close before using SPI somewhere else.

digital pin 13 SCK
digital pin 12 MISO
digital pin 11 MOSI
digital pin 10 SS

I want it for blocks of data, but it can still be used for individual bytes.
This is the example RWbyte you will find in the library:

#include <SRAM.h>

void setup()
{
Serial.begin(9600); // initialize serial communications
}//end of setup

void loop()
{
SRAM.writestream(22767); //open writestream address from 0 to 32767

SRAM.RWdata(75); //the byte to write to SRAM at current address

SRAM.readstream(22767); //open readstream address from 0 to 32767

int data = SRAM.RWdata(0xff); //read byte from SRAM at current address

Serial.println(data); //check data

delay(1000);
}//end of loop

This example to write and read all 32768 bytes is in the library:

#include <SRAM.h>

void setup()
{
Serial.begin(9600); // initialize serial communications
}//end of setup

void loop()
{
SRAM.writestream(0); // start address from 0

unsigned long stopwatch = millis(); //start stopwatch

for(unsigned int i = 0; i < 32768; i++)
SRAM.RWdata(0x55); //write to every SRAM address

Serial.print(millis() - stopwatch);
Serial.println(" ms to write full SRAM");

SRAM.readstream(0); // start address from 0

for(unsigned int i = 0; i < 32768; i++)
{
if(SRAM.RWdata(0xFF) != 0x55) //check every address in the SRAM
{
Serial.println("error in location ");
Serial.println(i);
break;
}//end of print error
if(i == 32767)
Serial.println(“no errors in the 32768 bytes”);
}//end of get byte

delay(1000);
}//end of loop

If it all works you should see:

116 ms to write full SRAM
no errors in the 32768 bytes

For the library and circuit download SRAMclass.zip from:

https://sourceforge.net/projects/mysudoku/files/

i tried it and it works really well.

do you know of a way to add more of this RAM, in a chain? i would like to have about 10 of them in one chain.. but i cant get it to work.. what i have tried so far is to use the hold pin to select the RAM, but this is not working..

Yes you can have more than one using SPI.

The hold is still held high on all of them - we do not use the hold control.

All the SI pins will be connected to digital 11. All the SO pins will be connected to digital 12. All the SCK pins will be connected to digital 13.

The CS (chip select) must have a seperate pin(or a decoded combination).

First just try to make it work with 2.

Leave CS1 connected to digital 10.

Then connect CS2 to digital 9.

Then you will have to modify the SRAMclass.

In the .h file you will see:

define selectSS PORTB &= ~0x04 //set the SS to 0 to select

define deselectSS PORTB |= 0x04 //set the SS to 1 to deselect

You will need to add the control for digital 9(0x02)

define selectSS2 PORTB &= ~0x02 //set the SS2 to 0 to select

define deselectSS2 PORTB |= 0x02 //set the SS2 to 1 to deselect

The .cpp file will also have to have to be changed.

There are many ways to do this but for a start you could copy it and rename to SRAM2.ccp. Change the SS to SS2 in this second file.

You can then write to this SRAM using SRAM2.Rdata ect The first SRAM would have to be deselected first.

Give that a try if you want.

I'll help some more if you need.

i tried using another pin as the CS pin (i tried pin 8). but i could not get it to work..

i put pin 8 as output in my arduino sketch.

and in the lib i changed this:

define selectSS PORTB &= ~0x01 //set the SS to 0 to select

define deselectSS PORTB |= 0x01 //set the SS to 1 to deselect

The CS pin makes part of the hardware SPI so you must use that pin.

I needed to have 64k bytes of SRAM.

I wired up 2 23K256 and wrote a new class SRAM2.

It has 6 functions:

writestream1: Setup SRAM 1 in sequential write mode starting from the passed address.
Bytes can then be written one byte at a time using RWdata(byte data).
Each byte is stored in the next location and it wraps around 32767.

writestream2: Setup SRAM 2 in sequential write mode starting from the passed address.
Bytes can then be written one byte at a time using RWdata(byte data).
Each byte is stored in the next location and it wraps around 32767.

readstream1: Setup SRAM 1 in sequential read mode starting from the passed address.
Bytes can then be read one byte at a time using byte RWdata(0).The passed data is irrelavent.
Each byte is read from the next location and it wraps around 32767.

readstream2: Setup SRAM 2 in sequential read mode starting from the passed address.
Bytes can then be read one byte at a time using byte RWdata(0).The passed data is irrelavent.
Each byte is read from the next location and it wraps around 32767.

RWdata: Write or read a byte at any time from the selected SRAM (1 or 2 may be selected).
If the writesteam is open the passed byte will be written to the current address.
If the readstream is open the byte from the current address will be returned.

closeRWstream: Use to close the open read or write stream for both SRAM 1 and 2.
Dont need when changing between read/write.
Close before using SPI somewhere else.

digital pin 13 SCK
digital pin 12 MISO
digital pin 11 MOSI
digital pin 10 SS for SRAM 1
digital pin 9 SS for SRAM 2

The example RWbyte2 - write and read one byte to each SRAM:

#include <SRAM2.h>

void setup()
{
Serial.begin(9600); // initialize serial communications
}//end of setup

void loop()
{
SRAM2.writestream1(22670); //open writestream to write a byte to SRAM 1
SRAM2.RWdata(75); //the byte to write to SRAM at current address
SRAM2.readstream1(22670); //open readstream to read a byte from SRAM 1
int data = SRAM2.RWdata(0xff); //read byte from SRAM at current address
Serial.println(data); //check data

SRAM2.writestream2(670); //open writestream to write a byte to SRAM 2
SRAM2.RWdata(205); //the byte to write to SRAM at current address
SRAM2.readstream2(670); //open readstream to read a byte from SRAM 2
data = SRAM2.RWdata(0xff); //read byte from SRAM at current address
Serial.println(data); //check data

delay(1000);
}//end of loop

The example RWSRAM2 - writes and reads all the bytes on both SRAMs:

#include <SRAM2.h>

void setup()
{
Serial.begin(9600); // initialize serial communications
}//end of setup

void loop()
{
SRAM2.writestream1(0); // write to SRAM 1
unsigned long stopwatch = millis(); //start stopwatch
for(unsigned int i = 0; i < 32768; i++)
SRAM2.RWdata(0x55); //write to every SRAM 1 address
Serial.print(millis() - stopwatch);
Serial.println(" ms to write full SRAM 1");
SRAM2.readstream1(0); // start address from 0
for(unsigned int i = 0; i < 32768; i++)
{
if(SRAM2.RWdata(0xFF) != 0x55) //check every address in the SRAM1
{
Serial.print("error in SRAM 1 at location “);
Serial.println(i);
break;
}//end of print error
if(i == 32767)
Serial.println(“no errors for SRAM 1 in its 32768 bytes”);
}//end of get byte
Serial.println(” ");

SRAM2.writestream2(0); // write to SRAM 2
stopwatch = millis(); //start stopwatch
for(unsigned int i = 0; i < 32768; i++)
SRAM2.RWdata(0xaa); //write to every SRAM 2 address
Serial.print(millis() - stopwatch);
Serial.println(" ms to write full SRAM 2");
SRAM2.readstream2(0); // start address from 0
for(unsigned int i = 0; i < 32768; i++)
{
if(SRAM2.RWdata(0xFF) != 0xaa) //check every address in the SRAM2
{
Serial.print("error in SRAM 2 at location “);
Serial.println(i);
break;
}//end of print error
if(i == 32767)
Serial.println(“no errors for SRAM 2 in its 32768 bytes”);
}//end of get byte
Serial.println(” ");

delay(1000);
}//end of loop

If it all works you should see:

116 ms to write full SRAM 1
no errors for SRAM 1 in its 32768 bytes

118 ms to write full SRAM 2
no errors for SRAM 2 in its 32768 bytes

For the circuit and library download SRAM2class.zip from:

https://sourceforge.net/projects/mysudoku/files/

gijs

You can see how I added another SRAM.

Just extend the principle for extra SRAMs.

If it uses to many digital pins try using one or more 74HC138 1 out of 8 decoders.

With 2 you can have 15 SRAM chips by using 4 digital pins.

Hope it works out for you.

Cheers.