Page Sponsored by 
Back to Victorian University Electronics Intranet Home Page Mirror.

4Bit LCD test program for PIC16F877

Photo showing PIC004B target processor board and the LCD mounted on a 1 inch prototype board. Both boards are mounted on a DT004 motherboard.
 

Photo showing another view of the processor board.

Sample program for 4bit control of a LCD.

;4bit lcd test program for 16F877
;Written by Peter Averill
;14 June 1999
;assembled with mpasm v1.3
;
;
        list p=16c84, f=inhx8m

       include "p16c84.inc"
;
lcd_data         equ     PORTB
lcd_data_tris    equ     TRISB
lcd_cntl         equ     PORTB
;
;
;
; lcd display commands and control signal names.
;
;
e               equ     3      ; lcd enable control line
r_w             equ     1      ; lcd read/write control line
rs              equ     2      ; lcd register select control line
;

disp_on         equ     0x0c
clr_disp        equ     0x01
entry_inc       equ     0x06
dd_ram_addr     equ     0x80
;
;
temp1           equ     0x20
temp            equ     0x21
char            equ     0x22
msd             equ     0x23
lsd             equ     0x24 
;
;
;
                org     0              ; reset vector location
reset           goto    start          ;
;
;
;
;
                org     5
start                               ; power_on reset (beginning of program)

                movlw   TRISA
                movwf   FSR
                movlw   00
                movwf   INDF
                incf    FSR,    f
                movwf   INDF
;
;
; initilize the lcd display module
;
                clrf    lcd_cntl        ; all port output should output low.

display_init
                call    lcd_delay
                call    lcd_delay
                call    lcd_delay
                movlw   0x33
                call    send_cmd
                movlw   0x32
                call    send_cmd
                movlw   0x28
                call    send_cmd
                movlw   0x0e
                call    send_cmd
                movlw   0x02
                call    send_cmd
                movlw   0x01
                call    send_cmd
        
;demonstration of the use of a table to output a message
                movlw   0               ;table address of start of message
dispmsg 
                movwf   temp1           ;temp1 holds start of message address
                call    table
                andlw   0ffh            ;check if at end of message (zero
                btfsc   STATUS,Z        ;returned at end)
                goto    out             
                call    send_char       ;display character
                movf    temp1,w         ;point to next character
                addlw   1
                goto    dispmsg
out
loop
                goto    loop            ;stay here forever

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
lcd_delay       movlw   20              ;
                movwf   msd             ; 
                clrf    lsd             ;
loop2           decfsz  lsd, f          ; delay time = msd * ((3 * 256) + 3) * tcy
                goto    loop2           ;
                decfsz  msd, f          ;
end_lcd_delay
                goto    loop2           ;
                return
;
;
;
;*******************************************************************
;* the lcd module subroutines                                      *
;*******************************************************************
;
;*******************************************************************
;*sendchar - sends character to lcd                                *
;*this routine splits the character into the upper and lower       * 
;*nibbles and sends them to the lcd, upper nibble first.           *
;*******************************************************************
;
send_char
                movwf   char            ;character to be sent is in w
                movf    char, w          
                andlw   0xf0            ;get upper nibble
                movwf   lcd_data        ;send data to lcd
                bsf     lcd_cntl, rs    ;set lcd to data mode
                bsf     lcd_cntl, e     ;toggle e for lcd
                bcf     lcd_cntl, e
                call    lcd_delay
                swapf   char, w
                andlw   0xf0            ;get lower nibble
                movwf   lcd_data        ;send data to lcd
                bsf     lcd_cntl, rs    ;set lcd to data mode            
                bsf     lcd_cntl, e     ;toggle e for lcd
                bcf     lcd_cntl, e
                call    lcd_delay
                return
;
;
;
;*******************************************************************
;* send_cmd - sends command to lcd                                 *
;* this routine splits the command into the upper and lower        * 
;* nibbles and sends them to the lcd, upper nibble first.          *
;*******************************************************************

send_cmd
                movwf   char            ; character to be sent is in w
                movf    char,w          
                andlw   0xf0            ; get upper nibble
                movwf   lcd_data        ; send data to lcd
                bsf     lcd_cntl,e      ; toggle e for lcd
                bcf     lcd_cntl,e
                call    lcd_delay
                swapf   char,w
                andlw   0xf0            ; get lower nibble
                movwf   lcd_data        ; send data to lcd
                bsf     lcd_cntl,e      ; toggle e for lcd
                bcf     lcd_cntl,e
                call    lcd_delay
                return
;
;
;
;
table
                addwf   PCL, f          ;jump to char pointed to in w reg
                retlw   'D'
                retlw   'o'
                retlw   'n'
                retlw   't'
                retlw   'r'
                retlw   'o'
                retlw   'n'
                retlw   'i'
                retlw   'c'
                retlw   's'
                retlw   0

                org     0x2007
                data    0xf31

                end
Written by Peter Averill
Victoria Universtiy TAFE
Electronics Technology Department
14 June 1999

Copying these pages:
 
Back to Victorian University Electronics Intranet Home Page Mirror.
DonTronics Links:
Home Products Prices Directory Order Contact New Books Files Links FAQ