;###############################################################################
;# TITLE   "LCD Display"	
;# 				
;#      Program         :External LCD(for 16F877)
;#      Version         :1.0	
;#      Revision Date   :   	
;#      Author          : Naveen Raj
;#									
;# Program demonstrates use of External LCD Module
;# general purpose library module.
;###############################################################################
    	list p=16f877			
        include <P16F877.INC>      			
 

    include "XLCD.inc"                      ;This include all required files and variables.	
    
;The user can deaclare different definitions and make necessary commands    
XLCDCursorOnBlinkOn     equ     B'00001111' ;Display on Cursor on and Blink on
XLCDCursorOnBlinKOff    equ     B'00001110' ;Display on Cursor on and Blink off
XLCDDisplayOnCursorOff  equ     B'00001100' ;Display on Cursor off     
XLCDDisplayOff          equ     B'00001000' ;Display off  	
XLCDAddrIncrOnShiftOn   equ     B'00000111' ;increment DDRAM address and Display shift during read and wite 
XLCDAddrIncrOnShiftOff  equ     B'00000110' ;increment DDRAM address no Display shift during read and wite
XLCDAddrIncrOffShiftOff equ     B'00000100' ;Do not increment DDRAM address and shift during read and wite
XLCDDisplayMoveRight    equ     B'00011100' ;Display moves right,DDRAM Content remain unchanged
XLCDDisplayMoveLeft     equ     B'00011000' ;Display moves left,DDRAM Content remain unchanged
XLCDCursorMoveRight     equ     B'00010100' ;Cursor moves right,DDRAM Content remain unchanged
XLCDCursorMoveLeft      equ     B'00010000' ;Cursor moves left,DDRAM Content remain unchanged 

UDATA   
reg res 1                                   ;used in checking flag status in non blocking mode
						
Starthere   code    0x0000			
            goto    Start					    			
Strt        code
;Message	dt      "LCD:", 0           ;display of string

