Shop OBEX P1 Docs P2 Docs Learn Events
Switching Program slots & Scratchpad memory — Parallax Forums

Switching Program slots & Scratchpad memory

SunztSunzt Posts: 9
edited 2007-03-26 05:59 in BASIC Stamp
Hello,

I'm using a POLLRUN command to run some code in Slot 1 after a POLLIN. In slot 1's code I have a "Run 0" command to return to the main code in slot 0.

When I return to the program in slot 0 will it start at the beginning of the code from line 1 or will it just start where it left off?

I want to be able to keep the value of a counter variable after I switch programs, but I initialized the variable to a value in the beginning of program 0. Is there a way to keep the value after switching programs?

Here's example code of what I have:


' {$STAMP BS2px, kill.bpx}
' {$PBASIC 2.5}
'*********** slot 0***************

count VAR Word

count = 0
PUT 100, Word count

Main:
POLLIN 0, 1
POLLRUN 1
POLLMODE 3

Do
'
'
GET 100, Word count
count = count +1
PUT 100, Word count
'
'
LOOP




'************* Slot 1 code *********
' Do stuff, but doesn't modify count
RUN 0

Post Edited (Sunzt) : 3/26/2007 4:23:27 AM GMT

Comments

  • ZootZoot Posts: 2,227
    edited 2007-03-26 04:28
    Whenver you change (run) a slot, the code always starts at the beginning and all variables will be shuffled around in their "real" registers unless you declare the variables in the same order on every page.

    See Dr. Tracy Allen's excellent write-ups on multi-slot program management (task managers) at his website: emesystems.com/BS2SX.htm#variables. His discussion is applicable to all the multi-slot Stamps.

    The short version is that if you want to run different slots, and if you are using polling esp., you need to track what to do next. Super simple example:

    - the variable flag is set to 0
    - at some point you change slots because of a pollrun
    - the program in the pollrun slot runs a bunch of code
    - that same pollrun slot code sets the flag to 1 --- this is a signal to your main program, say at slot 0
    - run slot 0
    - the beginning of slot 0 must have code like:
    IF flag = 1 THEN SomeLabelThatIsYourPostPollRunCode
    'Main program here

    Most of the time (and on reset) the variable flag = 0, so the PostPollRunCode never runs... it only runs if the pollrun program sets it. This is also advantageous because you can switch to other slots and back to the main program and never run that code.

    Is this helpful?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • ZootZoot Posts: 2,227
    edited 2007-03-26 04:36
    Actually, in looking at your code, you have two things going on. You don't really need to worry about the VAR so much if you are using the scratchpad -- the scratchpad is stable and doesn't change. You might even just call your var here "temp" or something -- it's a word variable that you can use and reuse over and over since you're storing your real count in SPRAM. Regardless, you always need to declare variables in the same order in every slot so that they remain stable when you switch slots.

    ' {$STAMP BS2px, kill.bpx}
    ' {$PBASIC 2.5}
    '*********** slot 0***************
    'declare vars in exact same order in every slot even if you don't use them!
    count VAR Word
    flag VAR Bit
    
    Reset:                     'you actually might not need this -- on reset all spram and vars will = 0
    'count = 0                 'if you do need to init this var manually, you will need extra bits for your task manager
    'PUT 100, Word count  'or it will get reset to 100 every time this slot is run
    
    TaskManager:
    IF flag = 1 THEN PostPollRun
    
    
    
    Main:
    POLLIN 0, 1
    POLLRUN 1
    POLLMODE 3
    
    Do
    '
    '
    GET 100, Word count
    count = count +1
    PUT 100, Word count
    '
    '
    LOOP
    
    
    PostPollRun:   'this code only runs after a pollrun
       flag = 0    'reset the flag
       'do some more stuff
       GOTO Main      'or whatever you want to do next
    
    
    
    
    '************* Slot 1 code *********
    ' must declare vars in EXACT SAME ORDER
    count VAR Word
    flag VAR Bit
    
    ' Do stuff, but doesn't modify count
    flag = 1
    RUN 0
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    Post Edited (Zoot) : 3/26/2007 4:40:36 AM GMT
  • SunztSunzt Posts: 9
    edited 2007-03-26 05:59
    wow thanks so much for the help. You're great! I declared the variables in the same order in both slots like you suggested.

    In regards to the flag bit thing, I realized after reading your suggestions that I could do something similar to what you said except that I can do it with some NVRAM that I have. I simply declared wrote address 0 in the NVRAM to "Z" to indicate that the program has been initialized. I would then read in that address at the start of my program. This makes it so I don't need to declare another variable and it also works if my chip accidentally reset.

    Thanks again!
Sign In or Register to comment.