;###############################################################################;
;# TITLE   "I2C Master General Purpose Library Checking Software"               ;
;#                                                                              ;
;#                                                                              ;
;#      Program:        I2CMInt.ex.txt                                          ;
;#      Version:        1.0                                                     ;
;#      Revision Date:                                                          ;
;#      Author:         Vidyadhar                                               ; 
;#                                                                              ;
;#                                                                              ;
;# Program demonstrates use of I2C Master general purpose library module.       ;
;# This program writes 16 bytes of Data into the EEPROM 24LC256 from the        ;
;# location 0000h and reads them back and copy into the buffer 'I2CReadData'    ;
;###############################################################################;
                                                ;
      list p=18f452                             ;
        #include <P18F452.INC>                  ;
                                                ;
;***********************************************;        
    #include   "I2CMInt.inc"                    ;This include all required files and variables.                
;***********************************************;

        UDATA
I2CTxCnt        res     .1                      ;
I2CRxCnt        res     .1                      ;
I2CReadData     res     .16                     ;
Temp            res     .1                      ;
                        
	UDATA_ACS
DupW	res	1				                    ;
DupStat	res	1				                    ;
;***********************************************;
                                                ;
Starthere       code    0x0000                  ;
                                                ;
        goto    Start                           ;
;-----------------------------------------------;
intrpt	        code	0x0008	    			;
					                        	;
	    goto	ISRHandler      				;
						                        ;
;-----------------------------------------------;
Strt    code                                    ;
                                                ;
GetI2CTxData:                                   ;
        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                             ;
                                                ;
;-----------------------------------------------;
                                                ;
;***********************************************;
Start:                                          ;
                                                ;
        ;                                       ;
        ;                                       ;
        ;                                       ;
                                                ;
                                                ;
                                                ;
;        bsf     PORTC,3                         ;
;        bcf     TRISC,3                         ;
;        bsf     PORTC,4                         ;
;        bcf     TRISC,4                         ;
                                                ;
        call    I2CMIntInit                     ;Initialise MSSP MOdule
                                                ;
;        bsf     PORTC,3                         ;
;        bsf     PORTC,4                         ;
        mSetI2CMIntHighPriority                 ;
                                                ;
;-----------------------------------------------;
; To Write 16 bytes Data into EEPROM whose      ;
; address (Slave Address) is A0 from the        ;
; location 0000h                                ;
                                                ;
I2CTransmit                                     ;
        call    I2CMIntStart                    ;Send Start
                                                ;
;-----------------------------------------------;
        movlw   0e0h                            ;SlaveAddress(with write bit)
        call    I2CMIntPut                      ;
                                                ;
;-----------------------------------------------;
        movlw   000h                            ;LocationAddress_High;
        call    I2CMIntPut                      ;
                                                ;
;-----------------------------------------------;
;Following two lines are not required if buffer ;
;size is more than two bytes. Uncomment         ;
;following lines only if buffer size is 2       ;
;-----------------------------------------------;
       ;btfsc   vI2CMIntStatus,I2CMBufFull      ;
       ;goto    $-2                             ;
                                                ;
        movlw   000h                            ;LocationAddress_Low;
        call    I2CMIntPut                      ;
                                                ;
;-----------------------------------------------;
        BANKSEL I2CTxCnt                        ;
        movlw   .16                             ;Load I2CTxCnt with the number of bytes to be transmitted
        movwf   I2CTxCnt                        ;
                                                ;
I2CNxtBytTx                                     ;
;-----------------------------------------------;
;Following two lines are not required if buffer ;
;size is equal to the number of bytes to be     ;
;transmitted including the Slave address and    ;
;any other bytes like location address.         ;
;Uncomment following lines if buffer size is    ;
;lesser than bytes to be transmitted            ;
;-----------------------------------------------;
       btfsc   vI2CMIntStatus,I2CMBufFull      ;
       goto    $-2                             ;
                                                ;
        rlncf   I2CTxCnt,w                      ;Data
        btfsc   STATUS,Z                        ;
        goto    I2CStop                         ;
                                                ;
        call    GetI2CTxData                    ;Getting data to be written into EEPROM
                                                ;
        call    I2CMIntPut                      ;
                                                ;
;-----------------------------------------------;
        BANKSEL I2CTxCnt                        ;
        decf    I2CTxCnt,f                      ;
        goto    I2CNxtBytTx                     ;
                                                ;
;-----------------------------------------------;
I2CStop                                         ;
        call    I2CMIntStop                     ;Send Stop
                                                ;
