;###############################################################################;
;# TITLE   "SPI Master General Purpose Library Checking Software"               ;
;#                                                                              ;
;#                                                                              ;
;#      Program:        SPIMPol.ex.txt                                          ;
;#      Version:        1.0                                                     ;
;#      Revision Date:                                                          ;
;#      Author:         Vidyadhar                                               ; 
;#                                                                              ;
;#                                                                              ;
;# Program demonstrates use of SPI Master general purpose library module.       ;
;# This program writes 16 bytes of Data into the EEPROM 25LC040 from the        ;
;# location 0000h and reads them back and copy into the buffer 'SPIReadData'    ;
;###############################################################################;
                                                ;
	list        p=18f452				        ;
    #include    <P18F452.INC>			        ;
                                                ;
;***********************************************;        
    #include    "SPIMPol.inc"                   ;This include all required files and variables.                
;***********************************************;
        UDATA
SPITxCnt        res     .1                      ;
SPIRxCnt        res     .1                      ;
SPIReadData     res     .16                     ;
Temp            res     .1                      ;
                        
;***********************************************;
                                                ;
Starthere       code    0x0000                  ;
                                                ;
        goto    Start                           ;
;-----------------------------------------------;
Strt    code                                    ;
                                                ;
SPIWait:                                        ;Wait till Data is Received
        call    SPIMPolIsDataReady              ;
                                                ;
        tstfsz  WREG                            ;
        bra     SPIWait                         ;
        return                                  ;
;-----------------------------------------------;
                                                ;
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                             ;
                                                ;
;-----------------------------------------------;
                                                ;
ChkSPIWriteCollisionErr macro                   ;
        xorlw   SPIMErrWriteCollision           ;Check whether Write Collision occured.
        bz      SPIWriteCollisionErr            ;If Write Collision has occured goto SPIWriteCollisionErr
                                                ;
        endm                                    ;
;-----------------------------------------------;
                                                ;
;***********************************************;
Start:                                          ;
                                                ;
        ;                                       ;
        ;                                       ;
        ;                                       ;
                                                ;
        bsf     PORTC,1                         ;RC1 is used to Select EEPROM 25LC040
        movlw   0d5h                            ;
        movwf   TRISC                           ;The pins RC1, SCK, SDO are made o/p
                                                ;
        call    SPIMPolInit                     ;Initialise SSP MOdule
                                                ;
        mSPIMPolSetClockIdleState Hi            ;
                                                ;
        mSPIMPolSetTransmitOnClockEdge IdleToActive;
                                                ;
        mSPIMPolSetSampleAtDataOut Ends         ;
                                                ;
        bcf     PORTC,1                         ;Select the EEPROM by making RC1 Low
                                                ;
;-----------------------------------------------;
; To Write 16 bytes Data into EEPROM from the   ;
; location 0000h                                ;
                                                ;
SPITransmit                                     ;
        movlw   006h                            ;Instruction to enable Write
        call    SPIMPolPut                      ;
                                                ;
        ChkSPIWriteCollisionErr                 ;Check whether Write Collision has occured
                                                ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        call    SPIMPolGet                      ;
                                                ;
        bsf     PORTC,1                         ;DeSelect the EEPROM by making RC1 Low
                                                ;
;-----------------------------------------------;
        nop                                     ;
        bcf     PORTC,1                         ;Select the EEPROM by making RC1 Low
                                                ;
        movlw   002h                            ;Instruction to Write Data
        call    SPIMPolPut                      ;
                                                ;
        ChkSPIWriteCollisionErr                 ;Check whether Write Collision has occured
                                                ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        call    SPIMPolGet                      ;
                                                ;
;-----------------------------------------------;
        movlw   000h                            ;LocationAddress
        call    SPIMPolPut                      ;
                                                ;
        ChkSPIWriteCollisionErr                 ;Check whether Write Collision has occured
                                                ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        call    SPIMPolGet                      ;
                                                ;
;-----------------------------------------------;
        BANKSEL SPITxCnt                        ;The buffer should be preloaded with data
        movlw   .16                             ;Load SPITxCnt with the number of bytes to be transmitted
        movwf   SPITxCnt                        ;
                                                ;
SPINxtBytTx                                     ;
        movf    SPITxCnt,w                      ;Data
        btfsc   STATUS,Z                        ;
        goto    SPIStop                         ;
                                                ;
        call    GetSPITxData                    ;Getting data to be written into EEPROM
                                                ;
        call    SPIMPolPut                      ;
                                                ;
        ChkSPIWriteCollisionErr                 ;Check whether Write Collision has occured
                                                ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        call    SPIMPolGet                      ;
                                                ;
;-----------------------------------------------;
        decf    SPITxCnt,f                      ;
        goto    SPINxtBytTx                     ;
                                                ;
