Write HELLO on a 16x2 character LCD
Drive an HD44780-style 16x2 LCD in 8-bit mode from a PIC16F84A: initialise it, send commands and characters, and pulse the Enable line.
Open the example in the simulator
Connections
The LCD widget uses the common 8-bit hookup:
- PORTB → data lines D0-D7
- RA0 → RS (0 = command, 1 = character)
- RA1 → R/W (kept low: the program only writes)
- RA2 → E (Enable; the LCD reads the data lines when E falls)
So the program makes PORTB all outputs (TRISB = 0x00) and RA0-RA2 outputs (TRISA = 0xF8).
Talking to the LCD
Every transfer is the same: set RS, put the byte on PORTB, then pulse E high and low. lcd_cmd clears RS for commands and lcd_data sets it for characters; both call lcd_pulse, which raises E, waits, lowers it and waits again.
Initialisation
lcd_init sends four commands:
0x38: function set, 8-bit interface, 2 lines, 5×8 dots0x0C: display on, cursor off, no blink0x01: clear the display and return home0x06: entry mode, move the cursor right after each character
After that, each character code sent with RS = 1 appears at the cursor. movlw 'H' loads the ASCII code of H (0x48) into W.
Timing on real hardware
delay_ms runs 250 passes of 4 cycles: about 1 ms at 4 MHz. That is plenty for the simulator. A real HD44780 also needs about 15 ms after power-up before the first command, and the clear command (0x01) takes about 1.5 ms, so on a breadboard add a longer delay at the start and after clearing.
Try it
- Open the example, assemble and run it. HELLO appears on the first line.
- Send command
0xC0(set the cursor to the start of line 2) before morelcd_datacalls to write on the second line. - Change
0x0Cto0x0Fto turn on a blinking cursor.
The complete program
This is examples/lcd-hello.asm, the same file the simulator loads.
;===========================================================
; Program: LCD 16x2 Display with PIC16F84A
;===========================================================
list p=16F84A
include <p16f84a.inc>
__CONFIG _FOSC_HS & _WDTE_OFF & _PWRTE_ON & _CP_OFF
;-------------------------------
; Pin connections
; LCD Data -> PORTB
; RS -> RA0
; RW -> RA1 (tied low, write only)
; EN -> RA2
;-------------------------------
cblock 0x20
temp
endc
;-------------------------------
; Reset vector
;-------------------------------
org 0x00
goto main
;-------------------------------
; Subroutines
;-------------------------------
delay_ms
movlw d'250'
movwf temp
d1 nop
nop
decfsz temp, f
goto d1
return
lcd_pulse
bsf PORTA,2 ; E=1
call delay_ms
bcf PORTA,2 ; E=0
call delay_ms
return
lcd_cmd
bcf PORTA,0 ; RS=0
movwf PORTB ; Send command
call lcd_pulse
return
lcd_data
bsf PORTA,0 ; RS=1
movwf PORTB ; Send data
call lcd_pulse
return
lcd_init
; Function Set: 8-bit, 2 line, 5x8 font
movlw 0x38
call lcd_cmd
; Display ON, Cursor OFF
movlw 0x0C
call lcd_cmd
; Clear display
movlw 0x01
call lcd_cmd
; Entry mode: auto-increment
movlw 0x06
call lcd_cmd
return
;-------------------------------
; Main Program
;-------------------------------
main
bsf STATUS, RP0
movlw 0x00
movwf TRISB ; PORTB = output
movlw 0xF8
movwf TRISA ; RA0-RA2 output, RA3-RA4 input
bcf STATUS, RP0
call lcd_init
; Write "HELLO"
movlw 'H'
call lcd_data
movlw 'E'
call lcd_data
movlw 'L'
call lcd_data
movlw 'L'
call lcd_data
movlw 'O'
call lcd_data
endless
goto endless
end