Shop OBEX P1 Docs P2 Docs Learn Events
EEPROM examples needed to store data and have it available on powerup. — Parallax Forums

EEPROM examples needed to store data and have it available on powerup.

T&E EngineerT&E Engineer Posts: 1,396
edited 2007-08-04 00:17 in General Discussion
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.

Comments

  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-07-31 12:59
    Since the help seems beyond reach, I've attached a demo for the 24LC512 -- I used it an a servo movement recorder for the September Nuts & Volts. This code will work as-is with the 24LC128 and 24LC32.
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-07-31 13:09
    Thank you JonnyMac.

    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!
  • BeanBean Posts: 8,129
    edited 2007-07-31 13:21
    Well, Jon beat me to it, but I'll throw my hat in the ring anyway.

    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
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-07-31 15:41
    If only two-byte arrays could be used like words in all cases... boy would that free up a lot of global space.
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-07-31 16:47
    How many times can I write and read to the EEPROM (100,000)?

    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.
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-07-31 16:52
    You only need to read the data sheet once to find out the number of write-cycles a given EEPROM will tolerate.
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-08-01 23:13
    Bean,

    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
  • BeanBean Posts: 8,129
    edited 2007-08-01 23:28
    Because you are using the BETA version of the compiler.
    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
    ·
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-08-01 23:38
    OK. I have the new version. compiles great. I see the differences.

    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
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-08-01 23:55
    SUB/ENDSUB and FUNC/ENDFUNC make your code v2.0 compatible -- it's a good idea.

    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.
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-08-02 00:29
    Thanks JonnyMac.

    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.
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-08-02 00:48
    What's the matter with the 24LC512 program I posted a couple days ago? -- it works with bytes and words. Don't be fooled by this:

    ' 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.
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-08-02 01:03
    Sorry. I assumed that the last posting EEPROM.sxb that you sent was what you recommended. I forgot about your original posting. I will look at it now.

    Thanks!
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-08-02 02:14
    JonnyMac,

    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
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-08-02 03:49
    You would be less confused if you spent a few minutes actually working with the programs that many of us post to assist you.
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-08-02 10:16
    OK. I will use your code. Its just that I already inserted Beans code and now I will have to remove it and use yours which is ok but just wanted to make sure the "PUT_EE address, byteVal1 {, byteVal2 }" routine would accept WORDs instead of 2 bytes (otherwise I would guess I could use Coin_LSB and Coin_MSB with the PUT_EE routine. Right?

    I do thank you and Bean (and others) for their great help!
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-08-02 13:38
    Both programs will do the job, albeit differently -- still, some effort on your part is required to learn how these things work so you can integrate them into your own projects.
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-08-02 23:48
    JonnyMac,

    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.
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-08-03 02:08
    When all else fails disconnect everything and reconnect it, making sure that you haven't crossed up the SDA and SCL pins, or connected WP to Vdd (you can let it float, there is an internal pull-down). And... PLEASE! download and study the manufacturer's data sheet. Many of the questions you post here are answered in these kinds of documents.
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-08-03 02:14
    I'm going on the assumption that you're still attempting to integrate mine or Terry's code into your program without fully understanding what we did with the I2C memory. Well, here's a problem that I just noticed from your code (copied this from above):

    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
    



    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.
  • Mike CookMike Cook Posts: 829
    edited 2007-08-03 02:20

    Also don't forget the pull-ups on SCL & SDA

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
    262 x 191 - 8K
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-08-03 10:28
    JonnyMac and others,

    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:
    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_MSB
         tmpW1 = 1
         EEWrite tmpW1, Coin_LSB
      ENDIF
      Value = Coin
      LCD_Coin
    ENDSUB
    

    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!
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-08-03 14:08
    It's an unbelievably simple exercise to test the Debug capabilities of the SX-Key -- the attached demo proves it. Download the program in Debug mode and then click on Poll. You'll see the value of tmpB1 increment from 0 to 255 and then repeat.
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-08-03 14:12
    That's great - exactly what I was looking for.

    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!
  • pjvpjv Posts: 1,903
    edited 2007-08-03 15:23
    Hi All;

    It seems they don't make "Engineers" like they used to.

    Cheers,

    Peter (pjv)
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-08-03 15:53
    Agreed! It's not a bad SX-Key, it's operator error -- been down this road many times....
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-08-03 20:54
    My SX-Key is good as JonnyMac stated. The program runs fine.

    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.
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-08-04 00:17
    I'm making some good progress in my program using·my EEPROM and JonnyMac's program (within my program)!

    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
Sign In or Register to comment.