Shop OBEX P1 Docs P2 Docs Learn Events
Constants with BS2 and PBASIC 2.5 — Parallax Forums

Constants with BS2 and PBASIC 2.5

JimGJimG Posts: 84
edited 2006-10-17 00:40 in BASIC Stamp
I want to be sure of what I am reading.· If I understand correctly you cannot define a CONSTANT that is a string, only a numeric value?· Is that any other method of pre-deining and naming a set of values short of setting up a variable for each one?



EX:



DoThisOrThat·· CON· "?f7"



Thanks,



Jim

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-16 20:43
    The Stamp processors have very little string manipulation ability. They're basically treated as sequences of byte values. Only the various output statements can do anything useful with literal strings, so it makes little sense to have named string constants. What specifically are you trying to do?
  • JimGJimG Posts: 84
    edited 2006-10-16 20:47
    Mike,

    You again, huh? smile.gif I am building a template to use to paint LCD's. It would be really nice if I could define all the various command strings with an English language Name. I can just put it all in as rem'ed text and pull out what I want...not as elegant but will work.

    Ex:

    MoveCursorRight CON "?i"



    Jim
  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-16 21:05
    The other thing you can do is to have tiny subroutines for each command. If you have a serial LCD:
    moveCursorRight:
      SEROUT lcdpin,baud,[noparse][[/noparse]"?i"]
      return
    
    
  • JimGJimG Posts: 84
    edited 2006-10-16 21:11
    Mike,

    What a great idea!! Doesn';t this consume lot of memory to store all the subs?

    J
  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-16 21:13
    Sure, but program memory is relatively abundant on the Stamps.
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-10-16 22:19
    Or you can declare your strings in "DATA" statements, then send them as "STR" variables.
    Thus:

    MyStr1 DATA "Hello", 0
    MyStr2 DATA "Hello again", 0

    SEROUT 16, 16864, [noparse][[/noparse]STR MyStr1, 13] ' Outputs "Hello" and CR, at 9600 baud, out programming port.
    SEROUT 16, 16384, [noparse][[/noparse]STR MyStr2, 13] ' Outputs "Hello again" and CR

    Your program is stored in EEPROM from 'top down', while DATA statements are stored in EEPROM from 'Bottom up' -- that is, address zero up.
  • JimGJimG Posts: 84
    edited 2006-10-16 22:36
    Allan,

    This might be a route to take too. Of course you have to be able to do simple math to use data staements and I am still on intermediate add and subtract. smile.gif


    Thanks,

    Jim
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-10-16 23:35
    The DATA statement doesn't take any math. It does however take a little subroutine to read and send the data:

    MoveCursorRight DATA "?i",0  ' null terminated strings
    Hello DATA "Hello", 0
    Helloagain DATA "Hello again", 0
    
    main:
    DO
    ' ...
    pointer = MoveCursorRight   ' reference by pointer
    GOSUB LCDput
    pointer=hello
    GOSUB LCDPUT
    ' ...
    LOOP
    
    LCDput:
      DO
        READ pointer,char
        IF CHAR=0 THEN RETURN   ' null terminated DATA string
        SEROUT LCDpin, 16864, [noparse][[/noparse]char] 
        pointer=pointer+1
      LOOP
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • JimGJimG Posts: 84
    edited 2006-10-16 23:40
    Maybe this is easier than I think it is. Let me chew on your sample for a little while. If I understand what you are saying, I can find the string on the EEPROM by name rather then having to figure out it's location, correct?

    J
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-10-17 00:32
    That's right, when you name some DATA, the name becomes the address of the data and you can use it in your program. For example,

    MoveCursorRight CON "?i"
    Debug DEC MoveCursorRight ' print the address
    pointer = MoveCursorRight ' now pointer contains the address
    READ pointer, char ' what's there, the first byte?
    DEBUG TAB,HEX2 char '

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • JimGJimG Posts: 84
    edited 2006-10-17 00:40
    I did a little more research in the book and think I understand.

    Thanks,

    Jim
Sign In or Register to comment.