Embedded Strings in your Code Space


Closed Thread
Results 1 to 40 of 50

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Hi Michael,

    Download the PICmicro® 18C MCU Family Reference Manual... or just the chapters you need from here;

    http://www.microchip.com/stellent/id...GE&nodeId=2054

    There's a whole section on table read/write with detailed explanations that should answer every question you have.

    Much easier than me re-typing it all. If you have questions after reading section 8 let me know.
    Regards,

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

  2. #2
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Lightbulb Another approach...

    Hi Darrel - Hi Bruce,

    Darrel I checked out your new message table code for 16 bit pics. Very Nice!

    Bruce I read up on the info you linked me to, and I have a pretty good grasp of what you are doing in your code. I also figured out that the GOTO $-2 is just a slick way to skip backwards without having to use a labeled reference.

    Well of course after getting my brain filled with a bit more knowledge I decided to try my hand at writing yet another variation on message string handling, but not without problems (and a smoking head). Check out this code:
    Code:
    DEFINE LOADER_USED 1
    DEFINE OSC 40               ' change to suit oscillator speed
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 20h
    DEFINE HSER_BAUD 19200      ' change to suit baud rate
    
    message var byte[40]
    stringsize var byte
    x var byte
    temp var byte
    Clr
    
    Main:
        Call getSTR
    @ data "This is a string",0
        Gosub putSTR
    
    '    Call getSTR
    '@ data "yet another string again",0
    '    Gosub putSTR
    Done:
       Goto Done
    
    putSTR:
        For x = 0 To (stringsize-1)
        temp = message[x]
        Hserout [temp]
        Next x
        Hserout [$0D,$0A]       ; send carriage return + line feed
        stringsize = 0          ; reset for next message
        Return    
    
    asm
    _getSTR
        LFSR    FSR0,_message   ; set message array base address
    
    ;copy return address to TBLPTR and then pop it off the stack
        movf    TOSU,W
        movwf   TBLPTRU
        movf    TOSH,W
        movwf   TBLPTRH
        movf    TOSL,W
        movwf   TBLPTRL
        POP
    
    ;TBLPTR should now be pointing to 1st character in string
    Next_Char
        tblrd   *+              ; table read and post increment TBLPTR
        movf    TABLAT,W        ; retrieve character to W register
        movwf   POSTINC0        ; xfer to message array and increment FSR
        incf    _stringsize,F   ; keep track of message string size
        movf    TABLAT,W        ; Is character = 0?
        bnz     Next_Char       ; If not, then get next character			
    
    ;use incremented value of TBLPTR as new return address (push it)
        movf   TBLPTRU,W
        movwf   TOSU
        movf   TBLPTRH,W
        movwf   TOSH
        movf   TBLPTRL,W
        movwf   TOSL
        PUSH
        return                  ;finished with message, return to caller
    endasm
    It's more of a variation of what Darrel's code does. Only I wanted to try using the POP and PUSH functions on the PIC18F to allow me to have embedded message strings (this is a pet peeve of mine). Anyway if you run the code as is (leave the 2nd message string commented out) everything works great. However if you try to use more then one message (uncomment the 2nd message string) then all hell breaks loose, and you end up with garbled messages and a runaway processor.

    Any ideas what I am doing wroung?

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


    Did you find this post helpful? Yes | No

    Default

    Well.. that certainly is a creative approach. I guess you did do some
    reading ehh..;o]

    Initialize your stringsize variable before you start incrementing it.

    Move the PUSH up to the entry point of the section "before" modifying
    TOS.

    See how this works.
    Code:
    DEFINE LOADER_USED 1
    DEFINE OSC 40               ' change to suit oscillator speed
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 20h
    DEFINE HSER_BAUD 19200      ' change to suit baud rate
    
    message var byte[40]
    stringsize var byte
    stringsize = 0      ; <-- initialize to zero here. Don't wanna incf if it's not
    x var byte          ; starting at zero
    temp var byte
    Clr
    
    Main:
        Call getSTR
    @ data "This is a string",0
        Gosub putSTR
    
        Call getSTR
    @ data "yet another string again",0
        Gosub putSTR
    Done:
       Goto Done
    
    putSTR:
        For x = 0 To (stringsize-1)
        temp = message[x]
        Hserout [temp]
        Next x
        Hserout [$0D,$0A]       ; send carriage return + line feed
        stringsize = 0          ; reset for next message
        Return    
    
    asm
    _getSTR
        LFSR    FSR0,_message   ; set message array base address
    
    ;copy return address to TBLPTR and then pop it off the stack
        movf    TOSU,W
        movwf   TBLPTRU
        movf    TOSH,W
        movwf   TBLPTRH
        movf    TOSL,W
        movwf   TBLPTRL
        POP
    
    ;TBLPTR should now be pointing to 1st character in string
    Next_Char
        tblrd   *+              ; table read and post increment TBLPTR
        movf    TABLAT,W        ; retrieve character to W register
        movwf   POSTINC0        ; xfer to message array and increment FSR
        incf    _stringsize,F   ; keep track of message string size
        movf    TABLAT,W        ; Is character = 0?
        bnz     Next_Char       ; If not, then get next character			
    
    ;use incremented value of TBLPTR as new return address (push it)
        PUSH ; <--- moved up to here
        movf   TBLPTRU,W
        movwf   TOSU
        movf   TBLPTRH,W
        movwf   TOSH
        movf   TBLPTRL,W
        movwf   TOSL
        ; PUSH
        return                  ;finished with message, return to caller
    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

    This is getting interesting, and now I have a few new ideas too.

    But, I think We've gone past melanie's original post of "Which PBP Compiler are you using?". So I've SPLIT it into a new thread "Embedded Strings in your Code Space". In the "mel PIC BASIC Pro" forum.

    Sorry if it caused any confusion.

    More from me soon,
    &nbsp;&nbsp;&nbsp;Darrel

  5. #5
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Thumbs up It works!!!!

    Thanks Bruce, it works great!

    And now here it is utilizing your RS232 code:
    Code:
    DEFINE LOADER_USED 1
    DEFINE OSC 40               ' change to suit oscillator speed
    
    temp var byte
    
    Main:
       CALL USART_Init
    
        Call prntSTR
    @ data "This is a string",0
    
        Call prntSTR
    @ data "yet another string again",0
    
        Call prntSTR
    @ data "and one more time!",0
    
    Done:
       Goto Done
    
    asm
    _USART_Init
        movlw   B'00100100'     ; initialize USART
        movwf   TXSTA           ; 8-bit, Async, High Speed
        movlw   .129            ; value for 19200 bps @ 40MHz
        movwf   SPBRG           ; set SPBRG for 19200 bps @ 40MHz
        movlw   B'10010000'     ; enable USART
        movwf   RCSTA
        return
    
    _prntSTR
    ;copy return address to TBLPTR and then pop it off the stack
        movf    TOSU,W
        movwf   TBLPTRU
        movf    TOSH,W
        movwf   TBLPTRH
        movf    TOSL,W
        movwf   TBLPTRL
        POP
    
    ;TBLPTR should now be pointing to 1st character in string
    Next_Char
        tblrd   *+              ; table read and post increment TBLPTR
        movff   TABLAT,_temp    ; fetch character from message string
        bra     Test_EOM        ; go test for EOM character
    	
    Continue                    ; If not EOM then...
        movf    _temp,W         ; move character into TXREG and...
        movwf   TXREG           ; send it!
        btfss   TXSTA,TRMT      ; has it been transmitted?
        goto    $-2             ; If not, keep checking
        bra     Next_Char       ; fetch next message character from table
    	
    Test_EOM
        movlw   .0              ; check for EOM character
        cpfseq  _temp,W         ; compare temp with w, if temp = 0 then end			
        bra     Continue        ; no EOM, so continue
        movlw   "\r"            ; move carriage return into TXREG and...
        movwf   TXREG           ; send it!
        btfss   TXSTA,TRMT      ; has it been transmitted?
        goto    $-2             ; If not, keep checking	
        movlw   "\n"            ; move line feed into TXREG and...
        movwf   TXREG           ; send it!
        btfss   TXSTA,TRMT      ; has it been transmitted?
        goto    $-2             ; If not, keep checking
    
    ;use incremented value of TBLPTR as new return address (push it)
        PUSH
        movf   TBLPTRU,W
        movwf   TOSU
        movf   TBLPTRH,W
        movwf   TOSH
        movf   TBLPTRL,W
        movwf   TOSL
        return                  ;finished with message, return to caller
    endasm
    This is real sweet, because all you have are 2 lines of code to send a message, and without any limitations as to how many or where they are located (assuming it fits in the program space).

    Well this is what I wanted in the first place, all it took was a bit of head scratching and some terrific help from you and Darrel. Heck until I started reading through the PIC18F reference material, I had assumed that there was no way to PUSH and POP a PIC chip (BTW, that is a great link to all kinds of good information).

    Next: Do the same thing for an LCD. Anyone got some good LCD asm code and an LCD to try it on?

    (yet another challenge for some brave soul)

    EDIT: Opps!! Darrel I guess you'll have to move this as well... Sorry

    see ya,
    Last edited by mytekcontrols; - 1st July 2005 at 05:04.

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


    Did you find this post helpful? Yes | No

    Default

    You know.. I just noticed from your earlier example that Clr wasn't returning
    an error. So just out of curiosity, I tried this.

    PBP v2.46, MPASM v4.01. Compiled for 18F452.
    Code:
    DEFINE LOADER_USED 1
    DEFINE OSC 20
    
    ptr_pos	VAR BYTE
    temp	VAR BYTE
    x       VAR BYTE
    Yahoo           '
    NoWay          '
    YeahSoWhat  ' <-- none of this returns an error..?
    
    Main:
        High 0
        pause 1000
        low 0
        pause 1000
        goto Main
    Anyone else get an error returned with this?
    Regards,

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

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


    Did you find this post helpful? Yes | No

    Default

    Any single word lines in PBP automaticaly become a Label. Even though they don't show up in MicroCode Studio until they have a colon ":" after them.

    So it should have been CLEAR. But it won't show up as an error if it's not.

    DT

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor
    Any single word lines in PBP automaticaly become a Label. Even though they don't show up in MicroCode Studio until they have a colon ":" after them.

    So it should have been CLEAR. But it won't show up as an error if it's not.

    DT
    Interesting. I never noticed that one before until looking at where he had
    Clr instead of clear.
    Regards,

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

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


    Did you find this post helpful? Yes | No

    Default

    I guess the move went well, it seems to still be notifying us, and replies are going to the new thread. That's the first time I've split a thread, so I wasn't sure.

    Back to the topic, this isn't quite what I had thought of earlier, but the code changed since then. I haven't had a chance to try it yet, but it does compile.

    So assuming everything is working, I wanted to add one thing that gets you closer to that 1 line "embedded message strings" that you're looking for.

    Here's a simple macro that should do the same thing that you have now, but only takes 1 line (of PBP code) to do it. It needs to be before Main:
    Code:
    ASM
    SendStr  macro Astring
        call _prntSTR
        data Astring,0
        endm
    ENDASM
    Now in the "Main" code, it'll look like this...
    Code:
    Main:
       CALL USART_Init
    
    @ SendStr "This is a string" 
    @ SendStr "yet another string again"
    @ SendStr "and one more time!"
    OR, of course it could also be...
    Code:
    ASM
      SendStr "This is a string" 
      SendStr "yet another string again"
      SendStr "and one more time!"
    ENDASM
    Darrel
    Last edited by Darrel Taylor; - 1st July 2005 at 06:29.

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