    list p=16f887
#include <p16f887.inc>
errorlevel-302
errorlevel-306
__CONFIG _CONFIG1, _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOR_OFF & _IESO_ON & _FCMEN_ON & _LVP_OFF & _DEBUG_OFF
__CONFIG _CONFIG2, _BOR40V & _WRT_OFF

	cblock	0x20			;start of general purpose registers
			count			;used in looping routines
			count1			;used in delay routine
			counta			;used in delay routine
			countb			;used in delay routine
			tmp1			;temporary storage
			tmp2
			templcd			;temp store for 4 bit mode
			templcd2

		endc

LCD_PORT	Equ	PORTB
LCD_TRIS	Equ	TRISB
LCD_RS		Equ	0x04			;LCD handshake lines
LCD_RW		Equ	0x06
LCD_E		Equ	0x05

		org	0x0000

    BSF	    STATUS,RP1	   
    BSF	    STATUS,RP0	;Bank 3 kiválasztva		
    CLRF    ANSELH      ;Port B digitálisra állítva     

START
  
    BCF     STATUS,RP1  ;Bank 1 kiválasztva
    BSF     STATUS,RP0	  	
    BSF     PORTB,6    
    BCF     PORTD,1              
    BCF     STATUS,RP0         
    BCF     STATUS,RP1
MAIN 
   	
    BTFSS   PORTB,7
    GOTO    MAIN

A1  BTFSC   PORTB,7
    GOTO    A1  	
    BSF     PORTD,1	
   
A2  BTFSS   PORTB,6
    GOTO    A2

A3  BTFSC   PORTB,6
    GOTO    A3
    BCF     PORTD,1


goto START
		       	
retlw	0x00



Initialise	clrf	count
		clrf	PORTB
		clrf	PORTB
    
SetPorts	bsf 	STATUS,		RP0	;select bank 1
		movlw	0x00			;make all pins outputs
		movwf	LCD_TRIS
		bcf 	STATUS,		RP0	;select bank 0
		call	Delay100		;wait for LCD to settle
		call	LCD_Init		;setup LCD
		clrf	count			;set counter register to zero

 
Message		movf	count, w		;put counter value in W
		call	Text			;get a character from the text table
		xorlw	0x00			;is it a zero?
		btfsc	STATUS, Z
		goto	NextMessage
		call	LCD_Char
		call	Delay255          ;255
		incf	count, f
                goto	Message
 
NextMessage	call	LCD_Line2		;move to 2nd row, first column

		clrf	count			;set counter register to zero
Message2	movf	count, w		;put counter value in W
		call	Text2			;get a character from the text table
		xorlw	0x00			;is it a zero?
		btfsc	STATUS, Z
		goto	START
		call	LCD_Char
		incf	count, f
                goto	Message2


LCD_Init	movlw	0x20			;Set 4 bit mode
		call	LCD_Cmd
		movlw	0x28			;Set display shift
		call	LCD_Cmd
		movlw	0x06			;Set display character mode
		call	LCD_Cmd
		movlw	0x0d			;Set display on/off and cursor command
		call	LCD_Cmd
		call	LCD_Clr			;clear display
		retlw	0x00

LCD_Cmd		movwf	templcd
		swapf	templcd,	w	;send upper nibble
		andlw	0x0f			;clear upper 4 bits of W
		movwf	LCD_PORT
		bcf	LCD_PORT, LCD_RS	;RS line to 0
		call	Pulse_e			;Pulse the E line high
		movf	templcd,	w	;send lower nibble
		andlw	0x0f			;clear upper 4 bits of W
		movwf	LCD_PORT
		bcf	LCD_PORT, LCD_RS	;RS line to 0
		call	Pulse_e			;Pulse the E line high
		call 	Delay5
		retlw	0x00

LCD_CharD	addlw	0x30
LCD_Char	movwf	templcd
		swapf	templcd,	w	;send upper nibble
		andlw	0x0f			;clear upper 4 bits of W
		movwf	LCD_PORT
		bsf	LCD_PORT, LCD_RS	;RS line to 1
		call	Pulse_e			;Pulse the E line high
		movf	templcd,	w	;send lower nibble
		andlw	0x0f			;clear upper 4 bits of W
		movwf	LCD_PORT
		bsf	LCD_PORT, LCD_RS	;RS line to 1
		call	Pulse_e			;Pulse the E line high
		call 	Delay5
		retlw	0x00

LCD_Line2	movlw	0xc0			;move to 2nd row, first column
		call	LCD_Cmd
		retlw	0x00

LCD_Clr		movlw	0x01			;Clear display
		call	LCD_Cmd
		retlw	0x00

LCD_HEX		movwf	tmp1
		swapf	tmp1,	w
		andlw	0x0f
		call	HEX_Table
		call	LCD_Char
		movf	tmp1, w
		andlw	0x0f
		call	HEX_Table
		call	LCD_Char
		retlw	0x00

Delay255	movlw	0xff		;delay 255 mS
		goto	d0
Delay100	movlw	d'100'		;delay 100mS
		goto	d0
Delay50		movlw	d'50'		;delay 50mS
		goto	d0
Delay20		movlw	d'20'		;delay 20mS
		goto	d0
Delay5		movlw	0x05		;delay 5.000 ms (4 MHz clock)
d0		movwf	count1
d1		movlw	0xC7	        ;delay 1mS
		movwf	counta
		movlw	0x01
		movwf	countb
Delay_0
		decfsz	counta, f
		goto	$+2
		decfsz	countb, f
		goto	Delay_0
		decfsz	count1	,f
		goto	d1
		retlw	0x00

Pulse_e		bsf	LCD_PORT, LCD_E
		nop
		bcf	LCD_PORT, LCD_E
		retlw	0x00

HEX_Table  	ADDWF   PCL       , f
            	RETLW   0x30
            	RETLW   0x31
            	RETLW   0x32
            	RETLW   0x33
            	RETLW   0x34
            	RETLW   0x35
            	RETLW   0x36
            	RETLW   0x37
            	RETLW   0x38
            	RETLW   0x39
            	RETLW   0x41
            	RETLW   0x42
            	RETLW   0x43
            	RETLW   0x44
            	RETLW   0x45
            	RETLW   0x46

Text	    	addwf	PCL, f
		        retlw	'H'
		        retlw	'e'
		        retlw	'l'
		        retlw	'l'
		        retlw	'o'
                retlw   '!'
                retlw   ' '
                retlw   ' '
                retlw   ' '
		        retlw	0x00

Text2		ADDWF   PCL, f
            	RETLW   'E'
            	RETLW   'Z'
            	RETLW   ' '
            	RETLW   'E'
            	RETLW   'G'
            	RETLW   'Y'
            	RETLW   ' '
            	RETLW   'P'
                RETLW   'R'
                RETLW   'O'
                RETLW   'B'
                RETLW   'A'
                RETLW   ' '
                RETLW   ' '
                RETLW   ' '
                RETLW   ' '
            	RETLW   0x00	


end 