*
* Lcd2.asm ---- New 16X2 LCD sample program for FOX11 board
*		with BUFFALO monitor, (c)2003, EVBplus.com
*		Written by Wayne Chu
*
*    Function:	This program displays a message on a 16X2 LCD display module
*		by calling sel_inst(instruction), sel_data, wrt_nibble and
*               wrt_byte subroutines. These subroutines can be used for
*               controlling many different types of LCD display.
*
*		The LCD driver source code is included in this example program
*
*  This sample program uses 4-bit transfer via port F:
;  PF0 ------- RS ( register select, 0 = register transfer, 1 = data transfer).
;  PF1 ------- Enable ( write pulse )
;  PF4 ------- Data Bit 4 of LCD
;  PF5 ------- Data Bit 5 of LCD
;  PF6 ------- Data Bit 6 of LCD
;  PF7 ------- Data Bit 7 of LCD
;
;
; Timing of 4-bit data transfer is shown on page 11 of the Seiko LCD
; application note on the distribution CD. The file name is Seikolcd.pdf
;

DB0:		equ	1
DB1:		equ	2
*

STACK:		equ	$8FFF

ONE_MS		equ	167	; for 8 MHz    1000us/6us=167
FIVE_MS		equ	835
TEN_MS		equ	1670


PORTF   	equ	$1403
d40us:		equ	15	; 15x6us= 90 us, 60us ok
REG_SEL:	equ	DB0	; 0=reg, 1=data
ENABLE:		equ     DB1
NOT_REG_SEL:	equ	$FE
NOT_ENABLE:	equ     $FD
NOT_RESET       equ     $F7     ; force bit 3 = 0

  		org	0
temp1:		rmb	1
pfimg:		rmb	1

LCDimg:		equ	pfimg
LCD_RSimg:	equ	pfimg
LCD_ENimg:	equ	pfimg

LCD:		equ	PORTF
LCD_RS:		equ	PORTF
LCD_EN:		equ	PORTF
*
	org	$D000
	jmp	start

delay_10ms:
	nop
d10ms:  pshx
	ldx     #TEN_MS
  	bsr	del1
  	pulx
	rts
d5ms:   pshx
	ldx     #FIVE_MS
  	bsr	del1
	pulx
	rts
del1:	dex
	inx
	dex
	bne	del1
	rts
*
lcd_ini:
	clra
	staa	pfimg
	staa	PORTF
       	ldx	#inidsp   	; point to init. codes.
       	jsr    	sel_inst
       	ldab   	0,x
       	inx
onext:	ldaa   	0,x
       	jsr    	wrt_nibble   	; initiate write pulse.
       	inx
	jsr	d5ms
       	decb
       	bne    	onext
   	rts
inidsp: fcb	12		; number of high nibbles
*				; use high nibbles only, low nibbles are ignored
	fcb	$30		; 1st reset code, must delay 4.1ms after sending
        fcb	$30		; 2nd reste code, must delay 100us after sending

; all following 10 nibbles must be delay 40us each after sending
	fcb     $30             ; 3rd reset code,
	fcb	$20		; 4th reste code,
        fcb	$20   		; 4 bit mode, 2 line, 5X7 dot
	fcb	$80   		; 4 bit mode, 2 line, 5X7 dot
        fcb	$00		; cursor increment, disable display shift
	fcb	$60		; cursor increment, disable display shift
        fcb	$00		; display on, cursor off, no blinking
	fcb	$C0		; display on, cursor off, no blinking
	fcb	$00		; clear display memory, set cursor to home pos
	fcb	$10		; clear display memory, set cursor to home pos
*
sel_data:
	psha
*	bset	LCD_RSimg REG_SEL	; select instruction
	ldaa	LCD_RSimg
	oraa	#REG_SEL
	bra	sel_i
sel_inst:
	psha
*	bclr	LCD_RSimg REG_SEL	; select instruction
	ldaa	LCD_RSimg
	anda	#NOT_REG_SEL
sel_i:	staa	LCD_RSimg
	anda    #NOT_RESET              ; force bit3=0, no matter what happens
	staa	LCD_RS
	pula
        rts
*
lcd_line1:
	ldab    #16
	jsr    	sel_inst		; select instruction
       	ldaa   	#$80     		; starting address for line1
	bra	line3
lcd_line2:
	ldab    #16
	jsr    	sel_inst
       	ldaa   	#$C0     		; starting address for line2
line3: 	bsr    	wrt_byte
*
       	jsr    	sel_data

* at entry, x must point to the begining of the message,
*           b = number of characters to be sent out
*
msg_out:
	ldaa   	0,x
       	jsr    	wrt_byte
       	inx
       	decb
       	bne    	msg_out
	rts
*
*       @ enter, high nibble of Accu A=data to be sent out
*     		 low nibble of Accu A is ignored
*
wrt_nibble:
       	anda   	#$f0     		; mask out 4 low bits
       	staa   	temp1 			; save high nibble
       	ldaa   	LCDimg    		; get LCD port image
       	anda   	#$0f     		; need low 4 bits
       	oraa   	temp1    		; add it with high nibble
        staa   	LCDimg    		; save it
       	anda    #NOT_RESET              ; force bit3=0, no matter what happens
*      	                                ; to prevent system reset
       	staa   	LCD    			; output data to LCD port
        jsr	enable_pulse
 	rts
*
*       @ enter, a=data to output
*
wrt_byte:
	pshx
       	psha        			; save it tomporarily.
       	anda   	#$f0     		; mask out 4 low bits.
       	staa   	temp1 			; save nibble value.
       	ldaa   	LCDimg    		; get LCD port image.
       	anda   	#$0f     		; need low 4 bits.
       	oraa   	temp1    		; add in low 4 bits.
       	staa   	LCDimg    		; save it
       	anda    #NOT_RESET              ; force bit3=0, no matter what happens
       	staa   	LCD    			; output data
	bsr	enable_pulse

	pula
 	asla            		; move low bits over.
	asla
      	asla
       	asla
       	staa   	temp1			; store temporarily.
*
       	ldaa   	LCDimg 			; get LCD port image.
       	anda   	#$0f   			; need low 4 bits.
       	oraa   	temp1  			; add in loiw 4 bits.
       	staa   	LCDimg    		; save it
       	anda    #NOT_RESET              ; force bit3=0, no matter what happens
	staa   	LCD    			; output data
*
	bsr	enable_pulse
        ldx     #d40us
  	jsr     del1
        pulx
       	rts
*
enable_pulse:
*	bset	LCD_ENimg ENABLE	; ENABLE=high
	ldaa	LCD_ENimg
	oraa	#ENABLE
	staa	LCD_ENimg
	anda    #NOT_RESET              ; force bit3=0, no matter what happens
	staa	LCD_EN
*	bclr	LCD_ENimg ENABLE	; ENABLE=low
	ldaa	LCD_ENimg
	anda	#NOT_ENABLE
	staa	LCD_ENimg
	anda    #NOT_RESET              ; force bit3=0, no matter what happens
	staa	LCD_EN
       	rts
*
start:  lds	#STACK
   	jsr	delay_10ms	; delay 20ms during power up
    	jsr	delay_10ms
    	jsr	lcd_ini		; initialize LCD
                      	
	ldx    	#MSG1		; MSG1 for line1, x points to MSG1
       	jsr	lcd_line1
*
    	ldx    	#MSG2		; MSG2 for line2, x points to MSG2
        jsr	lcd_line2
  	swi                     ; go back to buffalo monitor


MSG1:   FCC     " YOU DESERVE A  "
MSG2:   FCC     "  BREAK TODAY   "

       	end


