Use of the PIC-n-LCD Serial LCD Unit

copyright, Peter H. Anderson, Baltimore, MD 21239, Feb, '98

Introduction

Throughout the semester we will use the PIC-n-LCD to display strings and data on a four line LCD display. The PIC-n-LCD is a very powerful human interface and debugging tool.

There will be four of these units in the lab and I will try to get more.

Connect as shown;

	Your PIC			Serial LCD

	RA.0 (term 17)			Term 3 on DB9 Connector
	+5V				Red Lead
	GRD				Black Lead
I have developed two standard routines, LCD_CHAR and LCD_VAL which you may use to display characters and values.
	MOVLW	"A"	
	CALL 	LCD_CHAR	; displays the character "A"

	MOVLW	7FH		
	CALL	LCD_VAL		; displays "7F"
These routines are contained in file LCD_CTRL.ASM and you may simply include this file near the end of your routine as shown in the following examples. Be sure to download a copy of LCD_CTRL.ASM to your floppy.

Program STRING_1.ASM.

In program STRING_1.ASM, "Hello World" is displayed on line 1. This is followed by "T="value on line 3.

Note that each character is fetched into the W register and a call is made to LCD_CHAR.

The PIC-n-LCD unit also provides the capability for handling special characters so as to format the LCD. Examples which are used in STRING_1.ASM are;

	0CH	ASCII Form Feed, Clears LCD and locates cursor to
		upper right

	0AH	ASCII Line Feed, Advances cursor to same position on
		next line

	0CH	ASCII Carriage Return, Moves cursor to extreme left.

	09H	ASCII Tab
Note that the variable T_C is displayed by moving T_C to W and then calling LCD_VAL. This causes the value to be displayed in hexadecimal format. Thus, 23 decimal is displayed as 17.

Note that the LCD_CTRL routine is included at the end of the program.

; Program STRING_1.ASM
;
; Illustrates how to write strings to a the PIC-n-LCD.
;
; Program clears the display and writes "Hello World" to the first line.
;
; It then writes "T_C = 23 Degrees C", where 23 is assumed to be the 
; result of a measurment.
;
; Note that this uses program LCD_CTRL.ASM which is inlcuded at the bottom
; of this program.
;
; PORTA, Bit 0 (terminal 17) ------ TX ----------> to RX on Serial LCD
;
; copyright, Peter H. Anderson, Morgan State University, July 5, '97

	LIST p=16f84	
#include <c:\mplab\p16f84.inc>	
	__CONFIG 11h

	CONSTANT VARS=0CH

T_C		EQU VARS+0	; used for illustration	
STR_INDEX	EQU VARS+1


	ORG 000H

	BSF STATUS, RP0		; RP1 = 0, RP0 = 1, BANK1
	BCF TRISA, 0		
	BCF STATUS, RP0		; bank 0
	
	BCF PORTA, 0		; serial output low

MAIN:
	; perform a temperature measurement	
	MOVLW .23		; but i will dummy the vaue of T_C = 23
	MOVWF T_C

	MOVLW 0CH		; special code to clear LCD
	CALL LCD_CHAR		; send it as a character

	MOVLW "H"		; display "Hello World"
	CALL LCD_CHAR
	MOVLW "e"
	CALL LCD_CHAR
	MOVLW "l"
	CALL LCD_CHAR
	MOVLW "l"
	CALL LCD_CHAR
	MOVLW "o"
	CALL LCD_CHAR
	MOVLW " "
	CALL LCD_CHAR

	MOVLW "W"
	CALL LCD_CHAR
	MOVLW "o"
	CALL LCD_CHAR
	MOVLW "r"
	CALL LCD_CHAR
	MOVLW "l"
	CALL LCD_CHAR
	MOVLW "d"
	CALL LCD_CHAR

	MOVLW 0AH
	CALL LCD_CHAR		; Line Feed - special code 
	
	MOVLW 0AH
	CALL LCD_CHAR

	MOVLW 0DH		; CR - special code
	CALL LCD_CHAR


	MOVLW 09H		; Tab
	CALL LCD_CHAR

	MOVLW "T"
	CALL LCD_CHAR
	MOVLW "="
	CALL LCD_CHAR

	MOVF T_C, W		; prints value in Hex format
	CALL LCD_VAL

DONE:	GOTO DONE

#include <a:\lcd_ctrl.asm>	

	END
Program STRING_2.ASM.

The code required to display "Hello World" in the previous routine is inefficient and tedious and a much better technique is illustrated in program STRING_2.ASM.

In routine OUT_STR1, a string index is initialized to zero. The index is loaded into the W register, followed by a call to STR_1_LOOKUP which is simply a table of characters. The value of the W register is added to the PCL program counter and the character at that address is returned in the W register.

For example, if the string index is zero, the value of W when making the call to STR_1_LOOKUP is the same. Thus, the "zeroth" character ("H") is returned in W.

Note that the assembler directive;

	DT "Hello World", 0
is used.

The assembler handles this as a series of RETLW instructions. That is;

	RETLW "H"
	RETLW "e"
	RETLW "l"
	etc
	RETLW "d"	
	RETLW 0
The returned character is then tested as to whether it is a NULL character and if so, the routine OUT_STR1 is exited. Otherwise, the character is output to the LCD and the string index is incremented. This continues until the NULL character is encountered.

Note the ADDLW 0 instruction which follows the call to the lookup table. The reason for this instruction, which does not alter the value of the W register, is to perform an arithmetic operation on the content of the W register to determine if the value is zero. Note that the RETLW instruction does not affect any of the STATUS register flags.


