The number of general purpose RAM variables which may be conventionally addressed is limited to 14 bytes, B0 - B13. I always find it amazing that I always seem to use up all of this and seem to need yet more
For the PICAXE-18X, there are an additional 48 bytes at locations $50 - $7F and yet another 48 at locations $C0 - $EF. These locations may be peeked and poked and thus effectively add 96 additional byte variables. Note that these locations are RAM. They are not flash memory locations where one might be concerned with frequent writes eventually destroying the flash location.
In program PeekPoke.Bas I tried to illustrate how to use the peek - poke capability to conserve general purpose RAM locations. The idea of the program is to time for five minutes and every 30 seconds to ripple some LEDs on the low nibble of the output pins. After this five minutes, there is a two minute quiet interval, and then the entire process is repeated.
Note that variables, Mi, Se, Index and CurrentPortB all share the same general purpose byte variable, B0. One additional byte, B1, was required as the LookUp command uses two variables.
In all honesty, I can't say this program is necessarily good programming as this program has only five variables and there really is no point to poke and peek if the number of variables may be accommodated in the 12-byte RAM expanse. The added peek and pokes adds to the amount of program memory used and they add complexity to the understanding of the program. However, I wanted to keep the example simple to illustrate a technique you may find useful when you find you are out of RAM locations.
Note that in implementing the nominal five minute loop, I used nested for-next loops and clearly Mi and Se must exist at the same time. However, the content of Mi in B0 is first saved using the poke command and B0 is then used for variable Se. If either 1 or 30 seconds have elapsed, the program redirects to DisplayArrow. Variable Se is then saved using the poke command and Index then uses B0. Index generates a Patt (B1) and Index is then saved using the poke command and variable CurrentPortB then uses B0. After outputting the pattern on the low four bit of the output pins, CurrentPortB is no longer required and Index is peeked into B0. Once the five patterns are output, the value of Se is peeked into B0 and when Se reaches 60, the value of Mi is peeked into B0.
Some confusion is eliminated by using names for the memory locations used to save the various variables. That is, MiSave has a bit more meaning than $50.
My suggestion is that when developing a complex project, do it in steps with several programs. Develop and debug each and don't use this peek - poke technique unless forced to. But, don't be terribly afraid to conserve variables.
Then as you are bringing the small programs into your final product, use this peek - poke technique, one step at a time. That is, don't try to pull five programs into one with all manner of peeks and pokes, only to find the program doesn't work. Rather, one step at a time.
Note that in this program, I opted to poke and peek only one byte as I needed the RAM location. A simple technique for a subroutine is to poke a number of RAM locations and on leaving the subroutine, peek these back into general purpose memory. For example;
GoSub ASub ... ASub: Poke B0Save, B0 Poke B1Save, B1 Poke B2Save, B2 now do whatever Peek B0Save, BO Peek B1Save, B1 Peek B2Save, B2 Return
' PeekPoke.Bas ' ' This program is internded to illustrate the use of the peek and poke commands ' to save general purpose RAM locations. ' ' For five minutes, every 30 seconds, an "arrow" is output to the low nibble of ' PORTB. This is followed by a two minute quiet interval. ' ' Note that four variables share the same memory location, B0. ' ' copyright, Peter H Anderson, Baltimore, MD, June, '04 Symbol Mi = B0 Symbol Se = B0 Symbol Index = B0 Symbol CurrentPortB = B0 Symbol Patt = B1 Symbol MiSave = $50 Symbol SeSave = $51 Symbol IndexSave = $52 Pause 6000 ' used during development Main: For Mi = 1 to 5 ' for five minutes Poke MiSave, Mi ' save Mi For Se = 1 to 60 SerTxD (#Se, 13, 10) ' used during development Pause 1000 If Se = 1 Or Se = 30 Then DisplayArrow Main_1: Next Peek MiSave, Mi ' restore Mi Next For Mi = 1 to 5 Poke MiSave, Mi For Se = 1 to 60 Pause 1000 Next Peek MiSave, Mi Next GoTo Main DisplayArrow: Poke SeSave, Se ' save Se For Index = 0 to 4 Lookup Index, ($01, $03, $07, $0f, $00), Patt Poke IndexSave, Index ' save Index Peek $06, CurrentPortB Pins = CurrentPortB & $F0 | Patt Peek IndexSave, Index ' restore Index Pause 250 Next Peek SeSave, Se ' restore Se GoTo Main_1