• Pic® Basic


  • Interfacing to a 23K256 - 32 kbyte SRAM

    RAM and I/O lines are two of the items that are usually in short supply on a microcontroller, therefore with this handy little SRAM device we can address both these issues, as it will give us an extra 32 kbytes of RAM, and only use four of our precious I/O lines, three of which can also be used for other purposes when not accessing the device. The chip in question is the Microchiptm 23K256 SPI SRAM.

    The SRAM chip comes in a few package types, from tiny surface mount, to standard DIL, which makes it ideal for prototyping. It has an architecture of 32768 byte sized RAM cells (32 kbytes), and can be interfaced through a fast (up to 20MHz) SPI interface.

    As usual for me, the prototype is built on the trusty Amicus18 Companion shield, as shown below:














    Click here for a larger image.


    A top down view of the same layout is shown below:



















    Click here for a larger image.


    The circuit for the above layouts is shown below:


























    Click here for a larger image.


    The sketchup models can be downloaded from here: Sketchup - Companion Shield 23K256 models.zip.

    23K256 macro library

    The 23K256 macro library adds the following commands:

    SRAM_Init Initialise the SRAM SPI interface
    SRAM_WriteByte Write a byte to the SRAM
    SRAM_ReadByte Read a byte from the SRAM
    SRAM_WritePage Write to a page of SRAM
    SRAM_ReadPage Read from a page in SRAM
    SRAM_WriteSeq Write sequentially to the SRAM
    SRAM_ReadSeq Read sequentially from the SRAM
    SRAM_WriteStatus Write to the Status register of the SRAM
    SRAM_ReadStatus Read from the Status register of the SRAM

    Adding the functionality of the macros is as simple as including it into the program via the Include directive:

    Include "23K256.inc" ' Load the 23K256 SPI SRAM macros into the program

    The macros use the microcontroller’s SPI peripheral to interface with the SRAM chip, however, because the microcontroller does not have a dedicated pin for use as a CS line a $define allows any (within reason) of it’s I/O lines to be used for this purpose. The define is:

    $define SRAM_CS Port.Bit

    Note.
    The define must be placed before the macros are included into the program:

    $define SRAM_CS PORTC.0 ' SRAM’s CS pin

    Include "23K256.inc" ' Load the 23K256 SPI SRAM macros into the program

    The include directive itself, should be placed at the top of the program, before any user constants, or variables are created.

    SRAM_Init

    SRAM_Init()

    Initialise the microcontroller's peripheral SPI interface for the 23K256 SRAM. The mode used is 1:1, sampled in the middle, with a 4MHz bitrate.

    Note.
    The SPI interface is automatically initialised when the 23K256.inc file is included into the program. However, if the SPI peripheral is used with a different SPI mode, then the SRAM_Init macro will need to be re-implemented in order to place the mode back to the SRAM's requirements.

    SRAM_WriteStatus

    SRAM_WriteStatus(pMode)

    Write to the Status register within the SRAM.

    Operators.
    • pMode: One of three pre-configured modes that can be written to the SRAM’s Status register. These are:
      • cModeByte - Place the SRAM in byte read and write mode
      • cModePage - Place the SRAM in page read and write mode
      • cModeSeq - Place the SRAM in sequential read and write mode
    Example.
    Code:
      Include "Amicus18.inc"        ' Configure the compiler to use a PIC18F25K20. i.e. An Amicus18 board
      Include "23K256.inc"          ' Load the 23K256 SPI SRAM macros into the program
    
     SRAM_WriteStatus(cModeByte)   ' Place the SRAM into byte modes
      SRAM_WriteStatus(cModePage)   ' Place the SRAM into page mode
     SRAM_WriteStatus(cModeSeq)    ' Place the SRAM into sequential mode
    Note.
    See the 23K256 SRAM’s data sheet for further information concerning its status register. This can be downloaded from here: 23K256 - Data Sheet.pdf

    SRAM_ReadStatus

    Var = SRAM_ReadStatus()

    Read the contents of the SRAM’s status register.

    Operator.
    • Var: Can be a variable of type; Bit, Byte, Word, Dword, or an array’s element.
    Example:
    Code:
    
      Include "Amicus18.inc"      ' Configure the compiler to use a PIC18F25K20. i.e. An Amicus18 board
      Include "23K256.inc"        ' Load the 23K256 SPI SRAM macros into the program
      
      Dim bTemp as Byte           ' Create a byte sized variable for the status read
      bTemp = SRAM_ReadStatus()   ' Read the SRAM’s status register
      Hrsout Dec bTemp, 13        ' Display status register value on the serial terminal
    Note.
    See the 23K256 SRAM’s data sheet for further information concerning its status register. This can be downloaded from here: 23K256 - Data Sheet.pdf

    SRAM_WriteByte

    SRAM_WriteByte(pAddress, pVar)

    Write a single byte to the SRAM.

    Operators.
    • pAddress: The address to start within the SRAM.
    • pVar: The byte to write to the SRAM.
    Example.
    Code:
    
      Include "Amicus18.inc"            ' Configure the compiler to use a PIC18F25K20. i.e. An Amicus18 board
      Include "23K256.inc"              ' Load the 23K256 SPI SRAM macros into the program
    
       Dim wAddress as Word              ' Create a word sized variable for the start address
       Dim bData as Byte                 ' Create a byte sized variable for the byte to write
    
      SRAM_WriteStatus(cModeByte)       ' Place the SRAM into byte mode
    '
    ' Write to SRAM
    '
      bData = 0
      For wAddress = 0 to 19            ' Form a loop for the address
        SRAM_WriteByte(wAddress, bData) ' Write a byte at current address
        Inc bData                       ' Increment the byte to be written
      Next                              ' Close the address loop
    '
    ' Read from SRAM and display
    '
      For wAddress = 0 to 19            ' Form a loop for the address
        bData = SRAM_ReadByte(wAddress) ' Read a byte from SRAM
        Hrsout Dec bData, 13            ' Display the byte’s value on the serial terminal    
      Next                              ' Close the address loop
    Note.
    See the 23K256 data sheet for further information concerning writing bytes to the SRAM. This can be downloaded from here: 23K256 - Data Sheet.pdf

    SRAM_ReadByte

    Var = SRAM_ReadByte(pAddress)

    Read a single byte from the SRAM.

    Operators.
    • pAddress: The address to start within the SRAM.
    • Var: Can be a variable of type; Bit, Byte, Word, Dword, or an array’s element.
    Example.
    Code:
    
      Include "Amicus18.inc"            ' Configure the compiler to use a PIC18F25K20. i.e. An Amicus18 board
      Include "23K256.inc"              ' Load the 23K256 SPI SRAM macros into the program
    
      Dim wAddress as Word              ' Create a word sized variable for the start address
      Dim bData as Byte                 ' Create a byte sized variable for the byte to write
    
      SRAM_WriteStatus(cModeByte)       ' Place the SRAM into byte mode
    '
    ' Write to SRAM
    '
      bData = 0
      For wAddress = 0 to 19            ' Form a loop for the address
        SRAM_WriteByte(wAddress, bData) ' Write a byte at current address
        Inc bData                       ' Increment the byte to be written
      Next                              ' Close the address loop
    '
    ' Read from SRAM and display
    '
      For wAddress = 0 to 19            ' Form a loop for the address
        bData = SRAM_ReadByte(wAddress) ' Read a byte from SRAM
        Hrsout Dec bData, 13            ' Display the byte’s value on the serial terminal     
      Next                              ' Close the address loop
    Note.
    See the 23K256 data sheet for further information concerning reading bytes from the SRAM. This can be downloaded from here: 23K256 - Data Sheet.pdf

    SRAM_WritePage

    SRAM_WritePage(pPage, pAmount, pSource)

    Write to a page within the SRAM.

    Operators.
    • pPage: The page of interest within the SRAM. The 23K256 has 1024 pages, of 32 bytes each.
    • pAmount: The amount of bytes to write within the page. (0 to 31)
    • pSource: Can be a byte array or a string that holds the data to write to the SRAM.
    Example.
    Code:
    
      Include "Amicus18.inc"          ' Configure the compiler to use a PIC18F25K20. i.e. An Amicus18 board
      Include "23K256.inc"            ' Load the 23K256 SPI SRAM macros into the program
    
      Dim bData as Byte               ' Create a byte variable for the byte to write/read
      Dim bIndex as Word              ' Create a byte sized variable as an index
      Dim bMyArray[32] as Byte        ' Create a 32 element byte array
    
      SRAM_WriteStatus(cModePage)     ' Place the SRAM into page mode
    '
    ' Fill the array with values
    '
      For bIndex = 0 to 31            ' Form a loop
        bMyArray[bIndex] = bIndex     ' Write a value to the array
      Next                            ' Close the loop
    
      SRAM_WritePage(2, 32, bMyArray) ' Write to full page 2 within the SRAM
    
      Clear bMyArray                  ' Clear the array written to the SRAM
    
      SRAM_ReadPage(2, 32, bMyArray)  ' Read from full page 2 within the SRAM
    '
    ' Display the values read from the SRAM
    '
      For bIndex = 0 to 31            ' Form a loop
        Hrsout Dec bMyArray[bIndex], 13 ' Display from the array on the serial terminal
      Next                            ' Close the loop
    Note.
    See the 23K256 data sheet for further information concerning writing pages to the SRAM. This can be downloaded from here: 23K256 - Data Sheet.pdf

    SRAM_ReadPage

    SRAM_ReadPage(pPage, pAmount, pDest)

    Read from a page within the SRAM.

    Operators.
    • pPage: The page of interest within the SRAM. The 23K256 has 1024 pages, of 32 bytes each.
    • pAmount: The amount of bytes to read within the page. (0 to 31)
    • pDest: Can be a byte array or a string that holds the data read from the SRAM.
    Example.
    Code:
    
      Include "Amicus18.inc"          ' Configure the compiler to use a PIC18F25K20. i.e. An Amicus18 board
      Include "23K256.inc"            ' Load the 23K256 SPI SRAM macros into the program
    
      Dim bData as Byte               ' Create a byte variable for the byte to write/read
      Dim bIndex as Word              ' Create a byte sized variable as an index
      Dim bMyArray[32] as Byte        ' Create a 32 element byte array
    
      SRAM_WriteStatus(cModePage)     ' Place the SRAM into page mode
    '
    ' Fill the array with values
    '
      For bIndex = 0 to 31            ' Form a loop
        bMyArray[bIndex] = bIndex     ' Write a value to the array
      Next                            ' Close the loop
    
      SRAM_WritePage(2, 32, bMyArray) ' Write to full page 2 within the SRAM
    
      Clear bMyArray                  ' Clear the array written to the SRAM
    
      SRAM_ReadPage(2, 32, bMyArray)  ' Read from full page 2 within the SRAM
    '
    ' Display the values read from the SRAM
    '
      For bIndex = 0 to 31            ' Form a loop
        Hrsout Dec bMyArray[bIndex], 13 ' Display from the array on the serial terminal
      Next                            ' Close  the loop
    Note.
    See the 23K256 data sheet for further information concerning reading pages from the SRAM. This can be downloaded from here: 23K256 - Data Sheet.pdf

    SRAM_WriteSeq

    SRAM_WriteSeq(pAddress, pAmount, pSource)

    Write bytes sequentially to the SRAM.

    Operators.
    • pAddress: The address to start within the SRAM.
    • pAmount: The amount of bytes to write.
    • pSource: Can be a byte array or a string that holds the data to write to the SRAM.
    Example.
    Code:
    
      Include "Amicus18.inc"              ' Configure the compiler to use a PIC18F25K20. i.e. An Amicus18 board
      Include "23K256.inc"                ' Load the 23K256 SPI SRAM macros into the program
    
      Dim bData as Byte                   ' Create a byte variable for the byte to write/read
      Dim bIndex as Word                  ' Create a byte sized variable as an index
      Dim bMyArray[200] as Byte           ' Create a 200 element byte array
    
      SRAM_WriteStatus(cModeSeq)          ' Place the SRAM into sequential mode
    '
    ' Fill the array with values
    '
      For bIndex = 0 to 199               ' Form a loop
        bMyArray[bIndex] = bIndex         ' Write a value to the array
      Next                                ' Close the loop
    
      SRAM_WriteSeq($0000, 200, bMyArray) ' Write 200 bytes from address $0000 within the SRAM
    
      Clear bMyArray                      ' Clear the array written to the SRAM
    
      SRAM_ReadSeq($0000, 200, bMyArray)  ' Read 200 bytes from address $0000 within the SRAM
    '
    ' Display the values read from the SRAM
    '
      For bIndex = 0 to 199               ' Form a loop
        Hrsout Hex2 bMyArray[bIndex], "," ' Display from the array on the serial terminal
        If bIndex // 8 = 0 Then           ' Break it into lines of 8 values each
          HRSOut 13
        EndIf
      Next                                ' Close the loop
    Note.
    See the 23K256 data sheet for further information concerning writing sequentially to the SRAM. This can be downloaded from here: 23K256 - Data Sheet.pdf

    SRAM_ReadSeq

    SRAM_ReadSeq(pAddress, pAmount, pDest)

    Read bytes sequentially from the SRAM.

    Operators.
    • pAddress: The address to start within the SRAM.
    • pAmount: The amount of bytes to read.
    • pDest: Can be a byte array or a string that holds the data read from the SRAM.
    Example.
    Code:
    
      Include "Amicus18.inc"              ' Configure the compiler to use a PIC18F25K20. i.e. An Amicus18 board
      Include "23K256.inc"                ' Load the 23K256 SPI SRAM macros into the program
    
      Dim bData as Byte                   ' Create a byte variable for the byte to write/read
      Dim bIndex as Word                  ' Create a byte sized variable as an index
      Dim bMyArray[200] as Byte           ' Create a 200 element byte array
    
      SRAM_WriteStatus(cModeSeq)          ' Place the SRAM into sequential mode
    '
    ' Fill the array with values
    '
      For bIndex = 0 to 199               ' Form a loop
        bMyArray[bIndex] = bIndex         ' Write a value to the array
      Next                                ' Close the loop
    
      SRAM_WriteSeq($0000, 200, bMyArray) ' Write 200 bytes from address $0000 within the SRAM
    
      Clear bMyArray                      ' Clear the array written to the SRAM
    
      SRAM_ReadSeq($0000, 200, bMyArray)  ' Read 200 bytes from address $0000 within the SRAM
    '
    ' Display the values read from the SRAM
    '
      For bIndex = 0 to 199               ' Form a loop
        Hrsout Hex2 bMyArray[bIndex], "," ' Display from the array on the serial terminal
        If bIndex // 8 = 0 Then           ' Break it into lines of 8 values each
          HRSOut 13
        EndIf
      Next                                ' Close the loop
    Note.
    See the 23K256 data sheet for further information concerning reading sequentially from the SRAM. This can be downloaded from here: 23K256 - Data Sheet.pdf

    The 23K256 library source code and a demonstration program can be downloaded from here: 23K256-Source Code.zip
  • Recent Activity

    towlerg-21522

    PIC18F1330 PLL problem

    Thread Starter: rcurl

    I'm just starting out on a new project that uses a PIC18F1330. I haven't used this specific chip before, so I began by trying to blink an LED just...

    towlerg Yesterday, 16:26 Go to last post
    towlerg-21522

    Pic16f18877 oread

    Thread Starter: evoortman

    Hi, On a PIC16F18877 the OREAD command doesn't seem to work. The code is working on a PIC16F1939. Both controllers use 32MHz int osc. If i...

    towlerg Yesterday, 16:25 Go to last post
    palamont-28794

    MID$ Problem with a PIC18F25K22

    Thread Starter: palamont

    Hi everybody, I encounter a strange problem : I have a string of 230 characters. This string is divided into 10 segments of 23 characters, with a...

    palamont Yesterday, 10:08 Go to last post