;-----------------------------------------------;
SPIStop                                         ;
        bsf     PORTC,1                         ;DeSelect the EEPROM by making RC1 Low
                                                ;
;-----------------------------------------------;
WaitTillWriteIsOver                             ;
        nop                                     ;
        bcf     PORTC,1                         ;Select the EEPROM by making RC1 Low
                                                ;
        movlw   005h                            ;Instruction to Disable Write
        call    SPIMPolPut                      ;
                                                ;
        ChkSPIWriteCollisionErr                 ;Check whether Write Collision has occured
                                                ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        call    SPIMPolGet                      ;Dummy Read neccessary to clear BF flag
                                                ;
        movlw   000h                            ;Instruction to Disable Write
        call    SPIMPolPut                      ;
                                                ;
        ChkSPIWriteCollisionErr                 ;Check whether Write Collision has occured
                                                ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        call    SPIMPolGet                      ;Dummy Read neccessary to clear BF flag
                                                ;
        bsf     PORTC,1                         ;DeSelect the EEPROM by making RC1 Low
                                                ;
        andlw   001h                            ;
        btfss   STATUS,Z                        ;
        goto    WaitTillWriteIsOver             ;
                                                ;
;-----------------------------------------------;
        nop                                     ;
        bcf     PORTC,1                         ;Select the EEPROM by making RC1 Low
                                                ;
        movlw   004h                            ;Instruction to Disable Write
        call    SPIMPolPut                      ;
                                                ;
        ChkSPIWriteCollisionErr                 ;Check whether Write Collision has occured
                                                ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        call    SPIMPolGet                      ;Dummy Read neccessary to clear BF flag
                                                ;
        bsf     PORTC,1                         ;DeSelect the EEPROM by making RC1 Low
                                                ;
;-----------------------------------------------;
        ;                                       ;
        ;                                       ;
        ;                                       ;
        ;                                       ;
                                                ;
        goto    SPIReceive                      ;
                                                ;
;-----------------------------------------------;
SPIWriteCollisionErr                            ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        goto    SPINxtBytTx                     ;Repeat transmission of the unfinished byte
                                                ;
;***********************************************;
; To Read 16 bytes from EEPROM starting from the;
; locationand 0000h and saving them in  a buffer;
                                                ;
SPIReceive                                      ;
        bcf     PORTC,1                         ;Select the EEPROM by making RC1 Low
                                                ;
        movlw   003h                            ;Instruction to Read
        call    SPIMPolPut                      ;
                                                ;
        ChkSPIWriteCollisionErr                 ;Check whether Write Collision has occured
                                                ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        call    SPIMPolGet                      ;Dummy Read neccessary to clear BF flag
                                                ;
;-----------------------------------------------;
        movlw   000h                            ;LocationAddress
        call    SPIMPolPut                      ;
                                                ;
        ChkSPIWriteCollisionErr                 ;Check whether Write Collision has occured
                                                ;
        call    SPIWait                         ;Wait till Transmission is over
                                                ;
        call    SPIMPolGet                      ;Dummy Read neccessary to clear BF flag
                                                ;
;-----------------------------------------------;
        clrf    SPIRxCnt                        ;Clear SPIRxCnt-Read Pointer offset
                                                ;
SPINxtRx                                        ;
        movlw   000h                            ;
        call    SPIMPolPut                      ;Dummy Write neccessary to initiate reception
                                                ;
        ChkSPIWriteCollisionErr                 ;Check whether Write Collision has occured
                                                ;
        call    SPIWait                         ;Wait till Reception is over
                                                ;
        call    SPIMPolGet                      ;Read received value in W
                                                ;
        lfsr    FSR0,SPIReadData                ;SPIReadData is the starting location
                                                ;of the buffer, where the read data
                                                ;should be stored
        movwf   Temp                            ;
                                                ;
        movf    SPIRxCnt,w                      ;Read Pointer
        addwf   FSR0L                           ;
        btfsc   STATUS,C                        ;
        incf    FSR0H                           ;
                                                ;
        movf    Temp,w                          ;
        movwf   INDF0                           ;Received data is stored in buffer
                                                ;
        incf    SPIRxCnt,f                      ;increment read pointer offset
        movf    SPIRxCnt,w                      ;
        xorlw   .16                             ;check whether 16bytes received
        btfss   STATUS,Z                        ;
        goto    SPINxtRx                        ;
                                                ;
        bsf     PORTC,1                         ;DeSelect the EEPROM by making RC1 Low
                                                ;
        mSPIMPolDisable                         ;Disables SSP Module
;-----------------------------------------------;
        goto        $                           ;
;-----------------------------------------------;
;***********************************************;
        end                                     ;
;***********************************************;

