loading

Microcontrollers like the PIC and Arduino are great, but sometimes you need to store a large array of data and the internal memory just isn't enough, and that's where external memory comes into play. SRAM is generally easier to work with than dynamic memory(which needs to be refreshed to prevent data loss) and that's what i've chosen to use here. specifically, i'm using the 23lc512, which is a 512 kilobit, or 64 kilobyte, SRAM module with SPI interface.

I love SPI, it's hands down one of the easiest ways to interface with peripherals using a bus layout, at the cost of just 1 additional pin per peripheral. older SRAM modules require 8 pins for data, and as many as ten or more pins for addressing. try using that on an arduino, even with multiplexers to reduce the pin count, and you'll quickly find that the added complexity means it's a royal pain on an atmega328, and downright impossible on an attiny.

in this instructable i'll cover how to interface with a single SRAM, or multiple in a bus arrangement.

Step 1: The SRAM

my SRAM of choice today, the 23LC512 from microchip, is a simple 8 pin, SPI SRAM module. when CS is pulled low, the RAM knows that it needs to start listening to its SI pin for orders from the master, with the help of a clock signal coming in on the CLK pin to set the pace. because we're using SPI we won't be needing pin 3/SIO2, which is only used in serial dual or serial quad interface mode, and i'm not touching on those here(because they are way over my head). the HOLD pin is pulled low to pause the serial communication in case an interrupt is triggered and the microcontroller or microprocessor has to divert its attention elsewhere, but for now we're just going to tie it to VCC to keep it out of our way.

there are only seven commands needed to interface with the LC512, three of them are only for the alternative IO modes, and two more for register manipulation on the LC512 which we'll also be leaving alone, and that just leaves us with two simple commands, read (0x03) and write(0x02). how much simpler can it get? one pin to drive to toggle the chip, and two 8 bit commands to control it.

Step 2: Hooking Up the SRAM

look over the diagram shown here and hook up your SRAM, making adjustments for your particular choice if IC. once you have everything hooked up, it's just a matter of executing some code to let your arduino talk to it.

The starting code here is borrowed from the blog of Jim Eli, at uc experiment because frankly his code was better than mine, and probably easier to explain.

To write to memory, you first drive the CS pin low, then over SPI send the write command (0x02) followed by the 16 bit address you want to write to. by default, the lc512 is now ready to accept data, 8 bits at a time, write them starting at the address you provided, and increment the address by 1. if you keep writing past 0xFFFF it will simply roll over to 0x0000 and keep going. it'll write for as long as you offer it data.

Reading from memory is basically the same thing. send 0x03 to read, the address you want to start reading at, and then tell your microcontroller to start listening for the data being sent to it. as long as you keep listening, and sending a clock signal, the SRAM will keep sending data to you, looping through the memory addresses forever if you want it to.

when you're done, just drive the CS pin high so that it knows to shut up because you're done with it. that's all it takes to communicate with SRAM over SPI. great, isn't it? now you can add extra memory to your projects so you can log data, or whatever you need more RAM for. but what if you need two chips? how do you do that? read on.

Step 3: Two RAM Chips? Madness

so how do you do it? easy.

you add another chip, and connect all but the CS pin to the same lines, only CS needs a unique connection to your microcontroller. the modified code attached writes to the first 32 bytes of chip 1, reads back the data, and then does the same to chip 2. the only difference in the code for the two chips is which pins are driven high or low by the PORTB command to access which chip.

that's all there is to it, drive one pin low and the other high to access the chip with the low pin, flip it around to access the other. just remember that if you toggle both chips at once you'll get bus contention issues as both chips try to send data to the arduino at the same time and all you'll get is garbage. and if you don't drive any pin low, nothing will be listening for commands, they'll both just be sitting patiently in the corner waiting for you to call on them. handy, huh? you can add as many chips as you have free pins.

i'm sure that the code could be optimized so that you don't need individual functions to call on each chip, but i have had zero luck making any of my customized functions work better than what you see here. hopefully you've learned something and thought of some interesting ways to take advantage of added RAM for your projects. the best part is, the things you've learned here can also apply to EEPROMs, except they aren't volatile.

Have Fun!

<p>Very well documented. Thanks for sharing your arduino project! </p>

About This Instructable

11,703views

34favorites

License:

More by weish:Massage oil for aches and pains Interface an SRAM bus to your arduino Crochet amigurumi octopus/jellyfist convertible doll 
Add instructable to: