Message String Table using Readcode


Closed Thread
Results 1 to 3 of 3
  1. #1
    mytekcontrols's Avatar
    mytekcontrols Guest

    Default Message String Table using Readcode

    I've tested the following code on the PIC18F series (I don't know if it will work on other PIC's).

    Code:
    MsgBaseAddr var word
    MsgIdx var byte
    Msg_Ctr var byte
    Char_Ctr var byte
    ascii var byte
    
    '================================================
    ' Program Start
    '================================================
    'GetMesg Program Routine retrieves character string from Message Table
    'Format: "MsgIdx = X: GoSub GetMesg" (where X = 0 to last message in table)
    
    GetMesg:
    'Initialize counters
        Msg_Ctr = 0
        Char_Ctr = 0
    
    'Retrieve Message Table Base Address
    asm
        movlw  Low Msg_table
        movff  WREG, _MsgBaseAddr
        movlw  High Msg_table
        movff  WREG, _MsgBaseAddr + 1
    endasm
    
    'Locate beginning of message string and adjust base address to match
        While MsgIdx > Msg_Ctr                  ' When we have a match, exit!
        Readcode MsgBaseAddr, ascii             ' Retrieve character and
        MsgBaseAddr = MsgBaseAddr + 1           ' move index to next position. 
        If ascii = 0 Then                       ' If at end-of-message...
            Msg_Ctr = Msg_Ctr + 1               ' Increment to next mesg string and
            If MsgBaseAddr // 2 = 0 Then        ' Determine end-of-string padding.
                MsgBaseAddr = MsgBaseAddr + 2   ' If Even #, pad 2 spaces
            Else
                MsgBaseAddr = MsgBaseAddr + 1   ' If Odd #, pad 1 space
            Endif
        Endif
        Wend
    
    ;Retrieve message and send it
    GetString:
        Readcode (MsgBaseAddr + Char_Ctr), ascii	' Retrieve character
        If ascii = 0 Then Return			' Leave if end-of-message!
    
    '---- your output code goes here (HSEROUT [ascii], LCDOUT ascii, ect) ----
    
        Char_Ctr = Char_Ctr + 1			' set pointer to next character
        Goto GetString
    
    '================================================
    ' Message Table
    '================================================
    
    ' Each string is terminated with a 0
    asm
    Msg_table
        DATA "message #0",0
        DATA "message #1",0
        DATA "message #2",0
        DATA "message #3",0
        DATA "message #4",0
        DATA "message #5",0
        DATA "message #6",0
        DATA "message #7",0
        DATA "message #8",0
        DATA "message #9",0
        DATA "message #10",0
    endasm
    Here is the format to call the above routine, in order to output the individual message strings:
    Code:
        MsgIdx = 0: GoSub GetMesg
        MsgIdx = 1: GoSub GetMesg
        MsgIdx = 2: GoSub GetMesg
    In the section: "---- your output code goes here"
    You can do whatever you desire to fullfill your particular needs. For instance; In the following example, we add a carriage return and linefeed at the end of the message string, sending it all out the hardware usart (RS232).
    Code:
    ;Retrieve message and send it
    GetString:
        Readcode (MsgBaseAddr + Char_Ctr), ascii	' Retrieve character
        If ascii = 0 Then Exit			' Leave if end-of-message!
        HSEROUT [ascii]
        Char_Ctr = Char_Ctr + 1			' set pointer to next character
        Goto GetString
    Exit:
        HSEROUT [$0D,$0A]
        Return
    Anything is possible, just use your imagination.

    Edit: corrected missing variable declaration for ascii

    Enjoy,
    Last edited by mytekcontrols; - 10th July 2005 at 23:05. Reason: forgot to declare variable

  2. #2
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Michael,

    apart from the fact that you have missed to declare ascii VAR BYTE

    it works just fine on my 18F's

    That way I can probably get rid of an external EEPROM I was using for message strings.
    (presuming there is codespace left to store the strings)

    MANY THANKS!

    I'll do some further testing and let you know the results.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  3. #3
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Wink Oppps!

    Thanks Ralph,
    apart from the fact that you have missed to declare ascii VAR BYTE
    I fixed it.

    That way I can probably get rid of an external EEPROM I was using for message strings.
    (presuming there is codespace left to store the strings)
    Depending on how you were doing this before, you might see a tremendous savings (especially true if you were using PBP's Table Lookup function).

    I certainly can't take all the credit for the idea, since much of this was made possible by looking at other people's code examples. However I do think my approach is possibly unique, since I have been looking, and looking for a solution like this for quite some time now, and never found anything exactly like it. I would be curious to know what other PICs this will work on, since all I use now-a-days are the 18F series, and have no easy way to confirm this.

Similar Threads

  1. How about String Variables?
    By mytekcontrols in forum PBP Wish List
    Replies: 40
    Last Post: - 20th January 2015, 12:53
  2. Lookup Table
    By yasser hassani in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 11th March 2008, 10:38
  3. Embedded Strings in your Code Space
    By mytekcontrols in forum mel PIC BASIC Pro
    Replies: 49
    Last Post: - 9th March 2008, 07:50
  4. Visual Basic 6 & Access 2000
    By Demon in forum Off Topic
    Replies: 33
    Last Post: - 7th September 2006, 04:39
  5. PIC 18F452 Table Pointer/ Table Read
    By sougata in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 14th March 2006, 03:07

Members who have read this thread : 2

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