PDA

View Full Version : Message String Table using Readcode



mytekcontrols
- 10th July 2005, 20:44
I've tested the following code on the PIC18F series (I don't know if it will work on other PIC's).



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:


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).


;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,

NavMicroSystems
- 10th July 2005, 21:39
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.

mytekcontrols
- 11th July 2005, 00:17
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.