;###############################################################################;
;# TITLE   "SPI Master General Purpose Library Checking Software"               ;
;#                                                                              ;
;#                                                                              ;
;#      Program:        SPIMInt.ex.txt                                          ;
;#      Version:        1.0                                                     ;
;#      Revision Date:                                                          ;
;#      Author:         Vidyadhar                                               ;
;#                                                                              ;
;#                                                                              ;
;# Program demonstrates use of SPI Master general purpose library module.       ;
;###############################################################################;
                                                ;
    list p=16f877                               ;
        include <P16F877.INC>                   ;
;        include <P16c63.INC>                   ;
                                                ;
                                                ;
;***********************************************;    
    include    "spimint.inc"                    ;This include all required files and variables.        
;***********************************************;

        UDATA
SPITxCnt        res     .1                      ;
SPIRxCnt        res     .1                      ;
SPIReadData     res     .16                     ;
Temp            res     .1                      ;
                        
	UDATA_SHR
DupW	res	1				                    ;
DupStat	res	1				                    ;
;***********************************************;
                                                ;
Starthere    code    0x0000                     ;
                                                ;
        goto    Start                           ;
;-----------------------------------------------;
                                                ;
intr    code    0x0004                          ;
                                                ;
        goto    ISRHandler                      ;
                                                ;
;-----------------------------------------------;
Strt    code                                    ;
                                                ;
SPIWait:                                        ;Wait till Data is Transmitted/Received
    btfsc    vSPIMIntStatus,SPIMBusy            ;
    goto    $-1                                 ;
    return                                      ;
                                                ;
;-----------------------------------------------;
Dat     code                                    ;
GetSPITxData:                                   ;
        PAGESEL GetSPITxData                    ;
        addwf   PCL,f                           ;These are the 16 bytes of data to be
        nop                                     ;written into EEPROM
        retlw   .15                             ;
        retlw   .14                             ;
        retlw   .13                             ;
        retlw   .12                             ;
        retlw   .11                             ;
        retlw   .10                             ;
        retlw   .09                             ;
        retlw   .08                             ;
        retlw   .07                             ;
        retlw   .06                             ;
        retlw   .05                             ;
        retlw   .04                             ;
        retlw   .03                             ;
        retlw   .02                             ;
        retlw   .01                             ;
        retlw   .00                             ;
                                                ;
;-----------------------------------------------;
Begin   code
Start:                                          ;
                                                ;
    ;                                           ;
    ;                                           ;
    ;                                           ;
                                                ;
                                                ;
        BANKSEL PORTC                           ;
        bsf     PORTC,1                         ;RC1 is used to Select EEPROM 25LC040
        BANKSEL TRISC                           ;
        movlw   0d5h                            ;
        movwf   TRISC                           ;The pins RC1, SCK, SDO are made o/p
                                                ;
        PAGESEL SPIMIntInit                     ;
        call    SPIMIntInit                     ;Initialise SSP MOdule
                                                ;
        mSPIMIntSetClockIdleState Hi            ;Set Idle state of Clock line high
                                                ;
        mSPIMIntSetTransmitOnClockEdge IdleToActive        ;Set to Transmit on falling edge
                                                ;
        mSPIMIntSetSampleAtDataOut Mids         ;Set to sample received data on rising edge.
                                                ;
                                                ;
;-----------------------------------------------;
; To Write 16 bytes Data into EEPROM from the   ;
; location 0000h                                ;
                                                ;
SPITransmit                                     ;
        BANKSEL PORTC                           ;
        bcf     PORTC,1                         ;Select the EEPROM by making RC1 Low
                                                ;
        PAGESEL SPIMIntPut                      ;
        movlw    006h                           ;Instruction to enable Write
        call    SPIMIntPut                      ;
                                                ;
        PAGESEL SPIWait                         ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        BANKSEL PORTC                           ;
        bsf     PORTC,1                         ;DeSelect the EEPROM by making RC1 Low
    ;                                           ;
    ;                                           ;
    ;                                           ;
                                                ;
                                                ;
;-----------------------------------------------;
        BANKSEL PORTC                           ;
        bcf     PORTC,1                         ;Select the EEPROM by making RC1 Low
                                                ;
        PAGESEL SPIMIntPut                      ;
        movlw   002h                            ;Instruction to Write Data
        call    SPIMIntPut                      ;
                                                ;
;-----------------------------------------------;
        movlw   000h                            ;LocationAddress
        call    SPIMIntPut                      ;
                                                ;
;-----------------------------------------------;
        BANKSEL SPITxCnt                        ;
        movlw   .16                             ;Load SPITxCnt with the number of bytes to be transmitted
        movwf   SPITxCnt                        ;
                                                ;
SPINxtBytTx                                     ;
;-----------------------------------------------;
;Following two lines are not required if buffer ;
;size is equal to the number of bytes to be     ;
;transmitted including the write command and    ;
;any other bytes like location address.         ;
;Uncomment following lines if buffer size is    ;
;lesser than bytes to be transmitted            ;
;-----------------------------------------------;
        btfsc   vSPIMIntStatus,SPIMTxBufFull    ;
        goto    $-1                             ;
                                                ;
        movf    SPITxCnt,w                      ;Data
        btfsc   STATUS,Z                        ;
        goto    SPIStop                         ;
                                                ;
        PAGESEL GetSPITxData                    ;
        call    GetSPITxData                    ;Getting data to be written into EEPROM
                                                ;
        PAGESEL SPIMIntPut                      ;
        call    SPIMIntPut                      ;
                                                ;
