*********************************************************
* TRYLCD - LCD Check out program                        *
*          Initialize LCD module and display ABCDEF...S *
*********************************************************

* Register Equates
PORTA	EQU	$00	LCD display data
PORTB	EQU	$01	Keypad Row4,3,2,1;Col1,2,3,4
PORTC	EQU	$02	Fan*,Heat*,Cool*,Beep;ADen*,E,RS,R/W
DDRA	EQU	$04	Data direction, Port A (all output)
DDRB	EQU	$05	Data direction, Port B (7-4in,3-0out)
DDRC	EQU	$06	Data direction, Port C (all output)

* RAM Equates
TEMPA	EQU	$9E	One byte temp storage location
TEMPX	EQU	$9F	One byte temp storage location

	ORG	$100
* Set Port data patterns and directions
TRYLCD	LDA	#$E8	Fan*,Heat*,Cool*,Beep;ADen*,E,RS,R/W
	STA	PORTC	Initial values for Thermostat controls
	LDA	#$FF
	STA	DDRA	Port A all outputs
	STA	DDRC	Port C all outputs

* LCD display peripheral needs to be initialized
	LDA	#$01
	JSR	WCTRL	Clear
	LDA	#$02
	JSR	WCTRL	Home
	LDA	#$38
	JSR	WCTRL	Function Set- 8-bit,2-line,5X7
	LDA	#$0C
	JSR	WCTRL	Display on, Cursor off
	LDA	#$06
	JSR	WCTRL	Entry mode- Inc addr, no shift
	
	LDA	#'A	ASCII 'A'
DLP	JSR	WDAT	Display a character
	INCA		To next ASCII character
	CMP	#'T	Go ABCDEFGHIJKLMNOPQRS & stop
	BNE	DLP	Loop till T
HERE	BRA	HERE	Stop

***************
* SUBROUTINES *
***************

********************************************************
* WCTRL - Write control word to LCD peripheral         *
*	Enter with control word in accumulator         *
*	Return with original value of X                *
*	Delay ~4.5mS if A=$01 or $02 else delay ~120S *
********************************************************
WCTRL	STX	TEMPX	Save X
	STA	PORTA	Write control word to LCD
	BSET	2,PORTC	E->1
	BCLR	2,PORTC	E->0
	LDX	#20	20*6~*1S/~=120S
L120U	DECX		Delay loop ~120S
	BNE	L120U	20-19,19-18...1-0
	CMP	#$02	Commands $01 & $02 req extra delay
	BHI	ARN5M	If command > $02 skip long delay
L5M	JSR	ANRTS	JSR+RTS TAKES 12~ (just want delay)
	DECX		TAKES 3~ (X=0->1 on first pass)
	BNE	L5M	3~ Loop 256*18~*1S/~=4.608mS Delay
ARN5M	LDX	TEMPX	Restore X
ANRTS	RTS		** RETURN **

********************************************************
* WDAT - Write data word to LCD peripheral             *
*	Enter with data word in accumulator            *
*	Return with original values of X & A           *
*	Delay ~120S after data write                  *
********************************************************
WDAT	STX	TEMPX	Save X
	STA	TEMPA	Save A
	STA	PORTA	Write data word to LCD
	BSET	1,PORTC	RS->1
	BSET	2,PORTC	E->1
	BCLR	2,PORTC	E->0
	BCLR	1,PORTC	RS->0
	LDX	#20	20*6~*1S/~=120S
L120	DECX		Delay loop ~120S
	BNE	L120	20-19,19-18...1-0
	LDA	TEMPA	Restore A
	LDX	TEMPX	Restore X
	RTS		** RETURN **

