Extra Variables

TTOP

New Member
If all the Variables - b1 to b27 are used in a 08M2 or 14M2 and two more are needed, how can this be done or can it be done? In the manual it says "M2 parts have 8 word variables which are reserved for system hardware use. However if that piece of system hardware is not used within a program the variables may be used as general purpose variables." s_w0 How would this be used in a command? Also can a RAM location be used as a variable and can the value in Ram be incremented?
 

lbenson

Senior Member
As Rich says, push and pop may well be the cleanest way to get more variables now:
http://www.picaxe.com/BASIC-Commands/Advanced-PICAXE-Configuration/push/
http://www.picaxe.com/BASIC-Commands/Advanced-PICAXE-Configuration/pop/

Also see pushram and popram in the online "BASIC-Commands" page

You can use one of your named registers as a scratch register, and then use upper ram (above address 27 for the M2 devices):

symbol scratch=b27
symbol vExtraVar1=28 ' first byte above named variables
symbol vExtraVar2=29 ' 2nd byte above named variables

scratch = 1
poke vExtraVar1,scratch
scratch = 100
poke vExtraVar2,scratch
' now increment var1
peek vExtraVar1,scratch
inc scratch
poke vExtraVar1,scratch
' add 10 to var2
peek vExtraVar2,scratch
scratch = scratch + 10
poke vExtraVar2,scratch

(With X2 devices you can do the same thing with scratchpad using, for instance, the same symbol definitions, by replacing peek and poke with get and put.)

Yes, you can use the "extra" word variables, s_w1 - s_w6 (I'd reserve s_w0 ("task") and s_w7 ("time)).

You can also use the word variable bptr (and on x2 devices, ptr).

Many ways now to skin that cat.
 

westaust55

Moderator
It is also possible to re-use variables.
A variable use in one part of the program can be use for a different purpose elsewhere if the first value dos not need to be kept.

Using the bptr indirect addressing pointer allows access to all a available RAM memory locations starting with the lowest (= b0) through to the highest (b27 in your case) and beyond where there are no pre-defined (Bxx type) variables.

Folks here can give generic possibilities but without knowing exactly how you are using the existing variables or Ned to use the additional variables the solution needs your decision/assessment from the proffered options.
 

inglewoodpete

Senior Member
Possibly the most important thing about writing (relatively) large programs for (relatively) small chips is to structure your program carefully to suit your chip.

While you will have a main loop and some variables critical to that loop, there will be pieces of code that only get called periodically. These code segments are best placed in subroutines. If structured well, subroutines should have one entry point and one exit point (return statement). Subroutines are an ideal way to use variables for their duration and 'discard' their contents on exit. If you are still short on variables, then use the Poke/Peek commands on entry or a subroutine and then Peek/Poke on exit.
 
Top