Shop OBEX P1 Docs P2 Docs Learn Events
Variable Sharing between slots — Parallax Forums

Variable Sharing between slots

GuidoGuido Posts: 195
edited 2007-01-04 10:22 in BASIC Stamp
I was under the understanding that you could share a Variable between slots. If I save a word variable in slot one using the the PUT command and read it on slot 0, or use the same word variable in Slot·1 again using the Put command, I seem not to be able to read the value in slot 0. Each program would run, Slot 0,Slot 1,and Slot 2. Each Word Variable is Put (*)·and debugged in the same program....Seems it does not like to have this variable shared.

Comments

  • bytor95bytor95 Posts: 53
    edited 2007-01-02 14:43
    Say X is a word stored in any slot and its value is 63106

    Store X in scratchpad RAM like this:


    PUT 3,x.LOWBYTE:PUT 4,x.HIGHBYTE


    Yes it's that simple! This stores the value of X in Scratchpad ram locations 3 and 4 for example.

    Retrieve X like this from any other slot or the current slot:



    GET 3,lowpart:GET 4,highpart: X=HIGHPART*256+LOWPART

    'x will equal 63106

    Can't get any simpler right?

    Good luck. I just figured that out over the weekend so I had to share it with you. I'm not sure if there's even a better way to exchange word variables between slots but the above method works and is very intuitive to understand.


    Engin
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-01-02 14:44
    Typically, "variable sharing" refers to declaring variables in the same order between slots. A "Variable" refers to one of the 'register' locations of the PIC chip. If you declare them in the same order in two slots, then when the second slot refers to the same variable referred to in the first slot, it will refer to the same 'register' location.

    When you "PUT" a 'variable', I believe you save that variable in the 'scratchpad' memory of the PIC. This is a different topic than what Parallax means when they say "sharing variables between slots". I'm sure there's a technique that can work, however.

    I believe, only the BS2p and BS2pe have 'scratchpad' memory.
  • bytor95bytor95 Posts: 53
    edited 2007-01-02 14:49
    BS2x has it, that's what I'm using.

    Engin
  • GuidoGuido Posts: 195
    edited 2007-01-02 22:41
    Allan,

    I think your right about what you are saying. Basically I am running a DS1620, in two seperate slots. I am trying to save (PUT)·TempF,Low temp, High Temp·and display them in Slot 0. I assume since the stamp program is line by line that there should be no problem, but there is. Like you said it displays the first slot on Low temp and High temp.

    PUT 0,TEMPF.LOWBYTE:PUT 1,TEMPF.HIGHBYTE
    PUT 2,TL.LOWBYTE:PUT 3,TL.HIGHBYTE
    PUT 4,TH.LOWBYTE:PUT 5,TH.HIGHBYTE

    ....Any Ideas or is this not possible.

    Thanks

    Guido
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2007-01-02 23:45
    Guido,

    You can also use the PBASIC 2.5 syntax,

    PUT 0, WORD tempf ' uses locations 0 and 1
    PUT 2, WORD tlo ' 2 and 3
    PUT 4, WORD thi ' 4 and 5

    That amounts to the same thing, but it saves you some typing. On the other end,

    GET 0, WORD tempf
    GET 2, WORD tlo
    GET 4, WORD thi

    One common mistake//bug is using a READ where it should be a GET. Not what you want! devil.gif

    The BS2sx and BS2e each have 63 bytes of user scratchpad (adress 0 to 62), while the BS2p, BS2pe and BS2px each have 127 bytes (address 0-126). The original BS2 does not have any scratchpad.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • GuidoGuido Posts: 195
    edited 2007-01-03 00:25
    Tracy,

    Thanks for the reply, I was using the GET statement and actually was using the PBASIC 2.5 style you listed....I just had to try low and High byte to see if I was reading it wrong. I have to run,but I will post the code tomorrow and see if anyone can figure out what this brainless Italian is doing wrong.....Also how can you run Inialize only one time when running slots....Setting TH=000 and TL=999...

    Thanks Again Tracy!

    Guido
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-03 15:08
    Guido,
    ·
    ·· When switching slots if you don’t want to run certain initialization code you can set a flag within the initialization code (or even an SPRAM location) then upon entry into the slot you have a BRANCH command (or IF…THEN) which bypasses that code if the flag is set.· I hope this helps.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • GuidoGuido Posts: 195
    edited 2007-01-03 22:57
    Thank You Chris,
    I thought about using a Flag, but it seemed to me that when I went back to a certain bank it would clear the Flag. But like you said I could save it in scratch pad.
    Thanks Again
    Guido
  • NewzedNewzed Posts: 2,503
    edited 2007-01-03 23:14
    Guido, I jump between banks all the time, almost like a GOSUB.· Suppose you have a routine in Bank 0 called WRITEIT, and you have a routine in Bank 1 called READIT.· At the very beginning of Bank0 write:

    if com = 1 then writeit

    Then somewhere in your program you want to jump to READIT in Bank 1.· You would write:

    com = 1
    RUN 1

    At the very beginning of Bank 1 you write:

    if com = 1 then readit

    At the end of READIT you write:

    com = 1
    RUN 0

    I have a program with six banks and I can jump to a precise routine in any bank and then return to a precise point in any other bank.

    I hope you understand what I have said.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Don't have VGA?

    Newzed@aol.com
    ·
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-03 23:46
    Guido,
    ·
    ·· The variables (flags) are only cleared if you don’t declare the variables the same in each slot.· Of course, they’re not really cleared, just re-mapped when you don’t declare them the same.· So definitely copy and paste your variable declarations between slots.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • GuidoGuido Posts: 195
    edited 2007-01-03 23:59
    Ok, Maybe I am getting more confused. I though with using slots I could not go back to a certain location or subroutine in another bank. Basically you had to run the complete program when you jump back from a slot. Here is what I am dealing with, and yes it is the first time trying to work with slots.

    Thanks

    Guido
  • GuidoGuido Posts: 195
    edited 2007-01-04 01:56
    Also, I do realalize that I am saving th and tl on the slot 1 and two and calling them up on slot 0, But I was trying to get the proper information to display, In other words just playing around
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2007-01-04 02:22
    Your program wants to use slot 1 to acquire outside temperature, slot 2 to acquire indoor temperature, and slot 0 to display the data. Mi despiace Guido, there are several things wrong with the program logic.

    The first 5 word variables in all 3 slots are the same. That is good:
    tempIn VAR Word ' RAW TEMPERATURE
    tempC VAR Word ' Celsius
    tempF VAR Word ' Fahrenheit
    TL VAR Word ' LOW TEMPERATURE WORD
    TH VAR Word ' HIGH TEMPERATURE WORD

    Slot 1 reads the outside temperature and populates all 5 of those varaiables. At the end of slot 1 there is a RUN 2 command, whereupon the code in slot 2 reads the other DS1620. But it reuses exactly the same variables as slot 1, so the readings that were acquired in slot 1 are lost. Also, nothing is ever PUT into the scrachpad ram. Now at the end of slot 2 there is a RUN 0 command. Slot 0 is evidently where the data display is to take place. When slot 0 starts up, the temperatures that were acquired in slot 2 are still held in the above 5 variables. However, the slot 0 code GETs bytes from SPRAM locations 0, 1, 2, and overwrites the results that were stored there by the temperature measurement in slot 2. The code does some math, and displays the results (expecting them to be the two temperatures?), but not. Never was any reading PUT into the scratchpad.

    Guido, I think maybe you have a confusion between the scratchpad RAM and the main Stamp RAM. They are two completely different memory spaces.

    Exercise 1:
    Try this simplified version:
    Main:
    DO:
       DEBUG HOME
       GOSUB DIS    ' do not call "convert", which is redundant (already done in slot 2)
       RUN 2      ' <--- skip 1 for now, ping pong back and forth between slots 0 and 2 for now
    LOOP
    
    


    It will give the wrong answer the first time through, but the second pass and subsequently, it should display the temperature readings acquired in slot 2 from the indoor sensor. No GET or PUT involved. The main RAM variables hold the consistent data between slots.

    Once you have that working, you can move on to include slot 1 in the loop, 0->1->2->0... To do that will require that you either set aside additional unique variables in the main RAM for outdoor and indoor temperatures, or else define a word data buffer in the scratchpad RAM to hold the outdoor and indoor readings.

    Is that making any sense now? I didn't actually check over the DS1620 logic carefully. Does that work okay?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-01-04 10:22
    Sid -

    I use your technique but taken to another level if you will. The reason why I like to do it the way I do is because it's nearly self-documenting, it compacts some potentially bulky code and it's easy to use on a program-to-program basis:

    'Common Variables: Copy EXACTLY into the beginning of each slot used!

    Return_Code VAR BYTE
    R_C VAR Return_Code
    Iternations VAR NIB
    ...etc. ...

    Return_Code = 255 ' Defaul location set

    Primary_Loop:

    'Return_Code (R_C) can be set in this slot or any other slot to modify the future program execution

    ON R_C (RTN0. RTN1, RTN2, RTN3, RTN4, RTN5, RTN6, RTN7, RTN8, RTN9, RTN10)
    'Errors and Default drop through above instruction

    Default_RTN:
    ...
    'Initialization code
    ...

    RTN0:
    ...process...
    R_C = 4 'Set to return to RTN4 on next run through

    RUN 1
    'NO RETURN HERE
    ...

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
Sign In or Register to comment.