Shop OBEX P1 Docs P2 Docs Learn Events
How do you recycle VAR I need to free up some RAM in a Basic Stamp (Resloved) — Parallax Forums

How do you recycle VAR I need to free up some RAM in a Basic Stamp (Resloved)

sam_sam_samsam_sam_sam Posts: 2,286
edited 2009-08-05 22:05 in General Discussion
Hi EveryOne

This is a project that I have been ·ask to do·by employer

As of right now I can not give all of the details.......................
................·but when the Project is done I will share with all of you on the Forum


I know that I have·This a another Post but this is a different question
http://forums.parallax.com/showthread.php?p=804156

http://forums.parallax.com/showthread.php?p=804554



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

·
·
·
·
Sam

Post Edited (sam_sam_sam) : 8/5/2009 2:09:55 AM GMT

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2009-08-04 23:40
    VAR word workVal


    Use workVal in all your routines where the value does not need to be saved. Even when it does need to be saved you can copy it.

    importantValue = workVal

    Aside from optimizing your code or using a different Stamp, I don't know what else you could do.

    Rich H
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-08-05 00:08
    VAR word workVal········ ·'This will not compile am i missing something in what you have here

    importantValue = workVal

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • W9GFOW9GFO Posts: 4,010
    edited 2009-08-05 01:25
    Oops! It should be;

    workVal VAR word

    Rich H
  • phil kennyphil kenny Posts: 233
    edited 2009-08-05 01:47
    Zoot helped me with this same topic.

    See

    http://forums.parallax.com/showthread.php?p=696777

    phil
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-08-05 02:06
    phil

    Thank For sharing that post link

    Time···· ·VAR· ·WORD
    S_Time·· VAR·· Time

    There is one thing about using this is that

    1·· You have to have them neat to each other or you get an error need a·label· for one of them ·S_Time or Time
    ··· ·I can not remember which gave the error

    2 You can only use this if that VAR is not need in two places at one time

    This will work if let say that you want to set a CON on the fly and use a switch to set the CON· and you go back to you main routine then this will
    work

    Time· VAR used in one place then you want use the S_Time VAR two command line down from the first one
    ·· I found out the Hard Way· this dose not work

    ·It also warn you about this in the on-line help




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 8/5/2009 2:18:15 AM GMT
  • phil kennyphil kenny Posts: 233
    edited 2009-08-05 03:05
    Hello Sam,

    Perhaps I mislead you. You can't free up RAM in the BS2. The use
    of aliasing (giving the same memory location two names) is of more
    use with the multi-slot Stamps such as BS2P, BS2x, and BS2Pe.

    Zoot was trying to demonstrate two things:

    1) create a Byte array named Time

    2) Using an alias to access the first byte in that array:

    ' here is another common example for timekeeping bytes -- 
    ' you can access individual elements by name, or write to the array:
    
    time VAR Byte(10)
    secs VAR time  ' alias to first "element", aka time(0)
    



    The variable Time must be defined first. The alias secs does not have
    to be defined next. It may be defined anywhere later in the code.

    Multi-slot Stamps have much more program space, but they still have
    only 26 bytes of RAM. However, they also have a scratchpad memory
    which can be used to gain space for your variables, but the process
    isn't straight forward.

    Tracy Allen has an excellent discussion of how to make use of the
    scratchpad memory at:

    www.emesystems.com/BS2SX.htm

    phil
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-08-05 03:11
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    'Sand_Silo_Test_Demo
    RC              PIN     15
    ' -----[noparse][[/noparse] Var ]-------------------------------------------------------
     
    
    PD              VAR     Word
    RC_Set          VAR     PD
    SL              VAR     Word
    Seconds         VAR     Word
     
    secs            VAR     Byte
    mins            VAR     Byte
    hrs             VAR     Byte
    J               VAR     Byte
    N               VAR     Word
    B               VAR     N
    D               VAR     Word
    F               VAR     Word
    x               VAR     Word
    'RC_Set          VAR     Word
    
    RC_Diff         CON     600
    RC_Max          CON     613
    RC_Min          CON     13
     
    
     DO
     
     IF IN0 = 0 THEN set
     
     IF SL = 50000 THEN EXIT
     
    
    'DEBUG CLS
    DEBUG HOME, DEC RC_Set, TAB, "1.", DEC3 X, CLREOL, CR,
               "PD", DEC5  PD, CR, "SL", DEC5  SL, CR, "B", DEC5  B, CR,
                DEC5 ? seconds, CR, DEC2 ? secs, CR, DEC2 ? mins, CR, DEC2 ? hrs, CR
     
     
    seconds  = seconds + 1
    secs = secs + 1
     
    SL = seconds + (seconds ** X)    ' x = a value some thing like 28626 that done with the SET routine   
    PD = Seconds  + (seconds ** X)       
    B = seconds + (seconds ** 28626)
     
    IF secs = 60 THEN
     Secs = 00
     Mins = Mins + 1
     
     IF Mins = 60 THEN
     Mins = 00
     Hrs = Hrs + 1
     
     ENDIF
     ENDIF
     
    LOOP
     
    END
    
    set:
     
    RC_Set = PD
    B = N
     
    DO WHILE IN0 = 0
    
        Main:
        HIGH RC                                            ' charge the cap
        PAUSE 1                                            ' for 1 ms
        RCTIME RC, 1, RC_SET                               ' measure RC discharge time
        PAUSE 50
     
     N = RC_set - RC_min MAX RC_diff  ' binary division loop
     D = RC_diff
     
     
     
    FOR J=15 TO 0               ' 16 bits
    N=N//D<<1                   ' remainder*2
    F.BIT0(J)=N/D               ' next bit
     
    NEXT
     
     DEBUG HOME, DEC RC_Set, TAB, "1.", DEC3 X, CLREOL
    
     x = f ** 1000
     
      LOOP
    
      RETURN
    

    In my case I have a routine that has two routine one routine runs when the switch =·1
    the other runs when the swich·= 0

    This allow me·to set a value that is need in the main routine

    This is not·ALL of the code routines and not all of the VAR are listed in this Demo code and as you can see that these two·routines use this much RAM··the SET·routine ·take 5 WORDs and 1 BYTE


    To add the SET routine with the compled code routine that I am using it put me 3 WORDs over so now what am I going·to do is reuse some VAR
    that are not need in the SET routine that are only need in the main routine


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 8/5/2009 3:48:00 AM GMT
  • W9GFOW9GFO Posts: 4,010
    edited 2009-08-05 05:56
    You might be able to free up one word if you get rid of the secs and mins variables.

    You can divide seconds by 60, if no remainder then one minute has passed. Same with dividing by 3600, then one hour has passed.

    It looks like PD is the same as SL which is the same as RC_set

    Does X always equal f ** 1000, if so, you don't need x.

    It seems really difficult to me to try to reduce you VAR space without knowing how those VARs are used in other parts of your code.

    Rich H
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2009-08-05 07:02
    The following rewrite of the set routine dispenses with several of the variables that need not be kept (N, D, F). At the end it saves the multiplier x as DATA, so that it can "stick" after a restart or power cycling. All the variables used in the set routine, except x are free again after the set routine finishes. Note that your program does a "GOTO set", but the set routine has a RETURN at the end. Use GOSUB instead of GOTO.

    PD  VAR Word    <---- reusing these two variables temporarily, alias name them if it makes you feel better
    x  VAR Word
    J   VAR Nib   <---- note J only has to be a NIB
    X0ee  DATA Word 28626
    RC_Max          CON     613
    RC_Min          CON     13
    RC_Diff         CON    RCmax - RCmin   <---- define this in terms of the two other constants that come from testing the adjustment range
    
    
    set:
      DO 
        HIGH RC                                            ' charge the cap
        PAUSE 1                                            ' for 1 ms
        RCTIME RC, 1, PD                             ' measure RC discharge time
        HIGH RC
        DEBUG HOME, DEC PD, TAB  <---- show this value now, going on to change PD
     
         PD = PD - RC_min MAX RC_diff  ' Make sure PD falls within range of the CONstants
     ' ---- binary division loop ----
        FOR J=15 TO 0               ' 16 bits   NOTE:  J only needs to be a NIB
          PD=PD//RC_diff<<1                   ' remainder*2 <--- note the divisor is constant, PD dividend is reduced directly
          x.BIT0(J)=PC/RC_diff               ' next bit   <- using x for the quotient
         NEXT
     
         DEBUG DEC x, TAB, "1.", DEC3 x** 1000, CLREOL, CR  ' <--- show ** multiplier and decimal equivalent
      LOOP WHILE IN0 = 0
      READ X0ee, Word PD   ' <--- now using PD for the old multiplier
      IF PD <> x THEN WRITE X0ee, Word x  ' <--- write new multiplier if it is different from old
      RETURN
    



    The main code should start off, when in0=1, with a READ of x,
    READ x0ee, Word x   ' <---- x multiplier value is stored in eeprom from calibration set step
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-08-05 21:41
    Tracy Allen

    ·Reusing·these·two·variables·temporarily,·alias·name·them·if·it·makes·you·feel·better
    I would feel better if I used· alias·name·it would be easyer for to know what VAR ·I am using and what it is·for
    ·I did think about just using what VAR not being used at that with out changing the name


    Thanks for sharing this the way you have this help me·understand more about what is happing when


    ···PD·=·PD·-·RC_min·MAX·RC_diff·················································· '·Make·sure·PD·falls·within·range·of·the·CONstants
    ··················································· '·----·binary·division·loop·----
    ····FOR·J=15·TO·0····································································· ·'·16·bits···NOTE:··J·only·needs·to·be·a·NIB
    ······PD=PD//RC_diff<<1··························································· · ·'·remainder*2·<---·note·the·divisor·is·constant,·PD·dividend·is·reduced·directly
    ······x.BIT0(J)=PC/RC_diff·····························································'·next·bit···<-·using·x·for·the·quotient

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2009-08-05 22:05
    Hi sam_sam_sam,

    I'm glad our forum gurus took good care of you! Just FYI for others who read this post:

    Two techniques to save variable space are introduced in the Stamps in Class text Applied Robotics with the SumoBot·(a free download). One is·the use of a multipurpose·"temp" variable, and the other is the use of a single byte·variable as a flag register·to quickly·evaluate and act upon the state of·6 sensors.

    Have fun,

    -Steph Lindsay

    Editor, Parallax Inc.
Sign In or Register to comment.