EEPROM examples needed to store data and have it available on powerup.
Can someone point me to some good examples of using an EEPROM with an SX28. I am looking·to recall a previously saved (written) value in a Byte variable and then upon power up of the SX28, have it available to read back. It doesn't sound that hard but I haven't worked really with EEPROMs and I2C but finally have a need for it.
Thanks.
BTW: I have the following EEPROM models to use - 24LC128 (16K) and 24LC32A (4K)
Thanks.
Thanks.
BTW: I have the following EEPROM models to use - 24LC128 (16K) and 24LC32A (4K)
Thanks.
Comments
I never thought about looking at the I2C commands in the Help for an example on the EEPROM. I'm glad your on top of things. I will give it a look tonight.
Thanks again!
I like to use array elements to hold the parameters inside the subroutines.
I try to save the variable memory for those all important WORD variables.
Bean.
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Teacher: What is the difference between ignorance and apathy ?
Student: I don't know and I don't care
Teacher: Correct !
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
Post Edited (Bean (Hitt Consulting)) : 8/1/2007 11:29:57 PM GMT
If I am manipulating a word variable by adding and subtracting values to it, how often should I write - every time I perform a calculation change or in an interrupt every 1 minute or so. If the value has not changed then I would not want to continue to write to it either.
Comments...
Thanks.
I got a couple of errors with your program. The first was because you had a Function (EERecv)·that should have ended with an ENDFUNC instead of an ENDSUB. That was easy to spot.
The next errors are·the same·in the line:
eeAddr = __WPARAM12
has the error -> AMBIGUOUS USE OF ARRAY NAME, USE "@" BEFORE OR "()" AFTER "eeAddr"
(two places for that error).
I also tried @eeAddr()=__WPARAM12 but this·gave "UNKNOWN COMMAND @"·errors.
I changed the line to read:
eeAddr()=__WPARAM12
and this fixed it (both statements)·but I don't understand what this does.
BTW: Is there any advantage to using SUB x and ENDSUB x vs simply using a label: and a Return statement?
(Same with the FUNC and ENDFUNC ? )
Finally....
Can I use something like:
· tempW2 = EERead tempW1
to·read the value previously written into address tempW1·and store it in tempW2 ?
Thanks again!
Post Edited (T&E Engineer) : 8/1/2007 11:38:05 PM GMT
I have uploaded a new version of EEPROM.SXB that should compile cleanly on either version.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Teacher: What is the difference between ignorance and apathy ?
Student: I don't know and I don't care
Teacher: Correct !
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
It looks like I was editing my last post when you posted your new program.
Post Edited (T&E Engineer) : 8/1/2007 11:50:05 PM GMT
I had to adjust the WriteDelay value for my EEPROM (24LC512), but otherwise my version of Terry's program works fine. I use a polling method instead of a fixed PAUSE in my program -- either works.
I need to be able to write a WORD variable and then read it but it looks like this program only works with BYTEs.
Can either of you modify it for WORDs?
I need to do something like this.
SUB RW_Display
· tmpB1 = 0······················' address of the·EEPROM first location.
· IF Coin = 0 THEN············ ' If power is restored·then Word variable 'Coin' is 0
···· Coin = EERead tmpB1··· ' wont work with WORD variable 'Coin'
· ELSE···························· ' No power outage·so Coin stays the same
···· EEWrite tmpB1, Coin···· ' Write the current WORD variable·'Coin' to the EEPROM.
· ENDIF
· Value = Coin··················' Copy the·WORD variable into Word·variable 'Value'
· LCD_Coin····················· ' LCD display routine to show Coin total.
ENDSUB
Thank you.
' PUT_EE address, byteVal1 {, byteVal2 }
When you send a word you're passing two bytes, Little-Endian (LSB, then MSB). The GET_EE2 function reads a word back, Little-Endian. That code is actually being used in my September N&V project so I know it works correctly.
Thanks!
I was a bit confused by the layout of your original program especially the "PUT_EE address, byteVal1 {, byteVal2 }" routine when comparing it to the EEPROM.sxb program.
Are you saying that I can insert a WORD into it instead of 2 BYTEs? If·not then I could use the Coin_LSB and Coin_MSB halves of the WORD Coin.
In the mean time I was looking again at the EEPROM.sxb program.
Would this routine work? It compiles ok.
SUB RW_Display
· tmpW1 = 0
· IF Coin = 0 THEN
···· Coin_MSB = EERead tmpW1
···· tmpW1 = 1
···· Coin_LSB = EERead tmpW1
· ELSE
···· tmpW1 = 0
···· EEWrite tmpW1, Coin_LSB
···· tmpW1 = 1
···· EEWrite tmpW1, Coin_MSB
· ENDIF
· Value = Coin
· LCD_Coin
ENDSUB
I do thank you and Bean (and others) for their great help!
I'm questioning if there may be something wrong with the DEBUG circuitry in my SX-Key. When I run your 24LC512.sxb using DEBUG the results state: eeWord 271 even if I uncomment the other comments in the MAIN routine (e.g. PUT_EE 0, 0, $FFFF
· eeWord = GET_EE2 0, 0· ... etc.).
I also noticed that you have OSCHS2 set at 20 MHz. I thought that you can damage your SX-Key if running DEBUG with a resonator installed.
I even changed the statements to:
DEVICE········· SX28, OSC4MHZ, TURBO, STACKX, OPTIONX, BOR42
FREQ··········· 4_000_000
ID············· "24LC512"
and did not use a resonator either time and still got the 271 answer for eeWord.
If I run other programs like EEPROM from you or BEAN I got the same answer everytime even if I changed the value and the address.
I then decided to run a program you wrote for the Basic Stamp to test the EEPROM itself. I used both a 24LC32A and a 24LC128 and they both seemed to PASS. The program was 24LC32.BS2.
I did follow the wiring as per the SXB Help 1_51.pdf (page 82) and also tried the I2C example and did not get the results I would expect. I tied pins 1-4,7 to GND and pin 8 to +5 along with the (2) 4.7K resistors to +5v and the other ends to SDA and SCL respectively.
Is there any other test I can use to make sure that my problem is not with my SX-Key? My SX-Key is fairly new·and I know not to insert a resonator in when using DEBUG.
If Coin is zero you read the value, MSB first; when coin is not zero you write the value LSB first. To restore a word value from the EEPROM you must read back the bytes in the same order in which you saved them. You've swapped the order which is probably giving you bogus results.
Also don't forget the pull-ups on SCL & SDA
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike
I have not done anything with "my program yet" when checking for correct values using DEBUG. As stated before the chips appear to "PASS" (using one of your BS2 eeprom programs like 24LC32.bs2) when connected with 2 PULL UP 4.7K resistors so I know I have it wired correctly as seen in the drawing that Mike Cook just posted.
My question was with whether DEBUG was working correctly. It seems when I am setting a value in my program such as the·WORD variable··Coin = 1234 (with or without an EEPROM connected), and then use a BREAK and a·WATCH COIN,16,UDEC .... I would expect to get back 1234 but usually get some other strange number repeatedly. So I think I need to see if DEBUG is correctly working first before I can even look at the EEPROM issues. It is·similar results too when·using Byte variables too like tmpB1 = 30.
Back to EEPROM issue for a moment...·Are saying that I·DONT·need to use the (2)·4.7K resistors? That is nice but a bit confusing since drawings always show them like·in the SXB Help 1_51.pdf (page 82) with "SDA· PIN RA.0 INPUT PULL UP"·· and··· "SCL· PIN RA.1 INPUT PULL UP" statements. I have been using them just like the other drawing the Mike Cook posted below.
As far as my subroutine...I did notice that I needed to have the LSB and MSB listing swapped to look like this:
However, what should go into the first address the MSB or the LSB byte?
I'll get back to this when I know my SX=Key DEBUG is working correctly.
Is there a good program to use to verify this since when I set a variable like Coin or tmpB1 and then try to see what is written with a Watch statement, it is always wrong (not 0 but could be any number that you would not think is correct).
Thanks again for your help!
I'll try it out tonight after work and let you know if I have a bad SX-Key or not.
Thanks again for hanging in there with me!
It seems they don't make "Engineers" like they used to.
Cheers,
Peter (pjv)
However, there is something I am still not doing right when writing an EEPROM value and then reading it back. I will look at this some more over the weekend and try to figure it out.
Thanks for your help.
I bow down to your genius in humbleness.
I think the problem I had before in DEBUG was that I was not WALKing, RUNing or POLLing and seeing initial values. Duh!
Anyway...more work on it later this weekend.
Thanks sooooo much JonnyMac!
Post Edited (T&E Engineer) : 8/4/2007 10:30:04 AM GMT