4-bit LCD on 18M2 (working)

Nikadie

New Member
This works for me (YMMV):

Code:
'* Symbols ************************************************

	symbol LcdSE = c.6
	symbol LcdRS = c.7

	symbol LcdByte = b1

'* Main Routine *******************************************

	
	gosub LCD_Init

	'send ABC...NOP to LCD
	for LcdByte = 65 to 80	
		gosub LCD_WrChr
	next LcdByte
	
	gosub LCD_Line2
		
	'send 012...DEF to LCD
	for LcdByte = 48 to 57	
		gosub LCD_WrChr
	next LcdByte
	for LcdByte = 65 to 70	
		gosub LCD_WrChr
	next LcdByte

	end


'* Initialise LCD *****************************************

LCD_Init:	
	
	'set relevant pins to output
	dirsb = %00001111
	dirsc = %11000000

	'wait for LCD to stabilise (>40ms after Vcc > 2.7V)
	pause 200

	'send 0011 three times to initialise LCD by instruction
	outpinsb = %00000011
	pulsout c.6,1
	pause 10
	pulsout c.6,1
	pause 1
	pulsout c.6,1
	pause 1
	
	'send 0010 to set interface to 4-bit
	outpinsb = %00000010
	pulsout c.6,10
	pause 1

	'interface is now 4-bit, we can use the LCD_WrIns routine
	
	'Function Set ...     0  0  1 DL  N  F  -  - 
	LcdByte = %00101000 'DL = 0, 4-bit: N = 1, 2 lines: F = 0, 5x8 font 
	gosub LCD_WrIns
	
	'Display Control ...  0  0  0  0  1  D  C  B
	LcdByte = %00001000 'D = 1, disp off: C = cursor off
	gosub LCD_WrIns
	
	'Display Control ...  0  0  0  0  1  D  C  B
	LcdByte = %00001100 'D = 1, disp on: C = cursor off
	gosub LCD_WrIns

	gosub LCD_Clear
	
	'Entry Mode ...       0  0  0  0  0  1 I/D S
	LcdByte = %00000110 'I/D = 1, increment: S = 0, no display shift
	gosub LCD_WrIns

	
	return
	
	
'* Display Character On LCD *******************************

LCD_WrChr:

	'set RS high (write data)
	high LcdRS
	
	'send high nibble by shifting it to the low nibble of port B
	outpinsb = LcdByte / 16
	pulsout LcdSE,10
	
	'send low nibble by masking it onto port B
	outpinsb = LcdByte & %00001111
	pulsout LcdSE,10

	return


'* Send Instruction to LCD ********************************

LCD_WrIns:

	'set RS low (issue command)
	low LcdRS
	
	'send high nibble by shifting it to the low nibble of port B
	outpinsb = LcdByte / 16
	pulsout LcdSE,10
	
	'send low nibble by masking it onto port B
	outpinsb = LcdByte & %00001111
	pulsout LcdSE,10

	return
	
	
'* Clear LCD **********************************************

LCD_Clear:

	LcdByte = 1
	gosub LCD_WrIns

	return


'* Move To Line 1 *****************************************

LCD_Line1:

	LcdByte = 128
	gosub LCD_WrIns

	return


'* Move To Line 2 *****************************************

LCD_Line2:

	LcdByte = 192
	gosub LCD_WrIns

	return
 

Attachments

westaust55

Moderator
Since you started a new thread on completion of your project, it may have been better to post in the completed projects section.
Nevertheless, congratulations
 
Top