Shop OBEX P1 Docs P2 Docs Learn Events
storing data to eeprom then recalling at a later time — Parallax Forums

storing data to eeprom then recalling at a later time

mike_1234mike_1234 Posts: 21
edited 2005-11-22 17:11 in BASIC Stamp
Hello,

i hope someone has some ideas for me.

Im trying to get my bs2pe to do some data logging for me.· For the moment i just want the inputs to store decimal numbers to simplify things example.... input 0 = decimal number 1.

I want to beable to store the number in eeprom then increment to the next memory location and store another number.·

Also i want to extract the data that was stored in eeprom, the unit will be operating with out a serial cable attached so when i plug it back in i need to download its contents.

Here is the code i have so far, its not much but hopefully someone can help me fill in the blanks... i'm kinda new to programming at higher level languages so be easy on·me. tongue.gif

' {$STAMP BS2pe}
' {$PBASIC 2.5}
n1·· PIN 0
n2·· PIN 1
n3·· PIN 2
n4·· PIN 3

idx·· VAR· Byte
value· VAR· Byte

Main:

IF n1=1 THEN SUB1

GOTO main

SUB1:
WRITE 0, 1

GOTO main

Thanks,

Mike

Comments

  • NewzedNewzed Posts: 2,503
    edited 2005-11-22 00:41
    If you just want to store the data in the EEPROM of the particular bank you are in, that is fine, but you will lose the data when you reboot.

    If you want to keep the data permanently then you should write:

    SUB1:
    store 8········· '8 is a bank for EEPROM only
    WRITE 0, 1
    return

    Then to read the data you would write:

    store 8
    read 0, variable
    return

    You will have to have a variable for every byte you store.· However, you can reuse the same variable if you "debug variable", because once you have debugged it you have the data.

    Does this help?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • NewzedNewzed Posts: 2,503
    edited 2005-11-22 00:48
    Also, if you prefer you can use a name for the address instead of using the address itself.· For example:

    store 8
    write dog, 1··· 'writes to address 0
    write cat, 2··· 'writes to address 1
    return

    store 8
    read cat, variable1··· 'reads address 1
    read dog, variable2···'reads address 0
    return

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • mike_1234mike_1234 Posts: 21
    edited 2005-11-22 00:57
    Yes that helps..



    I'll complete as much of the code as i can and repost it for people to review if i still cant get it going.



    Thanks again for the help, and if anyone else has suggestions please post them!!



    Thanks,



    Mike
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-11-22 01:25
    Newzed said...(trimmed)
    If you just want to store the data in the EEPROM of the particular bank you are in, that is fine, but you will lose the data when you reboot.
    Actually, you would only lose it if your rpogram wasn't designed to know when to download it, or if you re-programmed the Stamp Module.· Otherwise simply rebooting won't erase it.· Sicne you have a BASIC Stamp 2pe you can use the highest 8 banks without ever worrying about over-writing during a program download.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • mike_1234mike_1234 Posts: 21
    edited 2005-11-22 01:38
    Ok,

    here is my latest attempt to get this thing going.. obviously its not complete but im stuck.

    Everytime this thing stores a number to memory the memory location has to increment.· How do i do that?· Can you provide me an example?·

    And also to extract the information after, how do i do this as well?· I think there is a need for some kind of a loop to go through all the memory locations and print it out on the screen of the PC.

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    n1·· PIN 0
    n2·· PIN 1
    n3·· PIN 2
    n4·· PIN 3
    idx·· VAR· Byte
    value· VAR· Byte

    Main:
    IF n1=1 THEN SUB1
    IF n2=1 THEN SUB2
    IF n3=1 THEN SUB3
    If n4=1 THEN SUB4

    STORE 8
    READ 0, value

    GOTO main

    SUB1:
    STORE 8
    WRITE 0, 1
    RETURN

    SUB2
    STORE 8
    WRITE 1, 2
    RETURN

    SUB3
    STORE 8
    WRITE 2, 3
    return

    SUB4
    STORE 8
    WRITE 3, 4
    return
  • OrionOrion Posts: 236
    edited 2005-11-22 01:43
    {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    n1 PIN 0
    n2 PIN 1
    n3 PIN 2
    n4 PIN 3
    idx VAR Byte
    value VAR Byte
    loc VAR word

    Main:
    debug "current location =",dec loc,cr
    IF n1=1 THEN SUB1
    IF n2=1 THEN SUB2
    IF n3=1 THEN SUB3
    If n4=1 THEN SUB4

    STORE 8
    READ 0, value

    GOTO main

    SUB1:
    STORE 8
    loc = loc +1
    WRITE loc, 1
    RETURN

    SUB2
    STORE 8
    loc = loc+1
    WRITE loc, 2
    RETURN

    SUB3
    STORE 8
    loc = loc+1
    WRITE loc, 3
    return

    SUB4
    STORE 8
    loc = loc + 1
    WRITE loc, 4
    return



    Keep in mind you will have to limit how far you write in the eeprom, eventually you will run out and have to switch banks.


    Post Edited (Orion) : 11/22/2005 1:49:36 AM GMT
  • mike_1234mike_1234 Posts: 21
    edited 2005-11-22 02:10
    Now i'm on the right track,

    but the program seems to keep running... when i downloaded it the debug terminal just shows current location and keeps scrolling this down the page, and when i push a push button it starts counting.

    Anymore suggestions from anyone.. as to how to slow it down, and store the numbers.


    Keep the info coming, we've almost got this one nailed!

    and thanks again for all the info!
  • OrionOrion Posts: 236
    edited 2005-11-22 02:24
    1. How are your inputs wired?

    change

    debug "current location =",dec loc,cr

    to

    debug cls,"curent location =",dec loc

    that will stop the scrolling.

    ·
    2. but the program seems to keep running...
    ·
    YEP, that’s what “GOTO main” will do
    ·
    3. ….store the numbers
    ·
    It is with the write command you just don’t have any code to read it back
    ·
    4. we've almost got this one nailed
    ·
    You have few more lines to go I’m thinking…
    ·
    ·
    ·

    Post Edited (Orion) : 11/22/2005 2:32:14 AM GMT
  • mike_1234mike_1234 Posts: 21
    edited 2005-11-22 03:54
    my inputs are active high.·

    as you can see in my if statement· IF n1= 1

    The scrolling has stopped, but when i push any of the buttons it starts to count up

    at an uncontrolled rate... just a quick bounce on the switch and you go from 1 to 5 instantly.

    How can I slow that down and store one digit at a time.·

    Then to read it back, I dont fully understand the READ command.

    Forgive me i'm new with high level language and dont fully understand some of the concepts.· If this

    was a 68HC11, and i could use assembler i'd have this done in no time.

    I appriciate all the help, and I need alot more.· Please keep the suggestions coming.

    Thanks
  • OrionOrion Posts: 236
    edited 2005-11-22 04:04
    Do you have pull down resistors on the inputs?? What are you using to debounce your inputs??
  • mike_1234mike_1234 Posts: 21
    edited 2005-11-22 04:09
    Yes i have pull down resistors, 10k and 220 ohm respectively.

    As far as debounce, i thought the software compensated for the bounce
  • aliniousalinious Posts: 51
    edited 2005-11-22 04:26
    Try this code sample, I noticed that you were not using the GOSUB command in your IF statements to call the subroutines (SUB0, SUB1...)
    Also, the RD_E2PROM subroutine uses a FOR... NEXT loop to read out the first four locations of the E2PROM

    To debounce the switches, I would connect the positive side of a 1 microfarad electrolitic capacitor to the output of the switch and the negative side of the capacitor to ground. This should be done for all four switches.

    Switch debouncing can be done by using the BUTTON command, but, for now, try to get the main idea of your program working first, then experiment with the BUTTON command.

    Hope this helps,

    Alan Balich

    '{$STAMP BS2pe}
    '{$PBASIC 2.5}

    n1 PIN 0
    n2 PIN 1
    n3 PIN 2
    n4 PIN 3

    idx VAR Byte
    value VAR Byte
    loc VAR Byte

    DO
    GOSUB Chk_Inputs 'Check Inputs to see if any are high
    DEBUG "E2PROM CONTENTS", CR
    GOSUB Rd_E2PROM ' Reads the first 4 locations in the E2PROM
    LOOP

    Chk_Inputs:
    IF (n1=1) THEN GOSUB SUB1
    IF (n2=1) THEN GOSUB SUB2
    IF (n3=1) THEN GOSUB SUB3
    IF (n4=1) THEN GOSUB SUB4
    RETURN

    SUB1:
    STORE 8
    loc = loc +1
    WRITE loc, 1
    RETURN

    SUB2:
    STORE 8
    loc = loc+1
    WRITE loc, 2
    RETURN

    SUB3:
    STORE 8
    loc = loc+1
    WRITE loc, 3
    RETURN

    SUB4:
    STORE 8
    loc = loc + 1
    WRITE loc, 4
    RETURN

    Send_To_Display:
    DEBUG "Location ", DEC idx, " = ", DEC value, CR
    RETURN


    Rd_E2PROM:
    STORE 8
    FOR idx = 0 TO 3
    READ idx, value
    GOSUB Send_To_Display
    NEXT
    RETURN

    Post Edited (alinious) : 11/22/2005 5:13:04 PM GMT
  • OrionOrion Posts: 236
    edited 2005-11-22 04:27
    It can, but its not being done currently done in your code hence why you’re seeing the count behave like it is.

    There is a ton of examples of how to do this in the forums, its getting late for me

    http://search.parallax.com

    Here is a quick helper for reading

    for idx = 0 to loc
    read idx, value
    debug "location ",dec idx, " = ",dec value,cr
    next

    You of course will have to figure out how your going to stop checking the inputs and switch over to downloading the data. I would recommend reading the "What is a microcontroller" text which is free to download from the parallax website. It will make your life much easier.


    I was wondering how the RETURN without GOSUB was compiling??·
  • mike_1234mike_1234 Posts: 21
    edited 2005-11-22 13:36
    Hey guys,

    the code is working better now, except whenever i download it and go to the debug screen it prints out

    E2PROM CONTENTS

    LOCATION 0 = 1

    LOCATION 1 = 1

    LOCATION 2 = 1

    LOCATION 3 = 1

    Even though i disconnected the first pushbutton and tied it to grnd so it should·not read 1 at all.

    Im going to start reading through that material you suggested, in the mean time if someone has anymore suggestions feel free to pass them my way.

    Thanks again for the help,·I really appriciate it!

    ·'{$STAMP BS2pe}
    '{$PBASIC 2.5}

    n1 PIN 0
    n2 PIN 1
    n3 PIN 2
    n4 PIN 3

    idx·· VAR Byte
    value VAR Byte
    loc·· VAR Byte

    DO
    · GOSUB Chk_Inputs····· 'Check Inputs to see if any are high
    · DEBUG HOME, "E2PROM CONTENTS", CR
    · GOSUB Rd_E2PROM······ ' Reads the first 4 locations in the E2PROM
    LOOP

    Chk_Inputs:
    · IF (n1=1) THEN GOSUB SUB1
    · IF (n2=1) THEN GOSUB SUB2
    · IF (n3=1) THEN GOSUB SUB3
    · IF (n4=1) THEN GOSUB SUB4
    RETURN

    SUB1:
    · STORE 8
    · loc = loc +1
    · WRITE loc, 1
    RETURN

    SUB2:
    · STORE 8
    · loc = loc+1
    · WRITE loc, 2
    RETURN

    SUB3:
    · STORE 8
    · loc = loc+1
    · WRITE loc, 3
    RETURN

    SUB4:
    · STORE 8
    · loc = loc + 1
    · WRITE loc, 4
    RETURN

    Send_To_Display:
    · DEBUG "Location ",DEC idx, " = ", DEC value, CR
    RETURN

    Rd_E2PROM:
    · STORE 8
    · FOR idx = 0 TO 3
    ··· READ idx, value
    ··· GOSUB Send_To_Display
    · NEXT
    RETURN
  • aliniousalinious Posts: 51
    edited 2005-11-22 17:11
    Ah, I believe I see what you are trying to do:

    Instead of incrementing the address of the E2PROM address pointer, the addresses are set by the value stored in the LOC variable. Thus, in SUB1 the value 1 will be stored in address 0 of the E2PROM if N1 = 1. By using loc = loc + 1, the program is incrementing LOC based on the original valued stored in LOC. When the stamp is first turned on, LOC contains 0, therfore if n3 = 1, then LOC = 1 (based off of loc = loc + 1) which will be address 1 of the E2PROM. This is probably not where you wanted to store the value at in the E2PROM, but actually in address 3 of the E2PROM. This is why I changed the subroutines LOC variable to equal the exact address so that everytime each subroutine is executed, the E2PROM address corresponds correctly.

    I have also updated the attachment to reflect the changes I noted below.

    If you have any more questions or need any more explanations, I will gladly do so as soon as possible.

    Alan Balich

    SUB1:
    STORE 8
    loc = 0
    WRITE loc, 1
    RETURN

    SUB2:
    STORE 8
    loc = 1
    WRITE loc, 2
    RETURN

    SUB3:
    STORE 8
    loc = 2
    WRITE loc, 3
    RETURN

    SUB4:
    STORE 8
    loc = 3
    WRITE loc, 4
    RETURN

    Post Edited (alinious) : 11/22/2005 5:20:22 PM GMT
Sign In or Register to comment.