Beginner: Use EEPROM to Store/Retrieve Integer Value
Chad1
Posts: 26
Hi all,
I'm trying to program what is essentially a high-score monitoring system (like in an arcade game). I have two double-digit 7 segment displays; one shows a "current score" and the other shows the "high score." For simplicity, let's say the user controls their score by button presses. Button 1 increments the user's score (win), and button 2 resets it to zero (lose). Of course all data is lost once the Propeller is powered down, but I'd like to save the "high score" number. My plan was to store the "record value" in EEPROM (256K) and then retrieve it upon power up.
I defined an array of longs called NUM. Each element in NUM is a 32-bit value which translates to a 2-digit 7SD number. Each time button 1 is pressed (win), a counter variable i increments and the 7SD displays the ith element of NUM (i =< 100). Variable i will reset to zero with power, so I have another variable MAX_Value that I want to always equal the appropriate element of NUM. I'm leveraging the Propeller Eeprom.spin object file (attached), so I know I'll be using the fromram and toram commands. My problem is I don't understand how these commands work. Can someone help me figure out the correct information to include within those two commands in order to carry out my goal of integer storage/retrieval?
Say the third element of NUM (the number 2) is a new high score and I want to store that info in EEPROM. My (likely incorrect) guess in red...
if ina[BTN1] == 1 'If WIN
|-i := i + 1 'Pretend it now equals 3 for this example and represents a new High Score
|-Display(long[@Num]) 'Pseudo code for driving the 7SD with a live score
|-if i > MAX_Value 'If a new record is set
|--|-MAX_Value := i
|--|-eeprom.fromram(MAX_Value, MAX_Value, $1F400) 'Choosing memory location to begin at 128K for safety
|--|-Display(long[@Num][MAX_Value]) 'Pseudo code for driving the 7SD with the new high score
Then say upon power up I want to retrieve the high score information (i.e. 3rd NUM element) and set it equal to MAX_Value. My blind guess...
--eeprom.fromram(MAX_Value, MAX_Value, $1F400)
--MAX_Value := MAX_Value 'This doesn't make sense! Does the above line already assign the value stored in EEPROM to MAX_Variable?
--Display(long[@Num][MAX_Value]) 'Pseudo code for driving the 7SD with the high score
Thanks in advance for the help!
Propeller Eeprom.spin
I'm trying to program what is essentially a high-score monitoring system (like in an arcade game). I have two double-digit 7 segment displays; one shows a "current score" and the other shows the "high score." For simplicity, let's say the user controls their score by button presses. Button 1 increments the user's score (win), and button 2 resets it to zero (lose). Of course all data is lost once the Propeller is powered down, but I'd like to save the "high score" number. My plan was to store the "record value" in EEPROM (256K) and then retrieve it upon power up.
I defined an array of longs called NUM. Each element in NUM is a 32-bit value which translates to a 2-digit 7SD number. Each time button 1 is pressed (win), a counter variable i increments and the 7SD displays the ith element of NUM (i =< 100). Variable i will reset to zero with power, so I have another variable MAX_Value that I want to always equal the appropriate element of NUM. I'm leveraging the Propeller Eeprom.spin object file (attached), so I know I'll be using the fromram and toram commands. My problem is I don't understand how these commands work. Can someone help me figure out the correct information to include within those two commands in order to carry out my goal of integer storage/retrieval?
Say the third element of NUM (the number 2) is a new high score and I want to store that info in EEPROM. My (likely incorrect) guess in red...
if ina[BTN1] == 1 'If WIN
|-i := i + 1 'Pretend it now equals 3 for this example and represents a new High Score
|-Display(long[@Num]) 'Pseudo code for driving the 7SD with a live score
|-if i > MAX_Value 'If a new record is set
|--|-MAX_Value := i
|--|-eeprom.fromram(MAX_Value, MAX_Value, $1F400) 'Choosing memory location to begin at 128K for safety
|--|-Display(long[@Num][MAX_Value]) 'Pseudo code for driving the 7SD with the new high score
Then say upon power up I want to retrieve the high score information (i.e. 3rd NUM element) and set it equal to MAX_Value. My blind guess...
--eeprom.fromram(MAX_Value, MAX_Value, $1F400)
--MAX_Value := MAX_Value 'This doesn't make sense! Does the above line already assign the value stored in EEPROM to MAX_Variable?
--Display(long[@Num][MAX_Value]) 'Pseudo code for driving the 7SD with the high score
Thanks in advance for the help!
Propeller Eeprom.spin
Comments
1) You need to provide address, not the parameter value. You do this by adding a '@' to the parameter name
Using this your code will be (not tested as I have no propeller handy):
Save MAX_Value
Read MAX_Value
This was exactly what I wanted to implement from the start, but I failed to locate the area in the manual which covers this. Could you please provide a quick example of how you would program this in spin, i.e. the commands/instructions required?
As I mentioned in this other thread you started about this same topic:
Also from the other thread:
The link I posted above shows how to do what JonnyMac suggested.
If you don't want to store to EEPROM, then you need to store to some other memory which won't change with a power cycle. You can't store things permanently to RAM you have to use EEPROM and the link I provided will show you how.
The reason there's a "+3" is to include the extra three bytes in the long sized variable "value".
You could do the same thing with: ·
If you had second long sized variable immediately after "value" you wanted to backup and restore, you'd use "+7" instead of "+3" in order to include the next four bytes in the transfer.
Let us know if you need clarification.
Edit: Both examples of the "Restore" portion of the code are not needed since the RAM will be automatically updated with the new value when the Propeller is rebooted (just as Jon suggested).
Make sure you use the @ sign. The methods use the address of the variable, not the value stored in the variable.
Pick any address in eeprom starting at $8000 if you have a 64k eeprom. If not then you can write to high areas in your 32k eeprom BUT the values will get nuked once you F11 program your Prop from the IDE. Programming the Prop writes over the full 32k, but does not write above 32k. So if storing values in upper 32k you will need to re-write your preferences after any F11. It is best to use a 64K eeprom and not deal with the hassles. Then you have a full 32K to play with from $8000 - $FFFF addresses.
Here's how I do it using my own EEPROM object.
This would save a long variable (someVariable) to the EEPROM to the same address it occupies in RAM. This will cause it to be auto-loaded on subsequent reboot cycles. Of course, you have to start the EEPROM object -- to connect to the boot EEPROM you'd do this:
As I said before: easy-peasy.
Thanks for your time and help; I learned a lot from your explanations.
JonnyMac,
Thanks so much for your explanation and your object file. Everything works perfectly and you're right -- it was very easy!
No, it's not checked when the Prop boots. I think it's only checked when a new program is loaded from the PC.
Changing the EEPROM from within a program is very common. I've done it many times myself without any problems.