Introduction
In many applications, it is necessary to send serial characters, either to a PC or serial LCD.
This discussion illustrates how multiple strings may be handled using double table lookup. Although the routines were written for our PIC-an-LCD serial display that has some nice features, they may be used with some modification with other units.
Brute Force
Of course, the simplest technique for outputting text is by brute force.
MOVLW "T" CALL LCD_CHAR MOVLW "=" CALL LCD_CHAR MOVLW 17H ; see note CALL LCD_CHAR MOVF VAL, W ; get the value to display CALL LCD_CHAR MOVLW 0AH ; see note CALL LCD_CHAR MOVLW 0DH ; see note CALL LCD_CHARNote that control code 17H indicates to the PIC-an-LCD to print the next character as an unsigned decimal character. Control codes 0AH and ODH are line feed and carriage return.
Certainly, there is nothing wrong with this code, particularly for short messages.
Single Table Lookup
For longer messages, a loop with a single lookup table is more efficient;
OUT_STR: CLRF INDEX OUT_STR_1: MOVF INDEX, W CALL LOOK ; map index into a character ADDLW 0 ; perform an arithmetic function to set Z flag BTFSC STATUS, Z RETURN ; character was 00H INCF INDEX, F GOTO L1 LOOK: ADDWF PCL, F DT "Place probe in 4 inches of", ODH, OAH DT "water and depress CAL pushbutton", 0DH, 0AH, 00HNote that a different subroutine is required for each string. (Please also note that in the above I neglected to load PCLATH with the appropriate value).
Displaying Multiple Strings using Dual Table Lookup
The following presents a technique where any one of a number of multiple meassges may displayed using a common routine.
In calling OUT_STRING, the identity of the string to be displayed in passed in variable STR_NUM. STR_NUM is mapped into the address of the beginning of the selected string using a call to GET_STRING_ADDRESS.
The characters in the selected string are then fetched and output until the null character (00) is fetched.
Note that subroutine OUT_STRING and all of the lookup tables must be on the same 256 byte page. Of course, you can have multiple routines on different pages.
; Program STRING4.ASM ; ; Illustrates the use of double table lookup to selectively output ; a number of different strings. ; ; A STR_NUM, in this case, a number in the range of 0 to 3 is passed ; to subroutine OUT_STRING. STR_NUM is mapped into the address of ; the beginning of the selected string using a call to GET_STRING_ADDRESS. ; ; The characters in the selected string are then fetched and output until ; the null character (00) is fetched. ; ; Note that this routine uses module LCD_F84.ASM to display the characters. ; ; RA.0 (term 17) -----------------------------> PIC-an-LCD or similar ; ; Copyright, Peter H. Anderson, July, 98 LIST p=16f84 #include <p16f84.inc> __CONFIG 11h CONSTANT VARS=0CH STR_NUM EQU VARS+0 ; selected string in the range 0 - 3 INDEX EQU VARS+1 ; character within a string LOOP EQU VARS+2 ; timing loop ORG 000H BSF STATUS, RP0 BCF TRISA, 0 ; serial out to LCD BCF STATUS, RP0 BCF PORTA, 0 ; idle on serial lead TOP: MOVLW 0CH CALL LCD_CHAR ; clear the PIC-an-LCD MOVLW .50 CALL LCD_DELAY ; give some time for the LCD to clear MOVLW 0 ; display each string in turn MOVWF STR_NUM CALL OUT_STRING MOVLW 1 MOVWF STR_NUM CALL OUT_STRING MOVLW 2 MOVWF STR_NUM CALL OUT_STRING MOVLW 3 MOVWF STR_NUM CALL OUT_STRING MOVLW .4 MOVWF LOOP TIME: ; delay for a second MOVLW .250 CALL LCD_DELAY DECFSZ LOOP, F GOTO TIME GOTO TOP ; and do it again DONE: GOTO DONE ORG 0200H OUT_STRING: MOVLW 02H ; set to page 2 MOVWF PCLATH MOVF STR_NUM, W CALL GET_STRING_ADDRESS MOVWF INDEX ; this is the base index for the defined string OUT_STRING_1: MOVF INDEX, W ; now output each character CALL GET_CHAR ADDLW 0 BTFSC STATUS, Z GOTO OUT_STRING_2 CALL LCD_CHAR INCF INDEX, F GOTO OUT_STRING_1 OUT_STRING_2: RETURN GET_STRING_ADDRESS: ADDWF PCL, F RETLW STRING0-STRING0 RETLW STRING1-STRING0 RETLW STRING2-STRING0 RETLW STRING3-STRING0 GET_CHAR: ADDWF PCL, F STRING0: DT "This is string 0", 0DH, 0AH, 00H STRING1: DT "This is #1", 0DH, 0AH, 00H STRING2: DT "#2", 0DH, 0AH, 00H STRING3: DT "And, finally string #3", 0DH, 0AH, 00H #include <a:\lcd_f84.ASM> ENDNote that although this discussion is presented in the context of outputting characters, the same technique might be applied to any sequential type of operation. For example, the data might be a number of stepping motor task sequences where the speed and direction is contained in one byte and the number of steps in a second. The PIC fetches the first two bytes from defined sequence, performs the operation, and then fetches the next two bytes, etc, until it fetches a unique end of sequence character.