Embedded Strings in your Code Space


Closed Thread
Results 1 to 40 of 50

Hybrid View

  1. #1
    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

  2. #2
    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

  3. #3
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Default

    Great job Darrel! It just keeps getting better and better.

    Here is the tested version of the code incorporating your changes:
    Code:
    DEFINE LOADER_USED 1
    DEFINE OSC 40       ' change to suit oscillator speed
    setSPBRG con 129    ' 19200 baudrate w/ 40Mhz OSC
                        ' (change to suit requirements)
    
    temp var byte       ' temporary storage for character
    
        Goto main
    ' Send String Macro (must be located prior to Main)
    ASM
    SendStr  macro Astring
        call _prntSTR
        data Astring,0
        endm
    ENDASM
    
    '=======================================
    Main:
       CALL USART_Init  ' initialize hardware USART
    
    ' send messages through RS232
    ' format #1
    @ SendStr "This is a string" 
    @ SendStr "yet another string again"
    @ SendStr "and one more time!"
    
    ' format #2
    ASM
      SendStr "This is a string" 
      SendStr "yet another string again"
      SendStr "and one more time!"
    ENDASM
    
    Done:
       Goto Done        ' loop forever
    
    '=======================================
    
    ' USART initialization and print string code (locate anywhere)
    asm
    _USART_Init
        movlw   B'00100100'     ; initialize USART
        movwf   TXSTA           ; 8-bit, Async, High Speed
        movlw   _setSPBRG       ; pass constant to SPBRG to
        movwf   SPBRG           ; determine baudrate to be used
        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
    So now we have a single line RS232 messaging system that will work properly, assuming you don't use any interrupts in the rest of your code (that's right, this will probably fail with interrupts enabled).

    Now to get the interrupt compatible version we'll need an efficient way to disable global interrupts when doing the stack manipulations. Since there are 2 levels of interrupts (GIEL and GIEH) possible on an 18 series pic it's a little more complicated. Before I dive into this does anyone have a good suggestion or example code?

    Thanks,

  4. #4
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Smile Now before somebody thinks we're going crazy...

    If you look at the last code example you may be asking yourself "Hey isn't that the same thing as HSEROUT ["This is a string"]?"

    Well... yes it is, sort of. What I really see to be accomplished here, has more to do with creating the framework for a customizable message string handling Function, then just mimicing an already available PicBasic Pro function. For testing purposes, it is just easier to send it out unaltered through the RS232 port. Ulimately, it will hopefully evolve into a great way to send either message strings, data, or both to any device you wish. While at the same time doing it in a way that is more like a built-in PicBasic Pro function.

    A great example of what this could be used for, is imagine that you are interfaced to a device that would accept message strings via RS232, but always requires special commands to precede the message. Using what has already been developed here, all that would be required is to add these commands within the printSTR code (same as what was done for the carriage return and linefeed). Then every time you send a message such as @ SendStr "This is a string" the pre commands would be automatically sent as well.

    Even better still, imagine if you were interfaced to several different devices or gateways, each one having special communication requirements (I2C, SPI, One-Wire, RS232, ect.). Every protocol could concievably be supported within the printSTR code, thereby allowing you to broadcast simultaneously to all devices via the single line SendStr function. Pretty cool huh!

    We are only scratching the surface as to the possibilities.
    Last edited by mytekcontrols; - 1st July 2005 at 19:29. Reason: Just had another thought

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


    Did you find this post helpful? Yes | No

    Default

    Hiya Michael,

    And for my next installment of things you can change with the program you already have working ....

    Sometimes I go crazy with the macro's. But I kinda like this one.
    Code:
    '--[ Initialise USART for specified baud rate at current OSC speed ]------------
    ASM
    USART_Init  macro  Baud
        clrf    TXSTA, 0
    _SPBRG = (OSC * 1000000) / 16 / Baud - 1     ; calc SPBRG @ High baud rate
        if _SPBRG > 255                          ; if SPBRG is too high
    _SPBRG = (OSC * 1000000) / 64 / Baud - 1     ; calc for Low baud rate
            bcf   TXSTA, BRGH, 0                 ; Set BRGH to Low Speed
            if _SPBRG > 255
    _SPBRG = 255
            endif
        else                                     
            bsf   TXSTA, BRGH, 0                 ; Set BRGH to High Speed
        endif
        bsf     TXSTA, TXEN, 0                   ; Set Transmit Enable bit
        movlw   _SPBRG          
        movwf   SPBRG, 0                         ; load the calulated SPBRG
        movlw   B'10010000'                      ; enable USART
        movwf   RCSTA, 0
        endm
    ENDASM
    Now to initialize the USART all you have to do is this...
    Code:
    @ USART_Init 19200
    It should work with any OSC setting and any valid baud rate.

    -------------------------

    >> Ulimately, it will hopefully evolve into a great way to send either message strings, data, or both to any device you wish.

    I've also got some MAJOR changes that allow you to do the above quote. Haven't tested them completely yet, but they look promising.

    Best regards,
       Darrel

  6. #6
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Thumbs up I love your Macros, please keep them coming

    I've also got some MAJOR changes that allow you to do the above quote. Haven't tested them completely yet, but they look promising.
    Hey Darrel, I really think we got you hooked on this :-)
    Which is perfectly fine by me, and will surely be fun to see what's in store.

    Any good ideas on the best way to deal with the interrupt disable issue? I was reading up on this a bit, and it seems like some approaches could cause latency problems. I am also not 100% sure of which parts of the stack manipulation are no no's while an interrupt is occuring (in otherwords, is it only during the PUSH, or POP or both).

    My particular interest will be to implement the "printSTR" in an I2C format, since a video chip I am working with requires this. It also has fussy timing requirements that appear to lower than even the standard device speed. to top it off I have 2 chips on board, so I am using independant clock lines and a shared SDA, since they both have identical addresses. Presently I have everything working with a custom PicBasic Pro I2C routine, but I suspect I'll need to implement this as an ASM routine if it is to be incorporated into the "printSTR" routine. Unless your MAJOR changes would allow for this.

    BTW: I like the USART routine, it makes it very easy which we all love.

  7. #7
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Default Here's the latest code version

    Darrel this incorporates your USART macro (works great):
    Code:
    DEFINE LOADER_USED 1    ' use for Boot Loader
    DEFINE OSC 40   ' change to suit oscillator speed
    
    '============================================
    ' Equates
    '============================================
    
    temp var byte   ' temporary storage for character
    
        Goto main   ' skip around Macros
    
    '============================================
    ' Macros
    '============================================
    ' Send String Macro (must be located prior to Main)
    ASM
    SendStr  macro Astring
        call _prntSTR
        data Astring,0
        endm
    ENDASM
    
    ' USART Initialization Macro (must be located prior to Main)
    ' [ Initialize USART for specified baud rate at current OSC speed ]
    ASM
    USART_Init  macro  Baud
        clrf    TXSTA, 0
    _SPBRG = (OSC * 1000000) / 16 / Baud - 1     ; calc SPBRG @ High baud rate
        if _SPBRG > 255                          ; if SPBRG is too high
    _SPBRG = (OSC * 1000000) / 64 / Baud - 1     ; calc for Low baud rate
            bcf   TXSTA, BRGH, 0                 ; Set BRGH to Low Speed
            if _SPBRG > 255
    _SPBRG = 255
            endif
        else                                     
            bsf   TXSTA, BRGH, 0                 ; Set BRGH to High Speed
        endif
        bsf     TXSTA, TXEN, 0                   ; Set Transmit Enable bit
        movlw   _SPBRG          
        movwf   SPBRG, 0                         ; load the calulated SPBRG
        movlw   B'10010000'                      ; enable USART
        movwf   RCSTA, 0
        endm
    ENDASM
    
    '============================================
    ' Main Program
    '============================================
    Main:
    @ USART_Init 19200  ; set RS232 baudrate
    
    ' send messages through RS232
    ' format #1
    @ SendStr "This is a string" 
    @ SendStr "yet another string again"
    @ SendStr "and one more time!"
    
    ' format #2
    ASM
      SendStr "This is a string" 
      SendStr "yet another string again"
      SendStr "and one more time!"
    ENDASM
    
    Done:
       Goto Done        ' loop forever
    
    '============================================
    ' String Extraction and Print Routines
    '============================================
    ' print string code (locate anywhere)
    ASM
    _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

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