Switching Program slots & Scratchpad memory
Sunzt
Posts: 9
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
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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
Post Edited (Zoot) : 3/26/2007 4:40:36 AM GMT
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!