Start
        BANKSEL PORTA                       ;make PORTA digital(control signals are on PORTA)
        clrf    PORTA
        BANKSEL ADCON1
        movlw   0x06
        movwf   ADCON1

        PAGESEL XLCDInit
        call    XLCDInit                    ;initialize LCD module(machine cycle talken for this call
                                            ;is depended on MPamOption and if in READBF mode it is depended
                                            ;on LCDmodule
;        mXLCDSendMsg    Message             ;display of string (this is always blocking)

        goto    StartDisplay
 
        #if XLCDBLOCKING ==0                ;this part is assembled and called only in non blocking mode
PollBF:                                     ;XLCDIsBusy is called, if LCD module is busy w return with 1 else 0
        call    XLCDIsBusy                  ;in actual practice the user can continue with his code rather that
        BANKSEL reg                         ;polling like this and he can check by calling XLCDIsBusy to see if 
        movwf reg                           ;the module is free before sending his next command to LCD.  
        btfsc   reg,0                       ;(here in the example XLCDIsBusy is in a loop and stays there untill
        goto    PollBF                      ;the busy flag is clreared)
        return        
        #endif


StartDisplay        
        
        #if XLCDINTERFACE ==8               ;if data interface is 8 bit                    
        #if XLCDBLOCKING == 0               ;if non blocking call PollBF
        call    PollBF                      ;XLCDIsBusy is called to check whecher the LCDmodule is free
        #endif                              ;before calling XLCDCommand or XLCDPut  
        movlw   '8'
        call    XLCDPut             
        #endif
        
        #if XLCDINTERFACE ==4               ;if data interface is 4 bit
        #if XLCDBLOCKING == 0               ;if non blocking call PollBF
        call    PollBF                      ;XLCDIsBusy is called to check whecher the LCDmodule is free
        #endif                              ;before calling XLCDCommand or XLCDPut
        movlw   '4'
        call    XLCDPut
        #endif


        #if XLCDBLOCKING == 0               ;if non blocking call PollBF
        call    PollBF                      ;XLCDIsBusy is called to check whecher the LCDmodule is free
        #endif                              ;before calling XLCDCommand or XLCDPut        
        movlw   'b'
        call    XLCDPut 



        #if XLCDBLOCKING == 0               ;if non blocking call PollBF
        call    PollBF                      ;XLCDIsBusy is called to check whecher the LCDmodule is free
        #endif                              ;before calling XLCDCommand or XLCDPut   
        movlw   'i'
        call    XLCDPut


        #if XLCDBLOCKING == 0               ;if non blocking call PollBF
        call    PollBF                      ;XLCDIsBusy is called to check whecher the LCDmodule is free
        #endif                              ;before calling XLCDCommand or XLCDPut
        movlw   't'
        call    XLCDPut

     
        #if XLCDBLOCKING == 0               ;if non blocking call PollBF
        call    PollBF                      ;XLCDIsBusy is called to check whecher the LCDmodule is free
        #endif                              ;before calling XLCDCommand or XLCDPut
        movlw   ','
        call    XLCDPut        
        
        
                
        #if XLCDINTERFACE ==4
        
            #if XLCDNIBBLESEL == 0          ;if Lower Nibble 
                #if XLCDBLOCKING == 0       ;if non blocking call PollBF
                call    PollBF              ;XLCDIsBusy is called to check whecher the LCDmodule is free
                #endif                      ;before calling XLCDCommand or XLCDPut                    
            movlw   'L'
            call    XLCDPut
            #endif
            
            
            
            
            #if XLCDNIBBLESEL == 1                    
                #if XLCDBLOCKING == 0       ;if non blocking call PollBF
                call    PollBF              ;XLCDIsBusy is called to check whecher the LCDmodule is free
                #endif                      ;before calling XLCDCommand or XLCDPut 
            movlw   'U'
            call    XLCDPut
            #endif
        #endif
    
    
    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
    #endif                                  ;before calling XLCDCommand or XLCDPut or other XLCD Commands
                                            ;likeXLCDLine1,XLCDLine2,XLCDReturnHome,XLCDClear etc           
    call    XLCDL2home                      ;goto   Line 2 of the LCD        

    
    #if XLCDMODE == 1                       ;if mode is delay Non blocking is not possible    
    movlw   'D'
    call    XLCDPut
    movlw   'e'
    call    XLCDPut   
    movlw   'l'
    call    XLCDPut
    movlw   'a'
    call    XLCDPut
    movlw   'y'
    call    XLCDPut            
    #endif
    
            
    #if XLCDMODE == 0
    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
    #endif                                  ;before calling XLCDCommand or XLCDPut
    movlw   'B'
    call    XLCDPut
    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
    #endif                                  ;before calling XLCDCommand or XLCDPut
    movlw   'F'
    call    XLCDPut
    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
    #endif                                  ;before calling XLCDCommand or XLCDPut
    movlw   'R'
    call    XLCDPut
    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
    #endif                                  ;before calling XLCDCommand or XLCDPut
    movlw   'e'
    call    XLCDPut
    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
    #endif                                  ;before calling XLCDCommand or XLCDPut   
    movlw   'a'
    call    XLCDPut
    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
    #endif                                  ;before calling XLCDCommand or XLCDPut
    movlw   'd'
    call    XLCDPut
   
    #endif 


;user defined commands
;    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
;    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
;    #endif                                  ;before calling XLCDCommand or XLCDPut
    
;    movlw   XLCDDisplayMoveLeft             ;Move entire Display to left, DDRAM Content not changed
;    call    XLCDCommand
    
    
;    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
;    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
;    #endif                                  ;before calling XLCDCommand or XLCDPut
    
;    movlw   XLCDDisplayMoveRight            ;Move entire Display to right, DDRAM Content not changed
;    call    XLCDCommand 
    
    
;    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
;    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
;    #endif                                  ;before calling XLCDCommand or XLCDPut       
    
;    movlw   XLCDCursorMoveLeft              ;Move Cursor to left, DDRAM Content not changed
;    call    XLCDCommand
    
;    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
;    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
;    #endif                                  ;before calling XLCDCommand or XLCDPut    
    
;    movlw   XLCDCursorMoveRight             ;Move Cursor to right, DDRAM Content not changed
;    call    XLCDCommand
 
    #if XLCDBLOCKING == 0                   ;if non blocking call PollBF
    call    PollBF                          ;XLCDIsBusy is called to check whecher the LCDmodule is free
    #endif                                  ;before calling XLCDCommand or XLCDPut
    
    movlw   XLCDDisplayOnCursorOff          ;Dispay on cursor off
    call    XLCDCommand    
       
    goto    $    

   

        end
