please dont rip this site

PIC 4 bit 16x2 Hitachi LCD driver

by Barry


Another simple Hitachi LCD display control routine. This ASM code can drive a display with a PIC16F628 with the control lines on any PORTB pins - the actual connection are easily redefined in the code by changing the initial definitions.

; LCD driver by Barry (tron at zoidberg dot nl)
	list p=16f628

	; LCD control lines
	#define	LCD_RS	5		; 0 = Command, 1 = Data
	#define LCD_RW	6		; 0 = Write, 1 = Read
	#define LCD_E	4		; 1 to send data
	#define LCD_D4	0		; 4 data lines
	#define LCD_D5	1
	#define LCD_D6	2
	#define LCD_D7	3
; Registers we will be using
	#define R_DEL1			0x20	; Delay register
	#define R_DEL2			0x21	; Delay register
	#define R_WDATA_TMP1	0x22
	#define R_WDATA_TMP2	0x23
	#define R_SEND_W_TMP	0x24
	#define	R_WTMP			0x27	; W storage
	#define	R_STMP			0x28	; STATUS storage
	#define	R_STMP1			0x2E	; Send temp register
	#define R_PSEC			0x2F

	org	0
	goto	init

; Blank interrupt routine - simply saves and restores status & W
; Add code to do something useful...	
	org 4
	movwf	R_WTMP		; Save W & STATUS
	swapf	STATUS, W
	movwf	R_STMP
	bcf		STATUS, RP0
	movlw	b'11111000'	; Clear interrupt bits
	andwf	INTCON, F
	swapf	R_STMP, W	; Restore W & STATUS
	movwf	STATUS
	swapf	R_WTMP, F
	swapf	R_WTMP, W

; Init sets all I/O lines as outputs and selects PORTA as
; I/O lines by turning the comparators off.
	clrf	PORTB
	clrf	PORTA
	movlw	0x07
	movwf	CMCON		; Comparators off - PORTA on
	bsf		STATUS, RP0	; Select bank 1
	movlw	b'00000000'	; Select PORTB as outputs
	movwf	TRISB
	movlw	b'00000000'	; Select PORTA as outputs
	movwf	TRISA
	bcf		STATUS, RP0	; Select bank 0

	; Begin the LCD initialisation
	; Power up timer should give delay, but do it anyway
	movlw	0x14
	call	delay
	; Send the command to select 4-bit mode first
	movlw	0x02			; Still in 8-bit, so appears as 0x20 to LCD
	call	w_to_data
	call	pulse_e

; Should now be in 4-bit mode - send init
	movlw	b'00101000'
	call	send_w
	movlw	b'00001110'
	call	send_w
	movlw	b'00000110'
	call	send_w

	bcf		PORTB, LCD_RS	; Command mode
	movlw	b'00000010'		; Return cursor to home
	call	send_w
	bsf		PORTB, LCD_RS	; Data mode
	call	delay_1ms		; Takes a couple of ms
	movlw	'H'
	call	send_w
	movlw	'e'
	call	send_w
	movlw	'l'
	call	send_w
	movlw	'l'
	call	send_w
	movlw	'o'
	call	send_w
	movlw	'!'
	call	send_w
	goto	main_loop
; ----------------------

	movwf	R_SEND_W_TMP
	swapf	R_SEND_W_TMP, F
	movlw	0x0F
	andwf	R_SEND_W_TMP, W
	call	w_to_data
	call	pulse_e
	swapf	R_SEND_W_TMP, F
	movlw	0x0F
	andwf	R_SEND_W_TMP, W
	call	w_to_data
	call	pulse_e

	movwf	R_WDATA_TMP1
	movf	PORTB, W
	movwf	R_WDATA_TMP2
	bcf		R_WDATA_TMP2, LCD_D4
	bcf		R_WDATA_TMP2, LCD_D5
	bcf		R_WDATA_TMP2, LCD_D6
	bcf		R_WDATA_TMP2, LCD_D7
	btfsc	R_WDATA_TMP1, 0
		bsf		R_WDATA_TMP2, LCD_D4
	btfsc	R_WDATA_TMP1, 1
		bsf		R_WDATA_TMP2, LCD_D5
	btfsc	R_WDATA_TMP1, 2
		bsf		R_WDATA_TMP2, LCD_D6
	btfsc	R_WDATA_TMP1, 3
		bsf		R_WDATA_TMP2, LCD_D7
	movf	R_WDATA_TMP2, W
	movwf	PORTB
	bsf		PORTB, LCD_E
	bcf		PORTB, LCD_E
	call	delay_1ms
	call	delay_1ms

; Calls the delay_1ms routine the number of times specified by
; the W register.
	movwf	R_DEL2
	call	delay_1ms
	decfsz	R_DEL2, F
		goto	delay_loop
; When called gives a delay of about 1000 cycles, or 1ms at 4Mhz
; before the next instruction is executed.
	movlw	d'248'
	movwf	R_DEL1
	decfsz	R_DEL1, F
		goto	delay_1ms_loop


Stephen van Schalkwyk Says: " I am quite new to microprocessors and this code is going to be a great help - many thanks!
" +

file: /Techref/microchip/4bitlcd-b.htm, 4KB, , updated: 2010/7/2 16:39, local time: 2024/7/18 06:58, owner: RAK-a_kats-U,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> PIC 4 bit 16x2 Hitachi LCD driver by Barry</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2024 contributors:
o List host: MIT, Site host, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!