;***** JAVRBasic LCD RuneTime module *****
.EQU LCDPORT PORTB
.EQU LCDDDR  DDRB
.EQU LCDRS   3	; PB3
.EQU LCDEN   2	; PB2

;**** your LCD (char)data output goes here ****
;* You need to set the correct LCD d/c bit    *
;* Enter with r24 holding the char to display *
;**********************************************
_LCDInit:
	; write your own HD44780 etc, 4bit or 8bit setup.
	; there are several examples all over the Web.
	; Here is a 4bit example.  R/W is tied to GND,
	; and is always in WR mode.
	ser  	zl
	out  	LCDDDR,zl
	clr  	zl
	out  	LCDPORT,zl
	ldi 	r24,0x28	; Function set
	rcall	_LCDcout
	ldi 	r24,0x28
	rcall	_LCDcout
	ldi 	r24,0x28
	rcall	_LCDcout
	ldi 	r24,0x06	; Entry mode set
	rcall	_LCDcout  
	ldi 	r24,0x0c	; Disp on, hide cursor
	rcall	_LCDcout
	rcall 	_5msdly
	ldi  	r24,1		; CLS
	rcall	_LCDcout
	rjmp 	_5msdly
;
_5msdly:
	ldi  	zl,5
	rjmp 	_delayMsb
;
;**********************************
;* All ASCII text chars come here *
;**********************************
_LCDdout:
	sbi  	LCDPORT,LCDRS
	rjmp 	_LCDout
;
;**** Used with '@' in Basic ****
;** Lprint @2, "String" ' etc. **
;* to send LCD command such as: *
;* CLS,HOME,SETCURSOR, etc.     *
;* You need to set the d/c bit  *
;* Enter with zl holding the cmd*
;********************************
_LCDcout:
	cbi  	LCDPORT,LCDRS
	cpi  	r24,3
	brsh	_LCDout
	rcall	_LCDout
	rjmp	_5msdly
_LCDout:
	push 	r24
	rcall	__lcduout
	pop  	r24
	swap 	r24
__lcduout:
	push 	r25
	in  	r25,LCDPORT
	andi	r25,0x0f
	andi 	r24,0xf0
	or  	r24,r25
	out  	LCDPORT,r24
	nop
	sbi  	LCDPORT,LCDEN
	nop
	cbi  	LCDPORT,LCDEN
	pop  	r25
	ret
;
;;*** Call this to divert _txRS232 to LCD ***
;* AVR -> PC Host Off, AVR -> LCD screen On*
;*******************************************
_LCDfopen:
	lds  	r25,_sysflags
	sbr 	r25,0x20
	sts  	_sysflags,r25
	ret
;
;*** Call this to divert LCD back to _txRS232 ***
;* AVR -> PC Host On, AVR -> LCD screen Off     *
;************************************************
_LCDfclose:
	lds  	r25,_sysflags
	cbr 	r25,0x20
	sts  	_sysflags,r25
	ret
;
