User-adjustable parameters used in calculations

reywas

Member
I'm working on a subroutine to allow the user to step through several program parameters, adjust them, and store them to EEPROM. The initial default values will be written to EEPROM when compiled. All of these parameters are used in calculations within the program. The problem I have is a shortage of available variables (w0-w13) to assign the data to. Is there a way to read a value from EEPROM and assign it to a constant?

Any advise on strategies to accomplish this would be most welcome.
 
Last edited:

lbenson

Senior Member
You haven't said which chip you're using, but w0-w13 implies the M2 series. By moving to, say, a 20X2 you get w0-w27.

On the M2 parts you can use the ram above w0-w13 (starting with address 28). Bytes up there can be addressed using bptr. Words can be retrieve/saved with peek and poke.

So, for instance, using w13 to transfer variables, you can say

#picaxe 14m2

w13 = 1000
write 28, word w13
read 28, word w13
sertxd(#w13,cr,lf)

poke 28, word w13
peek 28, word w12
sertxd(#w12,cr,lf)

You can provide constants for your variables in upper ram:

Symbol rSomeVarAddr= 28
symbol rNextVarAddr = rSomeVarAddr+2 ' or 30
w13 = 1000
poke rSomeVarAddr, word w13
peek rSomeVarAddr, word w12
sertxd(#w12,cr,lf)

I prefix constants used as addresses in upper ram with "r" to remind myself what they are for. Note that you have to retrieve these into the w variables to do calculations. You can achieve the same by loading the w variable from the higher parts of eeprom.

If you just need a few more variables, you can use s_w1 through s_w6 (see page 13 of Manual 2).
 

techElder

Well-known member
Another part of creating editable code is to us SYMBOLS for you constants.

I seldom hard code a constant value (like "28") into a program. I'll always create a symbol for these kind of constants. It makes changing an address in RAM so much easier.

For instance, if I had to use a buffer of 32 bytes of memory starting at "28" and then another 32 bytes starting right after that I would use something like this:

Code:
symbol BUFFERLENGTH = 32
symbol BUFFER1START = 28
symbol BUFFER2START = BUFFER1START + BUFFERLENGTH

' ...
write BUFFER1START, word w13
read BUFFER1START, word w13
'...
write BUFFER2START, word w13
read BUFFER2START, word w13
'...
Then if you wanted to change memory locations, and you had 34 instances of the above code in your program all you would have to change is in the SYMBOL definitions.
 

inglewoodpete

Senior Member
The M2 PICAXEs also have another 6 unused word variables available. Go to the command manual pdf file and, under "Variables - System", look for sw_1 to s_w6. You can give them alternative names with Symbol directives, making them just like other word variables.
Code:
[color=Navy]#picaxe [/color][color=Black]08M2[/color]
[color=Blue]Symbol [/color][color=Purple]MyWord     [/color][color=DarkCyan]= [/color][color=Purple]s_w1[/color]
[color=Blue]Symbol [/color][color=Purple]YourWord   [/color][color=DarkCyan]= [/color][color=Purple]s_w2[/color]
 
Last edited:

hippy

Technical Support
Staff member
One trick is to use a SYMBOL to define where the constant is in EEPROM, have a macro which reads that into a byte or word variable depending if byte or word, another which writes back to EEPROM. For example -

Code:
Symbol TIME_CONSTANT     = 0 ; Word 1:0
Symbol MULTIPLE_CONSTANT = 2 ; Byte 2

#Macro LoadWord( wvar, address )
  Read address, Word wvar
#EndMacro

#Macro SaveWord( wvar, address )
  Write address, Word wvar
#EndMacro

#Macro LoadByte( bvar, address )
  Read address, bvar
#EndMacro

#Macro SaveByte( bvar, address )
  Write address, bvar
#EndMacro

LoadWord(w1,TIME_CONSTANT)
w1 = w1 + 1
SaveWord(w1,TIME_CONSTANT)

LoadByte(b9,MULTIPLE_CONSTANT)
b9 = b9 + 1
SaveByte(b9,MULTIPLE_CONSTANT)
 

BESQUEUT

Senior Member
The problem I have is a shortage of available variables (w0-w13) to assign the data to. Is there a way to read a value from EEPROM and assign it to a constant?

Any advise on strategies to accomplish this would be most welcome.
You can also use push/pop to save some variables...

IE : use only one variable to push few values within the SUB.
 

stan74

Senior Member
You can't use if peek x = peek y then..pity. Push ram could be useful for a gosub. I ran out of variables on a 28x2, 2 subs for a GLCD and eeprom for chars and 1024K for screen ram.
 
Top