Shop OBEX P1 Docs P2 Docs Learn Events
Write a variable into EEPROM — Parallax Forums

Write a variable into EEPROM

Tcpip_v6Tcpip_v6 Posts: 15
edited 2007-05-04 20:35 in BASIC Stamp
I’m trying the write a variable into the EEPROM of a BS2.
This is what I try to do:
WRITE 100, "Test"

This is how:
TxT VAR Word
TxT = "Test"
WRITE 100, TxT

This doesn’t work so if anyone could help it would be appreciated?

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-27 13:33
    Sadly, the "STR" modifier only operates in SEROUT (I think).

    So, there's the brute force approach:

    Txt(4) VAR BYTE ' Declare a byte array

    FOR I = 0 TO 3
    LOOKUP I, [noparse][[/noparse]"T", "e", "s", "t"], Txt(I)
    WRITE I, Txt(I) ' Write it to eeprom
    NEXT

    Now, if you KNOW what your string is, there's a much more direct approach:

    MyText1 DATA "Test", 0

    And a more 'complete', general purpose set of library code is attached.
  • Tcpip_v6Tcpip_v6 Posts: 15
    edited 2007-05-01 07:02
    Allanlane5, thanks..
    Maybe I need to explain a little bit more. I want to check the battery and display the status (voltage) on a LCD. I found a very usefull and properly working code at: http://www.emesystems.com/BS2rct.htm
    The variable Vx must be displayed as text and need to be placed between quotes. Otherwise intead of the value, the corresonding character will be displayed.
    The subroutine will read the EEPROM at 100 and will write it the the LCD (44780)

    BattChck:
    LOW Batt
    RCTIME Batt,0,rct
    LOW Batt
    Vx = 18150 / rct + -63
    DEBUG HOME,DEC? rct,"Vx=",DEC Vx/10,".",DEC1 Vx
    WRITE 100, Vx
    GOSUB LCDwrmain
    RETURN
  • ZootZoot Posts: 2,227
    edited 2007-05-01 12:45
    There are no "strings" on the Stamp, only Words (2 characters) and Bytes (1 character). To print a stored "string" or the like to a display, you need to do kind of the opposite of what allanlane5 suggested for writing the string. This is *very* common in Stamp applications -- needing to output strings of charactes to a display, speech module, whatever.

    Here is a super-simple example

    EEaddr VAR Word
    i VAR Nib
    Char VAR Byte
    
    String1 DATA "text", 0
    
    EEaddr = String1
    Loop:
      READ EEaddr, Char
      IF Char <> 0 THEN
         DEBUG Char
         EEaddr = EEaddr + 1
         GOTO Loop
      ENDIF
    
    End
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-05-01 12:58
    I'm assuming you're checking the battery voltage pretty often. You should know the eeprom only allows 1 million writes to a location before that location won't work anymore.

    So, your "WRITE 100, Vx" is going to wear out location 100 on the eeprom.

    If you WANT to write a WORD value, you can do

    WRITE 0, WORD Vx

    or WRITE 0, Vx.LowByte
    WRITE 1, Vx.HighByte

    You keep talking about "writing to an LCD", then you wind up "Writing to an EEPROM". Since the EEPROM is not connected to the LCD, I don't know what you hope to achieve by writing to the eeprom.
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-05-01 13:01
    And you don't say how you're going to 'write to the LCD' yet. Is it a serial interfaced LCD? Are you using a parallel interface?

    There's ways to do what you're talking about, once we're certain what you're talking about.
  • Tcpip_v6Tcpip_v6 Posts: 15
    edited 2007-05-01 13:23
    What I meant was that the subroutine "BattChck" writes the battery voltage to EEPROM and that the subroutine "LCDwrmain" reads the eeprom and writes it to the LCD.

    Anyway, maybe writing to the eeprom isn't a good idea as you pointed out. I also want to display other things to the LCD so there will be a lot of writing to the eeprom.

    Maybe anybody knows how to simply write to a LCD (44780) without using the eeprom? I've attached the eeprom version of the program for Controlling a LCD , which I used (copied from http://www.weethet.nl/english/basicstamp2_lcdcontrol.php#example1)
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-05-01 14:57
    You need to add the following:
    TempStr VAR BYTE(4)
    TempVal VAR WORD
    I VAR NIB

    ValueWr:
    FOR I = 0 TO 3
    TempStr(I) = (TempVal // 10) + $30·· ' "$30" is ascii value of 'zero'
    TempVal = TempVal / 10
    NEXT

    char = Quote
    GOSUB LCDWr
    FOR I = 3 TO 0
    char = TempStr(I)
    GOSUB LCDWr
    NEXT
    char = Quote
    GOSUB LCDWr
    RETURN

    Converting a 'value' stored in memory to a string version of itself is one of those classic early computer science tasks. The pseudocode for this is:

    Get the number into "TempVal"
    WHILE TempVal is not zero, DO
    The least significant character "Char" is "TempVal" MOD by 10.
    Add the ascii value for "0" to "Char" to get the ascii value for the number "Char" represents.
    Divide TempVal by 10.
    END WHILE
  • Tcpip_v6Tcpip_v6 Posts: 15
    edited 2007-05-02 12:52
    Thanks allanlane5!

    The only thing I need to do is to figure out how to add your code and test... hope I'll have some time this evening [noparse]:)[/noparse]
  • Tcpip_v6Tcpip_v6 Posts: 15
    edited 2007-05-04 12:13
    allanlane5, I'm struggling with adding your code and could use some help?

    Thanks..
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-05-04 14:53
    Sure. Upload what you've got so far, and I'll see what I could do.
  • Tcpip_v6Tcpip_v6 Posts: 15
    edited 2007-05-04 15:02
    Allanlane5, Great!

    This is what I've got so far, as you can see donno where to add your piece..
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-05-04 15:08
    Now, we talked about this. If you keep writing that value to the eeprom, you're going to wear the little bugger out. Just assign a variable for the value.

    Otherwise -- what's not working?

    In other words, what exactly IS the string that needs to be sent to the LCD?· Does it have leading and trailing quotes?· Is it JUST the Ascii characters of the value?
  • Tcpip_v6Tcpip_v6 Posts: 15
    edited 2007-05-04 20:35
    I want to have the value of "vx" send to the LCD as ascii characters and without the use of the eeprom. For exampe if vx = 520 I want to have 520 displayed on the LCD.

    Sorry that I wasn't clear enough...hope I'm now?
Sign In or Register to comment.