Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

reading writing to eeprom

Status
Not open for further replies.
You forgot at least one thing.
Wanna bet?

A hint, when you detect that no score has been stored (location 0 <> 0x77) then you write zero as the lowest score, how can anyone get lower than zero?

Mike.
Edit, at least that's what I think the code is doing.
 
Sigh.

Reading and writing to the EEPROM in the first subroutine looks ok:

Code:
  EE.Read(1,LastLOWScore)              //read the eeprom
    If Prev_score < LastLOWscore Then   //COMPARE LOW AND PRESENT SCORE ON EEPROM
        EE.Write(1,Prev_score)
        cls         //write previous lowscore to eeprom
        writeat(1,1,"last score",Prev_score)   //susposed to show what Prev_score is (write to eeprom
        delayms(2000)
    EndIf

But what the heck are you trying to do in the second subroutine?!???

Code:
    EE.Read(0,tmpByte)
    If tmpByte = $77 Then
        EE.Read(1,LastLOWScore)      //READ LASTLOWSCORE ON EEPROM
        cls
    WriteAt(1,1,"YOUR SCORE")
    WriteAt(2,1,"SECONDS  ",s,"     " )   //showscore(LastLOWScore)ON LCD
    delayms(2000)
    Else      
        LastlowScore = 0
        EE.Write(0,$77,LastLOWScore)
        WriteAt(1,1,"lOWEST SCORE")
        WriteAt(2,1,DecToStr(Prev_score))    //showscore(LastLOWScore)
    EndIf

Where does the $77 (decimal 119, ASCII lower case w) come from???

And in this line:

EE.Write(0,$77,LastLOWScore)

You're trying to save a number, not a string. It should be exactly the same format as in the first subroutine.

Your copy&paste coding style, without any effort to understand what you are copying, does not serve you well.
 
Where does the $77 (decimal 119, ASCII lower case w) come from???
And in this line:
EE.Write(0,$77,LastLOWScore)
I assume the $77 value is just a flag to say a valid score exist in the next two bytes.
The EE.Write(0,$77,LastLOWScore) I assume writes $77 to location 0 and LastLOWScore to locations 1 & 2. However it sets LastLOWScore to zero before writing it.
I was going to suggest writing the value circular. The first location that is $ffff is the first free location so the one before must be the last written location. A simple search will find the lowest score as it will be the lowest value in the EEPROM and it will reduce wear as it uses all locations but I didn't want to confuse matters.

Mike.
 
I assume the $77 value is just a flag to say a valid score exist in the next two bytes.
The EE.Write(0,$77,LastLOWScore) I assume writes $77 to location 0 and LastLOWScore to locations 1 & 2.
That's my takeaway too.

That whole approach is flawed. You might as well just read EE.Read(1, LastLOWScore) and if it's $FFFF then the data has never been initialized (which you could easily set in code when the device is first programmed).

The 'tmpbyte at 0 = $77' doesn't get you anything... all of that code for eeprom location 0 could be removed and it would be just as reliable.
Which is to say it isn't.
 
I assumed the $77 is the first slot in the eeprom
I used the tetris code example then working backwards to see what mistakes I need to correct.
 
I assumed the $77 is the first slot in the eeprom
I used the tetris code example then working backwards to see what mistakes I need to correct.
Nope! It starts at 0... If you start at hex 77 that's 118 locations you missed..
 
Code:
EE.Read(1,LastLOWScore)              //read the eeprom
    If Prev_score < LastLOWscore Then   //COMPARE LOW AND PRESENT SCORE ON EEPROM
        EE.Write(1,Prev_score)
        cls         //write previous lowscore to eeprom
        writeat(1,1,"last score",Prev_score)   //susposed to show what Prev_score is (write to eeprom
        delayms(2000)
    EndIf
maybe thats why the first run through it says my score is 6553
will try setting it to 0
looking at what is going on after the first run, thinking shouldn't lastLOWscore be equal to Prev_score?? after they are compared
 
do I really need the location slot or just put ee.write(0,lastscore)
I think the 0 means it writes to the first slot?
 
Maybe if you read the replies, your threads wouldn't go on for 10,000 posts.
 

Attachments

  • SmartSelect_20200327-105022_Firefox.jpg
    SmartSelect_20200327-105022_Firefox.jpg
    64.8 KB · Views: 194
I am LOST
been looking at the help files and other examples trying to figure out what is going on and why.
taking a break, back at it later as I am really lost with what is going on
 
You have two subroutines that both read and write to the EEPROM. My replied stated that the first subroutine does this the right way and the second is rubbish.

Follow the exact same form reading and reading to the EEPROM that you used in the first subroutine and it will work fine.
 
The bigest error (logic) is setting the initial value to zero, nobody can ever get a score less than zero!

Mike.
 
I'm reasonably sure you can't get a score lower than zero!
If it initializes to 32767 it might work. Not holding my breath.

Mike.
 
( Mmm I didn't read how this thread was going before i had my 2 cents I will return in a year or so see how it turned out ) I used a PIC16F877 eeprom as a code bank for a music keyboard .. key scan position to actual MiDi note value, wrote the device eeprom once (or so) must have been read thousands of times with each key touched . still good after years ..
 
I'm reasonably sure you can't get a score lower than zero!
If it initializes to 32767 it might work. Not holding my breath.

Mike.
I had to read Visitor's post four times until I realised what his point was. He was not questioning lowest score, but whether this logic error is the biggest of all the logic errors.

Made me smile (once I worked it out) :)

EDIT: Wonder why I assume Visitor is a he not a she? Must learn not to assume :( Not been corrected, so must have been right by chance.
 
Last edited:
I kind of figured that problem could be figured out once he could save and retrieve the value in eeprom. On the other hand, since he's sending the numeric values directly to the LCD without converting them to ASCII (using the DecToStr function), he won't know that he's retrieving the same number he stored.

For all the positive things I can say about Swordfish Basic, I must admit the examples provided are horrible.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top