;############################################################################
;# TITLE   "USART General Purpose Library Checking Software"
;#
;#
;#      Program:        PIC18Tst.ASM
;#      Version:        1.0
;#      Revision Date:
;#      Author:         Gaurang Kavaiya
;#
;#
;# Program demonstrates use of USART general purpose library module.
;############################################################################




        list p=18f452
        include <P18f452.INC>

        include "UARTInt.inc"


UARTTstRAM      UDATA
ISR_STAT    RES 02			;For saving STATUS value
#define     ISR_BSR	ISR_STAT+1	;For saving BSR




UARTTstShr	 UDATA_ACS
ISR_W	    RES 01	               ;For Saving W reg. value





STARThere       CODE    0x00
        goto    START



INTHserv        CODE    0x08	     ;High priority vector

;INTLserv         CODE    0x018      ;Low priority vector

        goto    ISR


InteruptServiceLocation     CODE   
ISRoutine

ISR:
;context savings (very important)
        movwf	ISR_W		        ;save Wreg in ISR_W, If NOP is used above
	movff	STATUS, ISR_STAT        ;put STATUSreg (swapped) into ISR_STAT
	movff   BSR, ISR_BSR            ;Save BSR

;call the interrupt function
	call	    UARTIntISR	        ;Call general purpose RTC interrupt service routine

;restore context

        movff   ISR_BSR,  BSR           ;Restore BSR
        movf    ISR_W,W
        movff   ISR_STAT, STATUS        ;Restore status
	retfie






Main    CODE

START
        ;Define the required TRIS and PORT settings here
        
;Make sure that UART pins are defined as i/p
        call    UARTIntInit


;Display Hello

        movlw   'H'
        call    UARTIntPutCh
        movlw   'E'
        call    UARTIntPutCh
        movlw   'L'
        call    UARTIntPutCh
        movlw   'L'
        call    UARTIntPutCh
        movlw   'O'
        call    UARTIntPutCh



;Change the pre-claculated baudrate, If required
;        mSetUARTBaud    .9600

        banksel vUARTIntStatus
WaitRxData
;Check if Receive buffer is full        
        btfss   vUARTIntStatus,UARTIntRxBufFul
        bra     WaitRxData


;If receive buffer is full then read the data
ReadAgain
        call    UARTIntGetCh

;check if Tx buffer is empty
        banksel vUARTIntStatus
WaitForTxBufEmpty
        btfsc   vUARTIntStatus,UARTIntTxBufFul
        bra     WaitForTxBufEmpty

;Echo back the received data                
        call    UARTIntPutCh

;Check if Rx Buffer is empty. If not keep reading it.
        banksel vUARTIntStatus
        btfss   vUARTIntStatus,UARTIntRxBufEmpty
        bra     ReadAgain

        bra     WaitRxData




        END
