Playground.arduino.cc will be read-only starting December 31st, 2018. For more info pleae look at this Forum Post

A Library for Reading and Writing Variables and Variable Arrays to the 23LC1024 chip with the Arduino Uno

Last Modified: August 2, 2018, at 01:16 AM
By: dndubins
Platforms: Built on Arduino IDE 1.8.7 (not tested with earlier platforms). Last compiled on Arduino IDE 1.8.7.

After falling in love with the little 23LC1024 chip (I mean, 1 megabit to play with? Wow!) I found a great sketch by J.B. Gallaher, presented on YouTube here:

https://www.youtube.com/watch?v=eIP_w5uizNw

The sketch compiled flawlessly, and the video was super clear! I was excited about the possibilities of this chip, so I decided to turn it into a library.

Link to library: https://github.com/dndubins/SRAMsimple


Introduction

This library was written to facilitate reading and writing to the awesome 23LC1024 chip. I expanded the library to handle transferring my favourite variable types: int, unsigned int, long, unsigned long, float, and arrays of those variable types. It was important to me that the library be flexible with the CS line, because I had a specific job in mind for this chip. It ended up not fitting with that project anyway, but you can set the CS line in the SetMode() function. Also see the example sketches for how this is done.

The functions available in the library include:

    void SetMode(byte CSpin, char Mode);
    void WriteByte(uint32_t address, byte data_byte);
    byte ReadByte(uint32_t address);
    void WriteByteArray(uint32_t address, byte *data, uint16_t big);
    void ReadByteArray(uint32_t address, byte *data, uint16_t big);
    void WriteInt(uint32_t address, int data);  
    int ReadInt(uint32_t address);
    void WriteIntArray(uint32_t address, int *data, uint16_t big);
    void ReadIntArray(uint32_t address, int *data, uint16_t big);
    void WriteUnsignedInt(uint32_t address, unsigned int data);
    unsigned int ReadUnsignedInt(uint32_t address);
    void WriteUnsignedIntArray(uint32_t address, unsigned int *data, uint16_t big);
    void ReadUnsignedIntArray(uint32_t address, unsigned int *data, uint16_t big);
    void WriteLong(uint32_t address, long data);
    long ReadLong(uint32_t address);
    void WriteLongArray(uint32_t address, long *data, uint16_t big);
    void ReadLongArray(uint32_t address, long *data, uint16_t big);
    void WriteUnsignedLong(uint32_t address, unsigned long data);
    unsigned long ReadUnsignedLong(uint32_t address);
    void WriteUnsignedLongArray(uint32_t address, unsigned long *data, uint16_t big);
    void SRAMsimple::ReadUnsignedLongArray(uint32_t address, unsigned long *data, uint16_t big);
    void WriteFloat(uint32_t address, float data);
    float ReadFloat(uint32_t address);
    void WriteFloatArray(uint32_t address, float *data, uint16_t big);
    void ReadFloatArray(uint32_t address, float *data, uint16_t big);

Usage

