Embedded Strings in your Code Space


Closed Thread
Results 1 to 40 of 50

Hybrid View

  1. #1
    mytekcontrols's Avatar
    mytekcontrols Guest

    Default

    Thanks for the heads up on the PDS. I'll check go check it out. Although on the current project I extremely doubt I'll be jumping ship (... I can almost see the shore as we speak). However on another project, It might certainly be a consideration. How is the optimization of the code it produces?

    Also as I said in my previous post, reliability plays a key role in my choice of programming tools. I tend to stick to what works, and usually find work arounds for minor ommisions. Case in point; the lack of string handling ability in PBP I quickly resolved by taking a different approach. At first I was stumped at not being able to set up a string variable that I could later pass to an I2C routine. Of course the solution came in the form of using lookup tables as shown below (printascii would go to a separate print routine for output, in my case a specialized I2C routine).

    For y=0 To 255
    Gosub mesghello
    If ascii = Null Then exit
    Gosub printascii
    Next y

    mesghello: Lookup y,["hello world",Null],ascii
    Return

    In my 6502 assembler days, I used a routine that would pop the return address off the stack, increment it by 1 to pick up the start of the ascii data to be sent, send the data, and then modify the RTS to return directly following the ascii data (next example).

    jsr printascii
    .byte "hello world",Null
    -- rentry point upon return from printascii routine --

    Much simpler, and some what more readable when looking at the source code (the messages appear where used, and not in a later table). However for messages that would be reused several times through out a program, the table approach is preferred in order to save code space. Unfortunately I haven't figured out a similar method that would work either within PBP or as an @asm script (can't pop the stack as far as I know).

    I ran a suggestion by the makers of PBP about the possibility of including something like the LCDOUT command maybe in the form "BYTEOUT var{,item...} example:

    ascii var byte[255]

    BYTEOUT ascii,"hello world",Null
    Gosub print ascii


    printascii:
    For y=0 To 255
    If ascii[y] = Null Then Return
    Gosub sendascii ' send character by whatever method desired
    Next y
    Return

    This would yield something similar to the 6502 assembly example.

    Anyway getting back to the original topic so to speak; picnaut thanks again for your sugestion about PDS.

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    mytekcontrolsm,

    Using a Lookup for strings can use up a huge amount of code. And I believe I may have the solution you're looking for.

    How to store and retrieve strings in Code Space

    Another person that I helped out a while back had his 877 completely maxed out with Lookups for strings to be displayed on an LCD. After converting them to the STRINGS in CODE format, it only took up 1.5K words. Leaving 6.5K for more program. We were both amazed.

    And, I too will not be Jumping ship anytime soon. PBP is just to good.

    Darrel

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Nice job Darrel, and thanks for the excellent resource. I've used several of
    your routines on various projects.

    Here's something similar for the 18F series.
    Code:
    DEFINE LOADER_USED 1
    DEFINE OSC 4
    
    ptr_pos	VAR BYTE
    temp	VAR BYTE
    x	VAR BYTE
    
    Main:
       CALL USART_Init
       ptr_pos = 0
       CALL Start
       ptr_pos = 16
       CALL Start
       ptr_pos = 48
       CALL Start
       ptr_pos = 176
       CALL Start
    
    Done:
       GOTO Done
        
    ASM
    _USART_Init
       movlw   B'00100100'   ;initialize USART
       movwf   TXSTA	 ;8-bit, Async, High Speed
       movlw   .25           ;value for 9600 bps @ 4MHz
       movwf   SPBRG	 ;set SPBRG for 9600 bps @ 4MHz
       movlw   B'10010000'   ;enable USART
       movwf   RCSTA
       return
    
    ;----Lookup & send text messages--------------------------
    _Start
       movlw   UPPER msg_table
       movwf   TBLPTRU
       movlw   HIGH msg_table
       movwf   TBLPTRH
       movlw   LOW msg_table
       movwf   TBLPTRL
       movf    _ptr_pos,W	;prt_pos must hold message address
       addwf   TBLPTRL,F
       clrf    WREG
       addwfc  TBLPTRH,F
       addwfc  TBLPTRU,F
    
    Next_Char
       tblrd   *+
       movff   TABLAT,_temp
       bra     Test_EOM      ;test for EOM character
    	
    Continue
       movf	  _temp,W	 ;move temp to w
       movwf  TXREG		 ;send it
       btfss  TXSTA,TRMT	 ;wait for data TX
       goto   $-2
       bra	  Next_Char      ;fetch next message character from table
    	
    Test_EOM
       movlw  "~"            ;check for EOM character
       cpfseq  _temp, 1	 ;compare temp with w, if temp = ~ then end			
       bra     Continue      ;no EOM, so continue
       movlw   "\r"		 ;move data into TXREG
       movwf   TXREG	 ;send carriage return
       btfss   TXSTA,TRMT	 ;wait for data TX
       goto    $-2	
       movlw   "\n"		 ;move data into TXREG
       movwf   TXREG	 ;send line feed
       btfss   TXSTA,TRMT	 ;wait for data TX
       goto    $-2
       return                ;finished with message, return to caller
    ENDASM
    
    ASM
    msg_table	; Message strings
       data	" Message #1~    ";Message #1 starts at msg_table 0
       data	" Message #2~    ";Message #2 starts at msg_table 16, etc,
       data	" Message #3~    ";32
       data	" Message #5~    ";48
       data	" Message #6~    ";64
       data	" Message #7~    ";80
       data	" Message #8~    ";96
       data	" Message #9~    ";112
       data	" Message #10~   ";128
       data	" Message #11~   ";144
       data	" Message #12~   ";160
       data	" Message #13~   ";176
    ENDASM
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Now there's an endorsement I can be proud of.

    Thanks Bruce!

  5. #5
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Smile I like it!

    Thanks Darrel and Bruce for your great examples. I would have to say I like Darrel's approach the best, since it seems less cryptic as to what the messages are (i.e; you can give them meaningfull names). Also I like the idea that they can be located anywhere in the program (kinda like my 6502 example). However I am using an 18F252 for my project, and apparently this approach wont work with a 16 bit processor. Any ideas how this could be changed?

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Code:
    ' Text message addresses w/specific names
    Intro CON 0
    PumpOn CON 16
    PumpOFF CON 32 ' etc,,
    
       ptr_pos = Intro     ' print intro message
       CALL Start
       ptr_pos = PumpON ' print pump on message, etc,,,
       CALL Start
    You now have meaningful names for your strings. It's not that cryptic once
    you've played with it a bit. The 18F series are easier to code in .asm than
    16F parts.

    Make your strings as long as you like. That was just a quick/simple example.

    I don't have an example for placing strings all over code space since I would
    never do that. PBP library functions & my app code get first pick. Huge
    chunks of text messages get the left-overs at the bottom...;o]
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Minimizing code space
    By Tobias in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th May 2009, 07:25
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Please help with storing strings in codespace
    By g-hoot in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th May 2008, 01:02

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts