PDA

View Full Version : Minimizing code....



TerdRatchett
- 11th February 2009, 18:27
My project uses non-standard hardware (a microwire port expander) so I could not use the LCDout function. I did manage to get my LCD working however with the following code.

gosub LCD_HOME 'clear the LCD and set the cursor to 00

For y=0 To 64
Gosub modescreen1b 'software version

OUT_BITS = ascii

gosub ioxwriteb
Pause 1
gosub LCD_DATA_EN 'send the "Software Version" message

Next y

initscreenb:
Lookup y,["Software Ver: DL1.00A "],ascii
Return


It works well, but I don't want to copy this block for ever screen I need to print. It would be a huge waste of code. I can't figure out how to minimize the code to just pass the menu to lookup to a sub.

All comments greatly appreciated!

Thanks,
TR

mackrackit
- 11th February 2009, 19:50
Maybe I am missing something...



'Write to the Display
GOSUB LCD_THINGY
'Do Whatever
'Write to the Display again
GOSUB LCD_THINGY
'Do Whatever again.....

LCD_THINGY:
gosub LCD_HOME 'clear the LCD and set the cursor to 00
For y=0 To 64
Gosub modescreen1b 'software version
OUT_BITS = ascii
gosub ioxwriteb
Pause 1
gosub LCD_DATA_EN 'send the "Software Version" message
Next y
initscreenb:
Lookup y,["Software Ver: DL1.00A "],ascii
Return

Is something like that what you had in mind?

TerdRatchett
- 11th February 2009, 20:02
' print message a
GOSUB LCD_THINGY(messagea)

' print message a
GOSUB LCD_THINGY(messageb)

etc...

messagea:
Lookup y,["Message A"],ascii
Return

messageb:
Lookup y,["Message B"],ascii
Return


Can you pass an argument to a sub like in Visual Basic?

mackrackit
- 11th February 2009, 20:24
No, but this might work for you..


' print message a
GOSUB messagea

' print messageb
GOSUB messageb

etc...

messagea:
Lookup y,["Message A"],ascii
GOSUB LCD_THINGY
Return

messageb:
Lookup y,["Message B"],ascii
GOSUB LCD_THINGY
Return

TerdRatchett
- 15th February 2009, 17:13
Hi Dave,

Thanks again for your help.

I have considered your idea but I can't figure out how to implement it because the GOSUB LCD_THINGY routine makes 64 lookups of the table in question.

TR

mackrackit
- 15th February 2009, 20:00
Might be a good time to post your code as it is right now, I am sure there is something I am missing.
From the manual :


An unlimited number of subroutines may be used in a program. Subroutines may also be nested. In other words, it is possible for a subroutine to call another subroutine. Such subroutine nesting should be restricted to no more than four levels deep (12 levels for 17Cxxx and 27 levels for 18Cxxx)
So as long as the look up table block does not call a sub of a sub then you shoulde OK with 64 GOSUBS to the lookup block and back.

Jerson
- 16th February 2009, 01:18
Hi Terd

This is how I do it when I have a number of messages to either print or put out to a 7 segment display.



' Thank DT for this Macro
ASM
GetAddress macro Label, Wout ; Returns the Address of a Label as a Word
CHK?RP Wout
movlw low Label
movwf Wout
movlw High Label
movwf Wout + 1
endm
ENDASM
Address var word
gr[0] var byte

' Examples of how to put your strings in memory
prn_SetDate: PokeCode "Date changed",0
prn_SetTime: PokeCode "Time changed",0
prn_LogPrinted: PokeCode "Log printed",0

' And this is how to use it
@ GetAddress _prn_SetDate, _Address
gosub Prt_puts ' This is my routine to put the string to Printer.
' You could change this to your LCDout routine.
return

' And this is My Prt_puts function
' Print an ASCIIZ string
' Input - Address points to the string
Prt_puts:
repeat
peekcode Address, gr[0] ' read a byte to GR[0] (general register 0)
if gr[0] then ' if end of message (0) not found,
gosub Prt_putc ' print this character
Address = Address+1 ' advance to the next position of input string
endif
until gr[0]=0 ' go back if the message end is found
return



This is not adapted to your requirement, but, I think you can do that yourself.