;-----------------------------------------------;
        BANKSEL SPITxCnt                        ;
        decf    SPITxCnt,f                      ;
        PAGESEL SPINxtBytTx                     ;
        goto    SPINxtBytTx                     ;
                                                ;
;-----------------------------------------------;
SPIStop                                         ;
        PAGESEL SPIWait                         ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        BANKSEL PORTC                           ;
        bsf     PORTC,1                         ;DeSelect the EEPROM by making RC1 Low
                                                ;
;-----------------------------------------------;
WaitTillWriteIsOver                             ;
        ;                                       ;
        ;                                       ;
        ;                                       ;
        ;                                       ;
                                                ;
        BANKSEL PORTC                           ;
        bcf     PORTC,1                         ;Select the EEPROM by making RC1 Low
                                                ;
        PAGESEL SPIMIntPut                      ;
        movlw   005h                            ;Instruction to Read Status register
        call    SPIMIntPut                      ;
                                                ;
        movlw   001h                            ;Set number of bytes to be received
        PAGESEL SPIMIntSetGetCount              ;
        call    SPIMIntSetGetCount              ;
                                                ;
        PAGESEL SPIWait                         ;
        call    SPIWait                         ;Wait till Transmission & reception are over
                                                ;
        BANKSEL PORTC                           ;
        bsf     PORTC,1                         ;DeSelect the EEPROM by making RC1 Low
                                                ;
        PAGESEL SPIMIntGet                      ;
        call    SPIMIntGet                      ;read the Status
                                                ;
        PAGESEL WaitTillWriteIsOver             ;
        andlw   001h                            ;Check for compleation of write
        btfss   STATUS,Z                        ;if not over goto read status
        goto    WaitTillWriteIsOver             ;
                                                ;
;-----------------------------------------------;
        BANKSEL PORTC                           ;
        bcf     PORTC,1                         ;Select the EEPROM by making RC1 Low
                                                ;
        PAGESEL SPIMIntPut                      ;
        movlw   004h                            ;Instruction to Disable Write
        call    SPIMIntPut                      ;
                                                ;
        PAGESEL SPIWait                         ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        BANKSEL PORTC                           ;
        bsf     PORTC,1                         ;DeSelect the EEPROM by making RC1 Low
                                                ;
;-----------------------------------------------;
        ;                                       ;
        ;                                       ;
        ;                                       ;
        ;                                       ;
                                                ;
;-----------------------------------------------;
;***********************************************;
; To Read 16 bytes from EEPROM starting from the;
; locationand 0000h and saving them in  a buffer;
                                                ;
SPIReceive                                      ;
        BANKSEL PORTC                           ;
        bcf     PORTC,1                         ;Select the EEPROM by making RC1 Low
                                                ;
        PAGESEL SPIMIntPut                      ;
        movlw   003h                            ;Instruction to Read
        call    SPIMIntPut                      ;
                                                ;
;-----------------------------------------------;
        movlw   000h                            ;LocationAddress
        call    SPIMIntPut                      ;
                                                ;
;-----------------------------------------------;
        movlw   .16                             ;Set number of bytes to be received
        PAGESEL SPIMIntSetGetCount              ;
        call    SPIMIntSetGetCount              ;
                                                ;
;-----------------------------------------------;
;        btfsc   vSPIMIntStatus,SPIMBusy        ;Use this if you want to read after 
;        goto    $-1                            ;compleation of reception
                                                ;
        BANKSEL SPIRxCnt                        ;
        clrf    SPIRxCnt                        ;Clear SPIRxCnt-Read Pointer offset
                                                ;
SPINxtRx                                        ;
        PAGESEL SPINxtRx                        ;
        btfsc   vSPIMIntStatus,SPIMRxBufEmpty   ;check is there any data in receive buffer
        goto    SPINxtRx                        ;
                                                ;
        PAGESEL SPIMIntGet                      ;Read received value in W
        call    SPIMIntGet                      ;
                                                ;
        BANKSEL SPITxCnt                        ;
        incf    SPIRxCnt,f                      ;increment read pointer offset
        movf    SPIRxCnt,w                      ;
        xorlw   .16                             ;check whether 16bytes received
        PAGESEL SPINxtRx                        ;
        btfss   STATUS,Z                        ;
        goto    SPINxtRx                        ;
                                                ;
        BANKSEL PORTC                           ;
        bsf     PORTC,1                         ;DeSelect the EEPROM by making RC1 Low
                                                ;
        mSPIMIntDisable                         ;Disables SSP Module
;-----------------------------------------------;
                                                ;
        goto        $                           ;
;-----------------------------------------------;
ISRHandler:                                     ;
                                                ;
	    movwf	DupW				            ;
	    swapf	STATUS,w			            ;
	    movwf	DupStat				            ;
        ;                                       ;
        PAGESEL SPIMIntISR                      ;
        call    SPIMIntISR                      ;
                                                ;
        ;                                       ;
        ;                                       ;
        ;                                       ;
	    swapf	DupStat,w			            ;
	    movwf	STATUS				            ;
	    swapf	DupW,f				            ;
	    swapf	DupW,w				            ;
        retfie                                  ;
                                                ;
;-----------------------------------------------;
;***********************************************;
        END                                     ;
;***********************************************;

