Shop OBEX P1 Docs P2 Docs Learn Events
Stamp2e - Shared Variables? — Parallax Forums

Stamp2e - Shared Variables?

everesteverest Posts: 141
edited 2010-02-11 21:54 in BASIC Stamp
I've dug through the manuals and I can't seem to figure out if the Stamp2e or better modules can share variables between programs running in different memory space. For example, in my application I need to have a few nibs and bits of data that can be stored in variables that is accessible by any program running on any slot. Do I have to use scratch RAM to accomplish that or is there a more simple solution, i.e. shared variables?

-Jeff

Post Edited (everest) : 6/10/2009 5:53:35 AM GMT

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-06-10 05:51
    Just to be clear, the stamp only runs one program at a time. You can give data from one slot to another in one of two ways internally: scratch pad (best), variable alignment (worse). If I remember right, when a new slot is given control the RAM isn't cleared, so any data that you have there is now a part of your new program. The better way is the use the scratch pad. Take a look at the code in this thread. It shares data between slots.
  • everesteverest Posts: 141
    edited 2009-06-10 06:03
    Thank you, I know they run serially, that's no problem. I just need to share some very basic values between programs and I want to do it as simply as possible. The example you've posted is an excellent project (wow!), but it looks like he's using a data logger vs. scratch pad RAM.

    It doesn't sound like shared variables is the way to do it, so I'll look at scratch RAM. That was my original intent anyways. . .we're only talking about 6 bits, 1 nib and 1 byte.

    -Jeff
  • SRLMSRLM Posts: 5,045
    edited 2009-06-10 06:23
    I used the datalogger to store the data long term, but I gave it a whole slot. So, I had to "pass in" the GPS data and sensiron data via SCRAM.

    You'll use the PUT and GET commands to access the SCRAM, and it you get any more data than the two bytes that you have now I'd suggest making a document with a listing of each slot (since you can't name them).
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-06-10 16:37
    Jeff,

    Shared variables are fine, too. PBASIC allows alias names to assign the smaller chunks (bytes nibs bits) as parts of a word variable and thus have more control of where it stays in memory. PBASIC always puts words first in the RAM, including the alias parts. For example, make your first word variable defined in every program slot:
    shared VAR Word
    mybyte VAR shared.byte0
    mynib VAR shared.nib2
    mybit0 VAR shared.bit12
    mybit0 VAR shared.bit13
    mybit0 VAR shared.bit14
    mybit0 VAR shared.bit15

    Subsequently, no matter how each slot defines its additional variables, that one word with its one byte, one nib and 4 bits will always be in the same place and accessible to all the slots. You can extend that to two or more words, with aliases if necessary. At times those variables can be used for other purposes when they are not needed for the data exchange purpose.

    I use scratchpad RAM a lot too. It holds variables that are not accessed as often or have to hold "permanent" values. The RAM is better for quick exchanges in passing commands and data, and also for the most frequently used "globals".

    oops, corrected typo, the byte, nib and bits are parts of the variable named "shared".

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 6/10/2009 6:22:28 PM GMT
  • YoshtiYoshti Posts: 108
    edited 2010-02-11 03:51
    Hi,
    Questions for the shared... I'm a bit lost here

    Can slot one hold Lets say" variable A,B,C,D
    And slot 2 will use only A,C (which I have to declare A,B,C anyway right?)
    Then Slot 3 use A,B,C,D then Add E

    Will that mess up my variables, or As long as I keep the order , I'm ok?
    Even If : I don't use them all in one of the slot, ;
    Or no matter what , Since slot 3 will use E, I should set it up in slot 1.

    I'm not sure to understand. But trying wink.gif

    Cheers
    Yosh
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-02-11 21:17
    Yosh,

    Adding the variable E in slot 3 will not mess up the variables so long as you keep them in the same order and also pay attention to the type. Type matters too, because PBASIC assigns WORDs first to locations in physical memory, then BYTES NIBS and lastly BITS. Even if a WORD is defined last in your program, it will still be allocated space before all the Bytes, Nibs and Bits.

    So, if your variables A, B, C and D are all Bytes, and slot 3 defines E also as a Byte, there will be no problem.
    ABCDE <-- in all slots same type
    But if EE is a Word instead of a Byte, then it will be slipped in before A,B,C,D in slot 3. If slot 3 WRITEs to EE, it will clobber the physical locations that the other slots know as A and B, and if slot 3 reads from A, what it will get is the data that the other slots put in C.
    But if EE is a word variable
    ABCDE <-- in slots 0,1,2
    EEABCD <--in slot 3

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • YoshtiYoshti Posts: 108
    edited 2010-02-11 21:54
    Hi,
    So best bet would be, to develop all slot and defined them in slot 0.
    My goal is to jump between slot 1 to 3 and at the end of slot 3 is to serout or debug, the output to the pc.
    Slot 0 is to set all pins and define them, and all variables used in slot 1 through 3.
    Then I jump from slot 1,2,3 then back at 1. At the end of 3, all useful values are then sent out.

    I'm not well verse in using a scratch pad, if there is one in the bs2p24 . I tried to understand...but would have to experiment.
    Its a stretch for me to use the scratch pad. Besides there is a lot in there I don' t get. wink.gif

    Thanks for your quick and well explained answer...there should be more people like you around me... blush.gif
    Cheers
    Yosh
Sign In or Register to comment.