Shop OBEX P1 Docs P2 Docs Learn Events
Run Initialization code only on power-up — Parallax Forums

Run Initialization code only on power-up

ediedi Posts: 19
edited 2005-09-09 19:43 in BASIC Stamp
How to run Initialization Code only on Power-Up & not when switching slots.


e.g.



Slot 0 code :

Initialization Code
.
.
.
Main
· Do
··· .
··· .
··· if (blah...blah) THEN
····· · RUN 1
··· ENDIF
··· .
··· .
· Loop



Slot 1 code :
.
.
.
.
.
RUN 0



When slot 1 returns control to slot 0, the initialization section is run again.
I do not want that to happen·....... how is this avoided ?

Thanks in advance .......

·

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-09-04 09:07
    edi -

    Set a switch in EEPROM after you've done the initialization. In the beginning of the Initialization code, check to see if that switch is set. If not, do the initialization. Just before you end the program, you can reset the switch if you need to, depending on how your program terminates.

    Regards,

    Bruce Bates
  • OrionOrion Posts: 236
    edited 2005-09-04 15:53
    Use the Scratchpad memory, its reset to all zeros upon power up. If you use eeprom you will have to set it to zero as it holds it's contents when power is lost.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-09-04 16:33
    Orion -

    Just to expand this topic a bit, and add a bit more clarification, I offer the following.

    Your method will only work for those Stamps which supports scratchpad memory. The method I suggested will work with any Stamp platform. Obviously the questioner here IS using a Stamp which has scratchpad memory (since it has multiple programming slots).

    Your method is limited to one-time-after power-up or reset. Mine will work for that case, as well as one-time-only. ever.

    By way of example, this latter would be the case if clearing an external EEPROM the first, and only the first time, or setting a non-voltile RTC chips to the correct time/date initially, when the program is first run. So too some sensors may need to be first-time calibrated, just on the initial iteration of the program, and never again.

    Either or both methods may be correct, depending on the exact circumstances, and how or when "first time only" is defined.

    Regards,

    Bruce Bates
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-04 17:00
    Bruce is write, the EEPROM method works with any BS2 module.· Here's how you actually implement that strategy:

    RunFlag····· DATA····· $FF

    Reset:
    · READ RunFlag, checkVal
    · IF (checkVal = $FF) THEN
    ··· ' do one-time initialization
    ··· WRITE RunFlag, 0
    · ENDIF

    When you download this program, the location called RunFlag will contain $FF.· This is read during the Reset section.· If $FF is detected, you run your initialization code then write 0 back to the RunFlag location.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-09-04 18:52
    I use an ordinary variable for that purpose.

    e.g.

    Slot 0 code :
    BRANCH inBank,[noparse][[/noparse]initialzation, main]    ' inBank=0 at power up, but bank 1 code sets inBank=1
    Initialization:
    'blah blah
    '... 
    Main
      Do
        if (blah...blah) THEN
            RUN 1
        ENDIF
      Loop
    -------------------------------------
    Slot 1 code :
    'blah blah
    inBank=1    ' <-- set up branch point for bank 0
    RUN 0      ' will jump to Main in bank 0
    
    



    That works if you want to run the initialization at every power up. On the other hand, as Bruce and Jon have pointed out, if you store the flag in eeprom, the initialization is run only once when you first load in the program. There are variations on eeprom theme too, for example, you can either store the eeprom flag in a location where it will be reinitialized every time you reload the program code, or you can sequester the eeprom flag in a location (like bank #15 on a bs2pe, or in an unallocated eeprom block), where it will take a special effort to rerun the initialization. Both kinds of initialization can be useful. The ram-based kind for ordinary bank swapping, the more durable kind for storing things like device serial numbers and one-time parameters.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • OrionOrion Posts: 236
    edited 2005-09-04 20:35
    Maybe I was reading between the lines.

    "Initialization Code only on Power-Up"

    I'm assumed that meant running the init code once on each power up. Not running init only once even after another power up / reset.

    Something like running the code to init a LCD display that needs to be done each reset. I see the value of the other for senor cal or setting RTC.
  • ediedi Posts: 19
    edited 2005-09-08 17:37
    Hi All,

    Thanks for your inputs.

    I think that using a (Scratchpad) RAM bit in the BS2e as my "init-code-run" flag will work the best for my application. I do want the init code to be run at every power up (and·also after a hardware reset - although not stated in the first posting). This flag is cleared on power-up (& after hardware reset ???). Hardware reset means actually pushing the reset button on my super carrier board.

    I want to use a Scratchpad RAM bit, however the BS2 manual says that the RAM will
    be cleared after reset ...... does this mean that both the main Registers + Scratchpad RAM OR does it just mean the main Registers.

    Can this specification of RAM reset at power-up be relied upon in future BS2's or is it left up to the whim of the manufacturer of the microprocessor chip (i.e. ubicom, microchip, etc.) ??

    Regards,

    Richard


    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-08 18:20
    If you want to initialize after a power-up or external reset then you don't have to do anything special.· The BS2 interpreter handles the clearing of RAM -- this is done by us and not "left up to the whim of..." well, anyone.· It's deliberate.

    If you look at any of my programs I usually have a section above the main body of code like this:

    Reset:
    · ' this code runs after a power-up or device reset

    Main:
    · DO
    ··· ' main program code here
    · LOOP

    You see, there is nothing special to do -- by having the reset code available at a program label the program logic can actually send it back if the some condition dictates that necessary.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • ediedi Posts: 19
    edited 2005-09-09 19:43
    Hi Jon,

    Thanks for clarifying what is responsible clearing RAM at power-up/reset. I.e that it
    is deliberate & done by BS2 interpreter. This is what I needed.

    Regards,

    Richard

    PS - Thanks Mr. Allen, Mr. Bates, & Orion (alphabetical order) for helping me to narrow
    ······ down my chioces on how to solve my problem.
Sign In or Register to comment.