;-----------------------------------------------;
                                                ;
        ;                                       ;To Write to be happened 10ms is required.
        ;                                       ;Give that delay before Read
        ;                                       ;
        ;                                       ;
                                                ;
        btfsc   vI2CMIntStatus,I2CMBusy         ;
        goto    $-2                             ;
                                                ;
        goto    I2CReceive                      ;
                                                ;
;-----------------------------------------------;
;***********************************************;
; To Read 16 bytes from EEPROM starting from the;
; locationand 0000h and saving them in  a buffer;
                                                ;
I2CReceive                                      ;
        call    I2CMIntStart                    ;Send Start
                                                ;
;-----------------------------------------------;
        movlw   0a0h                            ;SlaveAddress(with write bit)
        call    I2CMIntPut                      ;
                                                ;
;-----------------------------------------------;
        movlw   000h                            ;LocationAddress_High;
        call    I2CMIntPut                      ;
                                                ;
;-----------------------------------------------;
;Following two lines are not required if buffer ;
;size is more than two bytes. Uncomment         ;
;following lines only if buffer size is 2       ;
;-----------------------------------------------;
       ;btfsc   vI2CMIntStatus,I2CMBufFull      ;
       ;goto    $-2                             ;
                                                ;
        movlw   000h                            ;LocationAddress_Low;
        call    I2CMIntPut                      ;
                                                ;
;-----------------------------------------------;
        call    I2CMIntReStart                  ;Send ReStart
                                                ;
;-----------------------------------------------;
;Following two lines are not required if buffer ;
;size is more than two bytes. Uncomment         ;
;following lines only if buffer size is 2       ;
;-----------------------------------------------;
       ;btfsc   vI2CMIntStatus,I2CMBufFull      ;
       ;goto    $-2                             ;
                                                ;
        movlw   0a1h                            ;Send SlaveAddress(with read bit)
        call    I2CMIntPut                      ;
                                                ;
;-----------------------------------------------;
        movlw   .016                            ;Set the number bytes to be received to 16
        call    I2CMIntSetGetCount              ;
                                                ;
;-----------------------------------------------;
        call    I2CMIntStop                     ;Send Stop
                                                ;
;-----------------------------------------------;
        BANKSEL I2CRxCnt                        ;
        clrf    I2CRxCnt                        ;Clear I2CRxCnt-Read Pointer offset
                                                ;
I2CNxtRx                                        ;
        btfss   vI2CMIntStatus,I2CMDataReady    ;Wait till Data is ready
        goto    $-2                             ;
                                                ;
        call    I2CMIntGet                      ;Read received value in W
                                                ;
        incf    I2CRxCnt,f                      ;increment read pointer offset
        movf    I2CRxCnt,w                      ;
        xorlw   .16                             ;check whether 16bytes received
        btfss   STATUS,Z                        ;
        goto    I2CNxtRx                        ;send Ack if some more bytes are to be received
                                                ;
;-----------------------------------------------;
                                                ;
        btfsc   vI2CMIntStatus,I2CMBusy         ;
        goto    $-2                             ;
                                                ;
        mI2CMIntDisable                         ;Disable MSSP Module
                                                ;
;-----------------------------------------------;
        ;                                       ;
        ;                                       ;
        ;                                       ;
        ;                                       ;
                                                ;
I2CBusCollisionErr                              ;
        goto    $                               ;User can write his own logic for 
                                                ;bus collision
;-----------------------------------------------;
ISRHandler:                                     ;
                                                ;
	    movwf	DupW				            ;
	    swapf	STATUS,w			            ;
	    movwf	DupStat				            ;
        ;                                       ;
        ;                                       ;
        call    I2CMIntISR                      ;
                                                ;
;-----------------------------------------------;
;Following two lines are not required for       ;
;single master mode. Uncomment following lines  ;
;only for Multi-master mode                     ;
;-----------------------------------------------;
       ;btfsc   vI2CMIntStatus,I2CMErrBusCollision ;Check whether Bus Collision has occured
       ;call    I2CBusCollisionErr              ;
                                                ;
;-----------------------------------------------;
;        btfsc   vI2CMIntStatus,I2CMErrNoAck     ;Check whether Acknowledge has been received or not
;        call    I2CNoAckErr                     ;
                                                ;
;-----------------------------------------------;
        ;                                       ;
        ;                                       ;
        ;                                       ;
	    swapf	DupStat,w			            ;
	    movwf	STATUS				            ;
	    swapf	DupW,f				            ;
	    swapf	DupW,w				            ;
        retfie                                  ;
                                                ;
;***********************************************;
        end                                     ;
;***********************************************;


