INIT:
		bsf		STATUS,RP0		; Bank1

		movlw	0xFF
		movwf	TRISC			; All input


	    clrf    SSPSTAT			; Disable SMBus inputs
    	bsf     SSPSTAT,SMP		; Disable slew rate control
	    movlw   0x31			; Divide FOSC/4 by .50 (FOSC=.20000000Hz)
    	movwf   SSPADD			; Setup 100 kHz I2C clock
	    clrf    SSPCON2			; Clear control bits

		bsf		STATUS,RP1		; Bank3

	ifdef __16F886
		movlw	0x0F			; Only RA3, 2, 1, 0  analogue
	  endif
		movwf	ANSEL
		clrf	ANSELH
	endif

		bcf		STATUS,RP0		; Bank2
		bcf		STATUS,RP1		; Bank0

		movlw	b'00101000'
		movwf	SSPCON			; Enable SSP, select I2C Master mode

		return

;******************************************************************************
;	I2C RTC functions
;******************************************************************************

InitTime						; Init time from I2C RTC
		movlw	RTC_Second		; Address of Seconds
								; tddH = 0x02
		call	I2CByteReadSetAddr; Read seconds
		call	BcdToBin
		movwf	Second2
		addwf	Second2,f
								; tddH = 0x03

		call	I2CByteRead		; Read minute
		call	BcdToBin
		movwf	Minute
								; tddH = 0x04

		call	I2CByteRead		; Read hour
		call	BcdToBin3F		; Mask format and AM/PM
		movwf	Hour
								; tddH = 0x05

		call	I2CByteRead		; Read day
		call	BcdToBin3F
		movwf	Day
		swapf	tdd,f
		rrf		tdd,f
		rrf		tdd,w
		andlw	0x03
		movwf	dotindex		; Save year bit 1..0
								; tddH = 0x06

		call	I2CByteRead		; Read Month
		andlw	0x1F
		call	BcdToBin
		movwf	Month
		swapf	tdd,f
		rrf		tdd,w
		andlw	0x07
		movwf	WDay
								; tddH = 0x07

		movlw	RTC_Year		; Address of Year
		call	I2CByteReadSetAddr; Read year
		movwf	Year

		return

StoreTime
		movlw	0x80			; Disable counting command
		call	SendRtcCmd
								; tddH = 0x01

		incf	tddH,f			; Address of seconds
								; tddH = 0x02

		bcf		STATUS,C		; Store Second
		rrf		Second2,w
		call	I2CBcdWrite
								; tddH = 0x03

		movf	Minute,w		; Store Minute
		call	I2CBcdWrite
								; tddH = 0x04

		movf	Hour,w			; Store Hour
		call	I2CBcdWrite
								; tddH = 0x05

		swapf	Year,w			; Store Day and Year bit 1..0
		movwf	tdd
		rlf		tdd,f
		rlf		tdd,f
		movlw	0xC0
		andwf	tdd,f
		movf	Day,w
		call	BCDIorI2CWrite
								; tddH = 0x06

		swapf	WDay,w			; Store Month and Wday
		movwf	tdd
		rlf		tdd,f
		movlw	0xE0
		andwf	tdd,f
		movf	Month,w
		call	BCDIorI2CWrite
								; tddH = 0x07

		movlw	0x00			; Enable counting command
		call	SendRtcCmd
								; tddH = 0x01

StoreYear
		movlw	RTC_Year		; Address Year
		movwf	tddH
								; tddH = 0x10
		movf	Year,w
		call	I2CByteWrite
								; tddH = 0x11
		comf	tdd,w
		call	I2CByteWrite
								; tddH = 0x12
		movf	Century,w
		goto	I2CByteWrite

BCDIorI2CWrite
		call	Conv2BCD
		iorwf	tdd,w
		goto	I2CByteWrite


SendRtcCmd
		clrf	tddH			; tddH = 0x00
		goto	I2CByteWrite

