;###############################################################################;
;# TITLE   "SPI Master General Purpose Library Checking Software"		        ;
;# 										                                        ;
;#										                                        ;
;#      Program:        SPISInt.ex.txt 						                    ;
;#      Version:        1.0							                            ;
;#      Revision Date:   							                            ;
;#      Author:         Vidyadhar						                        ; 
;#										                                        ;
;# 										                                        ;
;# Program demonstrates use of SPI Master general purpose library module.	    ;
;# This program transmits 4 bytes of Data on reception 0f '0A' from Master      ;
;# This program receives 4 bytes of Data on reception 0f '0B' from Master       ;
;###############################################################################;
						                        ;
	list p=16f877				                ;
        include <P16F877.INC>		            ;
                                                ;
;***********************************************;    
    include    "SPISInt.inc"                    ;This include all required files and variables.        
;***********************************************;
        UDATA
SPITxCnt        res     .1                      ;
SPITxData       res     .4                      ;
SPIRxCnt        res     .1                      ;
SPIRxData       res     .4                      ;
Temp            res     .1                      ;
                        
	UDATA_SHR
DupW	res	1				                    ;
DupStat	res	1				                    ;
DupPC   res 1                                   ;
;***********************************************;

;***********************************************;
                                                ;
Starthere    code    0x0000                     ;
                                                ;
        goto    Start                           ;
;-----------------------------------------------;
                                                ;
intr    code    0x0004                          ;
                                                ;
        goto    ISRHandler                      ;
                                                ;
;-----------------------------------------------;
   
;-----------------------------------------------;
Strt    code                                    ;
;-----------------------------------------------;
                                                ;
GetSPITxData:                                   ;
        addwf   PCL,f                           ;These are the 16 bytes of data to be
        nop                                     ;written into EEPROM
        retlw   0fh                             ;
        retlw   1eh                             ;
        retlw   2dh                             ;
        retlw   3ch                             ;
;-----------------------------------------------;
Start:                                          ;
                                                ;
        ;                                       ;
        ;                                       ;
        ;                                       ;
                                                ;
        BANKSEL TRISC                           ;
        bcf     TRISC,5                         ;SDO is made o/p
        bsf     TRISC,4                         ;SDI is made i/p
        bsf     TRISC,3                         ;SCK is made i/p
        bsf     TRISA,5                         ;SS is made i/p
        bsf     TRISA,4                         ;SS is made i/p
                                                ;
        movlw   007h                            ;
        movwf   ADCON1                          ;
                                                ;
        PAGESEL SPISIntInit                     ;
        call    SPISIntInit                     ;to Initialise SSP MOdule
                                                ;
;        mSPISIntSetClockIdleState Lo            ;
        mSPISIntSetClockIdleState Hi            ;
                                                ;
        mSPISIntSetTransmitOnClockEdge IdleToActive;
;        mSPISIntSetTransmitOnClockEdge ActiveToIdle;
                                                ;
;loading the bytes to be transmitted into the buffer;
;-----------------------------------------------;
; To Send 4 bytes Data1, Data2, Data3, Data4    ;
; to the Master                                 ;
                                                ;
    #if SPIS_TX_BUF_LENGTH != 0                 ;
SPITx:                                          ;
        BANKSEL SPITxCnt                        ;
        clrf    SPITxCnt                        ;clear transmit count
SPIRptTx:                                       ;
        movf    SPITxCnt,w                      ;SPITxData is the starting location
                                                ;of the buffer, where the data to be 
                                                ;transmitted are stored
;        call    GetSPITxData                    ;Getting data to be written into EEPROM
                                                ;
        btfsc    vSPISIntStatus,SPISTxBufFull   ;
        goto     $-1                            ;
                                                ;
        mSPISIntPut                             ;
                                                ;
;        PAGESEL SPIRptTx                        ;
        BANKSEL SPITxCnt                        ;
        incf    SPITxCnt,f                      ;Check 4 bytes of transmission is over
;        movlw   004h                            ;
;        xorwf   SPITxCnt,w                      ;
;        btfss   STATUS,Z                        ;
        goto    SPIRptTx                        ;
                                                ;
    #endif                                      ;
                                                ;
;-----------------------------------------------;
;-----------------------------------------------;                                                ;
                                                ;
; To Receive 4 bytes from the Mastar            ;
    #if SPIS_RX_BUF_LENGTH != 0                 ;
                                                ;
SPIRx:                                          ;
                                                ;
        movlw   030h                            ;
        movwf   FSR                             ;
;        BANKSEL SPIRxCnt                        ;
;        clrf    SPIRxCnt                        ;clear receive count
SPIRptRx:                                       ;
        btfsc    vSPISIntStatus,SPISRxBufEmpty  ;
        goto    $-1                             ;
                                                ;
        mSPISIntGet                             ;Check weather Master wants Slave to
                                                ;
        movwf   INDF                            ;
        incf    FSR,f                           ;
;        PAGESEL SPIRptRx                        ;
;        BANKSEL SPIRxCnt                        ;
;        incf    SPIRxCnt,f                      ;
;        movlw   004h                            ;
;        xorwf   SPIRxCnt,w                      ;Check 4 bytes of reception is over
;        btfss   STATUS,Z                        ;
        goto    SPIRptRx                        ;
                                                ;
    #endif                                      ;
        goto    $                               ;
;-----------------------------------------------;
;-----------------------------------------------;
ISRHandler:                                     ;
                                                ;
	    movwf	DupW				            ;
	    swapf	STATUS,w			            ;
	    movwf	DupStat				            ;
        movf    PCLATH,w                        ;
        movwf   DupPC                           ;
        ;                                       ;
        PAGESEL SPISIntISR                      ;
        call    SPISIntISR                      ;
        ;                                       ;
        ;                                       ;
        ;                                       ;
        movf    DupPC,w                         ;
        movwf   PCLATH                          ;
	    swapf	DupStat,w			            ;
	    movwf	STATUS				            ;
	    swapf	DupW,f				            ;
	    swapf	DupW,w				            ;
        retfie                                  ;
                                                ;
;***********************************************;
        end                                     ;
;***********************************************;