This sketch shows an example sketch of how the library may be used. Example sketches in the Github library also have functions to read and write arrays of each variable type.

  1. /* Example program for use with SRAMsimple.h
  2.     Arduino Uno Memory Expansion Sample Program
  3.     Author:  J. B. Gallaher       07/09/2016
  4.     Library created and expanded by: D. Dubins 12-Nov-18
  5.  
  6.    Sample program to use a Serial SRAM chip to expand memory for an Arduino Uno
  7.    giving access to an additional 128kB of random access memory.  The 23LC1024 uses
  8.    the Serial Peripheral Interface (SPI) to transfer data and commands between the
  9.    UNO and the memory chip.
  10.  
  11.    Used the following components:
  12.    (1) Arduino Uno
  13.    (2) Microchip 23LC1024 SPI SRAM chip soldered on an Arduino Protoshield
  14.  
  15.    Wiring:
  16.    23LC1024 - Uno:
  17.    ---------------
  18.    Pin1 (JSC) -- Pin 10 (CS)
  19.    Pin2 (SO)  -- Pin 12 (MISO) (10K pullup to +5V)
  20.    Pin3 (NU)  -- 10K pullup to +5V
  21.    Pin4 (GND) -- GND
  22.    Pin5 (SI)  -- Pin 11 (MOSI)
  23.    Pin6 (SCK) -- Pin 13 (SCK)
  24.    Pin7 (HOLD) -- 10K pullup to +5V
  25.    Pin8 (V+)  -- +5V
  26. */
  27. #include <SRAMsimple.h>
  28.  
  29. #define CSPIN 10       // Default Chip Select Line for Uno (change as needed)
  30. SRAMsimple sram;       //initialize an instance of this class
  31.  
  32. /*******  Set up code to define variables and start the SCI and SPI serial interfaces  *****/
  33. void setup()
  34. {
  35.   uint32_t address = 0;                       // create a 32 bit variable to hold the address (uint32_t=long)
  36.   Serial.begin(9600);                         // set communication speed for the serial monitor
  37.   SPI.begin();                                // start communicating with the memory chip
  38.  
  39.   // And now the fun begins:
  40.   /**********Write a Single Byte *******************/
  41.   byte data = 0;                              // initialize the data
  42.   for(int i = 0; i <=5; i++){                 // Let's write 5 individual bytes to memory
  43.     address = i;                              // use the loop counter as the address
  44.     sram.WriteByte(address, data);            // now write the data to that address
  45.     data+=2;                                  // increment the data by 2
  46.   }
  47.  
  48. /********* Read a single Byte *********************/
  49.   Serial.println("Reading each data byte individually: ");
  50.   byte value;                                 // create variable to hold the data value read
  51.   for(int i = 0; i <=5; i++){                 // start at memory location 0 and end at 5
  52.     address = i;                              // use the loop counter as the memory address
  53.     value = sram.ReadByte(address);           // reads a byte of data at that memory location
  54.     Serial.println(value);                    // Let's see what we got
  55.   }
  56.  
  57. /************  Write an Integer *******************/
  58.   Serial.println("\nWriting integer using sequential: ");
  59.   int tempInt1=-32768;                         // highest integer # is 32767
  60.   sram.WriteInt(0, tempInt1);                  // send tempInt1 to SRAM starting from address 0
  61.  
  62. /************ Read an Integer from Memory into an Int **********/
  63.   Serial.println("Reading integer using sequential: ");
  64.   int tempInt2=sram.ReadInt(0);                // Read integer from memory address 0
  65.   Serial.println(tempInt2);                    // print as integer
  66.  
  67. /************  Write an Unsigned Integer *******************/
  68.   Serial.println("\nWriting unsigned integer using sequential: ");
  69.   unsigned int tempUnsignedInt1=65535;         // highest unsigned integer is 65535
  70.   sram.WriteInt(0, tempUnsignedInt1);          // send tempUnsignedInt1 to SRAM starting from address 0
  71.  
  72. /************ Read an Unsigned Integer from Memory into an Int **********/
  73.   Serial.println("Reading unsigned integer using sequential: ");
  74.   unsigned int tempUnsignedInt2=sram.ReadUnsignedInt(0);     // Read integer from memory address 0
  75.   Serial.println(tempUnsignedInt2);            // print as unsigned integer
  76.  
  77. /************  Write Long *******************/
  78.   Serial.println("\nWriting long using sequential: ");
  79.   long tempLong1=2147483647;                   // highest long #
  80.   sram.WriteLong(0, tempLong1);                // send tempLong1 to SRAM starting from address 0
  81.  
  82. /************ Read Long from Memory **********/
  83.   Serial.println("Reading long using sequential: ");
  84.   long tempLong2=sram.ReadLong(0);             // Read long from memory address 0
  85.   Serial.println(tempLong2);                   // print as long
  86.  
  87. /************  Write Unsigned Long *******************/
  88.   Serial.println("\nWriting unsigned long using sequential: ");
  89.   long tempUnsignedLong1=4294967295;           // highest unsigned long #
  90.   sram.WriteUnsignedLong(0, tempLong1);        // send tempUnsignedLong1 to SRAM starting from address 0
  91.  
  92. /************ Read Unsigned Long from Memory **********/
  93.   Serial.println("Reading long using sequential: ");
  94.   long tempUnsignedLong2=sram.ReadUnsignedLong(0);  // Read unsigned long from memory address 0
  95.   Serial.println(tempLong2);                   // print as long
  96.  
  97. /************  Write a Float using Sequential *******************/
  98.   Serial.println("\nWriting float using sequential: ");
  99.   float tempFloat1=3.141593;                   // a float #
  100.   sram.WriteFloat(0, tempFloat1);              // send tempFloat1 to SRAM starting at address 0
  101.  
  102. /************ Read Float from Memory into a Float **********/
  103.   Serial.println("Reading float using sequential: ");
  104.   float tempFloat2=sram.ReadFloat(0);           // Read float from memory address 0
  105.   Serial.println(tempFloat2,6);                 // print as float
  106. }
  107.  
  108. void loop()
  109. {
  110. }
  111. //END OF FILE