; Only tdd and tddH used in MSSP I2C routines


;*******************Start bit subroutine**************************
;           This routine generates a Start condition
;           (high-to-low transition of SDA while SCL
;           is still high.
;*****************************************************************
I2CBSTART
	    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
	    bsf     STATUS,RP0          ; Select Bank 01
	    bsf     SSPCON2,SEN         ; Generate Start condition
		goto	I2Cbstop_wait0

;*******************Restart bit subroutine**************************
;           This routine generates a Repeated Start
;           condition (high-to-low transition of SDA
;           while SCL is still high.
;*****************************************************************
I2CBRESTART
	    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
	    bsf     STATUS,RP0          ; Select Bank 01
	    bsf     SSPCON2,RSEN        ; Generate Restart condition
		goto	I2Cbstop_wait0

;*******************Data transmit subroutine**********************
;           This routine transmits the byte of data
;           stored in w to the I2C serial
;           device.
;*****************************************************************
I2CTX
	    bcf		PIR1,SSPIF          ; Clear SSP interrupt flag
    	movwf	SSPBUF              ; Write byte out to device

		goto	I2Cbstop_wait0		; Wait for oparation completed

;*******************Byte read test subroutine*********************
;           This routine tests the byte read feature
;           of the I2C serial device.  It will read
;           1 byte of data at address I2CAddress from the device.
;*****************************************************************

I2CByteReadSetAddr
		movwf	tddH

I2CByteRead
		call	I2CBSTART           ; Generate Start condition
        	                        ; Send control byte
		movlw   RTC_ADDR & 0xFE		; Load control byte for write
    	call    I2CTX               ; Send control byte to device

		movf	tddH,w				; Get address
    	call    I2CTX               ; Send address to device

    	call    I2CBRESTART         ; Generate Restart condition
            	                    ; Send control byte
    	movlw   RTC_ADDR | 0x01		; Load control byte for read
    	call    I2CTX               ; Send control byte to device

        	                        ; Read data byte
    	bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    	bsf     STATUS,RP0          ; Select Bank 01
    	bsf     SSPCON2,ACKDT       ; Select to send NO ACK bit
    	bsf     SSPCON2,RCEN        ; Initiate reception of byte

		call	I2Cbstop_wait0		; Wait for oparation completed

    	movf    SSPBUF,W            ; Copy byte to WREG
    	bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    	bsf     STATUS,RP0          ; Select Bank 01
    	bsf     SSPCON2,ACKEN       ; Generate ACK/NO ACK bit

		call	I2Cbstop_wait0		; Wait for oparation completed

		movwf	tdd					; Save data

;*******************Stop bit subroutine***************************
;           This routine generates a Stop condition
;           (low-to-high transition of SDA while SCL
;           is still high.
;*****************************************************************
I2CBSTOP
		incf	tddH,f				; Move address

	    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
	    bsf     STATUS,RP0          ; Select Bank 01
	    bsf     SSPCON2,PEN         ; Generate Stop condition
I2Cbstop_wait0
	    bcf     STATUS,RP0          ; Select Bank 00
bstop_wait
	    btfss   PIR1,SSPIF          ; Check if operation completed
	ifdef	DebugI2C
		return
	else
	    goto    bstop_wait          ; If not, keep checking
	endif

    	return

;*******************Byte write test subroutine********************
;           This routine tests the byte write feature
;           of the I2C serial device.
;*****************************************************************

I2CBcdWrite
		call	Conv2BCD

I2CByteWrite

		movwf	tdd					; Store data

	    call    I2CBSTART           ; Generate Start condition

	    movlw   RTC_ADDR & 0xFE		; Load control byte for write
    	call    I2CTX               ; Send control byte to device

		movf	tddH,w				; Get address
	    call    I2CTX

		movf	tdd,w				; Get data
    	call    I2CTX               ; Send data byte to device

	   	goto    I2CBSTOP            ; Generate Stop condition

