• AVR Freaks

Hot!Interfacing Pic with external SRAM and declaring external SRAM as an array

Author
pastelrazzi
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/10/20 02:12:38
  • Location: 0
  • Status: offline
2019/10/21 22:54:21 (permalink)
0

Interfacing Pic with external SRAM and declaring external SRAM as an array

I'm using a pic12f887 and trying to increase serial SRAM with an external chip (a 23LC1024), trying to declare the entirety of the external SRAM as an array, but struggling to find info online on how to write code that accesses specific points in the external SRAM memory. I could be missing something obvious or searching for incorrect terms as I'm new to programming.
 
This is my prosaic, clunky but working code without the external SRAM. The array is being used as a circular buffer. As is probably obvious, I'm using C.

#define buffersize 96                                     // this is the maximum number of frames the internal SRAM can handle 
 
void main(void) {
// variables //
    unsigned char buffer[buffersize];                // array / circular buffer holding 8 bit frames
    unsigned int write = 0;                              // frame in array I'm writing data to
    unsigned int read = 1;                              // frame in array I'm reading data from (maximum distance from write)
    unsigned int input;                                   // 10 bit reading from the ADC
    unsigned char output = 0;                        // 8 bit char that will be written to PORTB

while(1)    {
// read input //
     ADCON0bits.CHS=0x00;                         // ADC is connected to pin A0
     input = Read_ADC_Value();                    // "Read_ADC_Value" is a function, not included here, it returns 10 bit ADC value
     
    input = input / 4;                                   // convert 10 bit variable to 8 bit   

// push and pop //
     buffer[write] = input;                            // push "input" variable to frame in array
     output = buffer[read];                          // pop "read" frame value to output

     __delay_ms(1);                                   // this determines my sample rate

//write to output//
     PORTB = output;                                // write 8 bit char to PORTB

// increment read & write, & reset them to zero when buffer is full //
     write++;
     if(write == buffersize) {                      // if buffer is full....   reset write position
         write = 0;
     }
     read++;
     if(read == buffersize) {                      // if end of buffer reached....   reset read position
         read = 0;
    }
}}


 
I found these functions for initialising, writing to, waiting until successfully written to, and reading from the external SRAM. They're for the pic12f877 (I'm using pic12f887) but the compiler (xc8) does not seem to have issues with them.
https://circuitdigest.com...communication-tutorial

How can I combine these functions with my code to essentially use all that extra data storage as an array/circular buffer? I may also need to access specific buffer[positions] within the external SRAM (rather than continuously cycling through the circular buffer), how is that possible?

Will the compiler "realise" that the data memory has been increased and not pass errors when I increase "buffersize"?

Any help / pointers to useful literature appreciated. Thanks.
#1