; Progam STRING_2.ASM
;
; Illustrates how to write strings to a the PIC-n-LCD.
;
; Program clears the display and writes "Hello World" to the first line.
;
; It then writes "T_C = 17 , where 23 (decimal) is assumed to be the 
; result of a measurment.
;
; Note that this uses program LCD_CTRL.ASM which is inlcuded at the bottom
; of this program.
;
; PORTA, Bit 0 (terminal 17) ------ TX ----------> to RX on Serial LCD
;
; copyright, Peter H. Anderson, Morgan State University, Feb 18, '97

	LIST p=16f84	

#include <c:\mplab\p16f84.inc>	
	__CONFIG 11h

	CONSTANT VARS=0CH

T_C		EQU VARS+0	; used for illustration	
STR_INDEX	EQU VARS+1

	ORG 000H

	BSF STATUS, RP0		; RP1 = 0, RP0 = 1, BANK1
	BCF TRISA, 0		
	BCF STATUS, RP0		; bank 0
	
	BCF PORTA, 0		; serial output low

MAIN:
	; perform a temperature measurement	
	MOVLW .23		; but i will dummy the vaue of T_C = 23
	MOVWF T_C

	MOVLW 0CH		; clear LCD, cursor at beginning of line 1
	CALL LCD_CHAR
	
	CALL OUT_STR_1		; output first string to serial LCD

	CALL OUT_STR_2		; "T="

	MOVF T_C, W
	CALL LCD_VAL	

	CALL OUT_STR_3		; "Degrees C"

DONE: 	GOTO DONE

OUT_STR_1:
	CLRF STR_INDEX
OUT_STR_11:
	MOVF STR_INDEX, W
	CALL STR_1_LOOKUP	; fetch the character
	ADDLW 0			; set flags
	BTFSC STATUS, Z
	GOTO OUT_STR_12		; done if null		
	CALL LCD_CHAR
        INCF STR_INDEX, F
	GOTO OUT_STR_11		; keep going
OUT_STR_12:
	RETURN

STR_1_LOOKUP:
	ADDWF PCL, F
	DT "Hello World", 0AH, 0AH, 0DH, 0

OUT_STR_2:
	CLRF STR_INDEX
OUT_STR_21:
	MOVF STR_INDEX, W
	CALL STR_2_LOOKUP	; fetch the character
	ADDLW 0			; set flags
	BTFSC STATUS, Z
	GOTO OUT_STR_22		; done if null		
	CALL LCD_CHAR
        INCF STR_INDEX, F
	GOTO OUT_STR_21		; keep going
OUT_STR_22:
	RETURN

STR_2_LOOKUP:
	ADDWF PCL, F
	DT 09H, "T=", 0

OUT_STR_3:
	CLRF STR_INDEX
OUT_STR_31:
	MOVF STR_INDEX, W
	CALL STR_3_LOOKUP	; fetch the character
	ADDLW 0			; set flags
	BTFSC STATUS, Z
	GOTO OUT_STR_32		; done if null		
	CALL LCD_CHAR
        INCF STR_INDEX, F
	GOTO OUT_STR_31		; keep going
OUT_STR_32:
	RETURN

STR_3_LOOKUP:
	ADDWF PCL, F
	DT 09H, " Degrees C", 0

#include <a:\lcd_ctrl.asm>	

	END
Program STRING_3.ASM.

Program STRING_3.ASM program continually displays the value of a variable in both signed and unsigned form.

Note that the conversion from hexadecimal to signed or unsigned decimal is performed by the PIC-n-LCD using control characters 16H and 17H.

; Progam STRING_3.ASM
;
; Illustrates how to display variables in decimal using the PIC-n-LCD.
;
; Note that 16H and 17H are special characters that casue the next value
; to be printed in either signed or unsigned decimal.
;
; N is continually incremented and displayed in both signed and unsigned
; form.
;
; Note that this uses program LCD_CTRL.ASM which is included at the bottom
; of this program.
;
; PORTA, Bit 0 (terminal 17) ------ TX ----------> to RX on Serial LCD
;
; copyright, Peter H. Anderson, Morgan State University, July 5, '97

	LIST p=16f84	

#include <c:\mplab\p16f84.inc>	
	__CONFIG 11h

	CONSTANT VARS=0CH

N		EQU VARS+0
	
LOOP1		EQU VARS+1
LOOP2		EQU VARS+2

	ORG 000H

	BSF STATUS, RP0		; RP1 = 0, RP0 = 1, BANK1
	BCF TRISA, 0		
	BCF STATUS, RP0		; bank 0
	
	BCF PORTA, 0		; serial output low

	CLRF N

TOP:
	MOVLW 0CH	; clear LCD
	CALL LCD_CHAR
	
	MOVLW 16H	; print next character as a signed character
	CALL LCD_CHAR

	MOVF N, W
	CALL LCD_CHAR

	MOVLW 09H	; tab
	CALL LCD_CHAR

	MOVLW 17H	; print the next character as an unsigned quan
	CALL LCD_CHAR

	MOVF N, W
	CALL LCD_CHAR

	CALL DELAY
	
	INCF N, F	; increment N
	GOTO TOP
	

DELAY:	MOVLW	.250
	MOVWF	LOOP1		
OUTTER:
	MOVLW	.110	; close to 1.0 msec delay when set to .110
	MOVWF 	LOOP2
INNER:
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	DECFSZ	LOOP2, F
	GOTO INNER
	DECFSZ 	LOOP1, F
	GOTO OUTTER
	RETURN

#include <a:\lcd_ctrl.asm>	

	END