17 Replies Related Threads

    ric
    Super Member
    • Total Posts : 24282
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/22 17:04:29 (permalink)
    +3 (3)
    pastelrazzi
    I'm using a pic12f887

    There is no such device. Do you mean a PIC16F887?
     

    and trying to increase serial SRAM with an external chip (a 23LC1024), trying to declare the entirety of the external SRAM as an array, but struggling to find info online on how to write code that accesses specific points in the external SRAM memory. I could be missing something obvious or searching for incorrect terms as I'm new to programming.

    That is because you cannot do this. PICs are microcontrollers, not microprocessors. They do not have an external memory interface.
    Yes, you can interface to serial memories, but they will not appear as internal RAM.
     
    If you need more RAM, you have to change to a PIC with more RAM inside it.
    All PIC16F devices with three digits after the "16F" are ancient. Newer devices with 4 or 5 digits after the F are available with much more RAM, and are much easier to work with than those old devices.
     
    Also note, those old PIC16F devices only allow ONE array to reach 96 bytes, even when they have more than 96 bytes of RAM. This is a limitation of their architecture. Any further arrays are limited to 80 (or less) bytes.
    The "enhanced midrange" devices (i.e. the ones with 4 or 5 digits after the "F") do not have this limitation, and can declare multiple arrays larger than 96 bytes.
     
    post edited by ric - 2019/10/22 17:07:26

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11407
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/22 18:43:57 (permalink)
    +3 (3)
    Will the compiler "realise" that the data memory has been increased

     
    How could the compiler possibly know about some external hardware attached to your PIC?
     
    How can I combine these functions with my code to essentially use all that extra data storage as an array/circular buffer?

     
    Your serial RAM can be addressed byte-by-byte just like an array in the PIC's internal RAM can be.  You'll have to replace your array operations with serial read/write commands.
     
    But using a PIC appropriate for the job, as ric suggested, is the better way to go, unless you really need 128KB of RAM.
    #3
    pastelrazzi
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2019/10/20 02:12:38
    • Location: 0
    • Status: offline
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/23 00:04:12 (permalink)
    0
    ric
    There is no such device. Do you mean a PIC16F887?

     
    Yes! Apologies, pic16f887
     
    ric
    If you need more RAM, you have to change to a PIC with more RAM inside it.



    I have ordered more up-to-date pics with greater RAM, but I will require more RAM than is available in any pic I have seen, including dspic33s. Using external SRAM is unavoidable it seems.
     
    jtemples
    Your serial RAM can be addressed byte-by-byte just like an array in the PIC's internal RAM can be.  You'll have to replace your array operations with serial read/write commands.

     
    Thank you, this answers the bulk of my query. Searching along these terms brought up this Microchip application note for interfacing pic16s with SRAM. http://ww1.microchip.com/downloads/en/AppNotes/01287A.pdf
     
    It mentions code examples available in "SRAM_Driver.c" How can I find this file?
     
    My only issues then are configuring the write / read functions to target specific external SRAM addresses each time, and how I will increment those addresses (are they in hex format?) to effectively have a circular buffer.
     
    Thanks
    #4
    ric
    Super Member
    • Total Posts : 24282
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/23 02:02:14 (permalink)
    +2 (2)
    pastelrazzi
    ...
    I have ordered more up-to-date pics with greater RAM, but I will require more RAM than is available in any pic I have seen, including dspic33s. Using external SRAM is unavoidable it seems.

    You've not yet mentioned how much you actually need.
     

    It mentions code examples available in "SRAM_Driver.c" How can I find this file?

    Not sure, someone else may have it. It won't be very big, the code is pretty trivial.
     

    My only issues then are configuring the write / read functions to target specific external SRAM addresses each time, and how I will increment those addresses (are they in hex format?) to effectively have a circular buffer.

     For sure, the function will require a "start address", so it's just up to you to supply that form a variable.
    Not knowing how much you need, I can't suggest how wide that variable needs to be.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    Antipodean
    Super Member
    • Total Posts : 1767
    • Reward points : 0
    • Joined: 2008/12/09 10:19:08
    • Location: Didcot, United Kingdom
    • Status: offline
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/23 02:59:11 (permalink)
    +2 (2)
    pastelrazzi
     
    I have ordered more up-to-date pics with greater RAM, but I will require more RAM than is available in any pic I have seen, including dspic33s. Using external SRAM is unavoidable it seems.
     



    As others have said, just how much RAM do you need?
    What are you attempting to do that you need so much RAM?
     
    I believe members of the PIC24/33 family with EPMP port can have more RAM attached, but I have never tried it.
     
    Also there are the PIC32 family, some of which have humongous amounts of RAM (in microcontroller terms).
     
    But the thing you are going to find is that more RAM equates to larger package sizes.
     

    Do not use my alias in your message body when replying, your message will disappear ...

    Alan
    #6
    dan1138
    Super Member
    • Total Posts : 3262
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/23 03:07:39 (permalink)
    +2 (2)
    The Original Poster has said they need more RAM than is available in 8-bit, mid-range Microchip controllers.
     
    This would suggest that using any kind of controller that lacks an external bus would not offer a satisfactory solution.
     
    The OP should be looking at ARM, MIPS or RISC-V based microcomputer devices that have built in DRAM interfaces.
    #7
    ric
    Super Member
    • Total Posts : 24282
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/23 03:11:47 (permalink)
    +2 (2)
    It all depends upon how fast the access needs to be.
    If it's only moderate speed, then an external serial device may be ok.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    pcbbc
    Super Member
    • Total Posts : 1373
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/23 03:14:11 (permalink)
    +2 (2)
    pastelrazziAre they in hex format?

    It seems you may not have quite "got your head around" how integers (which includes addresses) are stored inside a computer.
     
    I'm sure you know all data is stored in one or more bytes.  As a byte can only hold a value from 0 through 255, larger values have to be stored as a sequence of bytes, for example 2 bytes (an int in 8-bit PICs) or 4 bytes (a long in 8-bit PICs).
     
    That sequence of bytes (lets say 2) doesn't really have a native "format".  I suppose at it's lowest level it is a sequence of binary bits, but most would say it is simply stored as a 16-bit integer (and not formatted as an integer or formatted as binary).
     
    Any value can only be said to have a "format" if you convert it to some human readable representation for display.  So a particular 16-bit integer value might be said to be:
    "4660" formatted in decimal
    "0x1234" formatted in hex
    "011064" formatted in octal
    "0b0001 0010 0011 0100" formatted in binary
     
    But the moment you convert your 16-bit integer to a "format" it ceases to be an integer, and instead becomes a string.  A string is a completely different sequence of a different number of bytes, with different values, and in some form of character encoding (maybe ASCII or UTF-8, etc).
     
    You are correct in one respect, when displaying address for humans to read they are most commonly formatted as hex (or in the old days octal) for display.  That doesn't mean that they are stored internally inside the computer as hex though.  Inside the computer that are just integers, entirely devoid of any "format", and stored just like any other integer type (usually 2 or 4 bytes, but these days also 8 bytes on 64-bit machines).
     
    The overriding way of sending addresses or data to any external memory is in their raw form, as one or more bytes (maybe in parallel on an address bus, or serially over I2C or SPI).  The only example where those bytes might be formatted is over a serial (UART) link - but that's a whole different kettle of fish from talking to an external memory such as an EEPROM or SRAM.
    #9
    pastelrazzi
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2019/10/20 02:12:38
    • Location: 0
    • Status: offline
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/23 06:07:26 (permalink)
    0
    Thank you pcbbc for the info... yep, I have a lot to learn. When I said "format" I meant in terms of how the addresses appear within the "read" and "write" functions in the elusive "SRAM_Driver.c" file, in case I could potentially increment the address position.
     
    I appreciate the code in there may be trivial. At this point I'm not sure how else to put this together, without seeing working examples. If there is anything you think I should read that will help me crack this nut, please point me towards it. I've been reliant on Microchip's code examples for communicating with peripherals thus far, it's much less bewildering a learning process if I can modify a working example.
     
    My required RAM size is fairly flexible. The absolute minimum is 64K x 8-bit. The 23LC1024 (external SRAM) I mentioned has double that. More than 256k x 8-bit would be superfluous. The more the merrier within those parameters.
     
    ric
    It all depends upon how fast the access needs to be.
    If it's only moderate speed, then an external serial device may be ok.


    This could be an issue, I'd have to try it to find out. "Success" is fairly subjective in this case, but as long as I can read/write to external SRAM in < 25 microseconds I should be okay (that's plenty time, isn't it?). Even with faster chips like dspic33's, there isn't enough internal RAM for my purposes.
    #10
    ric
    Super Member
    • Total Posts : 24282
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/23 14:34:26 (permalink)
    +1 (1)
    pastelrazzi
    Thank you pcbbc for the info... yep, I have a lot to learn. When I said "format" I meant in terms of how the addresses appear within the "read" and "write" functions in the elusive "SRAM_Driver.c" file, in case I could potentially increment the address position.

    They will be supplied in integer variables, so as pcbbc has been trying to explain, "format" does not come into it.
     

    ...
    "Success" is fairly subjective in this case, but as long as I can read/write to external SRAM in < 25 microseconds I should be okay (that's plenty time, isn't it?).

    A byte write to a 23LC1024 in SPI mode requires 40 clock cycles (Figure 2-2 in the 23LC1024 datasheet).
    That involves writing 5 bytes, one for the write instruction, three for the address, and one for the data.
     
    The fastest you can run a PIC16F887 is 20MHz, and the fastest SPI clock is Fosc/4 = 5MHz.
    So, the shortest possible write cycle, ignoring time to drive the CS pin, is 5/5MHz = 1us
    So, the memory chip itself is capable of the required speed.
    It does mean you have to supply a new byte to the SPI peripheral every eight instruction cycles (as your instruction clock is also Fosc/4). That could be possible, but requires very efficient code.
    When you allow for the overhead of incrementing the address etc, I would expect you'd actually get about a quarter of the theoretical speed, which is still probably acceptable, however that also depends upon what else you are doing at the same time to obtain the data.

    Moving to an "enhanced" device lets you increase the clock from 20MHz to 32MHz, so a 60% increase.
    It also has a better instruction set which can make the code more efficient too.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    ric
    Super Member
    • Total Posts : 24282
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/23 15:17:32 (permalink)
    +1 (1)
    n.b. I see you're using library functions to read a 10 bit ADC value.
    The 10 bit ADC in these PIC16F devices can operate in a "left justified" mode, which gives you the most significant 8 bits in a single register, so you don't have to do any calculation at all.
    It's always a good idea to read the device datasheet, and understand what the hardware can actually do.
    This becomes essential when you're trying to push maximum performance.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    pcbbc
    Super Member
    • Total Posts : 1373
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/24 00:02:41 (permalink)
    +1 (1)
    The ”driver” code is included as part of the sample code for AN1287.
    I’m with RIC on this one though. For absolutely maximum performance using libraries isn’t the best choice.
    #13
    pastelrazzi
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2019/10/20 02:12:38
    • Location: 0
    • Status: offline
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/24 23:11:19 (permalink)
    0
    Thank you ric for the info and pcbbc for the example code.

    Please help clarify my understanding on a few points.

    The example code (which is for the 23K256) sends 2 address bytes to the SRAM (a higher address and a lower address), whereas the 23LC1024 datasheet states that addresses are 24 bit. So when sending address data to my 23LC1024, I'd have to send 3 bytes of data. Can I write this in code as a single "long" integer?

    It seems that incrementing a 24 bit number when it is split into 3 seperate bytes would be a bit of a headache.

    Can you also confirm that, as I think pcbbc was saying, an integer can be written in C as decimal or hexadecimal interchangeably? And if I'm to write in hex, I should begin with "0x" so as not to be confused for decimal?

    That's kind of the same question twice. Thank you for your patience.
    #14
    1and0
    Access is Denied
    • Total Posts : 9900
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/25 00:07:16 (permalink)
    0
    pastelrazzi
    The example code (which is for the 23K256) sends 2 address bytes to the SRAM (a higher address and a lower address), whereas the 23LC1024 datasheet states that addresses are 24 bit. So when sending address data to my 23LC1024, I'd have to send 3 bytes of data. Can I write this in code as a single "long" integer?

    Yes, the address should be a long integer. You only break it up when sending to the SRAM.
     

    It seems that incrementing a 24 bit number when it is split into 3 seperate bytes would be a bit of a headache.

    Not really, but it's not the prefer method. ;)
     

    Can you also confirm that, as I think pcbbc was saying, an integer can be written in C as decimal or hexadecimal interchangeably? And if I'm to write in hex, I should begin with "0x" so as not to be confused for decimal?

    Correct. Yes. The numbers 42 and 0x2A are the same value.
    #15
    ric
    Super Member
    • Total Posts : 24282
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/25 00:33:37 (permalink)
    0
    pastelrazzi
    ...
    So when sending address data to my 23LC1024, I'd have to send 3 bytes of data. Can I write this in code as a single "long" integer?

    Note, XC8 has built in support for a 24 bit integer type.
    Use "short long" to declare the variable.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #16
    1and0
    Access is Denied
    • Total Posts : 9900
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/25 00:41:01 (permalink)
    0
    For unsigned 24-bit integer type, #include <stdint.h> and use "uint24_t" for C90 mode or "__uint24" for C99 mode.
    #17
    pastelrazzi
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2019/10/20 02:12:38
    • Location: 0
    • Status: offline
    Re: Interfacing Pic with external SRAM and declaring external SRAM as an array 2019/10/25 02:07:31 (permalink)
    0
    I modified the example code functions to include an extra "middle" address byte and used the method of splitting a 24 bit integer into 3 bytes (linked to below) and it works wonderfully!
     
     https://www.microchip.com/forums/m1011488.aspx 

     
    Thanks a lot for all the help everybody!
     
    #18
    Jump to:
    © 2019 APG vNext Commercial Version 4.5